mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00

* Add both an ID and an Email to API key requests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Use return UserIdentity response Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Replace GetValue with GetEmail Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Refactor Mainflux Key Add `Subject` field and reorganize Key manipulation. **Remove backward compatibility** Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix service test Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix DB tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix API tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix JWT tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Uncomment and fix API tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix SQL statements alignment Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Issue method docs Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix Retrieve API and API docs Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Update tests Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
93 lines
1.8 KiB
Go
93 lines
1.8 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package http
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
"github.com/mainflux/mainflux/authn"
|
|
)
|
|
|
|
func issueEndpoint(svc authn.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(issueKeyReq)
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
now := time.Now().UTC()
|
|
newKey := authn.Key{
|
|
IssuedAt: now,
|
|
Type: req.Type,
|
|
}
|
|
|
|
duration := time.Duration(req.Duration * time.Second)
|
|
if duration != 0 {
|
|
exp := now.Add(duration)
|
|
newKey.ExpiresAt = exp
|
|
}
|
|
|
|
key, secret, err := svc.Issue(ctx, req.token, newKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res := issueKeyRes{
|
|
ID: key.ID,
|
|
Value: secret,
|
|
IssuedAt: key.IssuedAt,
|
|
}
|
|
if !key.ExpiresAt.IsZero() {
|
|
res.ExpiresAt = &key.ExpiresAt
|
|
}
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
func revokeEndpoint(svc authn.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(keyReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := svc.Revoke(ctx, req.token, req.id); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return revokeKeyRes{}, nil
|
|
}
|
|
}
|
|
|
|
func retrieveEndpoint(svc authn.Service) endpoint.Endpoint {
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
req := request.(keyReq)
|
|
|
|
if err := req.validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
key, err := svc.Retrieve(ctx, req.token, req.id)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ret := retrieveKeyRes{
|
|
ID: key.ID,
|
|
IssuerID: key.IssuerID,
|
|
Subject: key.Subject,
|
|
Type: key.Type,
|
|
IssuedAt: key.IssuedAt,
|
|
}
|
|
if !key.ExpiresAt.IsZero() {
|
|
ret.ExpiresAt = &key.ExpiresAt
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
}
|