oauth2-proxy: keycloak-oidc broken out of the box with Keycloak 21.x

Opening this issue as a result of numerous related issues such as https://github.com/oauth2-proxy/oauth2-proxy/issues/1913 and https://github.com/oauth2-proxy/oauth2-proxy/issues/1931 being prematurely closed.

Oauth2 Proxy as of version v7.4.0 is broken with provider = "keycloak-oidc" using the latest available stable Keycloak version 21.x. The authentication flow fails with:

[2023/05/16 17:41:06] [oauthproxy.go:830] Error creating session during OAuth2 callback: audience from claim aud with value [account] does not match with any of allowed audiences map[oauth2-proxy:{}]

Previous issues were responded to with responses to the extent of “this is Keycloak’s fault” or “work with Keycloak to fix this.”

On the contrary, this is very much not “Keycloak’s fault”. We have a platform with around 20 applications integrated with Keycloak/OIDC, and oauth2-proxy is the only one that requires the additional step of adding proxy mapper in the Keycloak admin UI. It’s clunky and shouldn’t be necessary. At the very least it should be documented by oauth2-proxy. More appropriately, oauth2-proxy should update its API calls to Keycloak to get info that is already readily available.

keycloak-oauth2-proxy-mapper

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 12
  • Comments: 20 (3 by maintainers)

Most upvoted comments

Please reopen

Why is a mapper needed for “Oauth2 Proxy” but not for other apps? Why does Oauth2 Proxy need to enfoce audience check?

In our case we don’t want specific audiences in the access tokens. So as a workaround for Oauth2 Proxy we’ll need to allow the “account” audience in the proxy configuration which seems to be there by default when using Keycloak.

Okay, I have a workaround, but you aren’t going to like it

First of all, we are solving 2 problems which is always fun. 1 problem I know how to work around, the other disappeared and hopefully does for you too and I don’t understand why.


Problem 1: Audience You need to verify that your user has the correct audience name, because Keycloak seems to at first put in a weird ID string instead of the name you gave it. To verify yours is working do the following

Keycloak -> Realm -> <your oauth2 proxy client> -> Client scopes -> Evaluate -> Users

Pick a user from that list

On the right side click Generated access token

Verify under “aud”:{} that your oauth2 proxy client name matches. If you see a UUID looking thing there, go back and delete your client mapper and set it up again.

I don’t know why this works, but now mine does. I would poke around with the options, onetime I named my mapper NOT my client name, and just put the client name in the alternative field. But at this point, I have mine set the way the docs say and it works with also adding the name into the Included Custom Audience field Mapper Type: Audience Name: <myoauth2proxyclient> Included Client Audience: null Included Custom Audience: <myoauth2proxyclient> Add to ID token: On Add to access token: On Add to lightweight access token: On Add to token introspection: On


Problem 2: Roles aren’t seeming to matter, so that if you set --allowed-role=<correctrolname> you get 403 errors.

Here it seems oauth2proxy is doing a serach of the token, finds the very first word “role” pulls in those array values. And stops. It doesn’t look anymore for roles. Even if you direct the --allowed-role to the correct array with the clientname_roles:role name format

Clearly this is a bug, and I lack the abilites to find it, but hopefully my post will inspire someone who does understand it better than me.

But to work around your problem, you need to add a role for application access to your Realm Roles.

The good news is any user you have already created will NOT automatically inherit this role. What I can’t speak for as I can’t test it, is what happens when NEW user is created. Do they just inherit that realm role, and I assume they would.

But you can also take out realm roles from individual users to alleviate this problem. This is for sure not ideal, and if you are reading this post in the year 2025 or after, please verify you need to do this. Hopefully the issue is fixed and you won’t need this work around.

@olivierlambert I think you can fix your issue with the Problem 2 workaround, and I only shout you out because I love XCP-NG. Everyone should 😉

I’ve followed the oauth2-proxy docs for setting up a dedicated audience mapper, but I am still getting this exact error. Is there supposed to be a workaround?

with oidc_extra_audiences = ["account"] will make every user in realm can login.

Hey thanks! I will test it ASAP 👍 (well… in 2 weeks, when coming back from some deserved rest 😄 )

It seems I posted the message in the wrong place the first time.

I had this temporary “fix” in Keycloak for a while (using Mappers), and now with Keycloak 25.1, I can’t make it work anymore for any new client I add (or if I modify a previous client):

[oauthproxy.go:888] Error creating session during OAuth2 callback: audience from claim aud with value [xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx account] does not match with any of allowed audiences map[elastic:{}]

Note: the UUID (given with xxxx here) is the UUID of the client-id.

I checked 10 times, follow the doc in the exact same details and despite that, I can’t make it work anymore. Note there’s extra fields/boxes in Keycloak 25, that could made the configuration different: image

Does anyone with a recent Keycloak made it work?

Hi, any update on this ? I have the same issue and I do not use the --allowed-group flag ?