1
+ import 'dart:convert' ;
2
+
1
3
/// Authentication information.
2
4
class Authentication {
3
5
/// OAuth2 Token
@@ -9,26 +11,65 @@ class Authentication {
9
11
/// GitHub Password
10
12
final String ? password;
11
13
14
+ final String ? bearerToken;
15
+
16
+ // TODO: mark the pram as `String` to REQUIRE a non-null value.
17
+ // NEXT major version
12
18
/// Creates an [Authentication] instance that uses the specified OAuth2 [token] .
13
19
const Authentication .withToken (this .token)
14
20
: username = null ,
15
- password = null ;
21
+ password = null ,
22
+ bearerToken = null ;
23
+
24
+ /// Creates an [Authentication] instance that uses the specified
25
+ /// [bearerToken] .
26
+ const Authentication .bearerToken (String this .bearerToken)
27
+ : username = null ,
28
+ password = null ,
29
+ token = null ;
16
30
17
31
/// Creates an [Authentication] instance that has no authentication.
18
32
const Authentication .anonymous ()
19
33
: token = null ,
20
34
username = null ,
21
- password = null ;
35
+ password = null ,
36
+ bearerToken = null ;
22
37
38
+ // TODO: mark the `username` and `password` params as `String` to REQUIRE
39
+ // non-null values. - NEXT major version
23
40
/// Creates an [Authentication] instance that uses a username and password.
24
- Authentication .basic (this .username, this .password) : token = null ;
41
+ const Authentication .basic (this .username, this .password)
42
+ : token = null ,
43
+ bearerToken = null ;
25
44
26
45
/// Anonymous Authentication Flag
27
- bool get isAnonymous => ! isBasic && ! isToken;
46
+ bool get isAnonymous => ! isBasic && ! isToken && ! isBearer ;
28
47
29
48
/// Basic Authentication Flag
30
49
bool get isBasic => username != null ;
31
50
32
51
/// Token Authentication Flag
33
52
bool get isToken => token != null ;
53
+
54
+ // This instance represents a authentication with a "Bearer" token.
55
+ bool get isBearer => bearerToken != null ;
56
+
57
+ /// Returns a value for the `Authorization` HTTP request header or `null`
58
+ /// if [isAnonymous] is `true` .
59
+ String ? authorizationHeaderValue () {
60
+ if (isToken) {
61
+ return 'token $token ' ;
62
+ }
63
+
64
+ if (isBasic) {
65
+ final userAndPass = base64Encode (utf8.encode ('$username :$password ' ));
66
+ return 'basic $userAndPass ' ;
67
+ }
68
+
69
+ if (isBearer) {
70
+ return 'Bearer $bearerToken ' ;
71
+ }
72
+
73
+ return null ;
74
+ }
34
75
}
0 commit comments