-
Notifications
You must be signed in to change notification settings - Fork 674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MG-2048 - Authorize clients and users with PATs #2499
base: main
Are you sure you want to change the base?
Conversation
001bcaa
to
d9f739e
Compare
56daa42
to
923af86
Compare
f204e8c
to
00cb9c6
Compare
@@ -229,7 +384,7 @@ func (am *authorizationMiddleware) RemoveParentGroup(ctx context.Context, sessio | |||
} | |||
|
|||
if th.ParentGroup != "" { | |||
if err := am.extAuthorize(ctx, clients.GroupOpSetChildClient, authz.PolicyReq{ | |||
if err := am.extAuthorize(ctx, clients.GroupOpSetChildThing, authz.PolicyReq{ | |||
Domain: session.DomainID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Domain: session.DomainID, | |
if err := am.extAuthorize(ctx, clients.GroupOpSetChildClient, authz.PolicyReq{ |
@@ -200,7 +341,7 @@ func (am *authorizationMiddleware) SetParentGroup(ctx context.Context, session a | |||
return errors.Wrap(err, errSetParentGroup) | |||
} | |||
|
|||
if err := am.extAuthorize(ctx, clients.GroupOpSetChildClient, authz.PolicyReq{ | |||
if err := am.extAuthorize(ctx, clients.GroupOpSetChildThing, authz.PolicyReq{ | |||
Domain: session.DomainID, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Domain: session.DomainID, | |
if err := am.extAuthorize(ctx, clients.GroupOpSetChildClient, authz.PolicyReq{ | |
Domain: session.DomainID, |
internal/api/auth.go
Outdated
resp.Type = mgauthn.AccessToken | ||
if strings.HasPrefix(token, "pat"+seperator) { | ||
resp.Type = mgauthn.PersonalAccessToken | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be done in auth service, During authentication process.
Is there any reason behind this ?
internal/proto/auth/v1/auth.proto
Outdated
@@ -10,6 +10,7 @@ option go_package = "github.com/absmach/magistrala/internal/grpc/auth/v1"; | |||
// functionalities for magistrala services. | |||
service AuthService { | |||
rpc Authorize(AuthZReq) returns (AuthZRes) {} | |||
rpc AuthorizePAT(AuthZpatReq) returns (AuthZRes) {} | |||
rpc Authenticate(AuthNReq) returns (AuthNRes) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets have seperate RPC for PAT Authentication like PAT,
auth/api/http/pats/transport.go
Outdated
r.Post("/authorize", kithttp.NewServer( | ||
(authorizePATEndpoint(svc)), | ||
decodeAuthorizePATRequest, | ||
api.EncodeResponse, | ||
opts..., | ||
).ServeHTTP) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this endpoint, For now we can provide authoirzePAT via gRPC only.
auth/service.go
Outdated
if strings.HasPrefix(token, patPrefix+patSecretSeparator) { | ||
pat, err := svc.IdentifyPAT(ctx, token) | ||
if err != nil { | ||
return Key{}, err | ||
} | ||
return Key{ | ||
ID: pat.ID, | ||
Type: PersonalAccessToken, | ||
Subject: pat.User, | ||
User: pat.User, | ||
IssuedAt: pat.IssuedAt, | ||
ExpiresAt: pat.ExpiresAt, | ||
}, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this logic to Authenticate
function in file pkg/authn/authsvc/authn.go
Lets have seperate RPC for IdentifyPAT.
The Authenticate
function in file pkg/authn/authsvc/authn.go
will call IdentifyPAT base on token prefix
pkg/authn/authsvc/authn.go
Outdated
} | ||
return authn.Session{DomainUserID: res.GetId(), UserID: res.GetUserId(), DomainID: res.GetDomainId()}, nil | ||
return authn.Session{ID: res.GetId(), UserID: res.GetUserId(), DomainID: res.GetDomainId()}, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets have seperate RPC call IdentifyPAT, move the logic to here
Something like below code
} | |
return authn.Session{DomainUserID: res.GetId(), UserID: res.GetUserId(), DomainID: res.GetDomainId()}, nil | |
return authn.Session{ID: res.GetId(), UserID: res.GetUserId(), DomainID: res.GetDomainId()}, nil | |
} | |
switch { | |
case strings.HasPrefix(token, patPrefix+patSecretSeparator): | |
res, err := a.authSvcClient.AuthenticatePAT(ctx, token) | |
if err != nil { | |
return authn.Session{}, errors.Wrap(errors.ErrAuthentication, err) | |
} | |
return authn.Session{ID: res.GetId(), UserID: res.GetUserId(), DomainID: res.GetDomainId()}, nil | |
default: | |
res, err := a.authSvcClient.Authenticate(ctx, &grpcAuthV1.AuthNReq{Token: token}) | |
if err != nil { | |
return authn.Session{}, errors.Wrap(errors.ErrAuthentication, err) | |
} | |
return authn.Session{ID: res.GetId(), UserID: res.GetUserId(), DomainID: res.GetDomainId()}, nil | |
} | |
users/middleware/authorization.go
Outdated
@@ -179,6 +390,19 @@ func (am *authorizationMiddleware) Delete(ctx context.Context, session authn.Ses | |||
session.SuperAdmin = true | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do PAT Authorization before checkAdmin or UserAuthz
3f43476
to
4323d34
Compare
4323d34
to
c04c035
Compare
2e489f4
to
511e918
Compare
9bf293f
to
33a88f8
Compare
9d4dce1
to
4daa02b
Compare
511e918
to
eaf5536
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2499 +/- ##
==========================================
- Coverage 43.38% 41.94% -1.45%
==========================================
Files 398 326 -72
Lines 51476 44094 -7382
==========================================
- Hits 22335 18496 -3839
+ Misses 26951 24219 -2732
+ Partials 2190 1379 -811 ☔ View full report in Codecov by Sentry. |
07a8787
to
328600e
Compare
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
Signed-off-by: nyagamunene <[email protected]>
2c719f6
74f8cd9
to
2c719f6
Compare
What type of PR is this?
This is a feature because it adds the following functionality: It adds authorization to things and users using PATs.
What does this do?
It adds PATs authorization to things and users middleware
Which issue(s) does this PR fix/relate to?
Have you included tests for your changes?
Yes
Did you document any new/modified feature?
No
Notes