Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Rename import aliases
Browse files Browse the repository at this point in the history
Signed-off-by: felix.gateru <[email protected]>
  • Loading branch information
felixgateru committed Nov 22, 2023
1 parent 7a20ee2 commit 1568a66
Show file tree
Hide file tree
Showing 17 changed files with 199 additions and 199 deletions.
10 changes: 5 additions & 5 deletions auth/mocks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
28 changes: 14 additions & 14 deletions auth/postgres/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)

Check failure on line 57 in auth/postgres/domains.go

View workflow job for this annotation

GitHub Actions / Continuous Integration

File is not `gci`-ed with --skip-generated -s standard -s default (gci)
}

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()
Expand Down Expand Up @@ -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()
}
Expand Down
40 changes: 20 additions & 20 deletions auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -509,15 +509,15 @@ 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)
}

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,
Expand All @@ -527,15 +527,15 @@ 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)
}

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{
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
Loading

0 comments on commit 1568a66

Please sign in to comment.