Remove support for legacy credentials file (#3824)
diff --git a/lib/src/command/lish.dart b/lib/src/command/lish.dart index 8e16ca6..b5330c9 100644 --- a/lib/src/command/lish.dart +++ b/lib/src/command/lish.dart
@@ -210,7 +210,7 @@ // // This allows us to use `dart pub token add` to inject a token for use // with the official servers. - await oauth2.withClient(cache, (client) { + await oauth2.withClient((client) { return _publishUsingClient(packageBytes, client); }); } else {
diff --git a/lib/src/command/login.dart b/lib/src/command/login.dart index f140317..670d1a8 100644 --- a/lib/src/command/login.dart +++ b/lib/src/command/login.dart
@@ -23,7 +23,7 @@ @override Future<void> runProtected() async { - final credentials = oauth2.loadCredentials(cache); + final credentials = oauth2.loadCredentials(); if (credentials == null) { final userInfo = await _retrieveUserInfo(); if (userInfo == null) { @@ -44,7 +44,7 @@ } Future<_UserInfo?> _retrieveUserInfo() async { - return await oauth2.withClient(cache, (client) async { + return await oauth2.withClient((client) async { final discovery = await oauth2.fetchOidcDiscoveryDocument(); final userInfoEndpoint = discovery['userinfo_endpoint']; final userInfoRequest = await client.get(Uri.parse(userInfoEndpoint));
diff --git a/lib/src/command/logout.dart b/lib/src/command/logout.dart index 04ffe12..ee8bceb 100644 --- a/lib/src/command/logout.dart +++ b/lib/src/command/logout.dart
@@ -20,6 +20,6 @@ @override Future<void> runProtected() async { - oauth2.logout(cache); + oauth2.logout(); } }
diff --git a/lib/src/oauth2.dart b/lib/src/oauth2.dart index c729d4c..d4053f8 100644 --- a/lib/src/oauth2.dart +++ b/lib/src/oauth2.dart
@@ -5,7 +5,6 @@ import 'dart:async'; import 'dart:io'; -import 'package:collection/collection.dart' show IterableExtension; import 'package:http/http.dart' as http; import 'package:http/retry.dart'; import 'package:path/path.dart' as path; @@ -17,7 +16,6 @@ import 'http.dart'; import 'io.dart'; import 'log.dart' as log; -import 'system_cache.dart'; import 'utils.dart'; /// The global HTTP client with basic retries. Used instead of retryForHttp for @@ -84,27 +82,21 @@ Credentials? _credentials; /// Delete the cached credentials, if they exist. -void _clearCredentials(SystemCache cache) { +void _clearCredentials() { _credentials = null; - var credentialsFile = _credentialsFile(cache); + var credentialsFile = _credentialsFile(); if (credentialsFile != null && entryExists(credentialsFile)) { deleteEntry(credentialsFile); } } /// Try to delete the cached credentials. -void logout(SystemCache cache) { - var credentialsFile = _credentialsFile(cache); +void logout() { + var credentialsFile = _credentialsFile(); if (credentialsFile != null && entryExists(credentialsFile)) { log.message('Logging out of pub.dev.'); log.message('Deleting $credentialsFile'); - _clearCredentials(cache); - // Test if we also have a legacy credentials file. - final legacyCredentialsFile = _legacyCredentialsFile(cache); - if (entryExists(legacyCredentialsFile)) { - log.message('Also deleting legacy credentials at $legacyCredentialsFile'); - deleteEntry(legacyCredentialsFile); - } + _clearCredentials(); } else { log.message( 'No existing credentials file $credentialsFile. Cannot log out.', @@ -120,28 +112,28 @@ /// This takes care of loading and saving the client's credentials, as well as /// prompting the user for their authorization. It will also re-authorize and /// re-run [fn] if a recoverable authorization error is detected. -Future<T> withClient<T>(SystemCache cache, Future<T> Function(Client) fn) { - return _getClient(cache).then((client) { +Future<T> withClient<T>(Future<T> Function(Client) fn) { + return _getClient().then((client) { return fn(client).whenComplete(() { // TODO(sigurdm): refactor the http subsystem, so we can close [client] // here. // Be sure to save the credentials even when an error happens. - _saveCredentials(cache, client.credentials); + _saveCredentials(client.credentials); }); }).catchError((error) { if (error is ExpirationException) { log.error("Pub's authorization to upload packages has expired and " "can't be automatically refreshed."); - return withClient(cache, fn); + return withClient(fn); } else if (error is AuthorizationException) { var message = 'OAuth2 authorization failed'; if (error.description != null) { message = '$message (${error.description})'; } log.error('$message.'); - _clearCredentials(cache); - return withClient(cache, fn); + _clearCredentials(); + return withClient(fn); } else { throw error; } @@ -152,8 +144,8 @@ /// /// If saved credentials are available, those are used; otherwise, the user is /// prompted to authorize the pub client. -Future<Client> _getClient(SystemCache cache) async { - var credentials = loadCredentials(cache); +Future<Client> _getClient() async { + var credentials = loadCredentials(); if (credentials == null) return await _authorize(); var client = Client( @@ -164,7 +156,7 @@ basicAuth: false, httpClient: _retryHttpClient, ); - _saveCredentials(cache, client.credentials); + _saveCredentials(client.credentials); return client; } @@ -173,13 +165,13 @@ /// /// If the credentials can't be loaded for any reason, the returned [Future] /// completes to `null`. -Credentials? loadCredentials(SystemCache cache) { +Credentials? loadCredentials() { log.fine('Loading OAuth2 credentials.'); try { if (_credentials != null) return _credentials; - var path = _credentialsFile(cache); + var path = _credentialsFile(); if (path == null || !fileExists(path)) return null; var credentials = Credentials.fromJson(readTextFile(path)); @@ -199,10 +191,10 @@ /// Save the user's OAuth2 credentials to the in-memory cache and the /// filesystem. -void _saveCredentials(SystemCache cache, Credentials credentials) { +void _saveCredentials(Credentials credentials) { log.fine('Saving OAuth2 credentials.'); _credentials = credentials; - var credentialsPath = _credentialsFile(cache); + var credentialsPath = _credentialsFile(); if (credentialsPath != null) { ensureDir(path.dirname(credentialsPath)); writeTextFile(credentialsPath, credentials.toJson(), dontLogContents: true); @@ -211,26 +203,12 @@ /// The path to the file in which the user's OAuth2 credentials are stored. /// -/// This used to be PUB_CACHE/credentials.json. But the pub cache is not the -/// best place for storing secrets, as it might be shared. -/// -/// To provide backwards compatibility we use the legacy file if only it exists. -/// /// Returns `null` if there is no good place for the file. -String? _credentialsFile(SystemCache cache) { +String? _credentialsFile() { final configDir = dartConfigDir; - - final newCredentialsFile = - configDir == null ? null : path.join(configDir, 'pub-credentials.json'); - var file = [ - if (newCredentialsFile != null) newCredentialsFile, - _legacyCredentialsFile(cache) - ].firstWhereOrNull(fileExists); - return file ?? newCredentialsFile; -} - -String _legacyCredentialsFile(SystemCache cache) { - return path.join(cache.rootDir, 'credentials.json'); + return configDir == null + ? null + : path.join(configDir, 'pub-credentials.json'); } /// Gets the user to authorize pub as a client of pub.dev via oauth2.
diff --git a/test/oauth2/logout_test.dart b/test/oauth2/logout_test.dart index db083ac..e40af5f 100644 --- a/test/oauth2/logout_test.dart +++ b/test/oauth2/logout_test.dart
@@ -24,41 +24,6 @@ await configDir([d.nothing('pub-credentials.json')]).validate(); }); - test( - 'with an existing credentials file stored in the legacy location, deletes both.', - () async { - await servePackages(); - await d - .credentialsFile( - globalServer, - 'access-token', - refreshToken: 'refresh token', - expiration: DateTime.now().add(Duration(hours: 1)), - ) - .create(); - - await d - .legacyCredentialsFile( - globalServer, - 'access-token', - refreshToken: 'refresh token', - expiration: DateTime.now().add(Duration(hours: 1)), - ) - .create(); - - await runPub( - args: ['logout'], - output: allOf( - [ - contains('Logging out of pub.dev.'), - contains('Also deleting legacy credentials at ') - ], - ), - ); - - await d.dir(cachePath, [d.nothing('credentials.json')]).validate(); - await d.dir(configPath, [d.nothing('pub-credentials.json')]).validate(); - }); test('with no existing credentials.json, notifies.', () async { await d.dir(configPath, [d.nothing('pub-credentials.json')]).create(); await runPub(