Skip to content

Commit 5e56c31

Browse files
authored
dart analysis issues fixes (flutter#71)
* dart analysis issues fixes * fixed formatting
1 parent ca26631 commit 5e56c31

17 files changed

+320
-314
lines changed

lib/src/authorization_code_grant.dart

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ class AuthorizationCodeGrant {
7878
/// Callback to be invoked whenever the credentials are refreshed.
7979
///
8080
/// This will be passed as-is to the constructed [Client].
81-
CredentialsRefreshedCallback _onCredentialsRefreshed;
81+
final CredentialsRefreshedCallback _onCredentialsRefreshed;
8282

8383
/// Whether to use HTTP Basic authentication for authorizing the client.
8484
final bool _basicAuth;
8585

8686
/// A [String] used to separate scopes; defaults to `" "`.
87-
String _delimiter;
87+
final String _delimiter;
8888

8989
/// The HTTP client used to make HTTP requests.
9090
http.Client _httpClient;
@@ -105,7 +105,7 @@ class AuthorizationCodeGrant {
105105

106106
/// Allowed characters for generating the _codeVerifier
107107
static const String _charset =
108-
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
108+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';
109109

110110
/// The generated PKCE code verifier
111111
String _codeVerifier;
@@ -146,9 +146,10 @@ class AuthorizationCodeGrant {
146146
bool basicAuth = true,
147147
http.Client httpClient,
148148
CredentialsRefreshedCallback onCredentialsRefreshed,
149-
Map<String, dynamic> getParameters(MediaType contentType, String body)})
149+
Map<String, dynamic> Function(MediaType contentType, String body)
150+
getParameters})
150151
: _basicAuth = basicAuth,
151-
_httpClient = httpClient == null ? new http.Client() : httpClient,
152+
_httpClient = httpClient ?? http.Client(),
152153
_delimiter = delimiter ?? ' ',
153154
_getParameters = getParameters ?? parseJsonParameters,
154155
_onCredentialsRefreshed = onCredentialsRefreshed;
@@ -175,7 +176,7 @@ class AuthorizationCodeGrant {
175176
Uri getAuthorizationUrl(Uri redirect,
176177
{Iterable<String> scopes, String state}) {
177178
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.');
179180
}
180181
_state = _State.awaitingResponse;
181182

@@ -188,23 +189,23 @@ class AuthorizationCodeGrant {
188189
_codeVerifier = _createCodeVerifier();
189190
var codeChallenge = base64Url
190191
.encode(sha256.convert(ascii.encode(_codeVerifier)).bytes)
191-
.replaceAll("=", "");
192+
.replaceAll('=', '');
192193

193-
this._redirectEndpoint = redirect;
194-
this._scopes = scopes;
195-
this._stateString = state;
194+
_redirectEndpoint = redirect;
195+
_scopes = scopes;
196+
_stateString = state;
196197
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'
202203
};
203204

204205
if (state != null) parameters['state'] = state;
205206
if (scopes.isNotEmpty) parameters['scope'] = scopes.join(_delimiter);
206207

207-
return addQueryParameters(this.authorizationEndpoint, parameters);
208+
return addQueryParameters(authorizationEndpoint, parameters);
208209
}
209210

210211
/// Processes the query parameters added to a redirect from the authorization
@@ -227,19 +228,19 @@ class AuthorizationCodeGrant {
227228
Future<Client> handleAuthorizationResponse(
228229
Map<String, String> parameters) async {
229230
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.');
231232
} 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.');
233234
}
234235
_state = _State.finished;
235236

