Skip to content

Commit

Permalink
Move pat to auth
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene committed Oct 31, 2024
1 parent c6bd728 commit 562a5e6
Show file tree
Hide file tree
Showing 27 changed files with 2,253 additions and 1,170 deletions.
48 changes: 24 additions & 24 deletions pat/api/http/endpoint.go → auth/api/http/pats/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"context"

"github.com/absmach/magistrala/internal/api"
"github.com/absmach/magistrala/pat"
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/pkg/authn"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
"github.com/go-kit/kit/endpoint"
)

func createPATEndpoint(svc pat.Service) endpoint.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 {
Expand All @@ -25,16 +25,16 @@ func createPATEndpoint(svc pat.Service) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

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

return createPatRes{pat}, nil
return createPatRes{res}, nil
}
}

func retrievePATEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -46,16 +46,16 @@ func retrievePATEndpoint(svc pat.Service) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

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

return retrievePatRes{pat}, nil
return retrievePatRes{res}, nil
}
}

func updatePATNameEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -66,16 +66,16 @@ func updatePATNameEndpoint(svc pat.Service) endpoint.Endpoint {
if !ok {
return nil, svcerr.ErrAuthentication
}
pat, err := svc.UpdatePATName(ctx, session, req.id, req.Name)
res, err := svc.UpdatePATName(ctx, session, req.id, req.Name)
if err != nil {
return nil, err
}

return updatePatNameRes{pat}, nil
return updatePatNameRes{res}, nil
}
}

func updatePATDescriptionEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -87,16 +87,16 @@ func updatePATDescriptionEndpoint(svc pat.Service) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

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

return updatePatDescriptionRes{pat}, nil
return updatePatDescriptionRes{res}, nil
}
}

func listPATSEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -108,7 +108,7 @@ func listPATSEndpoint(svc pat.Service) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

pm := pat.PATSPageMeta{
pm := auth.PATSPageMeta{
Limit: req.limit,
Offset: req.offset,
}
Expand All @@ -121,7 +121,7 @@ func listPATSEndpoint(svc pat.Service) endpoint.Endpoint {
}
}

func deletePATEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -141,7 +141,7 @@ func deletePATEndpoint(svc pat.Service) endpoint.Endpoint {
}
}

func resetPATSecretEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -153,16 +153,16 @@ func resetPATSecretEndpoint(svc pat.Service) endpoint.Endpoint {
return nil, svcerr.ErrAuthentication
}

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

return resetPatSecretRes{pat}, nil
return resetPatSecretRes{res}, nil
}
}

func revokePATSecretEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -182,7 +182,7 @@ func revokePATSecretEndpoint(svc pat.Service) endpoint.Endpoint {
}
}

func addPATScopeEntryEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -203,7 +203,7 @@ func addPATScopeEntryEndpoint(svc pat.Service) endpoint.Endpoint {
}
}

func removePATScopeEntryEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -223,7 +223,7 @@ func removePATScopeEntryEndpoint(svc pat.Service) endpoint.Endpoint {
}
}

func clearPATAllScopeEntryEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand All @@ -243,7 +243,7 @@ func clearPATAllScopeEntryEndpoint(svc pat.Service) endpoint.Endpoint {
}
}

func authorizePATEndpoint(svc pat.Service) endpoint.Endpoint {
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 {
Expand Down
42 changes: 21 additions & 21 deletions pat/api/http/requests.go → auth/api/http/pats/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/absmach/magistrala/pat"
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/pkg/apiutil"
)

Expand All @@ -17,15 +17,15 @@ type createPatReq struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Duration time.Duration `json:"duration,omitempty"`
Scope pat.Scope `json:"scope,omitempty"`
Scope auth.Scope `json:"scope,omitempty"`
}

