Skip to content

Commit

Permalink
Add bearerToken constructor to Authentication class
Browse files Browse the repository at this point in the history
Also added `authorizationHeaderValue()` function.
Also added `const` qualifier to `basic` constructor.
  • Loading branch information
kevmoo committed Jul 25, 2023
1 parent 6480a79 commit 7ae2071
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
9 changes: 3 additions & 6 deletions lib/src/common/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,9 @@ class GitHub {
headers['Accept'] = preview;
}

if (auth.isToken) {
headers.putIfAbsent('Authorization', () => 'token ${auth.token}');
} else if (auth.isBasic) {
final userAndPass =
base64Encode(utf8.encode('${auth.username}:${auth.password}'));
headers.putIfAbsent('Authorization', () => 'basic $userAndPass');
final authHeaderValue = auth.authorizationHeaderValue();
if (authHeaderValue != null) {
headers.putIfAbsent('Authorization', () => authHeaderValue);
}

// See https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#user-agent-required
Expand Down
49 changes: 45 additions & 4 deletions lib/src/common/util/auth.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

/// Authentication information.
class Authentication {
/// OAuth2 Token
Expand All @@ -9,26 +11,65 @@ class Authentication {
/// GitHub Password
final String? password;

final String? bearerToken;

// TODO: mark the pram as `String` to REQUIRE a non-null value.
// NEXT major version
/// Creates an [Authentication] instance that uses the specified OAuth2 [token].
const Authentication.withToken(this.token)
: username = null,
password = null;
password = null,
bearerToken = null;

/// Creates an [Authentication] instance that uses the specified
/// [bearerToken].
const Authentication.bearerToken(String this.bearerToken)
: username = null,
password = null,
token = null;

/// Creates an [Authentication] instance that has no authentication.
const Authentication.anonymous()
: token = null,
username = null,
password = null;
password = null,
bearerToken = null;

// TODO: mark the `username` and `password` params as `String` to REQUIRE
// non-null values. - NEXT major version
/// Creates an [Authentication] instance that uses a username and password.
Authentication.basic(this.username, this.password) : token = null;
const Authentication.basic(this.username, this.password)
: token = null,
bearerToken = null;

/// Anonymous Authentication Flag
bool get isAnonymous => !isBasic && !isToken;
bool get isAnonymous => !isBasic && !isToken && !isBearer;

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

/// Token Authentication Flag
bool get isToken => token != null;

// This instance represents a authentication with a "Bearer" token.
bool get isBearer => bearerToken != null;

/// Returns a value for the `Authorization` HTTP request header or `null`
/// if [isAnonymous] is `true`.
String? authorizationHeaderValue() {
if (isToken) {
return 'token $token';
}

if (isBasic) {
final userAndPass = base64Encode(utf8.encode('$username:$password'));
return 'basic $userAndPass';
}

if (isBearer) {
return 'Bearer $bearerToken';
}

return null;
}
}

0 comments on commit 7ae2071

Please sign in to comment.