Skip to content

Commit 4162fe5

Browse files
authored
Merge pull request #17 from DutchCodingCompany/feature/add_very_good_analysis
✨ Add very good analysis
2 parents 76a0513 + 8232eea commit 4162fe5

16 files changed

+263
-140
lines changed

analysis_options.yaml

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
1-
# This file configures the static analysis results for your project (errors,
2-
# warnings, and lints).
3-
#
4-
# This enables the 'recommended' set of lints from `package:lints`.
5-
# This set helps identify many issues that may lead to problems when running
6-
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7-
# style and format.
8-
#
9-
# If you want a smaller set of lints you can change this to specify
10-
# 'package:lints/core.yaml'. These are just the most critical lints
11-
# (the recommended set includes the core lints).
12-
# The core lints are also what is used by pub.dev for scoring packages.
13-
14-
include: package:lints/recommended.yaml
15-
16-
# Uncomment the following section to specify additional rules.
17-
18-
# linter:
19-
# rules:
20-
# - camel_case_types
21-
22-
# analyzer:
23-
# exclude:
24-
# - path/to/excluded/files/**
25-
26-
# For more information about the core and recommended set of lints, see
27-
# https://dart.dev/go/core-lints
28-
29-
# For additional information about configuring this file, see
30-
# https://dart.dev/guides/language/analysis-options
1+
include: package:very_good_analysis/analysis_options.yaml
2+
3+
analyzer:
4+
errors:
5+
unawaited_futures: warning
6+
avoid_void_async: warning
7+
missing_return: error
8+
missing_required_param: error
9+
invalid_annotation_target: info
10+
11+
language:
12+
strict-casts: true
13+
strict-inference: true
14+
strict-raw-types: true

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/extensions/request.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import 'package:chopper/chopper.dart';
22

3+
/// Helper extension to easily apply a authorization header to a request.
34
extension ChopperRequest on Request {
4-
Request addAuthorizationHeader(String token) {
5-
final newHeaders = Map<String, String>.from(headers);
6-
newHeaders['Authorization'] = 'Bearer $token';
7-
return copyWith(headers: newHeaders);
8-
}
5+
/// Adds a authorization header with a bearer [token] to the request.
6+
Request addAuthorizationHeader(String token) => applyHeader(
7+
this,
8+
'Authorization',
9+
'Bearer $token',
10+
);
911
}

lib/src/oauth_authenticator.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,33 @@ import 'package:chopper/chopper.dart';
44
import 'package:oauth_chopper/oauth_chopper.dart';
55
import 'package:oauth_chopper/src/extensions/request.dart';
66

7+
/// Callback for error handling.
78
typedef OnErrorCallback = void Function(Object, StackTrace);
89

