Skip to content

Commit

Permalink
add authorization to create middleware
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene committed Feb 6, 2025
1 parent a28d588 commit 04fe267
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions channels/middleware/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func (am *authorizationMiddleware) CreateChannels(ctx context.Context, session a
}
}

if err := am.RoleManagerAuthorizationMiddleware.AuthorizeMembers(ctx, session, []string{session.UserID}); err != nil {
return []channels.Channel{}, []roles.RoleProvision{}, errors.Wrap(svcerr.ErrAuthorization, err)
}

Check warning on line 102 in channels/middleware/authorization.go

View check run for this annotation

Codecov / codecov/patch

channels/middleware/authorization.go#L100-L102

Added lines #L100 - L102 were not covered by tests

if err := am.extAuthorize(ctx, channels.DomainOpCreateChannel, smqauthz.PolicyReq{
Domain: session.DomainID,
SubjectType: policies.UserType,
Expand Down
4 changes: 4 additions & 0 deletions clients/middleware/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (am *authorizationMiddleware) CreateClients(ctx context.Context, session au
}
}

if err := am.RoleManagerAuthorizationMiddleware.AuthorizeMembers(ctx, session, []string{session.UserID}); err != nil {
return []clients.Client{}, []roles.RoleProvision{}, errors.Wrap(svcerr.ErrAuthorization, err)
}

Check warning on line 94 in clients/middleware/authorization.go

View check run for this annotation

Codecov / codecov/patch

clients/middleware/authorization.go#L92-L94

Added lines #L92 - L94 were not covered by tests

if err := am.extAuthorize(ctx, clients.DomainOpCreateClient, smqauthz.PolicyReq{
Domain: session.DomainID,
SubjectType: policies.UserType,
Expand Down
5 changes: 5 additions & 0 deletions domains/middleware/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/absmach/supermq/pkg/authn"
"github.com/absmach/supermq/pkg/authz"
smqauthz "github.com/absmach/supermq/pkg/authz"
"github.com/absmach/supermq/pkg/errors"
svcerr "github.com/absmach/supermq/pkg/errors/service"
"github.com/absmach/supermq/pkg/policies"
"github.com/absmach/supermq/pkg/roles"
rmMW "github.com/absmach/supermq/pkg/roles/rolemanager/middleware"
Expand Down Expand Up @@ -48,6 +50,9 @@ func AuthorizationMiddleware(entityType string, svc domains.Service, authz smqau
}

func (am *authorizationMiddleware) CreateDomain(ctx context.Context, session authn.Session, d domains.Domain) (domains.Domain, []roles.RoleProvision, error) {
if err := am.RoleManagerAuthorizationMiddleware.AuthorizeMembers(ctx, session, []string{session.UserID}); err != nil {
return domains.Domain{}, []roles.RoleProvision{}, errors.Wrap(svcerr.ErrAuthorization, err)
}
return am.svc.CreateDomain(ctx, session, d)
}

Expand Down
5 changes: 5 additions & 0 deletions groups/middleware/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (am *authorizationMiddleware) CreateGroup(ctx context.Context, session auth
return groups.Group{}, []roles.RoleProvision{}, errors.Wrap(svcerr.ErrUnauthorizedPAT, err)
}
}

if err := am.RoleManagerAuthorizationMiddleware.AuthorizeMembers(ctx, session, []string{session.UserID}); err != nil {
return groups.Group{}, []roles.RoleProvision{}, errors.Wrap(svcerr.ErrAuthorization, err)
}

Check warning on line 101 in groups/middleware/authorization.go

View check run for this annotation

Codecov / codecov/patch

groups/middleware/authorization.go#L99-L101

Added lines #L99 - L101 were not covered by tests

if err := am.extAuthorize(ctx, groups.DomainOpCreateGroup, smqauthz.PolicyReq{
Domain: session.DomainID,
SubjectType: policies.UserType,
Expand Down
4 changes: 2 additions & 2 deletions pkg/roles/rolemanager/middleware/authoirzation.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (ram RoleManagerAuthorizationMiddleware) RoleAddMembers(ctx context.Context
return []string{}, err
}

if err := ram.authorizeMembers(ctx, session, members); err != nil {
if err := ram.AuthorizeMembers(ctx, session, members); err != nil {
return []string{}, err
}
return ram.svc.RoleAddMembers(ctx, session, entityID, roleID, members)
Expand Down Expand Up @@ -320,7 +320,7 @@ func (ram RoleManagerAuthorizationMiddleware) RemoveMemberFromAllRoles(ctx conte
return ram.svc.RemoveMemberFromAllRoles(ctx, session, memberID)
}

func (ram RoleManagerAuthorizationMiddleware) authorizeMembers(ctx context.Context, session authn.Session, members []string) error {
func (ram RoleManagerAuthorizationMiddleware) AuthorizeMembers(ctx context.Context, session authn.Session, members []string) error {
switch ram.entityType {
case policies.DomainType:
for _, member := range members {
Expand Down

0 comments on commit 04fe267

Please sign in to comment.