From 2bee1096a2b83ad76c7d0ccfc0427c5096ca678e Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Tue, 25 Jul 2023 14:11:14 -0700 Subject: [PATCH 1/2] Add bearerToken constructor to Authentication class (#381) 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; + } } From 5ef10ff19858e21c66e14c0569439af488f39260 Mon Sep 17 00:00:00 2001 From: Rob Becker Date: Tue, 25 Jul 2023 16:12:26 -0600 Subject: [PATCH 2/2] prep 9.17.0 --- CHANGELOG.md | 7 +++++++ pubspec.yaml | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1443106..8027a432 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 9.17.0 + +* Add bearerToken constructor to Authentication class by @kevmoo in https://github.com/SpinlockLabs/github.dart/pull/381 + + +**Full Changelog**: https://github.com/SpinlockLabs/github.dart/compare/9.16.0...9.17.0 + ## 9.16.0 * Fix links and spelling nits in markdown files by @kevmoo in https://github.com/SpinlockLabs/github.dart/pull/379 diff --git a/pubspec.yaml b/pubspec.yaml index e6a7ced3..0a917ea4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: github -version: 9.16.0 +version: 9.17.0 description: A high-level GitHub API Client Library that uses Github's v3 API homepage: https://github.com/SpinlockLabs/github.dart 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