Allow use of token for talking to pub.dev (#3330) * Allow use of token for talking to pub.dev
diff --git a/lib/src/command/lish.dart b/lib/src/command/lish.dart index c927e07..99b5051 100644 --- a/lib/src/command/lish.dart +++ b/lib/src/command/lish.dart
@@ -165,13 +165,17 @@ // explicitly have to define mock servers as official server to test // publish command with oauth2 credentials. if (runningFromTest && - Platform.environment.containsKey('PUB_HOSTED_URL') && - Platform.environment['_PUB_TEST_AUTH_METHOD'] == 'oauth2') - Platform.environment['PUB_HOSTED_URL'], + Platform.environment.containsKey('_PUB_TEST_DEFAULT_HOSTED_URL')) + Platform.environment['_PUB_TEST_DEFAULT_HOSTED_URL'], }; - if (officialPubServers.contains(server.toString())) { - // Using OAuth2 authentication client for the official pub servers + final isOfficalServer = officialPubServers.contains(server.toString()); + if (isOfficalServer && !cache.tokenStore.hasCredential(server)) { + // Using OAuth2 authentication client for the official pub servers, when + // we don't have an explicit token from [TokenStore] to use instead. + // + // This allows us to use `dart pub token add` to inject a token for use + // with the official servers. await oauth2.withClient(cache, (client) { return _publishUsingClient(packageBytes, client); });
diff --git a/lib/src/source/hosted.dart b/lib/src/source/hosted.dart index 2cfc52a..7d08a55 100644 --- a/lib/src/source/hosted.dart +++ b/lib/src/source/hosted.dart
@@ -117,8 +117,15 @@ // Clearly, a bit of investigation is necessary before we update this to // pub.dev, it might be attractive to do next time we change the server API. try { + var defaultHostedUrl = 'https://pub.dartlang.org'; + // Allow the defaultHostedUrl to be overriden when running from tests + if (runningFromTest) { + defaultHostedUrl = + io.Platform.environment['_PUB_TEST_DEFAULT_HOSTED_URL'] ?? + defaultHostedUrl; + } return _defaultUrl ??= validateAndNormalizeHostedUrl( - io.Platform.environment['PUB_HOSTED_URL'] ?? 'https://pub.dartlang.org', + io.Platform.environment['PUB_HOSTED_URL'] ?? defaultHostedUrl, ); } on FormatException catch (e) { throw ConfigException(
diff --git a/test/lish/archives_and_uploads_a_package_test.dart b/test/lish/archives_and_uploads_a_package_test.dart index 88e4862..b033e28 100644 --- a/test/lish/archives_and_uploads_a_package_test.dart +++ b/test/lish/archives_and_uploads_a_package_test.dart
@@ -37,6 +37,31 @@ await pub.shouldExit(exit_codes.SUCCESS); }); + test('archives and uploads a package using token', () async { + await servePackages(); + await d.tokensFile({ + 'version': 1, + 'hosted': [ + {'url': globalServer.url, 'token': 'access token'}, + ] + }).create(); + var pub = await startPublish(globalServer); + + await confirmPublish(pub); + handleUploadForm(globalServer); + handleUpload(globalServer); + + globalServer.expect('GET', '/create', (request) { + return shelf.Response.ok(jsonEncode({ + 'success': {'message': 'Package test_pkg 1.0.0 uploaded!'} + })); + }); + + expect(pub.stdout, emits(startsWith('Uploading...'))); + expect(pub.stdout, emits('Package test_pkg 1.0.0 uploaded!')); + await pub.shouldExit(exit_codes.SUCCESS); + }); + test('publishes to hosted-url with path', () async { await servePackages(); await d.tokensFile({ @@ -48,7 +73,7 @@ var pub = await startPublish( globalServer, path: '/sub/folder', - authMethod: 'token', + overrideDefaultHostedServer: false, environment: {'TOKEN': 'access token'}, );
diff --git a/test/test_pub.dart b/test/test_pub.dart index 94e64fc..fcba6a6 100644 --- a/test/test_pub.dart +++ b/test/test_pub.dart
@@ -371,15 +371,17 @@ Future<PubProcess> startPublish( PackageServer server, { List<String>? args, - String authMethod = 'oauth2', + bool overrideDefaultHostedServer = true, Map<String, String>? environment, String path = '', }) async { var tokenEndpoint = Uri.parse(server.url).resolve('/token').toString(); args = ['lish', ...?args]; return await startPub(args: args, tokenEndpoint: tokenEndpoint, environment: { - 'PUB_HOSTED_URL': server.url + path, - '_PUB_TEST_AUTH_METHOD': authMethod, + if (overrideDefaultHostedServer) + '_PUB_TEST_DEFAULT_HOSTED_URL': server.url + path + else + 'PUB_HOSTED_URL': server.url + path, if (environment != null) ...environment, }); }
diff --git a/test/token/token_authentication_test.dart b/test/token/token_authentication_test.dart index c020ac0..350bca4 100644 --- a/test/token/token_authentication_test.dart +++ b/test/token/token_authentication_test.dart
@@ -19,8 +19,11 @@ {'url': globalServer.url, 'env': 'TOKEN'}, ] }).create(); - var pub = await startPublish(globalServer, - authMethod: 'token', environment: {'TOKEN': 'access token'}); + var pub = await startPublish( + globalServer, + overrideDefaultHostedServer: false, + environment: {'TOKEN': 'access token'}, + ); await confirmPublish(pub); handleUploadForm(globalServer); @@ -36,7 +39,10 @@ {'url': globalServer.url, 'token': 'access token'}, ] }).create(); - var pub = await startPublish(globalServer, authMethod: 'token'); + var pub = await startPublish( + globalServer, + overrideDefaultHostedServer: false, + ); await confirmPublish(pub); handleUploadForm(globalServer);
diff --git a/test/token/when_receives_401_removes_token_test.dart b/test/token/when_receives_401_removes_token_test.dart index 86c0559..add619b 100644 --- a/test/token/when_receives_401_removes_token_test.dart +++ b/test/token/when_receives_401_removes_token_test.dart
@@ -19,7 +19,7 @@ {'url': server.url, 'token': 'access token'}, ] }).create(); - var pub = await startPublish(server, authMethod: 'token'); + var pub = await startPublish(server, overrideDefaultHostedServer: false); await confirmPublish(pub); server.expect('GET', '/api/packages/versions/new', (request) {
diff --git a/test/token/when_receives_403_persists_saved_token_test.dart b/test/token/when_receives_403_persists_saved_token_test.dart index 45fc7a4..6db5538 100644 --- a/test/token/when_receives_403_persists_saved_token_test.dart +++ b/test/token/when_receives_403_persists_saved_token_test.dart
@@ -19,7 +19,7 @@ {'url': server.url, 'token': 'access token'}, ] }).create(); - var pub = await startPublish(server, authMethod: 'token'); + var pub = await startPublish(server, overrideDefaultHostedServer: false); await confirmPublish(pub); server.expect('GET', '/api/packages/versions/new', (request) {