func (cpr *createPatReq) UnmarshalJSON(data []byte) error {
var temp struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Duration string `json:"duration,omitempty"`
Scope pat.Scope `json:"scope,omitempty"`
Scope auth.Scope `json:"scope,omitempty"`
}
if err := json.Unmarshal(data, &temp); err != nil {
return err
Expand Down Expand Up @@ -184,10 +184,10 @@ func (req revokePatSecretReq) validate() (err error) {
type addPatScopeEntryReq struct {
token string
id string
PlatformEntityType pat.PlatformEntityType `json:"platform_entity_type,omitempty"`
PlatformEntityType auth.PlatformEntityType `json:"platform_entity_type,omitempty"`
OptionalDomainID string `json:"optional_domain_id,omitempty"`
OptionalDomainEntityType pat.DomainEntityType `json:"optional_domain_entity_type,omitempty"`
Operation pat.OperationType `json:"operation,omitempty"`
OptionalDomainEntityType auth.DomainEntityType `json:"optional_domain_entity_type,omitempty"`
Operation auth.OperationType `json:"operation,omitempty"`
EntityIDs []string `json:"entity_ids,omitempty"`
}

Expand All @@ -204,15 +204,15 @@ func (apser *addPatScopeEntryReq) UnmarshalJSON(data []byte) error {
return err
}

pet, err := pat.ParsePlatformEntityType(temp.PlatformEntityType)
pet, err := auth.ParsePlatformEntityType(temp.PlatformEntityType)
if err != nil {
return err
}
odt, err := pat.ParseDomainEntityType(temp.OptionalDomainEntityType)
odt, err := auth.ParseDomainEntityType(temp.OptionalDomainEntityType)
if err != nil {
return err
}
op, err := pat.ParseOperationType(temp.Operation)
op, err := auth.ParseOperationType(temp.Operation)
if err != nil {
return err
}
Expand All @@ -237,10 +237,10 @@ func (req addPatScopeEntryReq) validate() (err error) {
type removePatScopeEntryReq struct {
token string
id string
PlatformEntityType pat.PlatformEntityType `json:"platform_entity_type,omitempty"`
PlatformEntityType auth.PlatformEntityType `json:"platform_entity_type,omitempty"`
OptionalDomainID string `json:"optional_domain_id,omitempty"`
OptionalDomainEntityType pat.DomainEntityType `json:"optional_domain_entity_type,omitempty"`
Operation pat.OperationType `json:"operation,omitempty"`
OptionalDomainEntityType auth.DomainEntityType `json:"optional_domain_entity_type,omitempty"`
Operation auth.OperationType `json:"operation,omitempty"`
EntityIDs []string `json:"entity_ids,omitempty"`
}

Expand All @@ -257,15 +257,15 @@ func (rpser *removePatScopeEntryReq) UnmarshalJSON(data []byte) error {
return err
}

pet, err := pat.ParsePlatformEntityType(temp.PlatformEntityType)
pet, err := auth.ParsePlatformEntityType(temp.PlatformEntityType)
if err != nil {
return err
}
odt, err := pat.ParseDomainEntityType(temp.OptionalDomainEntityType)
odt, err := auth.ParseDomainEntityType(temp.OptionalDomainEntityType)
if err != nil {
return err
}
op, err := pat.ParseOperationType(temp.Operation)
op, err := auth.ParseOperationType(temp.Operation)
if err != nil {
return err
}
Expand Down Expand Up @@ -304,10 +304,10 @@ func (req clearAllScopeEntryReq) validate() (err error) {

type authorizePATReq struct {
token string
PlatformEntityType pat.PlatformEntityType `json:"platform_entity_type,omitempty"`
PlatformEntityType auth.PlatformEntityType `json:"platform_entity_type,omitempty"`
OptionalDomainID string `json:"optional_domain_id,omitempty"`
OptionalDomainEntityType pat.DomainEntityType `json:"optional_domain_entity_type,omitempty"`
Operation pat.OperationType `json:"operation,omitempty"`
OptionalDomainEntityType auth.DomainEntityType `json:"optional_domain_entity_type,omitempty"`
Operation auth.OperationType `json:"operation,omitempty"`
EntityIDs []string `json:"entity_ids,omitempty"`
}

Expand All @@ -327,22 +327,22 @@ func (tcpsr *authorizePATReq) UnmarshalJSON(data []byte) error {
tcpsr.OptionalDomainID = temp.OptionalDomainID
tcpsr.EntityIDs = temp.EntityIDs

pet, err := pat.ParsePlatformEntityType(temp.PlatformEntityType)
pet, err := auth.ParsePlatformEntityType(temp.PlatformEntityType)
if err != nil {
return err
}
tcpsr.PlatformEntityType = pet

if temp.OptionalDomainEntityType != "" {
odt, err := pat.ParseDomainEntityType(temp.OptionalDomainEntityType)
odt, err := auth.ParseDomainEntityType(temp.OptionalDomainEntityType)
if err != nil {
return err
}
tcpsr.OptionalDomainEntityType = odt
}

if temp.OptionalDomainID != "" {
op, err := pat.ParseOperationType(temp.Operation)
op, err := auth.ParseOperationType(temp.Operation)
if err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions pat/api/http/responses.go → auth/api/http/pats/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/http"

"github.com/absmach/magistrala"
"github.com/absmach/magistrala/pat"
"github.com/absmach/magistrala/auth"
)

var (
Expand All @@ -24,7 +24,7 @@ var (
)

type createPatRes struct {
pat.PAT
auth.PAT
}

func (res createPatRes) Code() int {
Expand All @@ -40,7 +40,7 @@ func (res createPatRes) Empty() bool {
}

type retrievePatRes struct {
pat.PAT
auth.PAT
}

func (res retrievePatRes) Code() int {
Expand All @@ -56,7 +56,7 @@ func (res retrievePatRes) Empty() bool {
}

type updatePatNameRes struct {
pat.PAT
auth.PAT
}

func (res updatePatNameRes) Code() int {
Expand All @@ -72,7 +72,7 @@ func (res updatePatNameRes) Empty() bool {
}

type updatePatDescriptionRes struct {
pat.PAT
auth.PAT
}

func (res updatePatDescriptionRes) Code() int {
Expand All @@ -88,7 +88,7 @@ func (res updatePatDescriptionRes) Empty() bool {
}

type listPatsRes struct {
pat.PATSPage
auth.PATSPage
}

func (res listPatsRes) Code() int {
Expand Down Expand Up @@ -118,7 +118,7 @@ func (res deletePatRes) Empty() bool {
}

type resetPatSecretRes struct {
pat.PAT
auth.PAT
}

func (res resetPatSecretRes) Code() int {
Expand Down Expand Up @@ -148,7 +148,7 @@ func (res revokePatSecretRes) Empty() bool {
}

type addPatScopeEntryRes struct {
pat.Scope
auth.Scope
}

func (res addPatScopeEntryRes) Code() int {
Expand All @@ -164,7 +164,7 @@ func (res addPatScopeEntryRes) Empty() bool {
}

type removePatScopeEntryRes struct {
pat.Scope
auth.Scope
}

func (res removePatScopeEntryRes) Code() int {
Expand Down
Loading

0 comments on commit 562a5e6

Please sign in to comment.