mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00

* remove owner id Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move authz into authn and merge into new service Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add groups endpoints Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add group type Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding mocks, some renaming, refactor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update proto Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding mocks, some renaming, refactor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter err,and comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * undo renaming, add interface for authn and authz Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renam some variables Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renaming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove extra slashes from comment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * resolving small remarks Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.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/auth"
|
|
)
|
|
|
|
func issueEndpoint(svc auth.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 := auth.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 retrieveEndpoint(svc auth.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.RetrieveKey(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
|
|
}
|
|
}
|
|
|
|
func revokeEndpoint(svc auth.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
|
|
}
|
|
}
|