NoPyGod
NoPyGod

Reputation: 5067

SPA with refresh tokens stored in cookie - how to configure with IdentityServer4?

I have an SPA which communicates with an API using a long-lived self-contained JWT for auth.

The SPA is currently storing this JWT in Local Storage.

Apart from this being pretty bad from a security standpoint, the other major problem with is that I have way of revoking access to the API (which btw, needs to remain stateless). One a user has a token, they can use it indefinitely.

I'd like to start using refresh tokens. I know these are typically not recommended for SPAs, however after reading The Ultimate Guide to handling JWTs on frontend clients I believe there is a way to do this securely.

What I would like:

This would seem to be about the most secure option, minimising both XRSF and CSRF:

If this method is as full proof as I think it is (prove me wrong, please!), why is it seldom mentioned online?

The IdentityServer4 docs don't seem to cover this. Can anybody suggest how it might be implemented? I hoped that there might be a property I could set on in the Client config along the lines of UseCookiesForRefresh, but no.

Upvotes: 3

Views: 1595

Answers (1)

TTCG
TTCG

Reputation: 9113

You can use ReferenceToken type instead of Jwt Token to revoke the tokens whenever the user's session is ended (or logout). Reference Token

You need to change the AccessTokenType in Client configuration

new Client
    {
        ClientName = "OAuth Test",
        ClientId = "TestClientId",
        AllowedGrantTypes = GrantTypes.Hybrid,
        AccessTokenLifetime = 300,
        AllowOfflineAccess = true,
        AccessTokenType = AccessTokenType.Reference,
        ......
    }

When the user logs out, you can use the following lines to revoke all tokens generated within that session.

await _interaction.RevokeTokensForCurrentSessionAsync();

Upvotes: 1

Related Questions