Reputation: 83
I'm exploring the TokenRelay filter of Spring Cloud Gateway in order to mediate between a SAML2 Session and OAuth2 Tokens (More on this in my previous question here).
I've tried to figure this out myself but I can't seem to sort it out: using this filter, who is the OAuth2 client?
I was expecting this to be the gateway itself, hence a confidential one.. but in my initial setup I see calls for the authorization code in the browser and I don't understand if this is the expected behavior or not.
If it's indeed how it's supposed to work and the Browser carries out the token exchange, does this mean that the client cannot be confidential? Should I switch to PKCE?
I might sound a little confused (I really am!) but I can't seem to find much about the matter.
Thanks!
Upvotes: 0
Views: 1736
Reputation: 12534
I'll make a distinction between:
When configuring single-page and mobile applications as public OAuth2 clients, the OAuth2 client is running inside the user-agent, so it's difficult to distinguish. This also has security implications that aren't the subject here (the token endpoint cannot be protected with a secret, and the tokens are exposed to the Javascript code and, as such, to XSS attacks).
When the OAuth2 client with the authorization code and refresh token flows (oauth2Login
in Spring Security's DSL) is configured on the backend, the token endpoint can be protected with a secret (and firewall rules if we get picky), and tokens are kept safe on the server. In this case the distinction between the user-agent and the OAuth2 client is clear.
Let's review how the authorization code flow works with this distinction in mind:
/oauth2/authorization/{registration-id}
by default with Spring Security)/login/oauth2/code/{registration-id}
by default)In OAuth2 BFF pattern, the OAuth2 client is the BFF itself => the browser is the vessel to transmit the authorization-code from the authorization server to the gateway, but it never sees the OAuth2 tokens (only the gateway and downstream resource servers do).
I published on Baeldung a tutorial for configuring Spring Cloud Gateway as an OAuth2 BFF with oauth2Login
and the TokenRelay
filter. It contains sample implementations for Angular, React (Next.js, but without the NextAuth lib), and Vue.
Upvotes: 3