mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-02 22:17:10 +08:00

* Uncomment all code Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(linters): add godox and dupword linters This commit adds two new linters, godox and dupword, to the linter configuration file (.golangci.yml). The godox linter checks for occurrences of TODO and FIXME comments in the codebase, helping to ensure that these comments are not forgotten or left unresolved. The dupword linter detects duplicate words in comments and strings, which can be a sign of typos or errors. These new linters will enhance the code quality and maintainability of the project. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * uncomment tests in /pkg/sdk/go/tokens_test.go Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
88 lines
1.7 KiB
Go
88 lines
1.7 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package keys
|
|
|
|
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
|
|
}
|
|
|
|
tkn, err := svc.Issue(ctx, req.token, newKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res := issueKeyRes{
|
|
Value: tkn.AccessToken,
|
|
}
|
|
|
|
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.Issuer,
|
|
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
|
|
}
|
|
}
|