@@ -61,30 +61,39 @@ class oauth2_exception : public std::exception
6161// / OAuth 2.0 configuration.
6262// /
6363// / Encapsulates functionality for:
64- // / - Authenticating requests with an access token.
65- // / - Performing the OAuth 2.0 authorization code grant authorization flow.
66- // / See: http://tools.ietf.org/html/rfc6749#section-4.1
64+ // / - Authenticating requests with an access token.
65+ // / - Performing the OAuth 2.0 authorization code grant authorization flow.
66+ // / See: http://tools.ietf.org/html/rfc6749#section-4.1
67+ // / - Performing the OAuth 2.0 implicit grant authorization flow.
68+ // / See: http://tools.ietf.org/html/rfc6749#section-4.2
6769// /
68- // / Usage for authorization:
70+ // / Performing OAuth 2.0 authorization:
6971// / 1. Set service and client/app parameters:
70- // / - Client/app key & secret (as provided by the service).
71- // / - The service authorization endpoint and token endpoint.
72- // / - Your client/app redirect URI.
73- // / - Set if bearer token is passed in query or header field (default: header).
74- // / See: http://tools.ietf.org/html/rfc6750#section-2
75- // / - If the service uses "non-standard" access token key, set it also (default: "access_token").
76- // / 2. Open web browser with URI from build_authorization_uri().
77- // / - The passed state string should be unique for this authorization session.
78- // / 3. In the web browser, the resource owner clicks "Yes" to authorize your client/app.
79- // / 4. To signal authorization, web browser is redirected to redirect_uri().
80- // / 5. The redirect contains the authorization code and the state string.
81- // / 6. Check the state string equals the one in step 2.
82- // / 7. Pass the authorization code to fetch_token() to create a token fetch task.
72+ // / - Client/app key & secret (as provided by the service).
73+ // / - The service authorization endpoint and token endpoint.
74+ // / - Your client/app redirect URI.
75+ // / - Use set_state() to assign a unique state string for the authorization
76+ // / session (default: "").
77+ // / - If needed, use set_bearer_auth() to control bearer token passing in either
78+ // / query or header (default: header). See: http://tools.ietf.org/html/rfc6750#section-2
79+ // / - If needed, use set_access_token_key() to set "non-standard" access token
80+ // / key (default: "access_token").
81+ // / - If needed, use set_implicit_grant() to enable implicit grant flow.
82+ // / 2. Build authorization URI with build_authorization_uri() and open this in web browser/control.
83+ // / 3. The resource owner should then clicks "Yes" to authorize your client/app, and
84+ // / as a result the web browser/control is redirected to redirect_uri().
85+ // / 5. Capture the redirected URI either in web control or by HTTP listener.
86+ // / 6. Pass the redirected URI to token_from_redirected_uri() to obtain access token.
87+ // / - The method ensures redirected URI contains same state() as set in step 1.
88+ // / - In implicit_grant() is false, this will create HTTP request to fetch access token
89+ // / from the service. Otherwise access token is already included in the redirected URI.
8390// /
8491// / Usage for issuing authenticated requests:
85- // / 1. Obtain token. (Perform authorization as above or get token otherwise.)
86- // / 2. Use http_client_config::set_oauth2() to set configuration, and construct http_client using it.
87- // / 3. All requests issued with that http_client will be OAuth 2.0 -authenticated.
92+ // / 1. Perform authorization as above to obtain the access token or use an existing token.
93+ // / - Some services provice option to generate access tokens for testing purposes.
94+ // / 2. Pass the resulting oauth2_config with the access token to http_client_config::set_oauth2().
95+ // / 3. Construct http_client with this http_client_config. After this all requests
96+ // / by that client will be OAuth 2.0 authenticated.
8897// /
8998// / </summary>
9099class oauth2_config
@@ -98,6 +107,7 @@ class oauth2_config
98107 m_auth_endpoint (auth_endpoint),
99108 m_token_endpoint (token_endpoint),
100109 m_redirect_uri (redirect_uri),
110+ m_implicit_grant (false ),
101111 m_bearer_auth (true ),
102112 m_http_basic_auth (true ),
103113 m_access_token_key (_XPLATSTR(" access_token" ))
@@ -106,6 +116,7 @@ class oauth2_config
106116
107117 oauth2_config (utility::string_t token) :
108118 m_token (std::move(token)),
119+ m_implicit_grant (false ),
109120 m_bearer_auth (true ),
110121 m_http_basic_auth (true ),
111122 m_access_token_key (_XPLATSTR(" access_token" ))
@@ -115,17 +126,25 @@ class oauth2_config
115126 // / <summary>
116127 // / Builds an authorization URI to be loaded in the web browser.
117128 // / The URI is built with auth_endpoint() as basis.
129+ // / The implicit_grant() affects the built URI by selecting
130+ // / either authorization code or implicit grant flow.
118131 // / </summary>
119132 _ASYNCRTIMP utility::string_t build_authorization_uri () const ;
120133
121134 // / <summary>
122- // / Parses authorization code from the redirected URI when resource owner
123- // / has accepted the authorization.
124- // / Redirected URI must satisfy the following (otherwise an exception is thrown):
125- // / - Must contain both 'code' and 'state' query parameters.
126- // / - The 'state' parameter must be equal to state().
135+ // / Get the access token based on redirected URI.
136+ // / Behavior depends on the implicit_grant() setting.
137+ // / If implicit_grant() is false redirect URI is parsed for 'code' query
138+ // / parameter which is then used to fetch a token
139+ // / from token_endpoint().
140+ // / Otherwise, redirect URI fragment part is parsed for 'access_token'
141+ // / parameter containing the token.
142+ // / In both cases 'state' parameter is parsed and verified to match state().
143+ // / When token is successfully obtained, set_token() is called, and config is
144+ // / ready for use.
145+ // / An oauth2_exception is thrown if anything fails.
127146 // / </summary>
128- _ASYNCRTIMP utility:: string_t parse_code_from_redirected_uri (uri redirected_uri) const ;
147+ _ASYNCRTIMP pplx::task< void > token_from_redirected_uri (uri redirected_uri);
129148
130149 // / <summary>
131150 // / Creates a task to fetch token from the token endpoint.
@@ -158,13 +177,21 @@ class oauth2_config
158177 const utility::string_t & state () const { return m_state; }
159178 // / <summary>
160179 // / State string should be unique for each authorization session.
161- // / This state string should be returned by the authorization server on redirect
180+ // / This state string should be returned by the authorization server on redirect.
162181 // / </summary>
163182 void set_state (utility::string_t state) { m_state = std::move (state); }
164183
165184 const utility::string_t & token () const { return m_token; }
166185 void set_token (utility::string_t token) { m_token = std::move (token); }
167186
187+ bool implicit_grant () const { return m_implicit_grant; }
188+ // / <summary>
189+ // / False means authorization code grant flow is used.
190+ // / True means implicit grant flow is used.
191+ // / Default: False.
192+ // / </summary>
193+ void set_implicit_grant (bool enable) { m_implicit_grant = std::move (enable); }
194+
168195 bool bearer_auth () const { return m_bearer_auth; }
169196 // / <summary>
170197 // / Bearer token passing method. This must be selected based on what the service accepts.
@@ -202,6 +229,7 @@ class oauth2_config
202229 utility::string_t m_scope;
203230 utility::string_t m_state;
204231
232+ bool m_implicit_grant;
205233 bool m_bearer_auth;
206234 bool m_http_basic_auth;
207235 utility::string_t m_access_token_key;
0 commit comments