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

feat: add ability to convert session to JWT when calling whoami #3472

Merged
merged 22 commits into from
Sep 18, 2023

Conversation

aeneasr
Copy link
Member

@aeneasr aeneasr commented Aug 31, 2023

This patch adds a query parameter tokenize_as to /session/whoami which encodes the session to a JWT. It is possible to customize the JWT claims by using a JsonNet template, and furthermore change the expiry of the token.

The tokenize feature supports multiple templates, which makes it easy to use the resulting JWT in a variety of use cases.

Closes #2487

@codecov
Copy link

codecov bot commented Sep 4, 2023

Codecov Report

Merging #3472 (e8902c9) into master (fc30304) will decrease coverage by 0.60%.
Report is 11 commits behind head on master.
The diff coverage is 68.47%.

❗ Current head e8902c9 differs from pull request most recent head cb469d4. Consider uploading reports for the commit cb469d4 to get more accurate results

@@            Coverage Diff             @@
##           master    #3472      +/-   ##
==========================================
- Coverage   78.76%   78.17%   -0.60%     
==========================================
  Files         341      341              
  Lines       23707    22643    -1064     
==========================================
- Hits        18673    17701     -972     
+ Misses       3667     3615      -52     
+ Partials     1367     1327      -40     
Files Changed Coverage Δ
driver/registry.go 40.62% <ø> (-7.11%) ⬇️
selfservice/strategy/oidc/provider_apple.go 20.83% <0.00%> (-5.49%) ⬇️
selfservice/strategy/oidc/provider_discord.go 0.00% <0.00%> (ø)
selfservice/strategy/oidc/provider_github.go 0.00% <0.00%> (ø)
selfservice/strategy/oidc/provider_github_app.go 0.00% <0.00%> (ø)
selfservice/strategy/oidc/provider_lark.go 0.00% <0.00%> (ø)
selfservice/strategy/oidc/provider_patreon.go 0.00% <0.00%> (ø)
selfservice/strategy/oidc/provider_slack.go 0.00% <0.00%> (ø)
selfservice/strategy/oidc/provider_spotify.go 0.00% <0.00%> (ø)
session/session.go 78.90% <ø> (-2.66%) ⬇️
... and 29 more

... and 4 files with indirect coverage changes

@aeneasr aeneasr marked this pull request as ready for review September 6, 2023 14:14
session/handler.go Outdated Show resolved Hide resolved
session/tokenizer.go Outdated Show resolved Hide resolved
session/tokenizer.go Show resolved Hide resolved
@aeneasr aeneasr changed the title feat: add ability to exchange session for short-lived JWT feat: add ability to convert session to JWT when calling whoami Sep 7, 2023
session/handler.go Outdated Show resolved Hide resolved
session/handler.go Outdated Show resolved Hide resolved
zepatrik
zepatrik previously approved these changes Sep 15, 2023
Copy link
Member

@zepatrik zepatrik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice 🎉
I only have a few final remarks, but it would be fine to merge IMO right away.

"description": "A list of different templates that govern how a session is converted to a token format.",
"type": "object",
"patternProperties": {
"[a-zA-Z0-9-_.]+": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not allow just any key here?

Suggested change
"[a-zA-Z0-9-_.]+": {
".+": {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prevent users from using spaces etc, which then need to be properly encoded in the URL

@@ -221,11 +243,19 @@ func (h *Handler) whoami(w http.ResponseWriter, r *http.Request, _ httprouter.Pa
// s.Devices = nil
s.Identity = s.Identity.CopyWithoutCredentials()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would make this a bit more complex, but did we consider expand here? It would be a breaking change to not expand everything on the "normal" whoami, but with this new API it would be worth a consideration to require expanding only certain fields. As the dependencies are basically set by the template, we should probably make this part of the template config instead of a request parameter.
Throwing this in here to ensure that we don't have breaking changes later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make more sense to later provide a new /sessions/v2/whoami API that would introduce expand across all APIs in a unique way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think /sessions/v2/whoami is the way to go

session/handler_test.go Outdated Show resolved Hide resolved
@@ -142,6 +142,9 @@ type Manager interface {
// MaybeRedirectAPICodeFlow for API+Code flows redirects the user to the return_to URL and adds the code query parameter.
// `handled` is true if the request a redirect was written, false otherwise.
MaybeRedirectAPICodeFlow(w http.ResponseWriter, r *http.Request, f flow.Flow, sessionID uuid.UUID, uiNode node.UiNodeGroup) (handled bool, err error)

// TokenizeSession sets a session's tokenized value.
TokenizeSession(ctx context.Context, template string, session *Session) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even used?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope :) deleted

@aeneasr aeneasr merged commit 57b7bb8 into master Sep 18, 2023
19 checks passed
@aeneasr aeneasr deleted the jwt-sessions branch September 18, 2023 07:52
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.

Consider implementing JSON Web Token Exchange endpoint
3 participants