-
Notifications
You must be signed in to change notification settings - Fork 672
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
NOISSUE - Remove OAuth2.0 tokens from Magistrala token #2106
Conversation
auth/jwt/tokenizer.go
Outdated
oauthRefreshTokenField: key.OAuth.RefreshToken, | ||
}) | ||
|
||
// when issuing an access token, the refresh token is not present and vice versa |
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.
Make this comment a full sentence.
auth/jwt/tokenizer.go
Outdated
|
||
// access token and refresh token are not mandatory either of them can be present | ||
accessToken, _ := claims[oauthAccessTokenField].(string) | ||
refreshToken, _ := claims[oauthRefreshTokenField].(string) |
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.
Fix according to the comment. Use only a single token (access or refresh):
if ok {
claims, ok := oauthToken.(pair)
if !ok {
return auth.Key{}, errors.Wrap(ErrParseToken, fmt.Errorf("invalid claims for %s token", provider.Name()))
}
// // access token and refresh token are not mandatory either of them can be present
// accessToken, _ := claims[oauthAccessTokenField].(string)
// refreshToken, _ := claims[oauthRefreshTokenField].(string)\
switch claims.key {
case oauthAccessTokenField:
if err := provider.Validate(ctx, claims.value)
Also, fix the messy switch, since both cases do almost the same. Validation should inspect the error, since fallback to refresh may not make sense in some scenarios (revoked access, for example).
20e15f0
to
9ada86b
Compare
auth/jwt/tokenizer.go
Outdated
|
||
// when issuing an access token, the refresh token is not present and vice versa | ||
if key.OAuth.AccessToken != "" && key.OAuth.RefreshToken == "" { | ||
builder.Claim(provider.Name(), map[string]interface{}{ |
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.
Why don't we make this a map[string]string
or a struct:
type pair struct {
key string
value string
}
So we have:
if key.OAuth.AccessToken != "" && key.OAuth.RefreshToken == "" {
builder.Claim(provider.Name(), pair{
key: oauthAccessTokenField,
value: key.OAuth.AccessToken,
})
}
if key.OAuth.AccessToken == "" && key.OAuth.RefreshToken != "" {
builder.Claim(provider.Name(), pair{
key: oauthRefreshTokenField,
value: key.OAuth.RefreshToken,
})
}
9ada86b
to
8a9c14c
Compare
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.
Simplify the Tokenizer Parse
method. Extract validation, key parsing, and Oauth handling to three separate functions or methods.
fee6c98
to
8025bf3
Compare
6e94aee
to
d4022a5
Compare
d4022a5
to
231bd84
Compare
4be7f43
to
e8dad89
Compare
Refactor code to enhance parsing of OAuth tokens, validate them with the provider and refresh if needed. Handle errors related to token issuance and update user's tokens. When issuing access token we don't need OAuth2.0 refresh token embedded inside it and vice versa. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
e8dad89
to
0e8a8dd
Compare
users/service.go
Outdated
OauthAccessToken: token.AccessToken, | ||
OauthRefreshToken: token.RefreshToken, | ||
UserId: rclient.ID, | ||
Type: 0, |
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.
Use constant/enum instead of literal.
Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
What type of PR is this?
This is a refactor PR because it refactors how OAuth2.0 works in relation to magistrala
What does this do?
Removes OAuth2.0 tokens from magistrala token
Which issue(s) does this PR fix/relate to?
No issue
Have you included tests for your changes?
Yes, I have included tests for my changes and tested them manually.
Did you document any new/modified feature?
Yes, I have added comments to the code.
Notes
Demo https://jam.dev/c/84106a0d-f6a1-43b4-92af-23d7db977041
Explanation
From https://datatracker.ietf.org/doc/html/rfc6749#section-1.4
We only use this token once when accessing identity provider to fetch user details during registration or signup