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

Blazor WASM oidc auth is trying to authenticate using expired refresh token #59810

Closed
1 task done
wim07101993 opened this issue Jan 10, 2025 · 6 comments
Closed
1 task done
Labels
area-security ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@wim07101993
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have an application which authenticates to a keycloak instance using openId connect. After a while the acces-token an refresh token are expired (on my environment the expiry-time is rather small to do some tests).

.Net however does not seem to notice this and requests a new access-token using the expired refresh-token. This ofcourse results in a 400 response from keycloak ({"error":"invalid_grant","error_description":"Token is not active"})

Expected Behavior

When the refresh-token is expired: The user should be redirected to keycloak, in this case, to reauthenticate.

Steps To Reproduce

My configuration:

        var configSection = configuration.GetSection("Authentication:Schemes:Oidc");

        services
            .AddOptions<OidcOptions>()
            .Bind(configSection)
            .ValidateDataAnnotations()
            .ValidateOnStart();

        var oidcOptions = configSection.Get<OidcOptions>();

        services
            .AddAuthorizationCore(options =>
            {
                // add policies
            })
            .AddCascadingAuthenticationState()
            .AddOidcAuthentication(options =>
            {
                options.ProviderOptions.Authority = oidcOptions.Authority;
                options.ProviderOptions.ClientId = oidcOptions.ClientId;
                options.ProviderOptions.ResponseType = "code";
            });

Exceptions (if any)

No response

.NET Version

.net8

Anything else?

No response

@MackinnonBuck
Copy link
Member

Thanks for reaching out.

This behavior is by-design. You can catch the AccessTokenNotAvailableException and handle the scenario explicitly, as shown in our docs: https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/additional-scenarios?view=aspnetcore-9.0#attach-tokens-to-outgoing-requests:~:text=%22WebAPI%22))%3B-,The%20configured%20HttpClient%20is%20used%20to%20make%20authorized%20requests%20using%20the%20try%2Dcatch%20pattern,-%3A

Please let us know if this helps your scenario!

@MackinnonBuck MackinnonBuck added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jan 13, 2025
@wim07101993
Copy link
Author

wim07101993 commented Jan 14, 2025

Even if the behavior of throwing an exception is expected, why would the application need to try to send the request to the idp? Wouldn't it be better to just throw the exception then?

(The solution provided by @MackinnonBuck did solve the user-problem)

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Jan 14, 2025
@MackinnonBuck
Copy link
Member

@wim07101993, the client doesn't necessarily know when the refresh token expires; the server can revoke the refresh token at any time.

@wim07101993
Copy link
Author

True but if the client already knows that the token has been expired, it does not need to make the network request. Does it?

@halter73
Copy link
Member

I'm not sure the client has a way to know if the refresh token is expired other than trying it. The normal expires_in property on the OIDC /token response is for the access_token not the refresh_token. A quick search indicates that some OIDC servers provide a refresh_expires_in value, but it's nonstandard. And I don't think the refresh token is required to be a JWT, so I don't think we can read an exp claim.

@MackinnonBuck
Copy link
Member

We're going to close this as answered. Please open a new issue if you still believe there's a product change we should make here.

@MackinnonBuck MackinnonBuck closed this as not planned Won't fix, can't repro, duplicate, stale Jan 22, 2025
@MackinnonBuck MackinnonBuck added ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-security ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

3 participants