Skip to content
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

Merged
merged 6 commits into from
Mar 14, 2024

Conversation

rodneyosodo
Copy link
Member

@rodneyosodo rodneyosodo commented Mar 6, 2024

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

Access tokens are credentials used to access protected resources. An access token is a string representing an authorization issued to the client.

We only use this token once when accessing identity provider to fetch user details during registration or signup

@dborovcanin dborovcanin changed the title NOISSUE - Reduce the size of the tokens generated with OAuth2.0 tokens NOISSUE - Separate OAuth2.0 access and refresh tokens Mar 7, 2024
oauthRefreshTokenField: key.OAuth.RefreshToken,
})

// when issuing an access token, the refresh token is not present and vice versa
Copy link
Collaborator

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.

Comment on lines 195 to 198

// access token and refresh token are not mandatory either of them can be present
accessToken, _ := claims[oauthAccessTokenField].(string)
refreshToken, _ := claims[oauthRefreshTokenField].(string)
Copy link
Collaborator

@dborovcanin dborovcanin Mar 7, 2024

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).


// 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{}{
Copy link
Collaborator

@dborovcanin dborovcanin Mar 7, 2024

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,
			})
		}

Copy link
Collaborator

@dborovcanin dborovcanin left a 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.

@rodneyosodo rodneyosodo force-pushed the split-oauth-token branch 2 times, most recently from fee6c98 to 8025bf3 Compare March 7, 2024 14:54
@rodneyosodo rodneyosodo changed the title NOISSUE - Separate OAuth2.0 access and refresh tokens NOISSUE - Remove OAuth2.0 tokens from Magistrala token Mar 8, 2024
@rodneyosodo rodneyosodo force-pushed the split-oauth-token branch 2 times, most recently from 4be7f43 to e8dad89 Compare March 14, 2024 11:11
rodneyosodo and others added 5 commits March 14, 2024 15:55
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>
users/service.go Outdated
OauthAccessToken: token.AccessToken,
OauthRefreshToken: token.RefreshToken,
UserId: rclient.ID,
Type: 0,
Copy link
Collaborator

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>
@dborovcanin dborovcanin merged commit 22a3c29 into absmach:main Mar 14, 2024
5 checks passed
@rodneyosodo rodneyosodo deleted the split-oauth-token branch October 22, 2024 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants