ServiceStack / Issues

Issue Tracker for the commercial versions of ServiceStack
11 stars 8 forks source link

GetUserSessionAsync in IUserSessionSourceAsync should have access to the current request when refreshing a JWT token. #760

Closed jhf closed 3 years ago

jhf commented 3 years ago

We are trying to implement a IUserSessionSourceAsync that validates the user and creates a new session, however, it is proving difficult to implement.

To create a new session with

SessionFeature.CreateNewSession(request, SessionExtensions.CreateRandomSessionId())

the current request is required, and it is not available to GetUserSessionAsync.

I have tried to use var request = HostContext.GetCurrentRequest(); to lookup the request, but it fails with the error

This AppHost does not support accessing the current Request via a Singleton

Looking at the source code, the current request is always available when GetUserSessionAsync is called, ref. https://github.com/ServiceStack/ServiceStack/blob/cae6c6a64923c227f1a9570099563ef720725e90/src/ServiceStack/RequestExtensions.cs#L303

I think that GetUserSessionAsync should have had another signature altogether, exposing the always available request. I.e.

Task<IAuthSession> GetUserSessionAsync(IRequest Request, string userAuthId, CancellationToken token=default)

Having that would make it trivial to implement.

And of course having another interface IUserSessionSourceWithRequestAsync that does add the request would solve it too, but I don't think it makes sense to create sessions without the request, in principle.

mythz commented 3 years ago

IUserSessionSourceAsync should not be populated from the current Request (which in JWT Requests would return a partial expired session), its purpose is for when you're not using an Auth Repository to return a populated session with the latest user info from your custom user data source. This up-to-date user info is what's used to populate the new partial session encapsulated in the newly issued JWT Bearer Token.

The JWT Payload specifies what info is embedded in JWT Tokens: