Skip to content

Commit 4259149

Browse files
committed
Add bearerToken constructor to Authentication class
Also added `authorizationHeaderValue()` function. Also added `const` qualifier to `basic` constructor.
1 parent 42e405e commit 4259149

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

lib/src/common/github.dart

+3-6
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,9 @@ class GitHub {
368368
headers['Accept'] = preview;
369369
}
370370

371-
if (auth.isToken) {
372-
headers.putIfAbsent('Authorization', () => 'token ${auth.token}');
373-
} else if (auth.isBasic) {
374-
final userAndPass =
375-
base64Encode(utf8.encode('${auth.username}:${auth.password}'));
376-
headers.putIfAbsent('Authorization', () => 'basic $userAndPass');
371+
final authHeaderValue = auth.authorizationHeaderValue();
372+
if (authHeaderValue != null) {
373+
headers.putIfAbsent('Authorization', () => authHeaderValue);
377374
}
378375

379376
// See https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#user-agent-required

lib/src/common/util/auth.dart

+45-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:convert';
2+
13
/// Authentication information.
24
class Authentication {
35
/// OAuth2 Token
@@ -9,26 +11,65 @@ class Authentication {
911
/// GitHub Password
1012
final String? password;
1113

14+
final String? bearerToken;
15+
16+
// TODO: mark the pram as `String` to REQUIRE a non-null value.
17+
// NEXT major version
1218
/// Creates an [Authentication] instance that uses the specified OAuth2 [token].
1319
const Authentication.withToken(this.token)
1420
: 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;
1630

1731
/// Creates an [Authentication] instance that has no authentication.
1832
const Authentication.anonymous()
1933
: token = null,
2034
username = null,
21-
password = null;
35+
password = null,
36+
bearerToken = null;
2237

38+
// TODO: mark the `username` and `password` params as `String` to REQUIRE
39+
// non-null values. - NEXT major version
2340
/// 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;
2544

2645
/// Anonymous Authentication Flag
27-
bool get isAnonymous => !isBasic && !isToken;
46+
bool get isAnonymous => !isBasic && !isToken && !isBearer;
2847

2948
/// Basic Authentication Flag
3049
bool get isBasic => username != null;
3150

3251
/// Token Authentication Flag
3352
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+
}
3475
}

0 commit comments

Comments
 (0)