diff --git a/auth/mocks/service.go b/auth/mocks/service.go index 13d562629..40199b8cd 100644 --- a/auth/mocks/service.go +++ b/auth/mocks/service.go @@ -8,7 +8,7 @@ import ( "github.com/absmach/magistrala" "github.com/absmach/magistrala/pkg/errors" - svcerror "github.com/absmach/magistrala/pkg/errors/service" + svcerr "github.com/absmach/magistrala/pkg/errors/service" "github.com/stretchr/testify/mock" "google.golang.org/grpc" ) @@ -24,7 +24,7 @@ type Service struct { func (m *Service) Issue(ctx context.Context, in *magistrala.IssueReq, opts ...grpc.CallOption) (*magistrala.Token, error) { ret := m.Called(ctx, in) if in.GetUserId() == InvalidValue || in.GetUserId() == "" { - return &magistrala.Token{}, errors.ErrAuthentication + return &magistrala.Token{}, svcerr.ErrAuthentication } return ret.Get(0).(*magistrala.Token), ret.Error(1) @@ -33,7 +33,7 @@ func (m *Service) Issue(ctx context.Context, in *magistrala.IssueReq, opts ...gr func (m *Service) Refresh(ctx context.Context, in *magistrala.RefreshReq, opts ...grpc.CallOption) (*magistrala.Token, error) { ret := m.Called(ctx, in) if in.GetRefreshToken() == InvalidValue || in.GetRefreshToken() == "" { - return &magistrala.Token{}, svcerror.ErrAuthentication + return &magistrala.Token{}, svcerr.ErrAuthentication } return ret.Get(0).(*magistrala.Token), ret.Error(1) @@ -42,7 +42,7 @@ func (m *Service) Refresh(ctx context.Context, in *magistrala.RefreshReq, opts . func (m *Service) Identify(ctx context.Context, in *magistrala.IdentityReq, opts ...grpc.CallOption) (*magistrala.IdentityRes, error) { ret := m.Called(ctx, in) if in.GetToken() == InvalidValue || in.GetToken() == "" { - return &magistrala.IdentityRes{}, svcerror.ErrAuthentication + return &magistrala.IdentityRes{}, svcerr.ErrAuthentication } return ret.Get(0).(*magistrala.IdentityRes), ret.Error(1) @@ -51,7 +51,7 @@ func (m *Service) Identify(ctx context.Context, in *magistrala.IdentityReq, opts func (m *Service) Authorize(ctx context.Context, in *magistrala.AuthorizeReq, opts ...grpc.CallOption) (*magistrala.AuthorizeRes, error) { ret := m.Called(ctx, in) if in.GetSubject() == InvalidValue || in.GetSubject() == "" { - return &magistrala.AuthorizeRes{Authorized: false}, svcerror.ErrAuthorization + return &magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization } if in.GetObject() == InvalidValue || in.GetObject() == "" { return &magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization diff --git a/auth/postgres/domains.go b/auth/postgres/domains.go index eb9e05207..b8be03c67 100644 --- a/auth/postgres/domains.go +++ b/auth/postgres/domains.go @@ -16,7 +16,7 @@ import ( "github.com/absmach/magistrala/internal/postgres" "github.com/absmach/magistrala/pkg/clients" "github.com/absmach/magistrala/pkg/errors" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" "github.com/jackc/pgtype" "github.com/jmoiron/sqlx" ) @@ -42,24 +42,24 @@ func (repo domainRepo) Save(ctx context.Context, d auth.Domain) (ad auth.Domain, dbd, err := toDBDomains(d) if err != nil { - return auth.Domain{}, errors.Wrap(repoerror.ErrCreateEntity, errors.ErrRollbackTx) + return auth.Domain{}, errors.Wrap(repoerr.ErrCreateEntity, errors.ErrRollbackTx) } row, err := repo.db.NamedQueryContext(ctx, q, dbd) if err != nil { - return auth.Domain{}, errors.Wrap(repoerror.ErrCreateEntity, postgres.HandleError(err)) + return auth.Domain{}, errors.Wrap(repoerr.ErrCreateEntity, postgres.HandleError(err)) } defer row.Close() row.Next() dbd = dbDomain{} if err := row.StructScan(&dbd); err != nil { - return auth.Domain{},errors.Wrap(repoerror.ErrFailedOpDB, err) + return auth.Domain{},errors.Wrap(repoerr.ErrFailedOpDB, err) } domain, err := toDomain(dbd) if err != nil { - return auth.Domain{}, errors.Wrap(repoerror.ErrFailedOpDB, err) + return auth.Domain{}, errors.Wrap(repoerr.ErrFailedOpDB, err) } return domain, nil @@ -100,7 +100,7 @@ func (repo domainRepo) RetrieveAllByIDs(ctx context.Context, pm auth.Page) (auth } query, err := buildPageQuery(pm) if err != nil { - return auth.DomainsPage{}, errors.Wrap(repoerror.ErrFailedOpDB, err) + return auth.DomainsPage{}, errors.Wrap(repoerr.ErrFailedOpDB, err) } if query == "" { return auth.DomainsPage{}, nil @@ -148,7 +148,7 @@ func (repo domainRepo) ListDomains(ctx context.Context, pm auth.Page) (auth.Doma var q string query, err := buildPageQuery(pm) if err != nil { - return auth.DomainsPage{}, errors.Wrap(repoerror.ErrFailedOpDB, err) + return auth.DomainsPage{}, errors.Wrap(repoerr.ErrFailedOpDB, err) } if query == "" { return auth.DomainsPage{}, nil @@ -237,19 +237,19 @@ func (repo domainRepo) Update(ctx context.Context, id string, userID string, dr } row, err := repo.db.NamedQueryContext(ctx, q, dbd) if err != nil { - return auth.Domain{}, errors.Wrap(repoerror.ErrUpdateEntity, postgres.HandleError(err)) + return auth.Domain{}, errors.Wrap(repoerr.ErrUpdateEntity, postgres.HandleError(err)) } // defer row.Close() row.Next() dbd = dbDomain{} if err := row.StructScan(&dbd); err != nil { - return auth.Domain{}, errors.Wrap(repoerror.ErrFailedOpDB, err) + return auth.Domain{}, errors.Wrap(repoerr.ErrFailedOpDB, err) } domain, err := toDomain(dbd) if err != nil { - return auth.Domain{}, errors.Wrap(repoerror.ErrFailedOpDB, err) + return auth.Domain{}, errors.Wrap(repoerr.ErrFailedOpDB, err) } return domain, nil @@ -266,7 +266,7 @@ func (repo domainRepo) Delete(ctx context.Context, id string) error { row, err := repo.db.NamedQueryContext(ctx, q, nil) if err != nil { - return errors.Wrap(repoerror.ErrRemoveEntity, postgres.HandleError(err)) + return errors.Wrap(repoerr.ErrRemoveEntity, postgres.HandleError(err)) } defer row.Close() @@ -282,7 +282,7 @@ func (repo domainRepo) SavePolicies(ctx context.Context, pcs ...auth.Policy) err dbpc := toDBPolicies(pcs...) row, err := repo.db.NamedQueryContext(ctx, q, dbpc) if err != nil { - return errors.Wrap(repoerror.ErrCreateEntity, postgres.HandleError(err)) + return errors.Wrap(repoerr.ErrCreateEntity, postgres.HandleError(err)) } defer row.Close() @@ -306,7 +306,7 @@ func (repo domainRepo) CheckPolicy(ctx context.Context, pc auth.Policy) error { dbpc := toDBPolicy(pc) row, err := repo.db.NamedQueryContext(ctx, q, dbpc) if err != nil { - return errors.Wrap(repoerror.ErrCreateEntity, postgres.HandleError(err)) + return errors.Wrap(repoerr.ErrCreateEntity, postgres.HandleError(err)) } defer row.Close() row.Next() @@ -346,7 +346,7 @@ func (repo domainRepo) DeletePolicies(ctx context.Context, pcs ...auth.Policy) ( dbpc := toDBPolicy(pc) row, err := tx.NamedQuery(q, dbpc) if err != nil { - return errors.Wrap(repoerror.ErrRemoveEntity, postgres.HandleError(err)) + return errors.Wrap(repoerr.ErrRemoveEntity, postgres.HandleError(err)) } defer row.Close() } diff --git a/auth/service.go b/auth/service.go index ea47c8910..3fec53ca5 100644 --- a/auth/service.go +++ b/auth/service.go @@ -12,7 +12,7 @@ import ( "github.com/absmach/magistrala" "github.com/absmach/magistrala/pkg/clients" "github.com/absmach/magistrala/pkg/errors" - svcerror "github.com/absmach/magistrala/pkg/errors/service" + svcerr "github.com/absmach/magistrala/pkg/errors/service" ) const ( @@ -162,11 +162,11 @@ func (svc service) Identify(ctx context.Context, token string) (Key, error) { case APIKey: _, err := svc.keys.Retrieve(ctx, key.Issuer, key.ID) if err != nil { - return Key{}, svcerror.ErrAuthentication + return Key{}, svcerr.ErrAuthentication } return key, nil default: - return Key{}, svcerror.ErrAuthentication + return Key{}, svcerr.ErrAuthentication } } @@ -177,18 +177,18 @@ func (svc service) Authorize(ctx context.Context, pr PolicyReq) error { if pr.SubjectKind == TokenKind { key, err := svc.Identify(ctx, pr.Subject) if err != nil { - return errors.Wrap(svcerror.ErrAuthentication, err) + return errors.Wrap(svcerr.ErrAuthentication, err) } if key.Subject == "" { if pr.ObjectType == GroupType || pr.ObjectType == ThingType || pr.ObjectType == DomainType { return errors.ErrDomainAuthorization } - return svcerror.ErrAuthentication + return svcerr.ErrAuthentication } pr.Subject = key.Subject } if err := svc.agent.CheckPolicy(ctx, pr); err != nil { - return errors.Wrap(svcerror.ErrAuthorization, err) + return errors.Wrap(svcerr.ErrAuthorization, err) } return nil } @@ -310,7 +310,7 @@ func (svc service) accessKey(ctx context.Context, key Key) (Token, error) { key.Subject, err = svc.checkUserDomain(ctx, key) if err != nil { - return Token{}, errors.Wrap(svcerror.ErrAuthorization, err) + return Token{}, errors.Wrap(svcerr.ErrAuthorization, err) } access, err := svc.tokenizer.Issue(key) @@ -344,7 +344,7 @@ func (svc service) refreshKey(ctx context.Context, token string, key Key) (Token key.Subject, err = svc.checkUserDomain(ctx, key) if err != nil { - return Token{}, errors.Wrap(svcerror.ErrAuthorization, err) + return Token{}, errors.Wrap(svcerr.ErrAuthorization, err) } key.ExpiresAt = time.Now().Add(svc.loginDuration) @@ -422,11 +422,11 @@ func (svc service) userKey(ctx context.Context, token string, key Key) (Token, e func (svc service) authenticate(token string) (string, string, error) { key, err := svc.tokenizer.Parse(token) if err != nil { - return "", "", errors.Wrap(svcerror.ErrAuthentication, err) + return "", "", errors.Wrap(svcerr.ErrAuthentication, err) } // Only login key token is valid for login. if key.Type != AccessKey || key.Issuer == "" { - return "", "", svcerror.ErrAuthentication + return "", "", svcerr.ErrAuthentication } return key.Issuer, key.Subject, nil @@ -451,7 +451,7 @@ func SwitchToPermission(relation string) string { func (svc service) CreateDomain(ctx context.Context, token string, d Domain) (do Domain, err error) { key, err := svc.Identify(ctx, token) if err != nil { - return Domain{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Domain{}, errors.Wrap(svcerr.ErrAuthentication, err) } d.CreatedBy = key.User @@ -490,7 +490,7 @@ func (svc service) RetrieveDomain(ctx context.Context, token string, id string) ObjectType: DomainType, Permission: ViewPermission, }); err != nil { - return Domain{}, errors.Wrap(svcerror.ErrAuthorization, err) + return Domain{}, errors.Wrap(svcerr.ErrAuthorization, err) } return svc.domains.RetrieveByID(ctx, id) @@ -499,7 +499,7 @@ func (svc service) RetrieveDomain(ctx context.Context, token string, id string) func (svc service) UpdateDomain(ctx context.Context, token string, id string, d DomainReq) (Domain, error) { key, err := svc.Identify(ctx, token) if err != nil { - return Domain{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Domain{}, errors.Wrap(svcerr.ErrAuthentication, err) } if err := svc.Authorize(ctx, PolicyReq{ Subject: key.Subject, @@ -509,7 +509,7 @@ func (svc service) UpdateDomain(ctx context.Context, token string, id string, d ObjectType: DomainType, Permission: EditPermission, }); err != nil { - return Domain{}, errors.Wrap(svcerror.ErrAuthorization, err) + return Domain{}, errors.Wrap(svcerr.ErrAuthorization, err) } return svc.domains.Update(ctx, id, key.User, d) } @@ -517,7 +517,7 @@ func (svc service) UpdateDomain(ctx context.Context, token string, id string, d func (svc service) ChangeDomainStatus(ctx context.Context, token string, id string, d DomainReq) (Domain, error) { key, err := svc.Identify(ctx, token) if err != nil { - return Domain{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Domain{}, errors.Wrap(svcerr.ErrAuthentication, err) } if err := svc.Authorize(ctx, PolicyReq{ Subject: key.Subject, @@ -527,7 +527,7 @@ func (svc service) ChangeDomainStatus(ctx context.Context, token string, id stri ObjectType: DomainType, Permission: AdminPermission, }); err != nil { - return Domain{}, errors.Wrap(svcerror.ErrAuthorization, err) + return Domain{}, errors.Wrap(svcerr.ErrAuthorization, err) } return svc.domains.Update(ctx, id, key.User, d) } @@ -535,7 +535,7 @@ func (svc service) ChangeDomainStatus(ctx context.Context, token string, id stri func (svc service) ListDomains(ctx context.Context, token string, p Page) (DomainsPage, error) { key, err := svc.Identify(ctx, token) if err != nil { - return DomainsPage{}, errors.Wrap(svcerror.ErrAuthentication, err) + return DomainsPage{}, errors.Wrap(svcerr.ErrAuthentication, err) } p.SubjectID = key.User if err := svc.Authorize(ctx, PolicyReq{ @@ -581,7 +581,7 @@ func (svc service) AssignUsers(ctx context.Context, token string, id string, use Object: MagistralaObject, ObjectType: PlatformType, }); err != nil { - return errors.Wrap(svcerror.ErrMalformedEntity, fmt.Errorf("invalid user id : %s ", userID)) + return errors.Wrap(svcerr.ErrMalformedEntity, fmt.Errorf("invalid user id : %s ", userID)) } } @@ -618,7 +618,7 @@ func (svc service) UnassignUsers(ctx context.Context, token string, id string, u func (svc service) ListUserDomains(ctx context.Context, token string, userID string, p Page) (DomainsPage, error) { res, err := svc.Identify(ctx, token) if err != nil { - return DomainsPage{}, errors.Wrap(svcerror.ErrAuthentication, err) + return DomainsPage{}, errors.Wrap(svcerr.ErrAuthentication, err) } if err := svc.Authorize(ctx, PolicyReq{ Subject: res.User, @@ -627,7 +627,7 @@ func (svc service) ListUserDomains(ctx context.Context, token string, userID str Object: MagistralaObject, ObjectType: PlatformType, }); err != nil { - return DomainsPage{}, errors.Wrap(svcerror.ErrAuthorization, err) + return DomainsPage{}, errors.Wrap(svcerr.ErrAuthorization, err) } if userID != "" && res.User != userID { p.SubjectID = userID diff --git a/auth/spicedb/policies.go b/auth/spicedb/policies.go index 024f46870..d4183cab1 100644 --- a/auth/spicedb/policies.go +++ b/auth/spicedb/policies.go @@ -11,8 +11,8 @@ import ( "github.com/absmach/magistrala/auth" mglog "github.com/absmach/magistrala/logger" "github.com/absmach/magistrala/pkg/errors" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" - svcerror "github.com/absmach/magistrala/pkg/errors/service" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" + svcerr "github.com/absmach/magistrala/pkg/errors/service" v1 "github.com/authzed/authzed-go/proto/authzed/api/v1" "github.com/authzed/authzed-go/v1" ) @@ -51,13 +51,13 @@ func (pa *policyAgent) CheckPolicy(ctx context.Context, pr auth.PolicyReq) error resp, err := pa.permissionClient.CheckPermission(ctx, &checkReq) if err != nil { - return errors.Wrap(svcerror.ErrMalformedEntity, errors.Wrap(errPermission, err)) + return errors.Wrap(svcerr.ErrMalformedEntity, errors.Wrap(errPermission, err)) } if resp.Permissionship == v1.CheckPermissionResponse_PERMISSIONSHIP_HAS_PERMISSION { return nil } if reason, ok := v1.CheckPermissionResponse_Permissionship_name[int32(resp.Permissionship)]; ok { - return errors.Wrap(svcerror.ErrAuthorization, errors.New(reason)) + return errors.Wrap(svcerr.ErrAuthorization, errors.New(reason)) } return errors.ErrAuthorization } @@ -85,7 +85,7 @@ func (pa *policyAgent) AddPolicies(ctx context.Context, prs []auth.PolicyReq) er } _, err := pa.permissionClient.WriteRelationships(ctx, &v1.WriteRelationshipsRequest{Updates: updates, OptionalPreconditions: preconds}) if err != nil { - return errors.Wrap(svcerror.ErrMalformedEntity, errors.Wrap(errAddPolicies, err)) + return errors.Wrap(svcerr.ErrMalformedEntity, errors.Wrap(errAddPolicies, err)) } return nil } @@ -108,7 +108,7 @@ func (pa *policyAgent) AddPolicy(ctx context.Context, pr auth.PolicyReq) error { } _, err = pa.permissionClient.WriteRelationships(ctx, &v1.WriteRelationshipsRequest{Updates: updates, OptionalPreconditions: precond}) if err != nil { - return errors.Wrap(svcerror.ErrMalformedEntity, errors.Wrap(errAddPolicies, err)) + return errors.Wrap(svcerr.ErrMalformedEntity, errors.Wrap(errAddPolicies, err)) } return nil } @@ -130,7 +130,7 @@ func (pa *policyAgent) DeletePolicies(ctx context.Context, prs []auth.PolicyReq) } _, err := pa.permissionClient.WriteRelationships(ctx, &v1.WriteRelationshipsRequest{Updates: updates}) if err != nil { - return errors.Wrap(svcerror.ErrMalformedEntity, errors.Wrap(errRemovePolicies, err)) + return errors.Wrap(svcerr.ErrMalformedEntity, errors.Wrap(errRemovePolicies, err)) } return nil } @@ -151,7 +151,7 @@ func (pa *policyAgent) DeletePolicy(ctx context.Context, pr auth.PolicyReq) erro }, } if _, err := pa.permissionClient.DeleteRelationships(ctx, req); err != nil { - return errors.Wrap(svcerror.ErrMalformedEntity, errors.Wrap(errRemovePolicies, err)) + return errors.Wrap(svcerr.ErrMalformedEntity, errors.Wrap(errRemovePolicies, err)) } return nil } @@ -169,7 +169,7 @@ func (pa *policyAgent) RetrieveObjects(ctx context.Context, pr auth.PolicyReq, n } stream, err := pa.permissionClient.LookupResources(ctx, resourceReq) if err != nil { - return nil, "", errors.Wrap(repoerror.ErrMalformedEntity, errors.Wrap(errRetrievePolicies, err)) + return nil, "", errors.Wrap(repoerr.ErrMalformedEntity, errors.Wrap(errRetrievePolicies, err)) } resources := []*v1.LookupResourcesResponse{} var token string @@ -187,7 +187,7 @@ func (pa *policyAgent) RetrieveObjects(ctx context.Context, pr auth.PolicyReq, n if len(resources) > 0 && resources[len(resources)-1].AfterResultCursor != nil { token = resources[len(resources)-1].AfterResultCursor.Token } - return objectsToAuthPolicies(resources), token, errors.Wrap(repoerror.ErrViewEntity, err) + return objectsToAuthPolicies(resources), token, errors.Wrap(repoerr.ErrViewEntity, err) } } } @@ -200,7 +200,7 @@ func (pa *policyAgent) RetrieveAllObjects(ctx context.Context, pr auth.PolicyReq } stream, err := pa.permissionClient.LookupResources(ctx, resourceReq) if err != nil { - return nil, errors.Wrap(svcerror.ErrMalformedEntity, errors.Wrap(errRetrievePolicies, err)) + return nil, errors.Wrap(svcerr.ErrMalformedEntity, errors.Wrap(errRetrievePolicies, err)) } tuples := []auth.PolicyRes{} for { @@ -247,7 +247,7 @@ func (pa *policyAgent) RetrieveSubjects(ctx context.Context, pr auth.PolicyReq, } stream, err := pa.permissionClient.LookupSubjects(ctx, &subjectsReq) if err != nil { - return nil, "", errors.Wrap(svcerror.ErrMalformedEntity, errors.Wrap(errRetrievePolicies, err)) + return nil, "", errors.Wrap(svcerr.ErrMalformedEntity, errors.Wrap(errRetrievePolicies, err)) } subjects := []*v1.LookupSubjectsResponse{} var token string @@ -266,7 +266,7 @@ func (pa *policyAgent) RetrieveSubjects(ctx context.Context, pr auth.PolicyReq, if len(subjects) > 0 && subjects[len(subjects)-1].AfterResultCursor != nil { token = subjects[len(subjects)-1].AfterResultCursor.Token } - return subjectsToAuthPolicies(subjects), token, errors.Wrap(repoerror.ErrViewEntity, err) + return subjectsToAuthPolicies(subjects), token, errors.Wrap(repoerr.ErrViewEntity, err) } } } diff --git a/bootstrap/service.go b/bootstrap/service.go index 9492f7edd..8800714a5 100644 --- a/bootstrap/service.go +++ b/bootstrap/service.go @@ -12,7 +12,7 @@ import ( "github.com/absmach/magistrala" "github.com/absmach/magistrala/pkg/errors" - svcerror "github.com/absmach/magistrala/pkg/errors/service" + svcerr "github.com/absmach/magistrala/pkg/errors/service" mgsdk "github.com/absmach/magistrala/pkg/sdk/go" ) @@ -123,7 +123,7 @@ func New(auth magistrala.AuthServiceClient, configs ConfigRepository, sdk mgsdk. func (bs bootstrapService) Add(ctx context.Context, token string, cfg Config) (Config, error) { owner, err := bs.identify(ctx, token) if err != nil { - return Config{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Config{}, errors.Wrap(svcerr.ErrAuthentication, err) } toConnect := bs.toIDList(cfg.Channels) @@ -170,7 +170,7 @@ func (bs bootstrapService) Add(ctx context.Context, token string, cfg Config) (C func (bs bootstrapService) View(ctx context.Context, token, id string) (Config, error) { owner, err := bs.identify(ctx, token) if err != nil { - return Config{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Config{}, errors.Wrap(svcerr.ErrAuthentication, err) } return bs.configs.RetrieveByID(ctx, owner, id) @@ -179,7 +179,7 @@ func (bs bootstrapService) View(ctx context.Context, token, id string) (Config, func (bs bootstrapService) Update(ctx context.Context, token string, cfg Config) error { owner, err := bs.identify(ctx, token) if err != nil { - return errors.Wrap(svcerror.ErrAuthentication, err) + return errors.Wrap(svcerr.ErrAuthentication, err) } cfg.Owner = owner @@ -190,7 +190,7 @@ func (bs bootstrapService) Update(ctx context.Context, token string, cfg Config) func (bs bootstrapService) UpdateCert(ctx context.Context, token, thingID, clientCert, clientKey, caCert string) (Config, error) { owner, err := bs.identify(ctx, token) if err != nil { - return Config{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Config{}, errors.Wrap(svcerr.ErrAuthentication, err) } cfg, err := bs.configs.UpdateCert(ctx, owner, thingID, clientCert, clientKey, caCert) if err != nil { @@ -202,7 +202,7 @@ func (bs bootstrapService) UpdateCert(ctx context.Context, token, thingID, clien func (bs bootstrapService) UpdateConnections(ctx context.Context, token, id string, connections []string) error { owner, err := bs.identify(ctx, token) if err != nil { - return errors.Wrap(svcerror.ErrAuthentication, err) + return errors.Wrap(svcerr.ErrAuthentication, err) } cfg, err := bs.configs.RetrieveByID(ctx, owner, id) @@ -256,7 +256,7 @@ func (bs bootstrapService) UpdateConnections(ctx context.Context, token, id stri func (bs bootstrapService) List(ctx context.Context, token string, filter Filter, offset, limit uint64) (ConfigsPage, error) { owner, err := bs.identify(ctx, token) if err != nil { - return ConfigsPage{}, errors.Wrap(svcerror.ErrAuthentication, err) + return ConfigsPage{}, errors.Wrap(svcerr.ErrAuthentication, err) } return bs.configs.RetrieveAll(ctx, owner, filter, offset, limit), nil @@ -265,7 +265,7 @@ func (bs bootstrapService) List(ctx context.Context, token string, filter Filter func (bs bootstrapService) Remove(ctx context.Context, token, id string) error { owner, err := bs.identify(ctx, token) if err != nil { - return errors.Wrap(svcerror.ErrAuthentication, err) + return errors.Wrap(svcerr.ErrAuthentication, err) } if err := bs.configs.Remove(ctx, owner, id); err != nil { return errors.Wrap(errRemoveBootstrap, err) @@ -295,7 +295,7 @@ func (bs bootstrapService) Bootstrap(ctx context.Context, externalKey, externalI func (bs bootstrapService) ChangeState(ctx context.Context, token, id string, state State) error { owner, err := bs.identify(ctx, token) if err != nil { - return errors.Wrap(svcerror.ErrAuthentication, err) + return errors.Wrap(svcerr.ErrAuthentication, err) } cfg, err := bs.configs.RetrieveByID(ctx, owner, id) @@ -368,7 +368,7 @@ func (bs bootstrapService) identify(ctx context.Context, token string) (string, res, err := bs.auth.Identify(ctx, &magistrala.IdentityReq{Token: token}) if err != nil { - return "", errors.Wrap(svcerror.ErrAuthentication, err) + return "", errors.Wrap(svcerr.ErrAuthentication, err) } return res.GetId(), nil diff --git a/certs/service.go b/certs/service.go index 227ed7759..7b10eded7 100644 --- a/certs/service.go +++ b/certs/service.go @@ -10,8 +10,8 @@ import ( "github.com/absmach/magistrala" "github.com/absmach/magistrala/certs/pki" "github.com/absmach/magistrala/pkg/errors" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" - svcerror "github.com/absmach/magistrala/pkg/errors/service" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" + svcerr "github.com/absmach/magistrala/pkg/errors/service" mgsdk "github.com/absmach/magistrala/pkg/sdk/go" ) @@ -86,7 +86,7 @@ type Cert struct { func (cs *certsService) IssueCert(ctx context.Context, token, thingID string, ttl string) (Cert, error) { owner, err := cs.auth.Identify(ctx, &magistrala.IdentityReq{Token: token}) if err != nil { - return Cert{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Cert{}, errors.Wrap(svcerr.ErrAuthentication, err) } thing, err := cs.sdk.Thing(thingID, token) @@ -119,7 +119,7 @@ func (cs *certsService) RevokeCert(ctx context.Context, token, thingID string) ( var revoke Revoke u, err := cs.auth.Identify(ctx, &magistrala.IdentityReq{Token: token}) if err != nil { - return revoke, errors.Wrap(svcerror.ErrAuthentication, err) + return revoke, errors.Wrap(svcerr.ErrAuthentication, err) } thing, err := cs.sdk.Thing(thingID, token) if err != nil { @@ -149,12 +149,12 @@ func (cs *certsService) RevokeCert(ctx context.Context, token, thingID string) ( func (cs *certsService) ListCerts(ctx context.Context, token, thingID string, offset, limit uint64) (Page, error) { u, err := cs.auth.Identify(ctx, &magistrala.IdentityReq{Token: token}) if err != nil { - return Page{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Page{}, errors.Wrap(svcerr.ErrAuthentication, err) } cp, err := cs.certsRepo.RetrieveByThing(ctx, u.GetId(), thingID, offset, limit) if err != nil { - return Page{}, errors.Wrap(repoerror.ErrNotFound, err) + return Page{}, errors.Wrap(repoerr.ErrNotFound, err) } for i, cert := range cp.Certs { @@ -172,7 +172,7 @@ func (cs *certsService) ListCerts(ctx context.Context, token, thingID string, of func (cs *certsService) ListSerials(ctx context.Context, token, thingID string, offset, limit uint64) (Page, error) { u, err := cs.auth.Identify(ctx, &magistrala.IdentityReq{Token: token}) if err != nil { - return Page{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Page{}, errors.Wrap(svcerr.ErrAuthentication, err) } return cs.certsRepo.RetrieveByThing(ctx, u.GetId(), thingID, offset, limit) @@ -181,12 +181,12 @@ func (cs *certsService) ListSerials(ctx context.Context, token, thingID string, func (cs *certsService) ViewCert(ctx context.Context, token, serialID string) (Cert, error) { u, err := cs.auth.Identify(ctx, &magistrala.IdentityReq{Token: token}) if err != nil { - return Cert{}, errors.Wrap(svcerror.ErrAuthentication, err) + return Cert{}, errors.Wrap(svcerr.ErrAuthentication, err) } cert, err := cs.certsRepo.RetrieveBySerial(ctx, u.GetId(), serialID) if err != nil { - return Cert{}, errors.Wrap(repoerror.ErrNotFound, err) + return Cert{}, errors.Wrap(repoerr.ErrNotFound, err) } vcert, err := cs.pki.Read(serialID) diff --git a/things/mocks/auth.go b/things/mocks/auth.go index 25b19cdfc..41f56076d 100644 --- a/things/mocks/auth.go +++ b/things/mocks/auth.go @@ -7,7 +7,7 @@ import ( "context" "github.com/absmach/magistrala" - svcerror "github.com/absmach/magistrala/pkg/errors/service" + svcerr "github.com/absmach/magistrala/pkg/errors/service" "github.com/stretchr/testify/mock" "google.golang.org/grpc" ) @@ -21,10 +21,10 @@ type Service struct { func (m *Service) Authorize(ctx context.Context, in *magistrala.AuthorizeReq, opts ...grpc.CallOption) (*magistrala.AuthorizeRes, error) { ret := m.Called(ctx, in) if in.GetSubject() == WrongID || in.GetSubject() == "" { - return &magistrala.AuthorizeRes{}, svcerror.ErrAuthorization + return &magistrala.AuthorizeRes{}, svcerr.ErrAuthorization } if in.GetObject() == WrongID || in.GetObject() == "" { - return &magistrala.AuthorizeRes{}, svcerror.ErrAuthorization + return &magistrala.AuthorizeRes{}, svcerr.ErrAuthorization } return ret.Get(0).(*magistrala.AuthorizeRes), ret.Error(1) diff --git a/things/mocks/clients.go b/things/mocks/clients.go index 58e9ce631..eddda9ac9 100644 --- a/things/mocks/clients.go +++ b/things/mocks/clients.go @@ -7,7 +7,7 @@ import ( "context" mgclients "github.com/absmach/magistrala/pkg/clients" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" "github.com/stretchr/testify/mock" ) @@ -28,11 +28,11 @@ func (m *Repository) ChangeStatus(ctx context.Context, client mgclients.Client) ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } if client.Status != mgclients.EnabledStatus && client.Status != mgclients.DisabledStatus { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -41,7 +41,7 @@ func (m *Repository) ChangeStatus(ctx context.Context, client mgclients.Client) func (m *Repository) Members(ctx context.Context, groupID string, pm mgclients.Page) (mgclients.MembersPage, error) { ret := m.Called(ctx, groupID, pm) if groupID == WrongID { - return mgclients.MembersPage{}, repoerror.ErrNotFound + return mgclients.MembersPage{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.MembersPage), ret.Error(1) @@ -63,7 +63,7 @@ func (m *Repository) RetrieveByID(ctx context.Context, id string) (mgclients.Cli ret := m.Called(ctx, id) if id == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -73,7 +73,7 @@ func (m *Repository) RetrieveBySecret(ctx context.Context, secret string) (mgcli ret := m.Called(ctx, secret) if secret == "" { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -83,7 +83,7 @@ func (m *Repository) Save(ctx context.Context, clis ...mgclients.Client) ([]mgcl ret := m.Called(ctx, clis) for _, cli := range clis { if cli.Owner == WrongID { - return []mgclients.Client{}, repoerror.ErrMalformedEntity + return []mgclients.Client{}, repoerr.ErrMalformedEntity } } return clis, ret.Error(1) @@ -93,7 +93,7 @@ func (m *Repository) Update(ctx context.Context, client mgclients.Client) (mgcli ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.Client), ret.Error(1) } @@ -102,10 +102,10 @@ func (m *Repository) UpdateIdentity(ctx context.Context, client mgclients.Client ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } if client.Credentials.Identity == "" { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -115,10 +115,10 @@ func (m *Repository) UpdateSecret(ctx context.Context, client mgclients.Client) ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } if client.Credentials.Secret == "" { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -128,7 +128,7 @@ func (m *Repository) UpdateTags(ctx context.Context, client mgclients.Client) (m ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -138,7 +138,7 @@ func (m *Repository) UpdateOwner(ctx context.Context, client mgclients.Client) ( ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.Client), ret.Error(1) diff --git a/things/mocks/things.go b/things/mocks/things.go index e99c5cb0d..bcb3d715d 100644 --- a/things/mocks/things.go +++ b/things/mocks/things.go @@ -7,7 +7,7 @@ import ( "context" "sync" - "github.com/absmach/magistrala/pkg/errors" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" "github.com/absmach/magistrala/things" ) @@ -37,7 +37,7 @@ func (tcm *clientCacheMock) ID(_ context.Context, key string) (string, error) { id, ok := tcm.things[key] if !ok { - return "", errors.ErrNotFound + return "", repoerr.ErrNotFound } return id, nil diff --git a/things/postgres/clients.go b/things/postgres/clients.go index f3c2274ab..e68d15680 100644 --- a/things/postgres/clients.go +++ b/things/postgres/clients.go @@ -12,7 +12,7 @@ import ( mgclients "github.com/absmach/magistrala/pkg/clients" pgclients "github.com/absmach/magistrala/pkg/clients/postgres" "github.com/absmach/magistrala/pkg/errors" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" ) var _ mgclients.Repository = (*clientRepo)(nil) @@ -43,7 +43,7 @@ func NewRepository(db postgres.Database) Repository { func (repo clientRepo) Save(ctx context.Context, cs ...mgclients.Client) ([]mgclients.Client, error) { tx, err := repo.ClientRepository.DB.BeginTxx(ctx, nil) if err != nil { - return []mgclients.Client{}, errors.Wrap(repoerror.ErrCreateEntity, err) + return []mgclients.Client{}, errors.Wrap(repoerr.ErrCreateEntity, err) } var clients []mgclients.Client @@ -54,32 +54,32 @@ func (repo clientRepo) Save(ctx context.Context, cs ...mgclients.Client) ([]mgcl dbcli, err := pgclients.ToDBClient(cli) if err != nil { - return []mgclients.Client{}, errors.Wrap(repoerror.ErrCreateEntity, err) + return []mgclients.Client{}, errors.Wrap(repoerr.ErrCreateEntity, err) } row, err := repo.ClientRepository.DB.NamedQueryContext(ctx, q, dbcli) if err != nil { if err := tx.Rollback(); err != nil { - return []mgclients.Client{}, errors.Wrap(repoerror.ErrCreateEntity, postgres.HandleError(err)) + return []mgclients.Client{}, errors.Wrap(repoerr.ErrCreateEntity, postgres.HandleError(err)) } - return []mgclients.Client{}, errors.Wrap(repoerror.ErrCreateEntity, err) + return []mgclients.Client{}, errors.Wrap(repoerr.ErrCreateEntity, err) } defer row.Close() row.Next() dbcli = pgclients.DBClient{} if err := row.StructScan(&dbcli); err != nil { - return []mgclients.Client{}, errors.Wrap(repoerror.ErrFailedOpDB, err) + return []mgclients.Client{}, errors.Wrap(repoerr.ErrFailedOpDB, err) } client, err := pgclients.ToClient(dbcli) if err != nil { - return []mgclients.Client{}, errors.Wrap(repoerror.ErrFailedOpDB, err) + return []mgclients.Client{}, errors.Wrap(repoerr.ErrFailedOpDB, err) } clients = append(clients, client) } if err = tx.Commit(); err != nil { - return []mgclients.Client{}, errors.Wrap(repoerror.ErrCreateEntity, err) + return []mgclients.Client{}, errors.Wrap(repoerr.ErrCreateEntity, err) } return clients, nil @@ -96,9 +96,9 @@ func (repo clientRepo) RetrieveBySecret(ctx context.Context, key string) (mgclie if err := repo.DB.QueryRowxContext(ctx, q, key).StructScan(&dbc); err != nil { if err == sql.ErrNoRows { - return mgclients.Client{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrNotFound, err) } - return mgclients.Client{}, errors.Wrap(repoerror.ErrViewEntity, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrViewEntity, err) } return pgclients.ToClient(dbc) diff --git a/things/service.go b/things/service.go index c67cc78c6..13e0694b8 100644 --- a/things/service.go +++ b/things/service.go @@ -10,8 +10,8 @@ import ( "github.com/absmach/magistrala/auth" mgclients "github.com/absmach/magistrala/pkg/clients" "github.com/absmach/magistrala/pkg/errors" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" - svcerror "github.com/absmach/magistrala/pkg/errors/service" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" + svcerr "github.com/absmach/magistrala/pkg/errors/service" mggroups "github.com/absmach/magistrala/pkg/groups" "github.com/absmach/magistrala/things/postgres" ) @@ -43,7 +43,7 @@ func NewService(uauth magistrala.AuthServiceClient, c postgres.Repository, grepo func (svc service) Authorize(ctx context.Context, req *magistrala.AuthorizeReq) (string, error) { thingID, err := svc.Identify(ctx, req.GetSubject()) if err != nil { - return "", errors.Wrap(svcerror.ErrAuthentication, err) + return "", errors.Wrap(svcerr.ErrAuthentication, err) } r := &magistrala.AuthorizeReq{ @@ -67,7 +67,7 @@ func (svc service) Authorize(ctx context.Context, req *magistrala.AuthorizeReq) func (svc service) CreateThings(ctx context.Context, token string, cls ...mgclients.Client) ([]mgclients.Client, error) { user, err := svc.identify(ctx, token) if err != nil { - return []mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return []mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } var clients []mgclients.Client for _, c := range cls { @@ -94,7 +94,7 @@ func (svc service) CreateThings(ctx context.Context, token string, cls ...mgclie saved, err := svc.clients.Save(ctx, clients...) if err != nil { - return nil, errors.Wrap(repoerror.ErrCreateEntity, err) + return nil, errors.Wrap(repoerr.ErrCreateEntity, err) } policies := magistrala.AddPoliciesReq{} @@ -127,7 +127,7 @@ func (svc service) CreateThings(ctx context.Context, token string, cls ...mgclie func (svc service) ViewClient(ctx context.Context, token string, id string) (mgclients.Client, error) { _, err := svc.authorize(ctx, auth.UserType, auth.TokenKind, token, auth.ViewPermission, auth.ThingType, id) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthorization, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthorization, err) } return svc.clients.RetrieveByID(ctx, id) @@ -138,7 +138,7 @@ func (svc service) ListClients(ctx context.Context, token string, reqUserID stri res, err := svc.identify(ctx, token) if err != nil { - return mgclients.ClientsPage{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.ClientsPage{}, errors.Wrap(svcerr.ErrAuthentication, err) } switch { @@ -149,16 +149,16 @@ func (svc service) ListClients(ctx context.Context, token string, reqUserID stri } rtids, err := svc.listClientIDs(ctx, auth.EncodeDomainUserID(res.GetDomainId(), reqUserID), pm.Permission) if err != nil { - return mgclients.ClientsPage{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.ClientsPage{}, errors.Wrap(repoerr.ErrNotFound, err) } ids, err = svc.filterAllowedThingIDs(ctx, res.GetId(), pm.Permission, rtids) if err != nil { - return mgclients.ClientsPage{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.ClientsPage{}, errors.Wrap(repoerr.ErrNotFound, err) } default: ids, err = svc.listClientIDs(ctx, res.GetId(), pm.Permission) if err != nil { - return mgclients.ClientsPage{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.ClientsPage{}, errors.Wrap(repoerr.ErrNotFound, err) } } @@ -181,7 +181,7 @@ func (svc service) listClientIDs(ctx context.Context, userID, permission string) ObjectType: auth.ThingType, }) if err != nil { - return nil, errors.Wrap(repoerror.ErrNotFound, err) + return nil, errors.Wrap(repoerr.ErrNotFound, err) } return tids.Policies, nil } @@ -195,7 +195,7 @@ func (svc service) filterAllowedThingIDs(ctx context.Context, userID, permission ObjectType: auth.ThingType, }) if err != nil { - return nil, errors.Wrap(repoerror.ErrNotFound, err) + return nil, errors.Wrap(repoerr.ErrNotFound, err) } for _, thingID := range thingIDs { for _, tid := range tids.Policies { @@ -210,7 +210,7 @@ func (svc service) filterAllowedThingIDs(ctx context.Context, userID, permission func (svc service) UpdateClient(ctx context.Context, token string, cli mgclients.Client) (mgclients.Client, error) { userID, err := svc.authorize(ctx, auth.UserType, auth.TokenKind, token, auth.EditPermission, auth.ThingType, cli.ID) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthorization, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthorization, err) } client := mgclients.Client{ @@ -226,7 +226,7 @@ func (svc service) UpdateClient(ctx context.Context, token string, cli mgclients func (svc service) UpdateClientTags(ctx context.Context, token string, cli mgclients.Client) (mgclients.Client, error) { userID, err := svc.authorize(ctx, auth.UserType, auth.TokenKind, token, auth.EditPermission, auth.ThingType, cli.ID) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthorization, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthorization, err) } client := mgclients.Client{ @@ -241,7 +241,7 @@ func (svc service) UpdateClientTags(ctx context.Context, token string, cli mgcli func (svc service) UpdateClientSecret(ctx context.Context, token, id, key string) (mgclients.Client, error) { userID, err := svc.authorize(ctx, auth.UserType, auth.TokenKind, token, auth.EditPermission, auth.ThingType, id) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthorization, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthorization, err) } client := mgclients.Client{ @@ -259,7 +259,7 @@ func (svc service) UpdateClientSecret(ctx context.Context, token, id, key string func (svc service) UpdateClientOwner(ctx context.Context, token string, cli mgclients.Client) (mgclients.Client, error) { userID, err := svc.authorize(ctx, auth.UserType, auth.TokenKind, token, auth.EditPermission, auth.ThingType, cli.ID) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthorization, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthorization, err) } client := mgclients.Client{ @@ -298,7 +298,7 @@ func (svc service) DisableClient(ctx context.Context, token, id string) (mgclien } if err := svc.clientCache.Remove(ctx, client.ID); err != nil { - return client, errors.Wrap(repoerror.ErrRemoveEntity, err) + return client, errors.Wrap(repoerr.ErrRemoveEntity, err) } return client, nil @@ -310,7 +310,7 @@ func (svc service) Share(ctx context.Context, token, id, relation string, userid return nil } if _, err := svc.authorize(ctx, auth.UserType, auth.UsersKind, user.GetId(), auth.DeletePermission, auth.ThingType, id); err != nil { - return errors.Wrap(svcerror.ErrAuthorization, err) + return errors.Wrap(svcerr.ErrAuthorization, err) } policies := magistrala.AddPoliciesReq{} @@ -339,7 +339,7 @@ func (svc service) Unshare(ctx context.Context, token, id, relation string, user return nil } if _, err := svc.authorize(ctx, auth.UserType, auth.UsersKind, user.GetId(), auth.DeletePermission, auth.ThingType, id); err != nil { - return errors.Wrap(svcerror.ErrAuthorization, err) + return errors.Wrap(svcerr.ErrAuthorization, err) } policies := magistrala.DeletePoliciesReq{} @@ -365,11 +365,11 @@ func (svc service) Unshare(ctx context.Context, token, id, relation string, user func (svc service) changeClientStatus(ctx context.Context, token string, client mgclients.Client) (mgclients.Client, error) { userID, err := svc.authorize(ctx, auth.UserType, auth.TokenKind, token, auth.DeletePermission, auth.ThingType, client.ID) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthorization, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthorization, err) } dbClient, err := svc.clients.RetrieveByID(ctx, client.ID) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrNotFound, err) } if dbClient.Status == client.Status { return mgclients.Client{}, mgclients.ErrStatusAlreadyAssigned @@ -391,14 +391,14 @@ func (svc service) ListClientsByGroup(ctx context.Context, token, groupID string ObjectType: auth.ThingType, }) if err != nil { - return mgclients.MembersPage{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.MembersPage{}, errors.Wrap(repoerr.ErrNotFound, err) } pm.IDs = tids.Policies cp, err := svc.clients.RetrieveAllByIDs(ctx, pm) if err != nil { - return mgclients.MembersPage{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.MembersPage{}, errors.Wrap(repoerr.ErrNotFound, err) } return mgclients.MembersPage{ @@ -415,10 +415,10 @@ func (svc service) Identify(ctx context.Context, key string) (string, error) { client, err := svc.clients.RetrieveBySecret(ctx, key) if err != nil { - return "", errors.Wrap(repoerror.ErrNotFound, err) + return "", errors.Wrap(repoerr.ErrNotFound, err) } if err := svc.clientCache.Save(ctx, key, client.ID); err != nil { - return "", errors.Wrap(repoerror.ErrUpdateEntity, err) + return "", errors.Wrap(repoerr.ErrUpdateEntity, err) } return client.ID, nil diff --git a/things/standalone/standalone.go b/things/standalone/standalone.go index 9aa11549a..a0657ac4e 100644 --- a/things/standalone/standalone.go +++ b/things/standalone/standalone.go @@ -7,7 +7,7 @@ import ( "context" "github.com/absmach/magistrala" - "github.com/absmach/magistrala/pkg/errors" + svcerr "github.com/absmach/magistrala/pkg/errors" "google.golang.org/grpc" ) @@ -40,7 +40,7 @@ func (repo singleUserRepo) Issue(ctx context.Context, in *magistrala.IssueReq, o func (repo singleUserRepo) Identify(ctx context.Context, in *magistrala.IdentityReq, opts ...grpc.CallOption) (*magistrala.IdentityRes, error) { if repo.token != in.GetToken() { - return nil, errors.ErrAuthentication + return nil, svcerr.ErrAuthentication } return &magistrala.IdentityRes{Id: repo.id}, nil @@ -48,7 +48,7 @@ func (repo singleUserRepo) Identify(ctx context.Context, in *magistrala.Identity func (repo singleUserRepo) Authorize(ctx context.Context, in *magistrala.AuthorizeReq, opts ...grpc.CallOption) (*magistrala.AuthorizeRes, error) { if repo.id != in.Subject { - return &magistrala.AuthorizeRes{Authorized: false}, errors.ErrAuthorization + return &magistrala.AuthorizeRes{Authorized: false}, svcerr.ErrAuthorization } return &magistrala.AuthorizeRes{Authorized: true}, nil diff --git a/twins/service.go b/twins/service.go index 305cf07e3..ab300d5c7 100644 --- a/twins/service.go +++ b/twins/service.go @@ -13,7 +13,7 @@ import ( "github.com/absmach/magistrala" "github.com/absmach/magistrala/logger" "github.com/absmach/magistrala/pkg/errors" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" "github.com/absmach/magistrala/pkg/messaging" "github.com/mainflux/senml" ) @@ -110,7 +110,7 @@ func (ts *twinsService) AddTwin(ctx context.Context, token string, twin Twin, de twin.ID, err = ts.idProvider.ID() if err != nil { - return Twin{}, errors.Wrap(repoerror.ErrUniqueID, err) + return Twin{}, errors.Wrap(repoerr.ErrUniqueID, err) } twin.Owner = res.GetId() @@ -132,7 +132,7 @@ func (ts *twinsService) AddTwin(ctx context.Context, token string, twin Twin, de twin.Revision = 0 if _, err = ts.twins.Save(ctx, twin); err != nil { - return Twin{}, errors.Wrap(repoerror.ErrCreateEntity, err) + return Twin{}, errors.Wrap(repoerr.ErrCreateEntity, err) } id = twin.ID @@ -153,7 +153,7 @@ func (ts *twinsService) UpdateTwin(ctx context.Context, token string, twin Twin, tw, err := ts.twins.RetrieveByID(ctx, twin.ID) if err != nil { - return errors.Wrap(repoerror.ErrNotFound, err) + return errors.Wrap(repoerr.ErrNotFound, err) } revision := false @@ -183,7 +183,7 @@ func (ts *twinsService) UpdateTwin(ctx context.Context, token string, twin Twin, tw.Revision++ if err := ts.twins.Update(ctx, tw); err != nil { - return errors.Wrap(repoerror.ErrUpdateEntity, err) + return errors.Wrap(repoerr.ErrUpdateEntity, err) } id = twin.ID @@ -203,7 +203,7 @@ func (ts *twinsService) ViewTwin(ctx context.Context, token, twinID string) (tw twin, err := ts.twins.RetrieveByID(ctx, twinID) if err != nil { - return Twin{}, errors.Wrap(repoerror.ErrNotFound, err) + return Twin{}, errors.Wrap(repoerr.ErrNotFound, err) } b, err = json.Marshal(twin) @@ -221,7 +221,7 @@ func (ts *twinsService) RemoveTwin(ctx context.Context, token, twinID string) (e } if err := ts.twins.Remove(ctx, twinID); err != nil { - return errors.Wrap(repoerror.ErrRemoveEntity, err) + return errors.Wrap(repoerr.ErrRemoveEntity, err) } return ts.twinCache.Remove(ctx, twinID) diff --git a/users/mocks/clients.go b/users/mocks/clients.go index 92458375e..73775c189 100644 --- a/users/mocks/clients.go +++ b/users/mocks/clients.go @@ -8,7 +8,7 @@ import ( mgclients "github.com/absmach/magistrala/pkg/clients" "github.com/absmach/magistrala/pkg/errors" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" "github.com/absmach/magistrala/users/postgres" "github.com/stretchr/testify/mock" ) @@ -25,11 +25,11 @@ func (m *Repository) ChangeStatus(ctx context.Context, client mgclients.Client) ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } if client.Status != mgclients.EnabledStatus && client.Status != mgclients.DisabledStatus { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -38,7 +38,7 @@ func (m *Repository) ChangeStatus(ctx context.Context, client mgclients.Client) func (m *Repository) Members(ctx context.Context, groupID string, pm mgclients.Page) (mgclients.MembersPage, error) { ret := m.Called(ctx, groupID, pm) if groupID == WrongID { - return mgclients.MembersPage{}, repoerror.ErrNotFound + return mgclients.MembersPage{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.MembersPage), ret.Error(1) @@ -60,7 +60,7 @@ func (m *Repository) RetrieveByID(ctx context.Context, id string) (mgclients.Cli ret := m.Called(ctx, id) if id == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -70,7 +70,7 @@ func (m *Repository) RetrieveByIdentity(ctx context.Context, identity string) (m ret := m.Called(ctx, identity) if identity == "" { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -79,10 +79,10 @@ func (m *Repository) RetrieveByIdentity(ctx context.Context, identity string) (m func (m *Repository) Save(ctx context.Context, client mgclients.Client) (mgclients.Client, error) { ret := m.Called(ctx, client) if client.Owner == WrongID { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } if client.Credentials.Secret == "" { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return client, ret.Error(1) @@ -92,7 +92,7 @@ func (m *Repository) Update(ctx context.Context, client mgclients.Client) (mgcli ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.Client), ret.Error(1) } @@ -101,10 +101,10 @@ func (m *Repository) UpdateIdentity(ctx context.Context, client mgclients.Client ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } if client.Credentials.Identity == "" { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -114,10 +114,10 @@ func (m *Repository) UpdateSecret(ctx context.Context, client mgclients.Client) ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } if client.Credentials.Secret == "" { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -127,7 +127,7 @@ func (m *Repository) UpdateTags(ctx context.Context, client mgclients.Client) (m ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -137,7 +137,7 @@ func (m *Repository) UpdateOwner(ctx context.Context, client mgclients.Client) ( ret := m.Called(ctx, client) if client.ID == WrongID { - return mgclients.Client{}, repoerror.ErrNotFound + return mgclients.Client{}, repoerr.ErrNotFound } return ret.Get(0).(mgclients.Client), ret.Error(1) @@ -147,7 +147,7 @@ func (m *Repository) RetrieveBySecret(ctx context.Context, key string) (mgclient ret := m.Called(ctx, key) if key == "" { - return mgclients.Client{}, repoerror.ErrMalformedEntity + return mgclients.Client{}, repoerr.ErrMalformedEntity } return ret.Get(0).(mgclients.Client), ret.Error(1) diff --git a/users/postgres/clients.go b/users/postgres/clients.go index 6ebcd89e8..9b96919d1 100644 --- a/users/postgres/clients.go +++ b/users/postgres/clients.go @@ -11,7 +11,7 @@ import ( mgclients "github.com/absmach/magistrala/pkg/clients" pgclients "github.com/absmach/magistrala/pkg/clients/postgres" "github.com/absmach/magistrala/pkg/errors" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" ) var _ mgclients.Repository = (*clientRepo)(nil) @@ -44,24 +44,24 @@ func (repo clientRepo) Save(ctx context.Context, c mgclients.Client) (mgclients. RETURNING id, name, tags, identity, metadata, COALESCE(owner_id, '') AS owner_id, status, created_at` dbc, err := pgclients.ToDBClient(c) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrCreateEntity, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrCreateEntity, err) } row, err := repo.ClientRepository.DB.NamedQueryContext(ctx, q, dbc) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrCreateEntity,postgres.HandleError(err)) + return mgclients.Client{}, errors.Wrap(repoerr.ErrCreateEntity,postgres.HandleError(err)) } defer row.Close() row.Next() dbc = pgclients.DBClient{} if err := row.StructScan(&dbc); err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrFailedOpDB, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrFailedOpDB, err) } client, err := pgclients.ToClient(dbc) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrFailedOpDB, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrFailedOpDB, err) } return client, nil diff --git a/users/service.go b/users/service.go index 8d0319f1b..69478ab8c 100644 --- a/users/service.go +++ b/users/service.go @@ -13,8 +13,8 @@ import ( "github.com/absmach/magistrala/auth" mgclients "github.com/absmach/magistrala/pkg/clients" "github.com/absmach/magistrala/pkg/errors" - repoerror "github.com/absmach/magistrala/pkg/errors/repository" - svcerror "github.com/absmach/magistrala/pkg/errors/service" + repoerr "github.com/absmach/magistrala/pkg/errors/repository" + svcerr "github.com/absmach/magistrala/pkg/errors/service" "github.com/absmach/magistrala/users/postgres" ) @@ -68,24 +68,24 @@ func (svc service) RegisterClient(ctx context.Context, token string, cli mgclien if !svc.selfRegister { userID, err := svc.Identify(ctx, token) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } if err := svc.checkSuperAdmin(ctx, userID); err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } } clientID, err := svc.idProvider.ID() if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrUniqueID, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrUniqueID, err) } if cli.Credentials.Secret == "" { - return mgclients.Client{}, errors.Wrap(repoerror.ErrMalformedEntity, errors.ErrMissingSecret) + return mgclients.Client{}, errors.Wrap(repoerr.ErrMalformedEntity, errors.ErrMissingSecret) } hash, err := svc.hasher.Hash(cli.Credentials.Secret) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrMalformedEntity, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrMalformedEntity, err) } cli.Credentials.Secret = hash if cli.Status != mgclients.DisabledStatus && cli.Status != mgclients.EnabledStatus { @@ -105,7 +105,7 @@ func (svc service) RegisterClient(ctx context.Context, token string, cli mgclien ObjectType: auth.PlatformType, }) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrCreateEntity, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrCreateEntity, err) } if !res.Authorized { return mgclients.Client{}, fmt.Errorf("failed to create policy") @@ -130,7 +130,7 @@ func (svc service) RegisterClient(ctx context.Context, token string, cli mgclien func (svc service) IssueToken(ctx context.Context, identity, secret, domainID string) (*magistrala.Token, error) { dbUser, err := svc.clients.RetrieveByIdentity(ctx, identity) if err != nil { - return &magistrala.Token{}, errors.Wrap(repoerror.ErrNotFound, err) + return &magistrala.Token{}, errors.Wrap(repoerr.ErrNotFound, err) } if err := svc.hasher.Compare(secret, dbUser.Credentials.Secret); err != nil { return &magistrala.Token{}, errors.Wrap(errors.ErrLogin, err) @@ -154,18 +154,18 @@ func (svc service) RefreshToken(ctx context.Context, refreshToken, domainID stri func (svc service) ViewClient(ctx context.Context, token string, id string) (mgclients.Client, error) { tokenUserID, err := svc.Identify(ctx, token) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } if tokenUserID != id { if err := svc.checkSuperAdmin(ctx, tokenUserID); err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } } client, err := svc.clients.RetrieveByID(ctx, id) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrNotFound, err) } client.Credentials.Secret = "" @@ -175,11 +175,11 @@ func (svc service) ViewClient(ctx context.Context, token string, id string) (mgc func (svc service) ViewProfile(ctx context.Context, token string) (mgclients.Client, error) { id, err := svc.Identify(ctx, token) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } client, err := svc.clients.RetrieveByID(ctx, id) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrNotFound, err) } client.Credentials.Secret = "" @@ -189,7 +189,7 @@ func (svc service) ViewProfile(ctx context.Context, token string) (mgclients.Cli func (svc service) ListClients(ctx context.Context, token string, pm mgclients.Page) (mgclients.ClientsPage, error) { userID, err := svc.Identify(ctx, token) if err != nil { - return mgclients.ClientsPage{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.ClientsPage{}, errors.Wrap(svcerr.ErrAuthentication, err) } if err := svc.checkSuperAdmin(ctx, userID); err == nil { return svc.clients.RetrieveAll(ctx, pm) @@ -209,12 +209,12 @@ func (svc service) ListClients(ctx context.Context, token string, pm mgclients.P func (svc service) UpdateClient(ctx context.Context, token string, cli mgclients.Client) (mgclients.Client, error) { tokenUserID, err := svc.Identify(ctx, token) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } if tokenUserID != cli.ID { if err := svc.checkSuperAdmin(ctx, tokenUserID); err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } } @@ -232,12 +232,12 @@ func (svc service) UpdateClient(ctx context.Context, token string, cli mgclients func (svc service) UpdateClientTags(ctx context.Context, token string, cli mgclients.Client) (mgclients.Client, error) { tokenUserID, err := svc.Identify(ctx, token) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } if tokenUserID != cli.ID { if err := svc.checkSuperAdmin(ctx, tokenUserID); err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } } @@ -254,12 +254,12 @@ func (svc service) UpdateClientTags(ctx context.Context, token string, cli mgcli func (svc service) UpdateClientIdentity(ctx context.Context, token, clientID, identity string) (mgclients.Client, error) { tokenUserID, err := svc.Identify(ctx, token) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } if tokenUserID != clientID { if err := svc.checkSuperAdmin(ctx, tokenUserID); err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } } @@ -294,11 +294,11 @@ func (svc service) GenerateResetToken(ctx context.Context, email, host string) e func (svc service) ResetSecret(ctx context.Context, resetToken, secret string) error { id, err := svc.Identify(ctx, resetToken) if err != nil { - return errors.Wrap(svcerror.ErrAuthentication, err) + return errors.Wrap(svcerr.ErrAuthentication, err) } c, err := svc.clients.RetrieveByID(ctx, id) if err != nil { - return errors.Wrap(repoerror.ErrNotFound, err) + return errors.Wrap(repoerr.ErrNotFound, err) } if c.Credentials.Identity == "" { return errors.ErrNotFound @@ -319,7 +319,7 @@ func (svc service) ResetSecret(ctx context.Context, resetToken, secret string) e UpdatedBy: id, } if _, err := svc.clients.UpdateSecret(ctx, c); err != nil { - return errors.Wrap(svcerror.ErrAuthorization, err) + return errors.Wrap(svcerr.ErrAuthorization, err) } return nil } @@ -327,21 +327,21 @@ func (svc service) ResetSecret(ctx context.Context, resetToken, secret string) e func (svc service) UpdateClientSecret(ctx context.Context, token, oldSecret, newSecret string) (mgclients.Client, error) { id, err := svc.Identify(ctx, token) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } if !svc.passRegex.MatchString(newSecret) { return mgclients.Client{}, ErrPasswordFormat } dbClient, err := svc.clients.RetrieveByID(ctx, id) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrNotFound, err) } if _, err := svc.IssueToken(ctx, dbClient.Credentials.Identity, oldSecret, ""); err != nil { return mgclients.Client{}, errors.Wrap(ErrIssueToken, err) } newSecret, err = svc.hasher.Hash(newSecret) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrMalformedEntity, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrMalformedEntity, err) } dbClient.Credentials.Secret = newSecret dbClient.UpdatedAt = time.Now() @@ -358,11 +358,11 @@ func (svc service) SendPasswordReset(_ context.Context, host, email, user, token func (svc service) UpdateClientRole(ctx context.Context, token string, cli mgclients.Client) (mgclients.Client, error) { tokenUserID, err := svc.Identify(ctx, token) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } if err := svc.checkSuperAdmin(ctx, tokenUserID); err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } client := mgclients.Client{ ID: cli.ID, @@ -416,14 +416,14 @@ func (svc service) DisableClient(ctx context.Context, token, id string) (mgclien func (svc service) changeClientStatus(ctx context.Context, token string, client mgclients.Client) (mgclients.Client, error) { tokenUserID, err := svc.Identify(ctx, token) if err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } if err := svc.checkSuperAdmin(ctx, tokenUserID); err != nil { - return mgclients.Client{}, errors.Wrap(svcerror.ErrAuthentication, err) + return mgclients.Client{}, errors.Wrap(svcerr.ErrAuthentication, err) } dbClient, err := svc.clients.RetrieveByID(ctx, client.ID) if err != nil { - return mgclients.Client{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.Client{}, errors.Wrap(repoerr.ErrNotFound, err) } if dbClient.Status == client.Status { return mgclients.Client{}, mgclients.ErrStatusAlreadyAssigned @@ -450,7 +450,7 @@ func (svc service) ListMembers(ctx context.Context, token, objectKind string, ob } if _, err := svc.authorize(ctx, auth.UserType, auth.TokenKind, token, authzPerm, objectType, objectID); err != nil { - return mgclients.MembersPage{}, errors.Wrap(svcerror.ErrAuthorization, err) + return mgclients.MembersPage{}, errors.Wrap(svcerr.ErrAuthorization, err) } duids, err := svc.auth.ListAllSubjects(ctx, &magistrala.ListSubjectsReq{ SubjectType: auth.UserType, @@ -459,7 +459,7 @@ func (svc service) ListMembers(ctx context.Context, token, objectKind string, ob ObjectType: objectType, }) if err != nil { - return mgclients.MembersPage{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.MembersPage{}, errors.Wrap(repoerr.ErrNotFound, err) } if len(duids.Policies) == 0 { return mgclients.MembersPage{ @@ -477,7 +477,7 @@ func (svc service) ListMembers(ctx context.Context, token, objectKind string, ob cp, err := svc.clients.RetrieveAll(ctx, pm) if err != nil { - return mgclients.MembersPage{}, errors.Wrap(repoerror.ErrNotFound, err) + return mgclients.MembersPage{}, errors.Wrap(repoerr.ErrNotFound, err) } return mgclients.MembersPage{ @@ -489,7 +489,7 @@ func (svc service) ListMembers(ctx context.Context, token, objectKind string, ob func (svc *service) checkSuperAdmin(ctx context.Context, adminID string) error { if _, err := svc.authorize(ctx, auth.UserType, auth.UsersKind, adminID, auth.AdminPermission, auth.PlatformType, auth.MagistralaObject); err != nil { if err := svc.clients.CheckSuperAdmin(ctx, adminID); err != nil { - return errors.Wrap(svcerror.ErrAuthorization, err) + return errors.Wrap(svcerr.ErrAuthorization, err) } } @@ -507,11 +507,11 @@ func (svc *service) authorize(ctx context.Context, subjType, subjKind, subj, per } res, err := svc.auth.Authorize(ctx, req) if err != nil { - return "", errors.Wrap(svcerror.ErrAuthorization, err) + return "", errors.Wrap(svcerr.ErrAuthorization, err) } if !res.GetAuthorized() { - return "", errors.Wrap(svcerror.ErrAuthorization, err) + return "", errors.Wrap(svcerr.ErrAuthorization, err) } return res.GetId(), nil } @@ -519,7 +519,7 @@ func (svc *service) authorize(ctx context.Context, subjType, subjKind, subj, per func (svc service) Identify(ctx context.Context, token string) (string, error) { user, err := svc.auth.Identify(ctx, &magistrala.IdentityReq{Token: token}) if err != nil { - return "", errors.Wrap(svcerror.ErrAuthentication, err) + return "", errors.Wrap(svcerr.ErrAuthentication, err) } return user.GetUserId(), nil } @@ -538,7 +538,7 @@ func (svc service) updateClientPolicy(ctx context.Context, userID string, role m return errors.Wrap(errAddPolicies, err) } if !resp.Authorized { - return errors.Wrap(svcerror.ErrAuthorization, err) + return errors.Wrap(svcerr.ErrAuthorization, err) } return nil case mgclients.UserRole: diff --git a/ws/handler.go b/ws/handler.go index 61ca404b3..b4be7a3cb 100644 --- a/ws/handler.go +++ b/ws/handler.go @@ -14,7 +14,7 @@ import ( "github.com/absmach/magistrala" "github.com/absmach/magistrala/logger" "github.com/absmach/magistrala/pkg/errors" - svcerror "github.com/absmach/magistrala/pkg/errors/service" + svcerr "github.com/absmach/magistrala/pkg/errors/service" "github.com/absmach/magistrala/pkg/messaging" "github.com/mainflux/mproxy/pkg/session" ) @@ -246,10 +246,10 @@ func (h *handler) authAccess(ctx context.Context, password, topic, action string } res, err := h.auth.Authorize(ctx, ar) if err != nil { - return errors.Wrap(svcerror.ErrAuthorization, err) + return errors.Wrap(svcerr.ErrAuthorization, err) } if !res.GetAuthorized() { - return errors.Wrap(svcerror.ErrAuthorization, err) + return errors.Wrap(svcerr.ErrAuthorization, err) } return nil