OAuth2: Support Resource Owner Password Credentials Grant
Would it be possible to add support for Resource Owner Password Credentials Grant? E.g. something like this:
class oauth2_config
{
public:
/// <summary>
/// Fetches a new access token (and possibly a new refresh token) using the resource owner password grant.
/// The task creates a HTTP request to the token_endpoint().
/// If successful, resulting access token is set as active via set_token().
/// See: http://tools.ietf.org/html/rfc6749#section-4.3.2
/// This also sets a new refresh token if one was returned.
/// </summary>
/// <returns>Task that fetches the token(s) using the resource owner password.</returns>
pplx::task<void> token_from_password(utility::string_t username, utility::string_t password)
{
uri_builder ub;
ub.append_query(details::oauth2_strings::grant_type, details::oauth2_strings::password, false);
ub.append_query(details::oauth2_strings::username, uri::encode_data_string(username), false);
ub.append_query(details::oauth2_strings::password, uri::encode_data_string(password), false);
return _request_token(ub);
}
This seems very reasonable. Note that _request_token() will need to be modified to handle the new flow; this is probably best handled by moving the grant type specific code into a separate step, called before _request_token(). At first glance, this would only affect token_from_refresh(), token_from_code(), and _request_token().
However, one thing is unclear to me: when requesting a refresh token for the Resource Owner Password Credentials Grant, do you need to re-authenticate with the username and password again? If so, the APIs may need to be more drastically modified.
I have tested the code I posted above and it works as is: there is no need to change anything in the _request_token(). The only additional thing needed is to add details::oauth2_strings::password and oauth2_strings::username constants.
The refresh token you get from the Resource Owner Password Credentials Grant is a normal refresh token that works exactly the same way as refresh token from the code grant flow. No need to provide user credentials again.
I'm concerned about the lines around https://github.com/Microsoft/cpprestsdk/blob/04d86e020c9c28e4435a6bebbf08d776d48d7b8a/Release/src/http/oauth/oauth2.cpp#L133. These will add the client_id and client_secret fields to the request even though they are not used in the Resource Owner Password Credentials Grant.
Thanks for clarifying the refresh flow.
Based on https://tools.ietf.org/html/rfc6749#section-3.2.1
A client MAY use the "client_id" request parameter to identify itself when sending requests to the token endpoint. In the "authorization_code" "grant_type" request to the token endpoint, an unauthenticated client MUST send its "client_id"
Otherwise there should not be difference in using the token endpoint in code grant flow and password grant flow. So I believe these two scenarios (auth code and password grant) could share the code.
I am not sure what are the lines around https://github.com/Microsoft/cpprestsdk/blob/04d86e020c9c28e4435a6bebbf08d776d48d7b8a/Release/src/http/oauth/oauth2.cpp#L133 about. I could guess the if-else there is about "authenticated" and "unauthenticated" clients, but then why clientSecret is also sent in the later case?
For our 3.0 release, I have been working on an overhaul to the OAuth2 APIs. As part of the overhaul, I added an API for the Resource Owner Credentials Grant. Additionally, since you're using the OAuth2 API, I'd appreciate any feedback you have on the proposed new APIs.
The Resource Owner Credentials Grant is at https://github.com/Microsoft/cpprestsdk/blob/192541718bdae3298cfcc06bd17d11d22a8cbbb1/Release/include/cpprest/oauth2.h#L423
The OAuth2Client sample has been updated to use the new APIs. https://github.com/Microsoft/cpprestsdk/blob/192541718bdae3298cfcc06bd17d11d22a8cbbb1/Release/samples/Oauth2Client/Oauth2Client.cpp
Hi, I think this looks very good. At first it was not obvious to me how information about token endpoint is passed to the resource_owner_creds_grant_flow() and set_token_via_refresh_token() methods. Then I figured out it is the token_client.base_uri. Looking forward to see the 3.0 released!
Is there any update on Client Credentials Grant support ?
Is there any update on Client Credentials Grant support ? @ras0219-msft @alot1 @lelongg