Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(googleapis_auth): rename AuthProvider to AuthEndpoints #1755

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions packages/shorebird_cli/lib/src/auth/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:http/http.dart' as http;
import 'package:jwt/jwt.dart';
import 'package:path/path.dart' as p;
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/auth/providers/providers.dart';
import 'package:shorebird_cli/src/auth/endpoints/endpoints.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/command_runner.dart';
import 'package:shorebird_cli/src/http_client/http_client.dart';
Expand All @@ -30,15 +30,15 @@ const googleJwtIssuer = 'https://accounts.google.com';
const microsoftJwtIssuerPrefix = 'https://login.microsoftonline.com/';

typedef ObtainAccessCredentials = Future<oauth2.AccessCredentials> Function(
oauth2.AuthProvider authProvider,
oauth2.AuthEndpoints authEndpoints,
oauth2.ClientId clientId,
List<String> scopes,
http.Client client,
void Function(String) userPrompt,
);

typedef RefreshCredentials = Future<oauth2.AccessCredentials> Function(
oauth2.AuthProvider authProvider,
oauth2.AuthEndpoints authEndpoints,
oauth2.ClientId clientId,
oauth2.AccessCredentials credentials,
http.Client client,
Expand Down Expand Up @@ -176,15 +176,15 @@ class Auth {
}

Future<AccessCredentials> loginCI(
oauth2.AuthProvider authProvider, {
oauth2.AuthEndpoints authEndpoints, {
required void Function(String) prompt,
}) async {
final client = http.Client();
try {
final credentials = await _obtainAccessCredentials(
authProvider,
authProvider.clientId,
authProvider.scopes,
authEndpoints,
authEndpoints.clientId,
authEndpoints.scopes,
client,
prompt,
);
Expand All @@ -206,7 +206,7 @@ class Auth {
}

Future<void> login(
oauth2.AuthProvider authProvider, {
oauth2.AuthEndpoints authEndpoints, {
required void Function(String) prompt,
}) async {
if (_credentials != null) {
Expand All @@ -216,9 +216,9 @@ class Auth {
final client = http.Client();
try {
_credentials = await _obtainAccessCredentials(
authProvider,
authProvider.clientId,
authProvider.scopes,
authEndpoints,
authEndpoints.clientId,
authEndpoints.scopes,
client,
prompt,
);
Expand Down Expand Up @@ -327,21 +327,21 @@ class UserNotFoundException implements Exception {
}

extension OauthAuthProvider on Jwt {
oauth2.AuthProvider get authProvider {
oauth2.AuthEndpoints get authProvider {
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
if (payload.iss == googleJwtIssuer) {
return oauth2.GoogleAuthProvider();
return oauth2.GoogleAuthEndpoints();
} else if (payload.iss.startsWith(microsoftJwtIssuerPrefix)) {
return MicrosoftAuthProvider();
return MicrosoftAuthEndpoints();
}

throw Exception('Unknown jwt issuer: ${payload.iss}');
}
}

extension OauthValues on oauth2.AuthProvider {
extension OauthValues on oauth2.AuthEndpoints {
oauth2.ClientId get clientId {
switch (runtimeType) {
case oauth2.GoogleAuthProvider:
case oauth2.GoogleAuthEndpoints:
return oauth2.ClientId(
/// Shorebird CLI's OAuth 2.0 identifier for GCP,
'''523302233293-eia5antm0tgvek240t46orctktiabrek.apps.googleusercontent.com''',
Expand All @@ -358,7 +358,7 @@ extension OauthValues on oauth2.AuthProvider {
/// For more info see: https://developers.google.com/identity/protocols/oauth2/native-app
'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',
);
case MicrosoftAuthProvider:
case MicrosoftAuthEndpoints:
return oauth2.ClientId(
/// Shorebird CLI's OAuth 2.0 identifier for Azure/Entra.
'0ff83897-ec85-4642-a250-48d5f595137c',
Expand All @@ -370,9 +370,9 @@ extension OauthValues on oauth2.AuthProvider {

List<String> get scopes {
switch (runtimeType) {
case oauth2.GoogleAuthProvider:
case oauth2.GoogleAuthEndpoints:
return ['openid', 'https://www.googleapis.com/auth/userinfo.email'];
case MicrosoftAuthProvider:
case MicrosoftAuthEndpoints:
return ['openid'];
}

Expand Down
2 changes: 2 additions & 0 deletions packages/shorebird_cli/lib/src/auth/endpoints/endpoints.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'package:googleapis_auth/auth_io.dart' show GoogleAuthEndpoints;
export 'microsoft_auth_endpoints.dart';
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:googleapis_auth/googleapis_auth.dart';

/// Endpoints for OAuth authentication with Azure/Entra/Microsoft.
class MicrosoftAuthProvider extends AuthProvider {
class MicrosoftAuthEndpoints extends AuthEndpoints {
@override
Uri get authorizationEndpoint =>
Uri.https('login.microsoftonline.com', 'common/oauth2/v2.0/authorize');
Expand Down
2 changes: 0 additions & 2 deletions packages/shorebird_cli/lib/src/auth/providers/providers.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LoginCiCommand extends ShorebirdCommand {
final AccessCredentials credentials;

try {
credentials = await auth.loginCI(GoogleAuthProvider(), prompt: prompt);
credentials = await auth.loginCI(GoogleAuthEndpoints(), prompt: prompt);
} on UserNotFoundException catch (error) {
logger
..err(
Expand Down
3 changes: 2 additions & 1 deletion packages/shorebird_cli/lib/src/commands/login_command.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:googleapis_auth/auth_io.dart';
import 'package:mason_logger/mason_logger.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/auth/endpoints/endpoints.dart';
import 'package:shorebird_cli/src/command.dart';
import 'package:shorebird_cli/src/logger.dart';

Expand All @@ -18,7 +19,7 @@ class LoginCommand extends ShorebirdCommand {
@override
Future<int> run() async {
try {
await auth.login(GoogleAuthProvider(), prompt: prompt);
await auth.login(GoogleAuthEndpoints(), prompt: prompt);
} on UserAlreadyLoggedInException catch (error) {
logger
..info('You are already logged in as <${error.email}>.')
Expand Down
12 changes: 6 additions & 6 deletions packages/shorebird_cli/test/src/auth/auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:path/path.dart' as p;
import 'package:platform/platform.dart';
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/auth/providers/providers.dart';
import 'package:shorebird_cli/src/auth/endpoints/endpoints.dart';
import 'package:shorebird_cli/src/command_runner.dart';
import 'package:shorebird_cli/src/http_client/http_client.dart';
import 'package:shorebird_cli/src/logger.dart';
Expand All @@ -22,7 +22,7 @@ import 'package:test/test.dart';
import '../fakes.dart';
import '../mocks.dart';

class FakeProvider extends oauth2.AuthProvider {
class FakeProvider extends oauth2.AuthEndpoints {
@override
Uri get authorizationEndpoint => Uri.https('example.com');

Expand Down Expand Up @@ -106,7 +106,7 @@ void main() {
});

test('returns AuthProvider.microsoft', () {
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
expect(jwt.authProvider, isA<MicrosoftAuthProvider>());
expect(jwt.authProvider, isA<MicrosoftAuthEndpoints>());
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
});
});

Expand All @@ -116,7 +116,7 @@ void main() {
});

test('returns AuthProvider.google', () {
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
expect(jwt.authProvider, isA<GoogleAuthProvider>());
expect(jwt.authProvider, isA<GoogleAuthEndpoints>());
});
});

Expand Down Expand Up @@ -152,8 +152,8 @@ void main() {
);
const refreshToken = '';
const scopes = <String>[];
final googleAuthProvider = GoogleAuthProvider();
final microsoftAuthProvider = MicrosoftAuthProvider();
final googleAuthProvider = GoogleAuthEndpoints();
final microsoftAuthProvider = MicrosoftAuthEndpoints();
bryanoltman marked this conversation as resolved.
Show resolved Hide resolved
final accessToken = oauth2.AccessToken(
'Bearer',
'accessToken',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import 'package:shorebird_cli/src/auth/providers/providers.dart';
import 'package:shorebird_cli/src/auth/endpoints/endpoints.dart';
import 'package:test/test.dart';

void main() {
group(MicrosoftAuthProvider, () {
group(MicrosoftAuthEndpoints, () {
test('has valid endpoints', () {
final provider = MicrosoftAuthProvider();
final provider = MicrosoftAuthEndpoints();
expect(provider.authorizationEndpoint, isNotNull);
expect(provider.tokenEndpoint, isNotNull);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:mason_logger/mason_logger.dart';
import 'package:mocktail/mocktail.dart';
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/auth/endpoints/endpoints.dart';
import 'package:shorebird_cli/src/commands/commands.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:test/test.dart';
Expand All @@ -30,7 +31,7 @@ void main() {
}

setUpAll(() {
registerFallbackValue(GoogleAuthProvider());
registerFallbackValue(GoogleAuthEndpoints());
});

setUp(() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:mocktail/mocktail.dart';
import 'package:path/path.dart' as p;
import 'package:scoped/scoped.dart';
import 'package:shorebird_cli/src/auth/auth.dart';
import 'package:shorebird_cli/src/auth/endpoints/endpoints.dart';
import 'package:shorebird_cli/src/commands/login_command.dart';
import 'package:shorebird_cli/src/logger.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -34,7 +35,7 @@ void main() {
}

setUpAll(() {
registerFallbackValue(GoogleAuthProvider());
registerFallbackValue(GoogleAuthEndpoints());
});

setUp(() {
Expand Down
20 changes: 10 additions & 10 deletions third_party/googleapis_auth/lib/auth_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export 'src/typedefs.dart';
/// {@macro googleapis_auth_not_close_the_baseClient}
/// {@macro googleapis_auth_listen_port}
Future<AutoRefreshingAuthClient> clientViaUserConsent(
AuthProvider authProvider,
AuthEndpoints authEndpoints,
ClientId clientId,
List<String> scopes,
PromptUserForConsent userPrompt, {
Expand All @@ -52,7 +52,7 @@ Future<AutoRefreshingAuthClient> clientViaUserConsent(
}

final flow = AuthorizationCodeGrantServerFlow(
authProvider,
authEndpoints,
clientId,
scopes,
baseClient,
Expand All @@ -73,7 +73,7 @@ Future<AutoRefreshingAuthClient> clientViaUserConsent(
}
return AutoRefreshingClient(
baseClient,
authProvider,
authEndpoints,
clientId,
credentials,
closeUnderlyingClient: closeUnderlyingClient,
Expand All @@ -96,7 +96,7 @@ Future<AutoRefreshingAuthClient> clientViaUserConsent(
/// {@macro googleapis_auth_close_the_client}
/// {@macro googleapis_auth_not_close_the_baseClient}
Future<AutoRefreshingAuthClient> clientViaUserConsentManual(
AuthProvider authProvider,
AuthEndpoints authEndpoints,
ClientId clientId,
List<String> scopes,
PromptUserForConsentManual userPrompt, {
Expand All @@ -110,7 +110,7 @@ Future<AutoRefreshingAuthClient> clientViaUserConsentManual(
}

final flow = AuthorizationCodeGrantManualFlow(
authProvider,
authEndpoints,
clientId,
scopes,
baseClient,
Expand All @@ -131,7 +131,7 @@ Future<AutoRefreshingAuthClient> clientViaUserConsentManual(

return AutoRefreshingClient(
baseClient,
authProvider,
authEndpoints,
clientId,
credentials,
closeUnderlyingClient: closeUnderlyingClient,
Expand Down Expand Up @@ -159,7 +159,7 @@ Future<AutoRefreshingAuthClient> clientViaUserConsentManual(
/// on the Google Cloud console.
/// {@endtemplate}
Future<AccessCredentials> obtainAccessCredentialsViaUserConsent(
AuthProvider authProvider,
AuthEndpoints authEndpoints,
ClientId clientId,
List<String> scopes,
Client client,
Expand All @@ -168,7 +168,7 @@ Future<AccessCredentials> obtainAccessCredentialsViaUserConsent(
int listenPort = 0,
}) =>
AuthorizationCodeGrantServerFlow(
authProvider,
authEndpoints,
clientId,
scopes,
client,
Expand All @@ -190,15 +190,15 @@ Future<AccessCredentials> obtainAccessCredentialsViaUserConsent(
///
/// {@macro googleapis_auth_user_consent_return}
Future<AccessCredentials> obtainAccessCredentialsViaUserConsentManual(
AuthProvider authProvider,
AuthEndpoints authEndpoints,
ClientId clientId,
List<String> scopes,
Client client,
PromptUserForConsentManual userPrompt, {
String? hostedDomain,
}) =>
AuthorizationCodeGrantManualFlow(
authProvider,
authEndpoints,
clientId,
scopes,
client,
Expand Down
2 changes: 1 addition & 1 deletion third_party/googleapis_auth/lib/googleapis_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ library googleapis_auth;

export 'src/auth_client.dart';
export 'src/auth_functions.dart';
export 'src/auth_provider.dart';
export 'src/auth_endpoints.dart';
export 'src/client_id.dart';
export 'src/exceptions.dart';
export 'src/response_type.dart';
Expand Down
6 changes: 3 additions & 3 deletions third_party/googleapis_auth/lib/src/adc_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'auth_http_utils.dart';

Future<AutoRefreshingAuthClient> fromApplicationsCredentialsFile(
File file,
AuthProvider authProvider,
AuthEndpoints authEndpoints,
String fileSource,
List<String> scopes,
Client baseClient,
Expand All @@ -38,10 +38,10 @@ Future<AutoRefreshingAuthClient> fromApplicationsCredentialsFile(
);
return AutoRefreshingClient(
baseClient,
authProvider,
authEndpoints,
clientId,
await refreshCredentials(
authProvider,
authEndpoints,
clientId,
AccessCredentials(
// Hack: Create empty credentials that have expired.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'known_uris.dart';

abstract class AuthProvider {
abstract class AuthEndpoints {
Uri get authorizationEndpoint;
Uri get tokenEndpoint;
}

class GoogleAuthProvider extends AuthProvider {
class GoogleAuthEndpoints extends AuthEndpoints {
@override
Uri get authorizationEndpoint => googleOauth2AuthorizationEndpoint;

Expand Down
Loading
Loading