Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bearerToken constructor to Authentication class #381

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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].
kevmoo marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}