Wrong implementation for refresh_token grant
The refresh_token grant implementation contains an error. The library remembers the scopes that were requested in the original authorization request ('scope' parameter). In a refresh_token grant these remembered scopes are used. If the user removed consent for some scopes on the consent page, every following refresh_token grant fails because broader scope is requested than originally granted. A correct implementation must use the scopes that are returned from the token request (the optional scope parameter in the token response will contain only the really granted scopes if they differ from the requested scopes).
I am not sure what the actual problem is. The implementation does reflect the described (correct) behavior: https://github.com/openid/AppAuth-Android/blob/master/library/java/net/openid/appauth/TokenResponse.java#L418 I am not sure what the "every following refresh_token grant fails because broader scope is requested than originally granted". As you stated correctly, the request will work and return a token that has access to a limited set of scopes and these scopes are then defined in the scopes parameter. The IdP might chose to refresh the refresh_token on that request as well, but the source of the issuance of the refresh token remains the same (the one with the user interaction), therefore all subsequent token exchanges (even with the too big list of scopes) are working as long as the refresh token(s) are valid - with limited access due to the removed scopes.
If the library does an automatic token refresh the scopes of the original authorization request are used: https://github.com/openid/AppAuth-Android/blob/f1dbeb51f4d905523d804d70363abdb00728d99c/library/java/net/openid/appauth/AuthState.java#L623 If these scopes were reduced by user consent the token refresh request still will request all original scopes including those removed by the user and the refresh fails. Example:
- Authorization request with scopes: A.read B.write C.write
- User removes consent on C.write
- access token with scopes A.read B.write is issued
- access token times out, automatic refresh is executed
- refresh request with scope A.read B.write C.write is generated
- refresh fails because C.write scope is not allowed in refresh request as the user removed consent originally
Ah, I see. So you are saying that the first authorization request was downgraded by the user to only contain a subset of scopes (the request never completed with the full set) and an access token along with a refresh token was issued that only contained that subset in the authorization flow.
I agree, in that case the scope should be different. In general, if there is a user-interaction (not just a token exchange) and a scope value was returned, that one should be used for subsequent none interactive token requests.
The spec is a bit scattered, so this is not an obvious read for everyone, but I guess it boils down to these sections here:
RFC 6749 Section 3.3 states (Code Grant Section):
If the issued access token scope is different from the one requested by the client, the authorization server MUST include the "scope" response parameter to inform the client of the actual scope granted.
So the spec does not demand a failure here. However, there is a specific error defined for such a case (Section 5.2):
invalid_scope . The requested scope is invalid, unknown, malformed, or exceeds the scope granted by the resource owner.
Looking at the part about the refresh token, the wording changes significantly (Section 6):
scope
OPTIONAL. The scope of the access request as described by Section 3.3. The requested scope MUST NOT include any scope not originally granted by the resource owner, and if omitted is treated as equal to the scope originally granted by the resource owner.
So, yeah - good and valid point! +1 Too bad, there is no maintainer for that project anymore, would have been a rather straight forward fix, I guess.
Not currently an issue since e9c6f5a48f03db5e21e4079bfd53d7defecb382f no longer passes in any scope. Interesting that mScope was being updated but not used by the request https://github.com/openid/AppAuth-Android/blob/4e962e64242c21e3eff3da768340fd755293074d/library/java/net/openid/appauth/AuthState.java#L393
There's also a related RFE https://github.com/openid/AppAuth-Android/issues/664 that may be of future interest.
Closing this for now.