Extract state when access is denied

Is there a good way to decode the state when the user denies access? The state has a lot of useful information stashes that will help us figure out where to send the user back to. Currently, I get an exception and it doesn’t seem to have the state information in there. I can see that the request sent back from the OAuth provider has the state variable.
It would’ve been really nice if the state extraction was separate from the handling of the received code or status. Looking thru the code, looks like it’s a protected resource.

To transport state to the OAuth2 flow you can use the different state implementations. There exists also a UserState handler that can transport arbitrary state through the flow.

What kind of Exception was thrown in your code? Does it come from Silhouette?

Its AccessDeniedException when the user decides not to grant access. I have the state passed in thru UserStateHandler. Just need to figure out the best way to extract it when the OAuth provider calls me back.

Here’s the rough code I’m using to receive the code:

          p2.authenticate(UserStateItem(Map())).flatMap {
            case Left(result) =>
              Future.successful(result)
            case Right(stat: StatefulAuthInfo[OAuth2Info, UserStateItem]) =>
              // Do Something useful

I have a Try block around it that is catching the exception and I don’t get the user state in that situation.

I found a way to deserialize. Not sure if there’s a better way to handle this. All this is in the receiveCode method:

val userStateItemHandler = new UserStateItemHandler(UserStateItem(Map.empty[String, String]))
val newStateHandler = stateHandler.withHandler(userStateItemHandler)
newStateHandler.unserialize(state) flatMap { userState =>
// Call authenticate in here and catch(recover) from the exception
}