9-
/// OAuthAuthenticator provides a authenticator that handles OAuth authorizations.
10+
/// {@template authenticator}
11+
/// OAuthAuthenticator provides a authenticator that handles
12+
/// OAuth authorizations.
1013
/// When the provided credentials are invalid it tries to refresh them.
11-
/// Can throw a exceptions if no [onError] is passed. When [onError] is passed exception will be passed to [onError]
14+
/// Can throw a exceptions if no [onError] is passed. When [onError] is passed
15+
/// exception will be passed to [onError]
16+
/// {@endtemplate}
1217
class OAuthAuthenticator extends Authenticator {
18+
/// {@macro authenticator}
1319
OAuthAuthenticator(this.oauthChopper, this.onError);
1420

21+
/// Callback for error handling.
1522
final OnErrorCallback? onError;
23+
24+
/// The [OAuthChopper] instance to get the token from and
25+
/// to refresh the token.
1626
final OAuthChopper oauthChopper;
1727

1828
@override
19-
FutureOr<Request?> authenticate(Request request, Response<dynamic> response,
20-
[Request? originalRequest]) async {
29+
FutureOr<Request?> authenticate(
30+
Request request,
31+
Response<dynamic> response, [
32+
Request? originalRequest,
33+
]) async {
2134
final token = await oauthChopper.token;
2235
if (response.statusCode == 401 && token != null) {
2336
try {

lib/src/oauth_chopper.dart

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import 'package:oauth_chopper/src/oauth_token.dart';
88
import 'package:oauth_chopper/src/storage/memory_storage.dart';
99
import 'package:oauth_chopper/src/storage/oauth_storage.dart';
1010

11-
/// OAuthChopper client for configuring OAuth authentication with [Chopper].
11+
/// {@template oauth_chopper}
12+
/// OAuthChopper client for configuring OAuth authentication with Chopper.
1213
///
1314
/// For example:
1415
/// ```dart
@@ -18,7 +19,22 @@ import 'package:oauth_chopper/src/storage/oauth_storage.dart';
1819
/// secret: secret,
1920
/// );
2021
/// ```
22+
/// {@endtemplate}
2123
class OAuthChopper {
24+
/// {@macro oauth_chopper}
25+
OAuthChopper({
26+
required this.authorizationEndpoint,
27+
required this.identifier,
28+
required this.secret,
29+
this.endSessionEndpoint,
30+
31+
/// OAuth storage for storing credentials.
32+
/// By default it will use a in memory storage [MemoryStorage].
33+
/// For persisting the credentials implement a custom [OAuthStorage].
34+
/// See [OAuthStorage] for more information.
35+
OAuthStorage? storage,
36+
}) : _storage = storage ?? MemoryStorage();
37+
2238
/// OAuth authorization endpoint.
2339
final Uri authorizationEndpoint;
2440

@@ -32,22 +48,11 @@ class OAuthChopper {
3248
final String secret;
3349

3450
/// OAuth storage for storing credentials.
35-
/// 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].
3653
/// See [OAuthStorage] for more information.
3754
final OAuthStorage _storage;
3855

39-
OAuthChopper({
40-
required this.authorizationEndpoint,
41-
required this.identifier,
42-
required this.secret,
43-
this.endSessionEndpoint,
44-
45-
/// OAuth storage for storing credentials.
46-
/// By default it will use a in memory storage [MemoryStorage]. For persisting the credentials implement a custom [OAuthStorage].
47-
/// See [OAuthStorage] for more information.
48-
OAuthStorage? storage,
49-
}) : _storage = storage ?? MemoryStorage();
50-
5156
/// Get stored [OAuthToken].
5257
Future<OAuthToken?> get token async {
5358
final credentialsJson = await _storage.fetchCredentials();
@@ -57,18 +62,21 @@ class OAuthChopper {
5762
}
5863

5964
/// Provides an [OAuthAuthenticator] instance.
60-
/// 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.
6168
OAuthAuthenticator authenticator({
62-
/// When provided [onError] handles exceptions if thrown.
6369
OnErrorCallback? onError,
6470
}) =>
6571
OAuthAuthenticator(this, onError);
6672

6773
/// Provides an [OAuthInterceptor] instance.
6874
OAuthInterceptor get interceptor => OAuthInterceptor(this);
6975

70-
/// Tries to refresh the available credentials and returns a new [OAuthToken] instance.
71-
/// 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.
7280
/// See [Credentials.refresh]
7381
Future<OAuthToken?> refresh() async {
7482
final credentialsJson = await _storage.fetchCredentials();
@@ -85,7 +93,9 @@ 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]

lib/src/oauth_grant.dart

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
11
import 'package:oauth2/oauth2.dart' as oauth;
22

3-
abstract class OAuthGrant {
3+
/// {@template oauth_grant}
4+
/// Interface for a OAuth grant.
5+
/// Grants are used to obtain credentials from an authorization server.
6+
/// {@endtemplate}
7+
abstract interface class OAuthGrant {
8+
/// {@macro oauth_grant}
49
const OAuthGrant();
510

11+
/// Obtains credentials from an authorization server.
612
Future<String> handle(
7-
Uri authorizationEndpoint, String identifier, String secret);
13+
Uri authorizationEndpoint,
14+
String identifier,
15+
String secret,
16+
);
817
}
918

19+
/// {@template resource_owner_password_grant}
1020
/// Obtains credentials using a [resource owner password grant](https://tools.ietf.org/html/rfc6749#section-1.3.3).
11-
class ResourceOwnerPasswordGrant extends OAuthGrant {
21+
///
22+
/// This grant uses the resource owner's [username] and [password] to obtain
23+
/// credentials.
24+
/// {@endtemplate}
25+
class ResourceOwnerPasswordGrant implements OAuthGrant {
26+
/// {@macro resource_owner_password_grant}
27+
const ResourceOwnerPasswordGrant({
28+
required this.username,
29+
required this.password,
30+
});
31+
32+
/// Username used for obtaining credentials.
1233
final String username;
13-
final String password;
1434

15-
const ResourceOwnerPasswordGrant(
16-
{required this.username, required this.password});
35+
/// Password used for obtaining credentials.
36+
final String password;
1737

1838
@override
1939
Future<String> handle(
20-
Uri authorizationEndpoint, String identifier, String secret) async {
40+
Uri authorizationEndpoint,
41+
String identifier,
42+
String secret,
43+
) async {
2144
final client = await oauth.resourceOwnerPasswordGrant(
2245
authorizationEndpoint,
2346
username,
@@ -29,13 +52,19 @@ class ResourceOwnerPasswordGrant extends OAuthGrant {
2952
}
3053
}
3154

55+
/// {@template client_credentials_grant}
3256
/// Obtains credentials using a [client credentials grant](https://tools.ietf.org/html/rfc6749#section-1.3.4).
33-
class ClientCredentialsGrant extends OAuthGrant {
57+
/// {@endtemplate}
58+
class ClientCredentialsGrant implements OAuthGrant {
59+
/// {@macro client_credentials_grant}
3460
const ClientCredentialsGrant();
3561

3662
@override
3763
Future<String> handle(
38-
Uri authorizationEndpoint, String identifier, String secret) async {
64+
Uri authorizationEndpoint,
65+
String identifier,
66+
String secret,
67+
) async {
3968
final client = await oauth.clientCredentialsGrant(
4069
authorizationEndpoint,
4170
identifier,
@@ -45,8 +74,11 @@ class ClientCredentialsGrant extends OAuthGrant {
4574
}
4675
}
4776

77+
/// {@template authorization_code_grant}
4878
/// Obtains credentials using a [authorization code grant](https://tools.ietf.org/html/rfc6749#section-1.3.1).
49-
class AuthorizationCodeGrant extends OAuthGrant {
79+
/// {@endtemplate}
80+
class AuthorizationCodeGrant implements OAuthGrant {
81+
/// {@macro authorization_code_grant}
5082
const AuthorizationCodeGrant({
5183
required this.tokenEndpoint,
5284
required this.scopes,
@@ -55,26 +87,50 @@ class AuthorizationCodeGrant extends OAuthGrant {
5587
required this.listen,
5688
});
5789

90+
/// A URL provided by the authorization server that this library uses to
91+
/// obtain long-lasting credentials.
92+
///
93+
/// This will usually be listed in the authorization server's OAuth2 API
94+
/// documentation.
5895
final Uri tokenEndpoint;
96+
97+
/// The redirect URL where the resource owner will redirect to.
5998
final Uri redirectUrl;
99+
100+
/// The specific permissions being requested from the authorization server may
101+
/// be specified via [scopes].
60102
final List<String> scopes;
103+
104+
/// Callback used for redirect the authorizationUrl given by the authorization
105+
/// server.
61106
final Future<void> Function(Uri authorizationUri) redirect;
107+
108+
/// Callback used for listening for the redirectUrl.
62109
final Future<Uri> Function(Uri redirectUri) listen;
63110

64111
@override
65112
Future<String> handle(
66-
Uri authorizationEndpoint, String identifier, String secret) async {
113+
Uri authorizationEndpoint,
114+
String identifier,
115+
String secret,
116+
) async {
67117
final grant = oauth.AuthorizationCodeGrant(
68118
identifier,
69119
authorizationEndpoint,
70120
tokenEndpoint,
71121
);
72-
var authorizationUrl =
73-
grant.getAuthorizationUrl(redirectUrl, scopes: scopes);
122+
123+
final authorizationUrl = grant.getAuthorizationUrl(
124+
redirectUrl,
125+
scopes: scopes,
126+
);
127+
74128
await redirect(authorizationUrl);
75-
var responseUrl = await listen(redirectUrl);
76-
oauth.Client client =
77-
await grant.handleAuthorizationResponse(responseUrl.queryParameters);
129+
final responseUrl = await listen(redirectUrl);
130+
131+
final client = await grant.handleAuthorizationResponse(
132+
responseUrl.queryParameters,
133+
);
78134

79135
return client.credentials.toJson();
80136
}

lib/src/oauth_interceptor.dart

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

7-
/// OAuthInterceptor is responsible for adding 'Authorization' header to requests.
8-
/// The header is only added if there is a token available. When no token is available no header is added.
7+
/// {@template oauth_interceptor}
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.
912
/// Its added as a Bearer token.
13+
/// {@endtemplate}
1014
class OAuthInterceptor implements RequestInterceptor {
15+
/// {@macro oauth_interceptor}
1116
OAuthInterceptor(this.oauthChopper);
1217

18+
/// The [OAuthChopper] instance to get the token from.
1319
final OAuthChopper oauthChopper;
1420

1521
@override

0 commit comments

Comments
 (0)