mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-06 19:29:15 +08:00

* Fix linting errors Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(linters): add ineffassign linter This commit adds the `ineffassign` linter to the project's `.golangci.yml` configuration file. The `ineffassign` linter helps identify and flag assignments to variables that are never used, helping to improve code quality and maintainability. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * Add extra linters Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(golangci): Add header check - Added goheader check to ensure all files have license headers - Added build tags for "nats" in the .golangci.yml file to include the necessary dependencies for the "nats" package during the build process. - Also, increased the maximum number of issues per linter and the maximum number of same issues reported by the linter to improve the code quality analysis. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(.golangci.yml): Add new linters Add the following new linters to the .golangci.yml configuration file: - asasalint - asciicheck - bidichk - contextcheck - decorder - dogsled - errchkjson - errname - execinquery - exportloopref - ginkgolinter - gocheckcompilerdirectives These linters will help improve code quality and catch potential issues during the code review process. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
90 lines
1.8 KiB
Go
90 lines
1.8 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,
|
|
}
|
|
// 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.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
|
|
}
|
|
}
|