Skip to content

Commit

Permalink
MG-2048 - Authorize clients and users with PATs (#2499)
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: Felix Gateru <[email protected]>
Signed-off-by: Arvindh <[email protected]>
Co-authored-by: Felix Gateru <[email protected]>
Co-authored-by: Arvindh <[email protected]>
  • Loading branch information
3 people authored Dec 30, 2024
1 parent f12aacd commit 6abc52d
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 41 deletions.
1 change: 1 addition & 0 deletions api/http/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
switch {
case errors.Contains(err, svcerr.ErrAuthorization),
errors.Contains(err, svcerr.ErrDomainAuthorization),
errors.Contains(err, svcerr.ErrUnauthorizedPAT),
errors.Contains(err, bootstrap.ErrExternalKey),
errors.Contains(err, bootstrap.ErrExternalKeySecure):
err = unwrap(err)
Expand Down
4 changes: 3 additions & 1 deletion auth/api/grpc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func EncodeError(err error) error {
err == apiutil.ErrMissingMemberType,
err == apiutil.ErrMissingPolicySub,
err == apiutil.ErrMissingPolicyObj,
err == apiutil.ErrMalformedPolicyAct:
err == apiutil.ErrMalformedPolicyAct,
err == apiutil.ErrMissingUserID,
err == apiutil.ErrMissingPATID:
return status.Error(codes.InvalidArgument, err.Error())
case errors.Contains(err, svcerr.ErrAuthentication),
errors.Contains(err, auth.ErrKeyExpired),
Expand Down
2 changes: 1 addition & 1 deletion auth/hasher/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// SPDX-License-Identifier: Apache-2.0

// Package hasher contains the domain concept definitions needed to
// support Magistrala users password hasher sub-service functionality.
// support Supermq users password hasher sub-service functionality.
package hasher
2 changes: 1 addition & 1 deletion auth/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (tm *tracingMiddleware) AuthorizePAT(ctx context.Context, userID, patID str
func (tm *tracingMiddleware) CheckPAT(ctx context.Context, userID, patID string, platformEntityType auth.PlatformEntityType, optionalDomainID string, optionalDomainEntityType auth.DomainEntityType, operation auth.OperationType, entityIDs ...string) error {
ctx, span := tm.tracer.Start(ctx, "check_pat", trace.WithAttributes(
attribute.String("user_id", userID),
attribute.String("patID", patID),
attribute.String("pat_id", patID),
attribute.String("platform_entity", platformEntityType.String()),
attribute.String("optional_domain_id", optionalDomainID),
attribute.String("optional_domain_entity", optionalDomainEntityType.String()),
Expand Down
223 changes: 189 additions & 34 deletions clients/middleware/authorization.go

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions pkg/authz/authsvc/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,23 @@ func (a authorization) checkDomain(ctx context.Context, subjectType, subject, do
return svcerr.ErrInvalidStatus
}
}

func (a authorization) AuthorizePAT(ctx context.Context, pr authz.PatReq) error {
req := grpcAuthV1.AuthZPatReq{
UserId: pr.UserID,
PatId: pr.PatID,
PlatformEntityType: uint32(pr.PlatformEntityType),
OptionalDomainId: pr.OptionalDomainID,
OptionalDomainEntityType: uint32(pr.OptionalDomainEntityType),
Operation: uint32(pr.Operation),
EntityIds: pr.EntityIDs,
}
res, err := a.authSvcClient.AuthorizePAT(ctx, &req)
if err != nil {
return errors.Wrap(errors.ErrAuthorization, err)
}
if !res.Authorized {
return errors.ErrAuthorization
}
return nil
}
17 changes: 16 additions & 1 deletion pkg/authz/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package authz

import "context"
import (
"context"

"github.com/absmach/supermq/auth"
)

type PolicyReq struct {
// Domain contains the domain ID.
Expand Down Expand Up @@ -42,9 +46,20 @@ type PolicyReq struct {
Permission string `json:"permission,omitempty"`
}

type PatReq struct {
UserID string `json:"user_id,omitempty"` // UserID
PatID string `json:"pat_id,omitempty"` // UserID
PlatformEntityType auth.PlatformEntityType `json:"platform_entity_type,omitempty"` // Platform entity type
OptionalDomainID string `json:"optional_domainID,omitempty"` // Optional domain id
OptionalDomainEntityType auth.DomainEntityType `json:"optional_domain_entity_type,omitempty"` // Optional domain entity type
Operation auth.OperationType `json:"operation,omitempty"` // Operation
EntityIDs []string `json:"entityIDs,omitempty"` // EntityIDs
}

// Authz is supermq authorization library.
//
//go:generate mockery --name Authorization --output=./mocks --filename authz.go --quiet --note "Copyright (c) Abstract Machines"
type Authorization interface {
Authorize(ctx context.Context, pr PolicyReq) error
AuthorizePAT(ctx context.Context, pr PatReq) error
}
18 changes: 18 additions & 0 deletions pkg/authz/mocks/authz.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/errors/service/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,7 @@ var (

// ErrRollbackRepo indicates a failure to rollback repository.
ErrRollbackRepo = errors.New("failed to rollback repo")

// ErrUnauthorizedPAT indicates failure occurred while authorizing PAT.
ErrUnauthorizedPAT = errors.New("failed to authorize PAT")
)
Loading

0 comments on commit 6abc52d

Please sign in to comment.