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 session and requester to refresh token webhook data #3204

Merged
merged 4 commits into from
Aug 1, 2022
Merged

feat: add session and requester to refresh token webhook data #3204

merged 4 commits into from
Aug 1, 2022

Conversation

sgal
Copy link
Contributor

@sgal sgal commented Jul 27, 2022

Related issue(s)

Feature: This patch adds session and requester data to the refresh webhook for session tracking by integrators. Fixes #3203.

Checklist

Further Comments

Changes outside of hook.go are created by running make sdk.

@CLAassistant
Copy link

CLAassistant commented Jul 27, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

Epic :) I think we can send the whole session in the payload!

To ensure that the change has the desired effect, can you please add the appropriate assertions to the test case:

t.Run("should call refresh token hook if configured", func(t *testing.T) {

The documentation will also need to explain the new key, the appropriate document for that should be: https://github.com/ory/docs/blob/45a1f9268d693b164d169e16272a30327860c4e9/docs/hydra/guides/updating-claims-at-refresh.mdx

oauth2/hook.go Outdated Show resolved Hide resolved
oauth2/hook.go Outdated Show resolved Hide resolved
@sgal
Copy link
Contributor Author

sgal commented Jul 27, 2022

@aeneasr Is there a good approach to make Session into a JSON and Swagger-friendly representation? I tried to use it as is, but it generates a lot of new files for fosite.DefaultSession and IDTokenClaims to map them to Swagger. I feel like there should be a better way to do it, but not sure where to look for examples. Could you please point me in the right direction?
Same question for requester.

@aeneasr
Copy link
Member

aeneasr commented Jul 27, 2022

A common practice, if the types don't work in swagger, is to create a replacement / patch like here:

- op: remove
path: /components/schemas/consentRequestSession/properties/access_token/type

Basically, this will patch the given path in

https://github.com/ory/hydra/blob/master/spec/api.json

with the value you provide. This can be helpful in scenarios where go-swagger creates types that just aren't correct.

Regarding the files generated, that is fine and sometimes expected with OpenAPI generator - feel free to push your changes if you want me to take a look.

oauth2/hook.go Show resolved Hide resolved
go.mod Show resolved Hide resolved
oauth2/oauth2_auth_code_test.go Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Jul 29, 2022

Codecov Report

Merging #3204 (0c50c15) into master (e66ba3c) will increase coverage by 0.04%.
The diff coverage is 100.00%.

❗ Current head 0c50c15 differs from pull request most recent head 6508ceb. Consider uploading reports for the commit 6508ceb to get more accurate results

@@            Coverage Diff             @@
##           master    #3204      +/-   ##
==========================================
+ Coverage   79.28%   79.32%   +0.04%     
==========================================
  Files         111      111              
  Lines        8077     8084       +7     
==========================================
+ Hits         6404     6413       +9     
+ Misses       1259     1258       -1     
+ Partials      414      413       -1     
Impacted Files Coverage Δ
oauth2/session.go 89.74% <ø> (ø)
oauth2/hook.go 68.11% <100.00%> (+3.59%) ⬆️
persistence/sql/persister_oauth2.go 79.91% <0.00%> (+0.81%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us.

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

Great, this looks really good already! :)

Once finished, could you please also update the docs PR accordingly? Thank you!

oauth2/hook.go Outdated Show resolved Hide resolved
oauth2/hook.go Show resolved Hide resolved
oauth2/oauth2_auth_code_test.go Outdated Show resolved Hide resolved
require.Equal(t, hookReq.Requester.ClientID, oauthConfig.ClientID)
require.ElementsMatch(t, hookReq.Requester.GrantedScopes, expectedGrantedScopes)

assertx.EqualAsJSONExcept(t, hookReq.Session, json.RawMessage(`{
Copy link
Member

Choose a reason for hiding this comment

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

If you use snapshotx. it will create a JSON file on the disk which makes the content a bit easier to read:

	snapshotx.SnapshotT(t, json.RawMessage("..."), snapshotx.ExceptPaths("...") )

To update snapshots set env var UPDATE_SNAPSHOTS=true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like snapshotx.SnapshotT is not available in the version of snapshotx that hydra uses (0.0.368). Tried to bump to lastest - 0.0.450 - a lot of tests fail. So I used SnapshotTExcept.

Also, it generated 6 snapshots with the same content - probably because tests are run in parallel.

"session.id_token.id_token_claims.rat",
"session.id_token.id_token_claims.auth_time",
}
snapshotx.SnapshotTExcept(t, hookReq, except)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is indeed tidier, thanks for pointing me into snapshotx!

@sgal sgal changed the title feat: add id_token_extra to refresh token webhook data feat: add session and requester to refresh token webhook data Jul 29, 2022
@sgal sgal marked this pull request as ready for review July 29, 2022 19:45
@sgal
Copy link
Contributor Author

sgal commented Jul 29, 2022

Docs PR is also updated with the latest schema.

@aeneasr aeneasr merged commit 6d23859 into ory:master Aug 1, 2022
@sgal sgal deleted the id-token-extra-in-refresh-webhook branch August 1, 2022 11:44
@vinckr
Copy link
Member

vinckr commented Aug 1, 2022

Hello @sgal
Congrats on merging your first PR in Ory 🎉 !
Your contribution will soon be helping secure millions of identities around the globe 🌏.
As a small token of appreciation we send all our first time contributors a gift package to welcome them to the community.
Please drop me an email and I will forward you the form to claim your Ory swag!

@mih-kopylov
Copy link
Contributor

Hey @aeneasr
I've reaslized that oauth2/.snapshots/TestAuthCodeWithMockStrategy-strategy=jwt-case=2-description=should_pass_because_prompt=none_and_max_age_<_auth_time-should_call_refresh_token_hook_if_configured.json file, that is added in this pull request, breaks ability to checkout the repository on Windows OS.

Is there a chance to rename the files so that Windows contributors are not blocked?

@aeneasr
Copy link
Member

aeneasr commented Aug 25, 2022

Yes it’s already fixed on master!

@mih-kopylov
Copy link
Contributor

Sorry @aeneasr, but these two files are still in master:

Their names contain < symbol which is reserved in Windows

  • TestAuthCodeWithMockStrategy-strategy=jwt-case=2-description=should_pass_because_prompt=none_and_max_age_<_auth_time-should_call_refresh_token_hook_if_configured.json
  • TestAuthCodeWithMockStrategy-strategy=opaque-case=2-description=should_pass_because_prompt=none_and_max_age_<_auth_time-should_call_refresh_token_hook_if_configured.json

Am I missing anything?

@aeneasr
Copy link
Member

aeneasr commented Aug 25, 2022

Oh I see, then it's only fixed on the v2.x branch!

@mih-kopylov
Copy link
Contributor

Is there a chance to get the changes backported to master?

@aeneasr
Copy link
Member

aeneasr commented Aug 25, 2022

Sure, can you maybe just cherry-pick the commit and make a PR?

@mih-kopylov
Copy link
Contributor

Sure, will do.

@mih-kopylov
Copy link
Contributor

@aeneasr #3229 here it is

@aeneasr
Copy link
Member

aeneasr commented Aug 25, 2022

👍

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.

External SessionID for integrators
5 participants