Skip to content

Commit c07c634

Browse files
author
Casey Hillers
authored
Make GitHub.auth non-nullable (#377)
* add const everywhere
1 parent fccb90e commit c07c634

File tree

11 files changed

+39
-27
lines changed

11 files changed

+39
-27
lines changed

example/index.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import 'common.dart';
33

44
void main() {
55
final tokenInput = querySelector('#token') as InputElement;
6-
tokenInput.value = github.auth!.token ?? '';
6+
tokenInput.value = github.auth.token ?? '';
77
window.sessionStorage['GITHUB_TOKEN'] = tokenInput.value!;
88
tokenInput.onKeyUp.listen((_) {
99
window.sessionStorage['GITHUB_TOKEN'] = tokenInput.value!;

example/user_info.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ void loadUser() {
5959
});
6060
});
6161

62-
if (github.auth!.token != null) {
63-
localToken!.value = github.auth!.token;
62+
if (github.auth.token != null) {
63+
localToken!.value = github.auth.token;
6464
loadBtn.click();
6565
}
6666
}

lib/src/browser/xplat_browser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Authentication findAuthenticationFromEnvironment() {
1111
// search the query string parameters first
1212
var auth = findAuthenticationInMap(_parseQuery(window.location.href));
1313
auth ??= findAuthenticationInMap(window.sessionStorage);
14-
return auth ?? Authentication.anonymous();
14+
return auth ?? const Authentication.anonymous();
1515
}
1616

1717
/// Parse the query string to a parameter `Map`

lib/src/common/github.dart

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ class GitHub {
1919
/// [endpoint] is the api endpoint to use
2020
/// [auth] is the authentication information
2121
GitHub({
22-
Authentication? auth,
22+
this.auth = const Authentication.anonymous(),
2323
this.endpoint = 'https://api.github.com',
2424
this.version = '2022-11-28',
2525
http.Client? client,
26-
}) : auth = auth ?? Authentication.anonymous(),
27-
client = client ?? http.Client();
26+
}) : client = client ?? http.Client();
2827

2928
static const _ratelimitLimitHeader = 'x-ratelimit-limit';
3029
static const _ratelimitResetHeader = 'x-ratelimit-reset';
@@ -34,7 +33,7 @@ class GitHub {
3433
static const versionHeader = 'X-GitHub-Api-Version';
3534

3635
/// Authentication Information
37-
Authentication? auth;
36+
final Authentication auth;
3837

3938
/// API Endpoint
4039
final String endpoint;
@@ -369,16 +368,16 @@ class GitHub {
369368
headers['Accept'] = preview;
370369
}
371370

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

380379
// See https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#user-agent-required
381-
headers.putIfAbsent('User-Agent', () => auth?.username ?? 'github.dart');
380+
headers.putIfAbsent('User-Agent', () => auth.username ?? 'github.dart');
382381

383382
if (method == 'PUT' && body == null) {
384383
headers.putIfAbsent('Content-Length', () => '0');
@@ -493,10 +492,6 @@ class GitHub {
493492
/// Disposes of this GitHub Instance.
494493
/// No other methods on this instance should be called after this method is called.
495494
void dispose() {
496-
// Destroy the Authentication Information
497-
// This is needed for security reasons.
498-
auth = null;
499-
500495
// Closes the HTTP Client
501496
client.close();
502497
}

lib/src/common/util/auth.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class Authentication {
1010
final String? password;
1111

1212
/// Creates an [Authentication] instance that uses the specified OAuth2 [token].
13-
Authentication.withToken(this.token)
13+
const Authentication.withToken(this.token)
1414
: username = null,
1515
password = null;
1616

1717
/// Creates an [Authentication] instance that has no authentication.
18-
Authentication.anonymous()
18+
const Authentication.anonymous()
1919
: token = null,
2020
username = null,
2121
password = null;

lib/src/common/xplat_common.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import 'package:github/src/common.dart';
1010
/// In both contexts it delegates to [findAuthenticationInMap] to find the
1111
/// github token or username and password.
1212
Authentication findAuthenticationFromEnvironment() =>
13-
Authentication.anonymous();
13+
const Authentication.anonymous();
1414

1515
/// Checks the passed in map for keys in [COMMON_GITHUB_TOKEN_ENV_KEYS].
1616
/// The first one that exists is used as the github token to call [Authentication.withToken] with.

lib/src/server/xplat_server.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ Authentication findAuthenticationFromEnvironment() {
2727
}
2828

2929
return findAuthenticationInMap(Platform.environment) ??
30-
Authentication.anonymous();
30+
const Authentication.anonymous();
3131
}

test/common/github_test.dart

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'dart:io';
22

3-
import 'package:github/src/common/github.dart';
3+
import 'package:github/src/common.dart';
44
import 'package:http/http.dart';
55
import 'package:http/testing.dart';
66
import 'package:test/test.dart';
@@ -38,5 +38,22 @@ void main() {
3838
final userAgent = request!.headers['User-Agent'];
3939
expect(userAgent, 'github.dart');
4040
});
41+
42+
test('anonymous auth passes no authorization header', () async {
43+
Request? request;
44+
final client = MockClient((r) async {
45+
request = r;
46+
return Response('{}', HttpStatus.ok);
47+
});
48+
49+
final github = GitHub(
50+
client: client,
51+
auth: const Authentication.anonymous(),
52+
);
53+
await github.getJSON(''); // Make HTTP request
54+
55+
expect(request, isNotNull);
56+
expect(request!.headers.containsKey('Authorization'), isFalse);
57+
});
4158
});
4259
}

test/experiment/crawler.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:github/github.dart';
22

33
void main() {
4-
final github = GitHub(auth: Authentication.anonymous());
4+
final github = GitHub(auth: const Authentication.anonymous());
55

66
final crawler = RepositoryCrawler(
77
github,

test/git_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const date = '2014-10-02T15:21:29Z';
1010
GitHub createGithub() {
1111
return GitHub(
1212
endpoint: fakeApiUrl,
13-
auth:
14-
Authentication.withToken('0000000000000000000000000000000000000001'));
13+
auth: const Authentication.withToken(
14+
'0000000000000000000000000000000000000001'));
1515
}
1616

1717
void main() {

0 commit comments

Comments
 (0)