Skip to content

Commit b9172be

Browse files
authored
chore(auth): hosted ui platforms to use AmplifyOutputs types instead of AmplifyConfig (#5273)
1 parent 4e943d7 commit b9172be

File tree

16 files changed

+147
-111
lines changed

16 files changed

+147
-111
lines changed

packages/auth/amplify_auth_cognito/example/integration_test/native_auth_bridge_test.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:amplify_auth_cognito/src/native_auth_plugin.g.dart';
1111
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
1212
import 'package:amplify_auth_cognito_test/amplify_auth_cognito_test.dart';
1313
import 'package:amplify_auth_integration_test/amplify_auth_integration_test.dart';
14+
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
1415
import 'package:amplify_flutter/amplify_flutter.dart';
1516
import 'package:flutter_test/flutter_test.dart';
1617
import 'package:http/http.dart' as http;
@@ -34,7 +35,7 @@ void main() {
3435

3536
setUp(() async {
3637
dependencyManager = DependencyManager()
37-
..addInstance<CognitoOAuthConfig>(hostedUiConfig)
38+
..addInstance<AuthOutputs>(mockConfig.auth!)
3839
..addInstance<SecureStorageInterface>(MockSecureStorage())
3940
..addInstance<http.Client>(
4041
MockClient((request) {
@@ -58,7 +59,7 @@ void main() {
5859
argPreferprivatesession,
5960
argBrowserpackagename,
6061
) async {
61-
expect(argUrl, contains(hostedUiConfig.webDomain));
62+
expect(argUrl, contains(mockConfig.auth?.oauth?.domain));
6263
expect(argCallbackurlscheme, testUrlScheme);
6364
expect(argPreferprivatesession, isFalse);
6465
expect(argBrowserpackagename, browserPackage);
@@ -86,7 +87,7 @@ void main() {
8687
argPreferprivatesession,
8788
argBrowserpackagename,
8889
) async {
89-
expect(argUrl, contains(hostedUiConfig.webDomain));
90+
expect(argUrl, contains(mockConfig.auth?.oauth?.domain));
9091
expect(argCallbackurlscheme, testUrlScheme);
9192
expect(argPreferprivatesession, isFalse);
9293
expect(argBrowserpackagename, browserPackage);

packages/auth/amplify_auth_cognito/lib/src/flows/hosted_ui/hosted_ui_platform_flutter.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,21 @@ class HostedUiPlatformImpl extends io.HostedUiPlatformImpl {
5858
if (!_isMobile) {
5959
return super.signInRedirectUri;
6060
}
61-
return config.signInRedirectUris.firstWhere(
62-
(uri) => uri.scheme != 'https' && uri.scheme != 'http',
63-
orElse: () => _noSuitableRedirect(signIn: true),
64-
);
61+
return authOutputs.oauth!.redirectSignInUri.map(Uri.parse).firstWhere(
62+
(uri) => uri.scheme != 'https' && uri.scheme != 'http',
63+
orElse: () => _noSuitableRedirect(signIn: true),
64+
);
6565
}
6666

6767
@override
6868
Uri get signOutRedirectUri {
6969
if (!_isMobile) {
7070
return super.signOutRedirectUri;
7171
}
72-
return config.signOutRedirectUris.firstWhere(
73-
(uri) => uri.scheme != 'https' && uri.scheme != 'http',
74-
orElse: () => _noSuitableRedirect(signIn: false),
75-
);
72+
return authOutputs.oauth!.redirectSignOutUri.map(Uri.parse).firstWhere(
73+
(uri) => uri.scheme != 'https' && uri.scheme != 'http',
74+
orElse: () => _noSuitableRedirect(signIn: false),
75+
);
7676
}
7777

7878
@override

packages/auth/amplify_auth_cognito/test/hosted_ui_platform_flutter_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void main() {
2626
setUp(() async {
2727
secureStorage = MockSecureStorage();
2828
dependencyManager = DependencyManager()
29-
..addInstance(hostedUiConfig)
29+
..addInstance(mockConfig.auth!)
3030
..addInstance<SecureStorageInterface>(secureStorage)
3131
..addInstance<NativeAuthBridge>(ThrowingNativeBridge());
3232
plugin = AmplifyAuthCognito()

packages/auth/amplify_auth_cognito_dart/lib/src/flows/hosted_ui/hosted_ui_config.dart

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,23 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import 'package:amplify_core/amplify_core.dart';
5-
6-
/// Configuration helpers for [CognitoUserPoolConfig].
7-
extension HostedUiJwks on CognitoUserPoolConfig {
8-
/// The JSON Web Key (JWK) URI.
9-
///
10-
/// References:
11-
/// - https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-verifying-a-jwt.html
12-
Uri get jwksUri => Uri.parse(
13-
'https://cognito-idp.$region.amazonaws.com/$poolId/.well-known/jwks.json',
14-
);
15-
}
5+
// ignore: implementation_imports
6+
import 'package:amplify_core/src/config/amplify_outputs/auth/oauth_outputs.dart';
167

178
/// Configuration helpers for [CognitoOAuthConfig].
189
///
1910
/// [Reference](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-userpools-server-contract-reference.html)
20-
extension HostedUiConfig on CognitoOAuthConfig {
21-
/// The parsed [webDomain] URI.
11+
extension HostedUiConfig on OAuthOutputs {
12+
/// The parsed [domain] URI.
2213
///
23-
/// If [webDomain] specifies a scheme, it is honored in requests in the same
14+
/// If [domain] specifies a scheme, it is honored in requests in the same
2415
/// way that it is honored for [signInUri], [tokenUri], and [signOutUri]. If
25-
/// no scheme is specified, it defaults to `https` and [webDomain] is
16+
/// no scheme is specified, it defaults to `https` and [domain] is
2617
/// interpreted as a host string.
2718
Uri get _webDomain {
28-
final uri = Uri.parse(webDomain);
19+
final uri = Uri.parse(domain);
2920
if (uri.hasScheme) return uri;
30-
return Uri(scheme: 'https', host: webDomain);
21+
return Uri(scheme: 'https', host: domain);
3122
}
3223

3324
/// The sign in URI.
@@ -37,14 +28,17 @@ extension HostedUiConfig on CognitoOAuthConfig {
3728
/// - https://docs.aws.amazon.com/cognito/latest/developerguide/login-endpoint.html
3829
Uri signInUri([AuthProvider? provider]) {
3930
Uri baseUri;
31+
// ignore: invalid_use_of_internal_member
4032
if (this.signInUri != null) {
33+
// ignore: invalid_use_of_internal_member
4134
baseUri = Uri.parse(this.signInUri!);
4235
} else {
4336
baseUri = _webDomain.replace(path: '/oauth2/authorize');
4437
}
4538
return baseUri.replace(
4639
queryParameters: <String, String>{
4740
if (provider != null) 'identity_provider': provider.uriParameter,
41+
// ignore: invalid_use_of_internal_member
4842
...?signInUriQueryParameters,
4943
},
5044
);
@@ -54,51 +48,43 @@ extension HostedUiConfig on CognitoOAuthConfig {
5448
///
5549
/// References:
5650
/// - https://docs.aws.amazon.com/cognito/latest/developerguide/logout-endpoint.html
57-
Uri get signOutUri {
51+
Uri signOutUri(String userPoolClientId) {
5852
return _webDomain.replace(
5953
path: '/logout',
6054
queryParameters: <String, String>{
55+
// ignore: invalid_use_of_internal_member
6156
...?signOutUriQueryParameters,
62-
'client_id': appClientId,
57+
'client_id': userPoolClientId,
6358
},
6459
);
6560
}
6661

6762
/// The sign in redirect URI to use.
6863
///
6964
/// Throws a [StateError] if there are no URIs registered.
70-
Uri get signInRedirectUri => signInRedirectUris.first;
65+
Uri get signInRedirectUri => Uri.parse(redirectSignInUri.first);
7166

7267
/// The sign out redirect URI to use.
7368
///
7469
/// Throws a [StateError] if there are no URIs registered.
75-
Uri get signOutRedirectUri => signOutRedirectUris.first;
70+
Uri get signOutRedirectUri => Uri.parse(redirectSignOutUri.first);
7671

7772
/// The `token` URI.
7873
///
7974
/// References:
8075
/// - https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html
8176
Uri get tokenUri {
8277
Uri baseUri;
78+
// ignore: invalid_use_of_internal_member
8379
if (this.tokenUri != null) {
80+
// ignore: invalid_use_of_internal_member
8481
baseUri = Uri.parse(this.tokenUri!);
8582
} else {
8683
baseUri = _webDomain.replace(path: '/oauth2/token');
8784
}
8885
return baseUri.replace(
86+
// ignore: invalid_use_of_internal_member
8987
queryParameters: tokenUriQueryParameters,
9088
);
9189
}
92-
93-
/// The `revoke` URI.
94-
///
95-
/// References:
96-
/// - https://docs.aws.amazon.com/cognito/latest/developerguide/revocation-endpoint.html
97-
Uri get revocationUri => _webDomain.replace(path: '/oauth2/revoke');
98-
99-
/// The `userinfo` URI.
100-
///
101-
/// References:
102-
/// - https://docs.aws.amazon.com/cognito/latest/developerguide/userinfo-endpoint.html
103-
Uri get userInfoUri => _webDomain.replace(path: '/oauth2/userInfo');
10490
}

packages/auth/amplify_auth_cognito_dart/lib/src/flows/hosted_ui/hosted_ui_platform.dart

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import 'package:amplify_auth_cognito_dart/src/flows/hosted_ui/hosted_ui_platform
1313
import 'package:amplify_auth_cognito_dart/src/model/hosted_ui/oauth_parameters.dart';
1414
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
1515
import 'package:amplify_core/amplify_core.dart';
16+
// ignore: implementation_imports
17+
import 'package:amplify_core/src/config/amplify_outputs/auth/auth_outputs.dart';
18+
// ignore: implementation_imports
19+
import 'package:amplify_core/src/config/amplify_outputs/auth/oauth_outputs.dart';
1620
import 'package:amplify_secure_storage_dart/amplify_secure_storage_dart.dart';
1721
import 'package:http/http.dart' as http;
1822
import 'package:meta/meta.dart';
@@ -35,12 +39,18 @@ abstract class HostedUiPlatform implements Closeable {
3539
@protected
3640
HostedUiPlatform.protected(this.dependencyManager);
3741

38-
/// The Hosted UI configuration.
42+
/// The Auth configuration.
3943
@protected
40-
CognitoOAuthConfig get config => dependencyManager.expect();
44+
AuthOutputs get authOutputs {
45+
final authOutputs = dependencyManager.get<AuthOutputs>();
46+
if (authOutputs?.oauth == null || authOutputs?.userPoolClientId == null) {
47+
throw const InvalidAccountTypeException.noUserPool();
48+
}
49+
return authOutputs!;
50+
}
4151

4252
/// The Hosted UI storage keys.
43-
late final HostedUiKeys _keys = HostedUiKeys(config.appClientId);
53+
late final HostedUiKeys _keys = HostedUiKeys(authOutputs.userPoolClientId!);
4454

4555
/// The secure storage plugin.
4656
SecureStorageInterface get _secureStorage => dependencyManager.getOrCreate();
@@ -114,14 +124,16 @@ abstract class HostedUiPlatform implements Closeable {
114124
);
115125

116126
_authCodeGrant = createGrant(
117-
config,
127+
authOutputs.oauth!, authOutputs.userPoolClientId!,
128+
// ignore: invalid_use_of_internal_member
129+
appClientSecret: authOutputs.appClientSecret,
118130
codeVerifier: codeVerifier,
119131
httpClient: httpClient,
120132
provider: provider,
121133
);
122134
final uri = _authCodeGrant!.getAuthorizationUrl(
123135
redirectUri ?? signInRedirectUri,
124-
scopes: config.scopes,
136+
scopes: authOutputs.oauth?.scopes,
125137
state: state,
126138
);
127139

@@ -137,7 +149,8 @@ abstract class HostedUiPlatform implements Closeable {
137149
@visibleForTesting
138150
@nonVirtual
139151
Uri getSignOutUri({Uri? redirectUri}) {
140-
final signOutUri = HostedUiConfig(config).signOutUri;
152+
final signOutUri = HostedUiConfig(authOutputs.oauth!)
153+
.signOutUri(authOutputs.userPoolClientId!);
141154

142155
return signOutUri.replace(
143156
queryParameters: <String, String>{
@@ -152,16 +165,18 @@ abstract class HostedUiPlatform implements Closeable {
152165
@visibleForTesting
153166
@nonVirtual
154167
oauth2.AuthorizationCodeGrant createGrant(
155-
CognitoOAuthConfig config, {
168+
OAuthOutputs oauthOutputs,
169+
String userPoolClientId, {
170+
String? appClientSecret,
156171
AuthProvider? provider,
157172
String? codeVerifier,
158173
http.Client? httpClient,
159174
}) {
160175
return oauth2.AuthorizationCodeGrant(
161-
config.appClientId,
162-
HostedUiConfig(config).signInUri(provider),
163-
HostedUiConfig(config).tokenUri,
164-
secret: config.appClientSecret,
176+
userPoolClientId,
177+
HostedUiConfig(authOutputs.oauth!).signInUri(provider),
178+
HostedUiConfig(authOutputs.oauth!).tokenUri,
179+
secret: appClientSecret,
165180
httpClient: httpClient,
166181
codeVerifier: codeVerifier,
167182

@@ -177,13 +192,15 @@ abstract class HostedUiPlatform implements Closeable {
177192
@visibleForTesting
178193
@nonVirtual
179194
oauth2.AuthorizationCodeGrant restoreGrant(
180-
CognitoOAuthConfig config, {
195+
OAuthOutputs oauthOutputs,
196+
String userPoolClientId, {
181197
required String state,
182198
required String codeVerifier,
183199
http.Client? httpClient,
184200
}) {
185201
final grant = createGrant(
186-
config,
202+
oauthOutputs,
203+
userPoolClientId,
187204
codeVerifier: codeVerifier,
188205
httpClient: httpClient,
189206
);
@@ -192,7 +209,7 @@ abstract class HostedUiPlatform implements Closeable {
192209
// Advances the internal state.
193210
..getAuthorizationUrl(
194211
signInRedirectUri,
195-
scopes: config.scopes,
212+
scopes: oauthOutputs.scopes,
196213
state: state,
197214
);
198215
}
@@ -247,7 +264,8 @@ abstract class HostedUiPlatform implements Closeable {
247264
final parameters = dependencyManager.get<OAuthParameters>();
248265
if (parameters != null) {
249266
authCodeGrant = restoreGrant(
250-
config,
267+
authOutputs.oauth!,
268+
authOutputs.userPoolClientId!,
251269
state: state,
252270
codeVerifier: codeVerifier,
253271
httpClient: httpClient,

packages/auth/amplify_auth_cognito_dart/lib/src/flows/hosted_ui/hosted_ui_platform_html.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
55
import 'package:amplify_auth_cognito_dart/src/flows/hosted_ui/hosted_ui_platform.dart';
6-
import 'package:amplify_core/amplify_core.dart';
76
// ignore: implementation_imports
87
import 'package:aws_common/src/js/common.dart';
98
import 'package:path/path.dart' show url;
@@ -31,15 +30,19 @@ class HostedUiPlatformImpl extends HostedUiPlatform {
3130
}
3231

3332
@override
34-
Uri get signInRedirectUri => config.signInRedirectUris.firstWhere(
35-
(uri) => uri.toString().startsWith(_baseUrl),
36-
orElse: () => _noSuitableRedirect(signIn: true),
33+
Uri get signInRedirectUri => Uri.parse(
34+
authOutputs.oauth!.redirectSignInUri.firstWhere(
35+
(uri) => uri.startsWith(_baseUrl),
36+
orElse: () => _noSuitableRedirect(signIn: true),
37+
),
3738
);
3839

3940
@override
40-
Uri get signOutRedirectUri => config.signOutRedirectUris.firstWhere(
41-
(uri) => uri.toString().startsWith(_baseUrl),
42-
orElse: () => _noSuitableRedirect(signIn: false),
41+
Uri get signOutRedirectUri => Uri.parse(
42+
authOutputs.oauth!.redirectSignOutUri.firstWhere(
43+
(uri) => uri.startsWith(_baseUrl),
44+
orElse: () => _noSuitableRedirect(signIn: false),
45+
),
4346
);
4447

4548
/// Launches the given URL.

0 commit comments

Comments
 (0)