@@ -9,7 +9,8 @@ import 'package:oauth_chopper/src/oauth_token.dart';
9
9
import 'package:oauth_chopper/src/storage/memory_storage.dart' ;
10
10
import 'package:oauth_chopper/src/storage/oauth_storage.dart' ;
11
11
12
- /// OAuthChopper client for configuring OAuth authentication with [Chopper] .
12
+ /// {@template oauth_chopper}
13
+ /// OAuthChopper client for configuring OAuth authentication with Chopper.
13
14
///
14
15
/// For example:
15
16
/// ```dart
@@ -19,7 +20,23 @@ import 'package:oauth_chopper/src/storage/oauth_storage.dart';
19
20
/// secret: secret,
20
21
/// );
21
22
/// ```
23
+ /// {@endtemplate}
22
24
class OAuthChopper {
25
+ /// {@macro oauth_chopper}
26
+ OAuthChopper ({
27
+ required this .authorizationEndpoint,
28
+ required this .identifier,
29
+ required this .secret,
30
+ this .endSessionEndpoint,
31
+ this .httpClient,
32
+
33
+ /// OAuth storage for storing credentials.
34
+ /// By default it will use a in memory storage [MemoryStorage] .
35
+ /// For persisting the credentials implement a custom [OAuthStorage] .
36
+ /// See [OAuthStorage] for more information.
37
+ OAuthStorage ? storage,
38
+ }) : _storage = storage ?? MemoryStorage ();
39
+
23
40
/// OAuth authorization endpoint.
24
41
final Uri authorizationEndpoint;
25
42
@@ -33,36 +50,24 @@ class OAuthChopper {
33
50
final String secret;
34
51
35
52
/// OAuth storage for storing credentials.
36
- /// By default it will use a in memory storage. For persisting the credentials implement a custom [OAuthStorage] .
53
+ /// By default it will use a in memory storage. For persisting the credentials
54
+ /// implement a custom [OAuthStorage] .
37
55
/// See [OAuthStorage] for more information.
38
56
final OAuthStorage _storage;
39
57
40
58
/// Provide a custom [http.Client] which will be passed to [oauth2] and used for making new requests.
41
59
final http.Client ? httpClient;
42
60
43
- OAuthChopper ({
44
- required this .authorizationEndpoint,
45
- required this .identifier,
46
- required this .secret,
47
- this .endSessionEndpoint,
48
- this .httpClient,
49
-
50
- /// OAuth storage for storing credentials.
51
- /// By default it will use a in memory storage [MemoryStorage] . For persisting the credentials implement a custom [OAuthStorage] .
52
- /// See [OAuthStorage] for more information.
53
- OAuthStorage ? storage,
54
- }) : _storage = storage ?? MemoryStorage ();
55
-
56
61
/// Get stored [OAuthToken] .
57
62
Future <OAuthToken ?> get token async {
58
63
final credentialsJson = await _storage.fetchCredentials ();
59
- return credentialsJson != null
60
- ? OAuthToken .fromJson (credentialsJson)
61
- : null ;
64
+ return credentialsJson != null ? OAuthToken .fromJson (credentialsJson) : null ;
62
65
}
63
66
64
67
/// Provides an [OAuthAuthenticator] instance.
65
- /// The authenticator can throw exceptions when OAuth authentication fails. If [onError] is provided exceptions will be passed to [onError] and not be thrown.
68
+ /// The authenticator can throw exceptions when OAuth authentication fails.
69
+ /// If [onError] is provided exceptions will be passed to [onError] and not be
70
+ /// thrown.
66
71
OAuthAuthenticator authenticator ({
67
72
/// When provided [onError] handles exceptions if thrown.
68
73
OnErrorCallback ? onError,
@@ -72,10 +77,12 @@ class OAuthChopper {
72
77
/// Provides an [OAuthInterceptor] instance.
73
78
OAuthInterceptor get interceptor => OAuthInterceptor (this );
74
79
75
- /// Tries to refresh the available credentials and returns a new [OAuthToken] instance.
76
- /// Throws an exception when refreshing fails. If the exception is a [AuthorizationException] it clears the storage.
80
+ /// Tries to refresh the available credentials and returns a new [OAuthToken]
81
+ /// instance.
82
+ /// Throws an exception when refreshing fails. If the exception is a
83
+ /// [AuthorizationException] it clears the storage.
77
84
/// See [Credentials.refresh]
78
- Future <OAuthToken ?> refresh () async {
85
+ Future <OAuthToken ?> refresh () async {
79
86
final credentialsJson = await _storage.fetchCredentials ();
80
87
if (credentialsJson == null ) return null ;
81
88
final credentials = oauth2.Credentials .fromJson (credentialsJson);
@@ -93,15 +100,17 @@ class OAuthChopper {
93
100
}
94
101
}
95
102
96
- /// Request an [OAuthGrant] and stores the credentials in the [storage] .
103
+ /// Request an [OAuthGrant] and stores the credentials in the
104
+ /// [_storage] .
105
+ ///
97
106
/// Currently supported grants:
98
107
/// - [ResourceOwnerPasswordGrant]
99
108
/// - [ClientCredentialsGrant]
100
109
/// - [AuthorizationCodeGrant]
101
110
/// Throws an exception if the grant fails.
102
111
Future <OAuthToken > requestGrant (OAuthGrant grant) async {
103
112
final credentials = await grant.handle (
104
- authorizationEndpoint, identifier, secret, httpClient);
113
+ authorizationEndpoint, identifier, secret, httpClient, );
105
114
106
115
await _storage.saveCredentials (credentials);
107
116
0 commit comments