Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Personal access tokens #2254

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion auth/api/http/keys/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ func newService() (auth.Service, *mocks.KeyRepository) {
krepo := new(mocks.KeyRepository)
prepo := new(mocks.PolicyAgent)
drepo := new(mocks.DomainsRepository)
patsRepo := new(mocks.PATSRepository)
hasher := new(mocks.Hasher)
idProvider := uuid.NewMock()

t := jwt.New([]byte(secret))

return auth.New(krepo, drepo, idProvider, t, prepo, loginDuration, refreshDuration, invalidDuration), krepo
return auth.New(krepo, drepo, patsRepo, hasher, idProvider, t, prepo, loginDuration, refreshDuration, invalidDuration), krepo
}

func newServer(svc auth.Service) *httptest.Server {
Expand Down
202 changes: 202 additions & 0 deletions auth/api/http/pats/endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package pats

import (
"context"

"github.com/absmach/magistrala/auth"
"github.com/go-kit/kit/endpoint"
)

func createPATEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createPatReq)
if err := req.validate(); err != nil {
return nil, err
}

pat, err := svc.CreatePAT(ctx, req.token, req.Name, req.Description, req.Duration, req.Scope)
if err != nil {
return nil, err
}

return createPatRes{pat}, nil
}
}

func retrievePATEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(retrievePatReq)
if err := req.validate(); err != nil {
return nil, err
}

pat, err := svc.RetrievePAT(ctx, req.token, req.id)
if err != nil {
return nil, err
}

return retrievePatRes{pat}, nil
}
}

func updatePATNameEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(updatePatNameReq)
if err := req.validate(); err != nil {
return nil, err
}

pat, err := svc.UpdatePATName(ctx, req.token, req.id, req.Name)
if err != nil {
return nil, err
}

return updatePatNameRes{pat}, nil
}
}

func updatePATDescriptionEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(updatePatDescriptionReq)
if err := req.validate(); err != nil {
return nil, err
}

pat, err := svc.UpdatePATDescription(ctx, req.token, req.id, req.Description)
if err != nil {
return nil, err
}

return updatePatDescriptionRes{pat}, nil
}
}

func listPATSEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listPatsReq)
if err := req.validate(); err != nil {
return nil, err
}

pm := auth.PATSPageMeta{
Limit: req.limit,
Offset: req.offset,
}
patsPage, err := svc.ListPATS(ctx, req.token, pm)
if err != nil {
return nil, err
}

return listPatsRes{patsPage}, nil
}
}

func deletePATEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(deletePatReq)
if err := req.validate(); err != nil {
return nil, err
}

if err := svc.DeletePAT(ctx, req.token, req.id); err != nil {
return nil, err
}

return deletePatRes{}, nil
}
}

func resetPATSecretEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(resetPatSecretReq)
if err := req.validate(); err != nil {
return nil, err
}

pat, err := svc.ResetPATSecret(ctx, req.token, req.id, req.Duration)
if err != nil {
return nil, err
}

return resetPatSecretRes{pat}, nil
}
}

func revokePATSecretEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(revokePatSecretReq)
if err := req.validate(); err != nil {
return nil, err
}

if err := svc.RevokePATSecret(ctx, req.token, req.id); err != nil {
return nil, err
}

return revokePatSecretRes{}, nil
}
}

func addPATScopeEntryEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(addPatScopeEntryReq)
if err := req.validate(); err != nil {
return nil, err
}

scope, err := svc.AddPATScopeEntry(ctx, req.token, req.id, req.PlatformEntityType, req.OptionalDomainID, req.OptionalDomainEntityType, req.Operation, req.EntityIDs...)
if err != nil {
return nil, err
}

return addPatScopeEntryRes{scope}, nil
}
}

func removePATScopeEntryEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(removePatScopeEntryReq)
if err := req.validate(); err != nil {
return nil, err
}

scope, err := svc.RemovePATScopeEntry(ctx, req.token, req.id, req.PlatformEntityType, req.OptionalDomainID, req.OptionalDomainEntityType, req.Operation, req.EntityIDs...)
if err != nil {
return nil, err
}
return removePatScopeEntryRes{scope}, nil
}
}

func clearPATAllScopeEntryEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(clearAllScopeEntryReq)
if err := req.validate(); err != nil {
return nil, err
}

if err := svc.ClearPATAllScopeEntry(ctx, req.token, req.id); err != nil {
return nil, err
}

return clearAllScopeEntryRes{}, nil
}
}

func authorizePATEndpoint(svc auth.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(authorizePATReq)
if err := req.validate(); err != nil {
return nil, err
}

if err := svc.AuthorizePAT(ctx, req.token, req.PlatformEntityType, req.OptionalDomainID, req.OptionalDomainEntityType, req.Operation, req.EntityIDs...); err != nil {
return nil, err
}

return authorizePATRes{}, nil
}
}
Loading
Loading