From 4259149ae4d479286e1ef8ff1014a6a82bcead4a Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Tue, 25 Jul 2023 13:49:23 -0700 Subject: [PATCH] Add bearerToken constructor to Authentication class Also added `authorizationHeaderValue()` function. Also added `const` qualifier to `basic` constructor. --- lib/src/common/github.dart | 9 +++---- lib/src/common/util/auth.dart | 49 ++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/src/common/github.dart b/lib/src/common/github.dart index c08140bf..ef803ea0 100644 --- a/lib/src/common/github.dart +++ b/lib/src/common/github.dart @@ -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 diff --git a/lib/src/common/util/auth.dart b/lib/src/common/util/auth.dart index b287f16c..1c4d6798 100644 --- a/lib/src/common/util/auth.dart +++ b/lib/src/common/util/auth.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + /// Authentication information. class Authentication { /// OAuth2 Token @@ -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; + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy