kratos icon indicating copy to clipboard operation
kratos copied to clipboard

feat: pass OIDC claims into post-login flow to include in web hook context

Open fenech opened this issue 1 year ago • 17 comments

The login flow doesn't trigger a refresh of the identity when the OIDC claims have changed. By passing the claims through to the web hook context, this means that an external handler can be configured to update the identity as appropriate, when there are changes.

Related issue(s)

#2898

Checklist

  • [x] I have read the contributing guidelines.
  • [x] I have referenced an issue containing the design document if my change introduces a new feature.
  • [x] I am following the contributing code guidelines.
  • [x] I have read the security policy.
  • [x] I confirm that this pull request does not address a security vulnerability. If this pull request addresses a security vulnerability, I confirm that I got the approval (please contact [email protected]) from the maintainers to push the changes.
  • [x] I have added tests that prove my fix is effective or that my feature works.
  • [ ] I have added or changed the documentation.

Further Comments

The PR looks a lot bigger due to the fact that the Claims type has been moved into its own separate package, to resolve a circular dependency. I suspect that it is not in the right place and should be moved elsewhere.

The linked issue refers to refreshing the identity directly as part of the login flow, to handle the case where the OIDC Claims have changed.

However, my follow-up proposal is different: if instead of directly updating the identity, we just pass the OIDC Claims through to the web hook context, we allow users to write their own web hook handler to update the identity as they would like. I think it's a much less invasive change to Kratos (just passing an extra object through the login/post-login flow).

fenech avatar May 14 '24 14:05 fenech

I'm periodically coming back to check the status of this PR and clicking "update" to rebase it, but I don't know if you'd prefer for me to just leave it as it is :smile:

I'm happy to contribute to some docs as well, just wanted to make sure the approach was good before starting to do that.

fenech avatar Jun 04 '24 16:06 fenech

Yes, this is crutial, otherwise we can't keep SSO information up to date.

kghost avatar Jun 06 '24 13:06 kghost

Codecov Report

Attention: Patch coverage is 78.00000% with 22 lines in your changes missing coverage. Please review.

Project coverage is 78.19%. Comparing base (b192c92) to head (a717bfa).

Files Patch % Lines
selfservice/strategy/oidc/provider_discord.go 0.00% 3 Missing :warning:
selfservice/strategy/oidc/provider_x.go 0.00% 3 Missing :warning:
selfservice/strategy/oidc/provider_apple.go 50.00% 2 Missing :warning:
selfservice/strategy/oidc/provider_github.go 0.00% 2 Missing :warning:
selfservice/strategy/oidc/provider_github_app.go 0.00% 2 Missing :warning:
selfservice/strategy/oidc/provider_lark.go 0.00% 2 Missing :warning:
selfservice/strategy/oidc/provider_patreon.go 0.00% 2 Missing :warning:
selfservice/strategy/oidc/provider_slack.go 0.00% 2 Missing :warning:
selfservice/strategy/oidc/provider_spotify.go 0.00% 2 Missing :warning:
selfservice/strategy/oidc/provider_dingtalk.go 66.66% 1 Missing :warning:
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3922      +/-   ##
==========================================
+ Coverage   78.17%   78.19%   +0.02%     
==========================================
  Files         363      365       +2     
  Lines       25453    25459       +6     
==========================================
+ Hits        19898    19908      +10     
+ Misses       4032     4030       -2     
+ Partials     1523     1521       -2     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Jun 14 '24 05:06 codecov[bot]

I just checked and the overall issue with this approach is that the webhook target cannot update the identity on login:

Modifying the identity is currently only possible during the registration and settings flows.

However, I think that adding the claims to the webhook is an OK solution, but not necessarily for the root problem. It is useful to notify other systems, but there will always be some delay between the login and update of the identity through another API call. This will be especially a problem for integrations using session to JWT or some kind of short-term cache.

IMO we should still merge this PR. I would however consider either allowing the webhook to update the identity in the response (adds latency to login), and/or add some other "quick" path for cases where one only wants to mirror claims into the identity (i.e. jsonnet).

zepatrik avatar Jun 17 '24 10:06 zepatrik

@zepatrik

Another PR is open that implements the jsonnet flow: https://github.com/ory/kratos/pull/3788

Not sure if that has been a consideration down the line

robinknaapen avatar Jun 17 '24 13:06 robinknaapen

Anything I can do to help move this along, @aeneasr (aside from pinging you :smile: )?

fenech avatar Jul 23 '24 13:07 fenech

I did not look into the implementation in depth, but it seems like the settings flow has not been considered? This would probably solve the issue mentioned here https://github.com/ory/kratos/issues/3816

As it is possible to set up new OIDC connections via the settings flow as well, would it make sense to also include the claims in the webhook there? For us it makes sense to have similar information as with registration to perform for example some sort of validation. While the login flow definitely makes sense to keep everything up to date, it does not fix the need for validation BEFORE accepting the new OIDC provider.

If that would already be the case, I would be very happy. Otherwise I feel like it could be a nice enhancement to this PR.

spijs avatar Jul 23 '24 13:07 spijs

As it is possible to set up new OIDC connections via the settings flow as well, would it make sense to also include the claims in the webhook there? For us it makes sense to have similar information as with registration to perform for example some sort of validation. While the login flow definitely makes sense to keep everything up to date, it does not fix the need for validation BEFORE accepting the new OIDC provider.

This isn't something that I'd considered, as our use case only involves self-registration and then login. Since we are already using a version of Kratos based on this branch, I'd quite like to keep the scope minimal to increase its chances of getting merged, but if the project members see it as a useful addition then I'm happy to make an update.

To be honest, this was always intended as a workaround for our use case, and for us it would be sufficient to just do the same Jsonnet mapping onto the identity at both registration and login time, as @zepatrik mentioned earlier in the thread.

fenech avatar Aug 30 '24 10:08 fenech

I've rebased this onto latest master and resolved the conflicts. There are some e2e tests that are failing, but as far as I can tell, they're not related to these changes.

fenech avatar Oct 01 '24 10:10 fenech

Totally my bad for the lack of response here. 2024 was a very busy year. I will revisit this now with the intention of merging it upstream.

aeneasr avatar Jan 22 '25 12:01 aeneasr

@aeneasr would you like me to rebase and fix the conflicts, or were you planning on making changes yourself?

fenech avatar Jan 23 '25 08:01 fenech

Totally my bad for the lack of response here. 2024 was a very busy year. I will revisit this now with the intention of merging it upstream.

looks like 2025 is shaping up to be a busy year too :smile: anyway let me know if you want me to resolve the conflicts!

fenech avatar Feb 25 '25 09:02 fenech

any news ?

fmiqbal avatar Mar 24 '25 02:03 fmiqbal