236237
if (_stateString != null) {
237238
if (!parameters.containsKey('state')) {
238-
throw new FormatException('Invalid OAuth response for '
239+
throw FormatException('Invalid OAuth response for '
239240
'"$authorizationEndpoint": parameter "state" expected to be '
240241
'"$_stateString", was missing.');
241242
} else if (parameters['state'] != _stateString) {
242-
throw new FormatException('Invalid OAuth response for '
243+
throw FormatException('Invalid OAuth response for '
243244
'"$authorizationEndpoint": parameter "state" expected to be '
244245
'"$_stateString", was "${parameters['state']}".');
245246
}
@@ -249,9 +250,9 @@ class AuthorizationCodeGrant {
249250
var description = parameters['error_description'];
250251
var uriString = parameters['error_uri'];
251252
var uri = uriString == null ? null : Uri.parse(uriString);
252-
throw new AuthorizationException(parameters['error'], description, uri);
253+
throw AuthorizationException(parameters['error'], description, uri);
253254
} else if (!parameters.containsKey('code')) {
254-
throw new FormatException('Invalid OAuth response for '
255+
throw FormatException('Invalid OAuth response for '
255256
'"$authorizationEndpoint": did not contain required parameter '
256257
'"code".');
257258
}
@@ -276,9 +277,9 @@ class AuthorizationCodeGrant {
276277
/// Throws [AuthorizationException] if the authorization fails.
277278
Future<Client> handleAuthorizationCode(String authorizationCode) async {
278279
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.');
280281
} 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.');
282283
}
283284
_state = _State.finished;
284285

@@ -288,35 +289,35 @@ class AuthorizationCodeGrant {
288289
/// This works just like [handleAuthorizationCode], except it doesn't validate
289290
/// the state beforehand.
290291
Future<Client> _handleAuthorizationCode(String authorizationCode) async {
291-
var startTime = new DateTime.now();
292+
var startTime = DateTime.now();
292293

293294
var headers = <String, String>{};
294295

295296
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
300301
};
301302

302303
if (_basicAuth && secret != null) {
303-
headers["Authorization"] = basicAuthHeader(identifier, secret);
304+
headers['Authorization'] = basicAuthHeader(identifier, secret);
304305
} else {
305306
// The ID is required for this request any time basic auth isn't being
306307
// 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;
309310
}
310311

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);
313314

314315
var credentials = handleAccessTokenResponse(
315316
response, tokenEndpoint, startTime, _scopes, _delimiter,
316317
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,
320321
basicAuth: _basicAuth,
321322
httpClient: _httpClient,
322323
onCredentialsRefreshed: _onCredentialsRefreshed);
@@ -343,21 +344,22 @@ class AuthorizationCodeGrant {
343344
class _State {
344345
/// [AuthorizationCodeGrant.getAuthorizationUrl] has not yet been called for
345346
/// this grant.
346-
static const initial = const _State("initial");
347+
static const initial = _State('initial');
347348

348349
// [AuthorizationCodeGrant.getAuthorizationUrl] has been called but neither
349350
// [AuthorizationCodeGrant.handleAuthorizationResponse] nor
350351
// [AuthorizationCodeGrant.handleAuthorizationCode] has been called.
351-
static const awaitingResponse = const _State("awaiting response");
352+
static const awaitingResponse = _State('awaiting response');
352353

353354
// [AuthorizationCodeGrant.getAuthorizationUrl] and either
354355
// [AuthorizationCodeGrant.handleAuthorizationResponse] or
355356
// [AuthorizationCodeGrant.handleAuthorizationCode] have been called.
356-
static const finished = const _State("finished");
357+
static const finished = _State('finished');
357358

358359
final String _name;
359360

360361
const _State(this._name);
361362

363+
@override
362364
String toString() => _name;
363365
}

lib/src/authorization_exception.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class AuthorizationException implements Exception {
2626
AuthorizationException(this.error, this.description, this.uri);
2727

2828
/// Provides a string description of the AuthorizationException.
29+
@override
2930
String toString() {
3031
var header = 'OAuth authorization error ($error)';
3132
if (description != null) {

lib/src/client.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,24 @@ class Client extends http.BaseClient {
9292
http.Client httpClient})
9393
: _basicAuth = basicAuth,
9494
_onCredentialsRefreshed = onCredentialsRefreshed,
95-
_httpClient = httpClient == null ? new http.Client() : httpClient {
95+
_httpClient = httpClient ?? http.Client() {
9696
if (identifier == null && secret != null) {
97-
throw new ArgumentError("secret may not be passed without identifier.");
97+
throw ArgumentError('secret may not be passed without identifier.');
9898
}
9999
}
100100

101101
/// Sends an HTTP request with OAuth2 authorization credentials attached.
102102
///
103103
/// This will also automatically refresh this client's [Credentials] before
104104
/// sending the request if necessary.
105+
@override
105106
Future<http.StreamedResponse> send(http.BaseRequest request) async {
106107
if (credentials.isExpired) {
107-
if (!credentials.canRefresh) throw new ExpirationException(credentials);
108+
if (!credentials.canRefresh) throw ExpirationException(credentials);
108109
await refreshCredentials();
109110
}
110111

111-
request.headers['authorization'] = "Bearer ${credentials.accessToken}";
112+
request.headers['authorization'] = 'Bearer ${credentials.accessToken}';
112113
var response = await _httpClient.send(request);
113114

114115
if (response.statusCode != 401) return response;
@@ -130,9 +131,7 @@ class Client extends http.BaseClient {
130131
var params = challenge.parameters;
131132
if (!params.containsKey('error')) return response;
132133

133-
throw new AuthorizationException(
134-
params['error'],
135-
params['error_description'],
134+
throw AuthorizationException(params['error'], params['error_description'],
136135
params['error_uri'] == null ? null : Uri.parse(params['error_uri']));
137136
}
138137

@@ -147,9 +146,9 @@ class Client extends http.BaseClient {
147146
/// [Credentials.scopes] field of [Client.credentials].
148147
Future<Client> refreshCredentials([List<String> newScopes]) async {
149148
if (!credentials.canRefresh) {
150-
var prefix = "OAuth credentials";
151-
if (credentials.isExpired) prefix = "$prefix have expired and";
152-
throw new StateError("$prefix can't be refreshed.");
149+
var prefix = 'OAuth credentials';
150+
if (credentials.isExpired) prefix = '$prefix have expired and';
151+
throw StateError("$prefix can't be refreshed.");
153152
}
154153

155154
_credentials = await credentials.refresh(
@@ -165,6 +164,7 @@ class Client extends http.BaseClient {
165164
}
166165

167166
/// Closes this client and its underlying HTTP client.
167+
@override
168168
void close() {
169169
if (_httpClient != null) _httpClient.close();
170170
_httpClient = null;

lib/src/client_credentials_grant.dart

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ Future<Client> clientCredentialsGrant(
4545
bool basicAuth = true,
4646
http.Client httpClient,
4747
String delimiter,
48-
Map<String, dynamic> getParameters(
49-
MediaType contentType, String body)}) async {
48+
Map<String, dynamic> Function(MediaType contentType, String body)
49+
getParameters}) async {
5050
delimiter ??= ' ';
51-
var startTime = new DateTime.now();
51+
var startTime = DateTime.now();
5252

53-
var body = {"grant_type": "client_credentials"};
53+
var body = {'grant_type': 'client_credentials'};
5454

5555
var headers = <String, String>{};
5656

@@ -63,16 +63,17 @@ Future<Client> clientCredentialsGrant(
6363
}
6464
}
6565

66-
if (scopes != null && scopes.isNotEmpty)
66+
if (scopes != null && scopes.isNotEmpty) {
6767
body['scope'] = scopes.join(delimiter);
68+
}
6869

69-
if (httpClient == null) httpClient = new http.Client();
70+
httpClient ??= http.Client();
7071
var response = await httpClient.post(authorizationEndpoint,
7172
headers: headers, body: body);
7273

7374
var credentials = await handleAccessTokenResponse(
7475
response, authorizationEndpoint, startTime, scopes, delimiter,
7576
getParameters: getParameters);
76-
return new Client(credentials,
77+
return Client(credentials,
7778
identifier: identifier, secret: secret, httpClient: httpClient);
7879
}

0 commit comments

Comments
 (0)