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

Documentation on how to use context on client. Why contextKey type? #7

Open
FranciscoBarao opened this issue Oct 28, 2022 · 2 comments

Comments

@FranciscoBarao
Copy link

After adding the middleware router.Use(oauth.Authorize("mySecretKey-10101", nil)) and being able to authorize a user, I had some issues figuring out how to access the information of the access token using the Context.

The middleware.go file adds the token information to the Context using a new type that is basically a string. Code in question:
`type contextKey string

const (
CredentialContext contextKey = "oauth.credential"
ClaimsContext contextKey = "oauth.claims"
ScopeContext contextKey = "oauth.scope"
TokenTypeContext contextKey = "oauth.tokentype"
AccessTokenContext contextKey = "oauth.accesstoken"
)`

Given that the context is accessed using r.Context().Value(key) and that key is basically a string, I thought I had to access the claims just by using r.Context().Value("oauth.claims") but this approach returned nil.

To successfully access the token information in the context I had to import the oauth package and use the consts that I put above since their type is not a string but contextKey. Working Example:
r.Context().Value(oauth.ClaimsContext)

I was wondering why this contextKey type is used instead of using a string

@rkgarcia
Copy link

This type is used to avoid mistakes and package colision you can read more about this https://medium.com/@matryer/context-keys-in-go-5312346a868d

@giorgenes
Copy link

+1 for this as I also had to figure this out myself.
I also want to notice that while the StoreTokenID callback returns what seems to be an UUID that is supposed to be saved to the user, when the middleware validates the token this token ID is nowhere to be found. The only useful information given is the original username, which kind defeats the purpose of saving this ID.

The user data seems to be encoded inside the encrypted token itself (akin to JWT token), making it unecessary to save anything to a database or retrieve a user by token.

Am I missing something?

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

No branches or pull requests

3 participants