@@ -78,13 +78,13 @@ class AuthorizationCodeGrant {
78
78
/// Callback to be invoked whenever the credentials are refreshed.
79
79
///
80
80
/// This will be passed as-is to the constructed [Client] .
81
- CredentialsRefreshedCallback _onCredentialsRefreshed;
81
+ final CredentialsRefreshedCallback _onCredentialsRefreshed;
82
82
83
83
/// Whether to use HTTP Basic authentication for authorizing the client.
84
84
final bool _basicAuth;
85
85
86
86
/// A [String] used to separate scopes; defaults to `" "` .
87
- String _delimiter;
87
+ final String _delimiter;
88
88
89
89
/// The HTTP client used to make HTTP requests.
90
90
http.Client _httpClient;
@@ -105,7 +105,7 @@ class AuthorizationCodeGrant {
105
105
106
106
/// Allowed characters for generating the _codeVerifier
107
107
static const String _charset =
108
- " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" ;
108
+ ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~' ;
109
109
110
110
/// The generated PKCE code verifier
111
111
String _codeVerifier;
@@ -146,9 +146,10 @@ class AuthorizationCodeGrant {
146
146
bool basicAuth = true ,
147
147
http.Client httpClient,
148
148
CredentialsRefreshedCallback onCredentialsRefreshed,
149
- Map <String , dynamic > getParameters (MediaType contentType, String body)})
149
+ Map <String , dynamic > Function (MediaType contentType, String body)
150
+ getParameters})
150
151
: _basicAuth = basicAuth,
151
- _httpClient = httpClient == null ? new http.Client () : httpClient ,
152
+ _httpClient = httpClient ?? http.Client (),
152
153
_delimiter = delimiter ?? ' ' ,
153
154
_getParameters = getParameters ?? parseJsonParameters,
154
155
_onCredentialsRefreshed = onCredentialsRefreshed;
@@ -175,7 +176,7 @@ class AuthorizationCodeGrant {
175
176
Uri getAuthorizationUrl (Uri redirect,
176
177
{Iterable <String > scopes, String state}) {
177
178
if (_state != _State .initial) {
178
- throw new StateError ('The authorization URL has already been generated.' );
179
+ throw StateError ('The authorization URL has already been generated.' );
179
180
}
180
181
_state = _State .awaitingResponse;
181
182
@@ -188,23 +189,23 @@ class AuthorizationCodeGrant {
188
189
_codeVerifier = _createCodeVerifier ();
189
190
var codeChallenge = base64Url
190
191
.encode (sha256.convert (ascii.encode (_codeVerifier)).bytes)
191
- .replaceAll ("=" , "" );
192
+ .replaceAll ('=' , '' );
192
193
193
- this . _redirectEndpoint = redirect;
194
- this . _scopes = scopes;
195
- this . _stateString = state;
194
+ _redirectEndpoint = redirect;
195
+ _scopes = scopes;
196
+ _stateString = state;
196
197
var parameters = {
197
- " response_type" : " code" ,
198
- " client_id" : this . identifier,
199
- " redirect_uri" : redirect.toString (),
200
- " code_challenge" : codeChallenge,
201
- " code_challenge_method" : " S256"
198
+ ' response_type' : ' code' ,
199
+ ' client_id' : identifier,
200
+ ' redirect_uri' : redirect.toString (),
201
+ ' code_challenge' : codeChallenge,
202
+ ' code_challenge_method' : ' S256'
202
203
};
203
204
204
205
if (state != null ) parameters['state' ] = state;
205
206
if (scopes.isNotEmpty) parameters['scope' ] = scopes.join (_delimiter);
206
207
207
- return addQueryParameters (this . authorizationEndpoint, parameters);
208
+ return addQueryParameters (authorizationEndpoint, parameters);
208
209
}
209
210
210
211
/// Processes the query parameters added to a redirect from the authorization
@@ -227,19 +228,19 @@ class AuthorizationCodeGrant {
227
228
Future <Client > handleAuthorizationResponse (
228
229
Map <String , String > parameters) async {
229
230
if (_state == _State .initial) {
230
- throw new StateError ('The authorization URL has not yet been generated.' );
231
+ throw StateError ('The authorization URL has not yet been generated.' );
231
232
} else if (_state == _State .finished) {
232
- throw new StateError ('The authorization code has already been received.' );
233
+ throw StateError ('The authorization code has already been received.' );
233
234
}
234
235
_state = _State .finished;
235
236
236
237
if (_stateString != null ) {
237
238
if (! parameters.containsKey ('state' )) {
238
- throw new FormatException ('Invalid OAuth response for '
239
+ throw FormatException ('Invalid OAuth response for '
239
240
'"$authorizationEndpoint ": parameter "state" expected to be '
240
241
'"$_stateString ", was missing.' );
241
242
} else if (parameters['state' ] != _stateString) {
242
- throw new FormatException ('Invalid OAuth response for '
243
+ throw FormatException ('Invalid OAuth response for '
243
244
'"$authorizationEndpoint ": parameter "state" expected to be '
244
245
'"$_stateString ", was "${parameters ['state' ]}".' );
245
246
}
@@ -249,9 +250,9 @@ class AuthorizationCodeGrant {
249
250
var description = parameters['error_description' ];
250
251
var uriString = parameters['error_uri' ];
251
252
var uri = uriString == null ? null : Uri .parse (uriString);
252
- throw new AuthorizationException (parameters['error' ], description, uri);
253
+ throw AuthorizationException (parameters['error' ], description, uri);
253
254
} else if (! parameters.containsKey ('code' )) {
254
- throw new FormatException ('Invalid OAuth response for '
255
+ throw FormatException ('Invalid OAuth response for '
255
256
'"$authorizationEndpoint ": did not contain required parameter '
256
257
'"code".' );
257
258
}
@@ -276,9 +277,9 @@ class AuthorizationCodeGrant {
276
277
/// Throws [AuthorizationException] if the authorization fails.
277
278
Future <Client > handleAuthorizationCode (String authorizationCode) async {
278
279
if (_state == _State .initial) {
279
- throw new StateError ('The authorization URL has not yet been generated.' );
280
+ throw StateError ('The authorization URL has not yet been generated.' );
280
281
} else if (_state == _State .finished) {
281
- throw new StateError ('The authorization code has already been received.' );
282
+ throw StateError ('The authorization code has already been received.' );
282
283
}
283
284
_state = _State .finished;
284
285
@@ -288,35 +289,35 @@ class AuthorizationCodeGrant {
288
289
/// This works just like [handleAuthorizationCode] , except it doesn't validate
289
290
/// the state beforehand.
290
291
Future <Client > _handleAuthorizationCode (String authorizationCode) async {
291
- var startTime = new DateTime .now ();
292
+ var startTime = DateTime .now ();
292
293
293
294
var headers = < String , String > {};
294
295
295
296
var body = {
296
- " grant_type" : " authorization_code" ,
297
- " code" : authorizationCode,
298
- " redirect_uri" : this . _redirectEndpoint.toString (),
299
- " code_verifier" : _codeVerifier
297
+ ' grant_type' : ' authorization_code' ,
298
+ ' code' : authorizationCode,
299
+ ' redirect_uri' : _redirectEndpoint.toString (),
300
+ ' code_verifier' : _codeVerifier
300
301
};
301
302
302
303
if (_basicAuth && secret != null ) {
303
- headers[" Authorization" ] = basicAuthHeader (identifier, secret);
304
+ headers[' Authorization' ] = basicAuthHeader (identifier, secret);
304
305
} else {
305
306
// The ID is required for this request any time basic auth isn't being
306
307
// used, even if there's no actual client authentication to be done.
307
- body[" client_id" ] = identifier;
308
- if (secret != null ) body[" client_secret" ] = secret;
308
+ body[' client_id' ] = identifier;
309
+ if (secret != null ) body[' client_secret' ] = secret;
309
310
}
310
311
311
- var response = await _httpClient. post ( this .tokenEndpoint,
312
- headers: headers, body: body);
312
+ var response =
313
+ await _httpClient. post (tokenEndpoint, headers: headers, body: body);
313
314
314
315
var credentials = handleAccessTokenResponse (
315
316
response, tokenEndpoint, startTime, _scopes, _delimiter,
316
317
getParameters: _getParameters);
317
- return new Client (credentials,
318
- identifier: this . identifier,
319
- secret: this . secret,
318
+ return Client (credentials,
319
+ identifier: identifier,
320
+ secret: secret,
320
321
basicAuth: _basicAuth,
321
322
httpClient: _httpClient,
322
323
onCredentialsRefreshed: _onCredentialsRefreshed);
@@ -343,21 +344,22 @@ class AuthorizationCodeGrant {
343
344
class _State {
344
345
/// [AuthorizationCodeGrant.getAuthorizationUrl] has not yet been called for
345
346
/// this grant.
346
- static const initial = const _State (" initial" );
347
+ static const initial = _State (' initial' );
347
348
348
349
// [AuthorizationCodeGrant.getAuthorizationUrl] has been called but neither
349
350
// [AuthorizationCodeGrant.handleAuthorizationResponse] nor
350
351
// [AuthorizationCodeGrant.handleAuthorizationCode] has been called.
351
- static const awaitingResponse = const _State (" awaiting response" );
352
+ static const awaitingResponse = _State (' awaiting response' );
352
353
353
354
// [AuthorizationCodeGrant.getAuthorizationUrl] and either
354
355
// [AuthorizationCodeGrant.handleAuthorizationResponse] or
355
356
// [AuthorizationCodeGrant.handleAuthorizationCode] have been called.
356
- static const finished = const _State (" finished" );
357
+ static const finished = _State (' finished' );
357
358
358
359
final String _name;
359
360
360
361
const _State (this ._name);
361
362
363
+ @override
362
364
String toString () => _name;
363
365
}
0 commit comments