Skip to content

Commit 8232eea

Browse files
committed
🚨 Fixed lint warnings
1 parent 62fb70d commit 8232eea

13 files changed

+117
-71
lines changed

example/oauth_chopper_example.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// ignore_for_file: unused_local_variable
1+
// ignore because its a example.
2+
// ignore_for_file: unused_local_variable, prefer_const_declarations,
3+
// ignore_for_file: prefer_const_constructors
24

35
import 'package:chopper/chopper.dart';
46
import 'package:oauth_chopper/oauth_chopper.dart';

lib/oauth_chopper.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// OAuthChopper for configuring OAuth authentication with [Chopper].
1+
/// OAuthChopper for configuring OAuth authentication with Chopper.
22
///
33
/// More dartdocs go here.
44
library oauth_chopper;

lib/src/oauth_authenticator.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class OAuthAuthenticator extends Authenticator {
2020

2121
/// Callback for error handling.
2222
final OnErrorCallback? onError;
23+
2324
/// The [OAuthChopper] instance to get the token from and
2425
/// to refresh the token.
2526
final OAuthChopper oauthChopper;

lib/src/oauth_chopper.dart

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:oauth_chopper/src/storage/memory_storage.dart';
99
import 'package:oauth_chopper/src/storage/oauth_storage.dart';
1010

1111
/// {@template oauth_chopper}
12-
/// OAuthChopper client for configuring OAuth authentication with [Chopper].
12+
/// OAuthChopper client for configuring OAuth authentication with Chopper.
1313
///
1414
/// For example:
1515
/// ```dart
@@ -29,7 +29,8 @@ class OAuthChopper {
2929
this.endSessionEndpoint,
3030

3131
/// OAuth storage for storing credentials.
32-
/// By default it will use a in memory storage [MemoryStorage]. For persisting the credentials implement a custom [OAuthStorage].
32+
/// By default it will use a in memory storage [MemoryStorage].
33+
/// For persisting the credentials implement a custom [OAuthStorage].
3334
/// See [OAuthStorage] for more information.
3435
OAuthStorage? storage,
3536
}) : _storage = storage ?? MemoryStorage();
@@ -47,36 +48,43 @@ class OAuthChopper {
4748
final String secret;
4849

4950
/// OAuth storage for storing credentials.
50-
/// By default it will use a in memory storage. For persisting the credentials implement a custom [OAuthStorage].
51+
/// By default it will use a in memory storage. For persisting the credentials
52+
/// implement a custom [OAuthStorage].
5153
/// See [OAuthStorage] for more information.
5254
final OAuthStorage _storage;
5355

5456
/// Get stored [OAuthToken].
5557
Future<OAuthToken?> get token async {
5658
final credentialsJson = await _storage.fetchCredentials();
57-
return credentialsJson != null ? OAuthToken.fromJson(credentialsJson) : null;
59+
return credentialsJson != null
60+
? OAuthToken.fromJson(credentialsJson)
61+
: null;
5862
}
5963

6064
/// Provides an [OAuthAuthenticator] instance.
61-
/// The authenticator can throw exceptions when OAuth authentication fails. If [onError] is provided exceptions will be passed to [onError] and not be thrown.
65+
/// The authenticator can throw exceptions when OAuth authentication fails.
66+
/// If [onError] is provided exceptions will be passed to [onError] and not be
67+
/// thrown.
6268
OAuthAuthenticator authenticator({
63-
/// When provided [onError] handles exceptions if thrown.
6469
OnErrorCallback? onError,
6570
}) =>
6671
OAuthAuthenticator(this, onError);
6772

6873
/// Provides an [OAuthInterceptor] instance.
6974
OAuthInterceptor get interceptor => OAuthInterceptor(this);
7075

71-
/// Tries to refresh the available credentials and returns a new [OAuthToken] instance.
72-
/// Throws an exception when refreshing fails. If the exception is a [AuthorizationException] it clears the storage.
76+
/// Tries to refresh the available credentials and returns a new [OAuthToken]
77+
/// instance.
78+
/// Throws an exception when refreshing fails. If the exception is a
79+
/// [AuthorizationException] it clears the storage.
7380
/// See [Credentials.refresh]
7481
Future<OAuthToken?> refresh() async {
7582
final credentialsJson = await _storage.fetchCredentials();
7683
if (credentialsJson == null) return null;
7784
final credentials = Credentials.fromJson(credentialsJson);
7885
try {
79-
final newCredentials = await credentials.refresh(identifier: identifier, secret: secret);
86+
final newCredentials =
87+
await credentials.refresh(identifier: identifier, secret: secret);
8088
await _storage.saveCredentials(newCredentials.toJson());
8189
return OAuthToken.fromCredentials(newCredentials);
8290
} on AuthorizationException {
@@ -85,14 +93,17 @@ class OAuthChopper {
8593
}
8694
}
8795

88-
/// Request an [OAuthGrant] and stores the credentials in the [storage].
96+
/// Request an [OAuthGrant] and stores the credentials in the
97+
/// [_storage].
98+
///
8999
/// Currently supported grants:
90100
/// - [ResourceOwnerPasswordGrant]
91101
/// - [ClientCredentialsGrant]
92102
///
93103
/// Throws an exception if the grant fails.
94104
Future<OAuthToken> requestGrant(OAuthGrant grant) async {
95-
final credentials = await grant.handle(authorizationEndpoint, identifier, secret);
105+
final credentials =
106+
await grant.handle(authorizationEndpoint, identifier, secret);
96107

97108
await _storage.saveCredentials(credentials);
98109

lib/src/oauth_grant.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ abstract interface class OAuthGrant {
99
const OAuthGrant();
1010

1111
/// Obtains credentials from an authorization server.
12-
Future<String> handle(Uri authorizationEndpoint, String identifier, String secret);
12+
Future<String> handle(
13+
Uri authorizationEndpoint,
14+
String identifier,
15+
String secret,
16+
);
1317
}
1418

1519
/// {@template resource_owner_password_grant}
@@ -32,7 +36,11 @@ class ResourceOwnerPasswordGrant implements OAuthGrant {
3236
final String password;
3337

3438
@override
35-
Future<String> handle(Uri authorizationEndpoint, String identifier, String secret) async {
39+
Future<String> handle(
40+
Uri authorizationEndpoint,
41+
String identifier,
42+
String secret,
43+
) async {
3644
final client = await oauth.resourceOwnerPasswordGrant(
3745
authorizationEndpoint,
3846
username,
@@ -52,7 +60,11 @@ class ClientCredentialsGrant implements OAuthGrant {
5260
const ClientCredentialsGrant();
5361

5462
@override
55-
Future<String> handle(Uri authorizationEndpoint, String identifier, String secret) async {
63+
Future<String> handle(
64+
Uri authorizationEndpoint,
65+
String identifier,
66+
String secret,
67+
) async {
5668
final client = await oauth.clientCredentialsGrant(
5769
authorizationEndpoint,
5870
identifier,
@@ -88,9 +100,11 @@ class AuthorizationCodeGrant implements OAuthGrant {
88100
/// The specific permissions being requested from the authorization server may
89101
/// be specified via [scopes].
90102
final List<String> scopes;
103+
91104
/// Callback used for redirect the authorizationUrl given by the authorization
92105
/// server.
93106
final Future<void> Function(Uri authorizationUri) redirect;
107+
94108
/// Callback used for listening for the redirectUrl.
95109
final Future<Uri> Function(Uri redirectUri) listen;
96110

@@ -114,7 +128,7 @@ class AuthorizationCodeGrant implements OAuthGrant {
114128
await redirect(authorizationUrl);
115129
final responseUrl = await listen(redirectUrl);
116130

117-
final oauth.Client client = await grant.handleAuthorizationResponse(
131+
final client = await grant.handleAuthorizationResponse(
118132
responseUrl.queryParameters,
119133
);
120134

lib/src/oauth_interceptor.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import 'package:oauth_chopper/oauth_chopper.dart';
55
import 'package:oauth_chopper/src/extensions/request.dart';
66

77
/// {@template oauth_interceptor}
8-
/// OAuthInterceptor is responsible for adding 'Authorization' header to requests.
9-
/// The header is only added if there is a token available. When no token is available no header is added.
8+
/// OAuthInterceptor is responsible for adding 'Authorization' header to
9+
/// requests.
10+
/// The header is only added if there is a token available. When no token is
11+
/// available no header is added.
1012
/// Its added as a Bearer token.
1113
/// {@endtemplate}
1214
class OAuthInterceptor implements RequestInterceptor {

lib/src/storage/memory_storage.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:async';
22

33
import 'package:oauth_chopper/oauth_chopper.dart';
44

5+
/// A simple in-memory storage for OAuth credentials.
56
class MemoryStorage implements OAuthStorage {
67
String? _credentials;
78

lib/src/storage/oauth_storage.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ abstract interface class OAuthStorage {
77
/// Fetch stored credentials.
88
FutureOr<String?> fetchCredentials();
99

10-
/// Save newly obtained credentials. This is called when authentication or refreshing tokens succeeds.
10+
/// Save newly obtained credentials. This is called when authentication or
11+
/// refreshing tokens succeeds.
1112
FutureOr<void> saveCredentials(String? credentialsJson);
1213

1314
/// Clear any stored credential. This is called when authentication fails.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ dependencies:
1212

1313
dev_dependencies:
1414
http: ^1.2.1
15-
very_good_analysis: ^5.1.0
1615
mocktail: ^1.0.3
1716
test: ^1.25.2
17+
very_good_analysis: ^5.1.0

test/oauth_authenticator_test.dart

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// ignore so the test is easier to read.
2+
// ignore_for_file: avoid_redundant_argument_values
3+
14
import 'dart:io';
25

36
import 'package:chopper/chopper.dart';
@@ -28,7 +31,7 @@ void main() {
2831

2932
test('only refresh on unauthorized and token', () async {
3033
// arrange
31-
when(() => mockOAuthChopper.refresh()).thenAnswer((_) async => testToken);
34+
when(mockOAuthChopper.refresh).thenAnswer((_) async => testToken);
3235
when(() => mockOAuthChopper.token).thenAnswer((_) async => testToken);
3336
final authenticator = OAuthAuthenticator(mockOAuthChopper, null);
3437
final expected = {'Authorization': 'Bearer token'};
@@ -38,13 +41,13 @@ void main() {
3841
await authenticator.authenticate(testRequest, unauthorizedResponse);
3942

4043
// assert
41-
verify(() => mockOAuthChopper.refresh()).called(1);
44+
verify(mockOAuthChopper.refresh).called(1);
4245
expect(result?.headers, expected);
4346
});
4447

4548
test("Don't refresh on authorized", () async {
4649
// arrange
47-
when(() => mockOAuthChopper.refresh()).thenAnswer((_) async => testToken);
50+
when(mockOAuthChopper.refresh).thenAnswer((_) async => testToken);
4851
when(() => mockOAuthChopper.token).thenAnswer((_) async => testToken);
4952
final authenticator = OAuthAuthenticator(mockOAuthChopper, null);
5053

@@ -53,13 +56,13 @@ void main() {
5356
await authenticator.authenticate(testRequest, authorizedResponse);
5457

5558
// assert
56-
verifyNever(() => mockOAuthChopper.refresh());
59+
verifyNever(mockOAuthChopper.refresh);
5760
expect(result, null);
5861
});
5962

6063
test("Don't refresh on token not available", () async {
6164
// arrange
62-
when(() => mockOAuthChopper.refresh()).thenAnswer((_) async => testToken);
65+
when(mockOAuthChopper.refresh).thenAnswer((_) async => testToken);
6366
when(() => mockOAuthChopper.token).thenAnswer((_) async => null);
6467
final authenticator = OAuthAuthenticator(mockOAuthChopper, null);
6568

@@ -68,13 +71,13 @@ void main() {
6871
await authenticator.authenticate(testRequest, unauthorizedResponse);
6972

7073
// assert
71-
verifyNever(() => mockOAuthChopper.refresh());
74+
verifyNever(mockOAuthChopper.refresh);
7275
expect(result, null);
7376
});
7477

7578
test("Don't add headers on failed refresh", () async {
7679
// arrange
77-
when(() => mockOAuthChopper.refresh()).thenAnswer((_) async => null);
80+
when(mockOAuthChopper.refresh).thenAnswer((_) async => null);
7881
when(() => mockOAuthChopper.token).thenAnswer((_) async => testToken);
7982
final authenticator = OAuthAuthenticator(mockOAuthChopper, null);
8083

@@ -83,31 +86,34 @@ void main() {
8386
await authenticator.authenticate(testRequest, unauthorizedResponse);
8487

8588
// assert
86-
verify(() => mockOAuthChopper.refresh()).called(1);
89+
verify(mockOAuthChopper.refresh).called(1);
8790
expect(result, null);
8891
});
8992

90-
test("Exception thrown if onError is null", () async {
93+
test('Exception thrown if onError is null', () async {
9194
// arrange
92-
when(() => mockOAuthChopper.refresh()).thenThrow(FormatException('failed'));
95+
when(mockOAuthChopper.refresh).thenThrow(const FormatException('failed'));
9396
when(() => mockOAuthChopper.token).thenAnswer((_) async => testToken);
9497
final authenticator = OAuthAuthenticator(mockOAuthChopper, null);
9598

9699
// act
97100
// assert
98101
expect(
99-
() async =>
100-
await authenticator.authenticate(testRequest, unauthorizedResponse),
101-
throwsFormatException);
102+
() async =>
103+
await authenticator.authenticate(testRequest, unauthorizedResponse),
104+
throwsFormatException,
105+
);
102106
});
103107

104-
test("Exception not thrown if onError is supplied", () async {
108+
test('Exception not thrown if onError is supplied', () async {
105109
// arrange
106110
FormatException? result;
107-
when(() => mockOAuthChopper.refresh()).thenThrow(FormatException('failed'));
111+
when(mockOAuthChopper.refresh).thenThrow(const FormatException('failed'));
108112
when(() => mockOAuthChopper.token).thenAnswer((_) async => testToken);
109113
final authenticator = OAuthAuthenticator(
110-
mockOAuthChopper, (e, s) => result = e as FormatException);
114+
mockOAuthChopper,
115+
(e, s) => result = e as FormatException,
116+
);
111117

112118
// act
113119
final responseResult =

0 commit comments

Comments
 (0)