Skip to content

Commit 555c4fe

Browse files
committed
feat(auth): add support for cognito oidc parameters in managed login
1 parent 520d2ad commit 555c4fe

File tree

12 files changed

+182
-7
lines changed

12 files changed

+182
-7
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ class HostedUiPlatformImpl extends io.HostedUiPlatformImpl {
9494
signInRedirectUri.scheme,
9595
options.isPreferPrivateSession,
9696
options.browserPackageName,
97+
options.nonce,
98+
options.language,
99+
options.loginHint,
100+
options.prompt?.map((obj) => obj.value).toList(),
101+
options.resource
97102
);
98103
dispatcher
99104
.dispatch(

packages/auth/amplify_auth_cognito/lib/src/native_auth_plugin.g.dart

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/auth/amplify_auth_cognito/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ platforms:
2121
dependencies:
2222
amplify_analytics_pinpoint: ">=2.7.0 <2.8.0"
2323
amplify_analytics_pinpoint_dart: ">=0.4.12 <0.5.0"
24-
amplify_auth_cognito_dart: ">=0.11.14 <0.12.0"
24+
amplify_auth_cognito_dart:
25+
path: ../amplify_auth_cognito_dart
2526
amplify_core: ">=2.7.0 <2.8.0"
2627
amplify_flutter: ">=2.7.0 <2.8.0"
2728
amplify_secure_storage: ">=0.5.13 <0.6.0"

packages/auth/amplify_auth_cognito_dart/lib/amplify_auth_cognito_dart.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export 'src/model/signin/cognito_confirm_sign_in_plugin_options.dart';
4040
export 'src/model/signin/cognito_sign_in_plugin_options.dart';
4141
export 'src/model/signin/cognito_sign_in_result.dart';
4242
export 'src/model/signin/cognito_sign_in_with_web_ui_plugin_options.dart';
43+
export 'src/model/signin/cognito_sign_in_with_web_ui_plugin_options_prompt.dart';
4344
export 'src/model/signout/cognito_sign_out_plugin_options.dart';
4445
export 'src/model/signout/cognito_sign_out_result.dart';
4546
export 'src/model/signup/cognito_confirm_sign_up_plugin_options.dart';

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
45
import 'package:amplify_core/amplify_core.dart';
56
// ignore: implementation_imports
67
import 'package:amplify_core/src/config/amplify_outputs/auth/oauth_outputs.dart';
@@ -26,7 +27,7 @@ extension HostedUiConfig on OAuthOutputs {
2627
/// References:
2728
/// - https://docs.aws.amazon.com/cognito/latest/developerguide/authorization-endpoint.html
2829
/// - https://docs.aws.amazon.com/cognito/latest/developerguide/login-endpoint.html
29-
Uri signInUri([AuthProvider? provider]) {
30+
Uri signInUri([AuthProvider? provider, CognitoSignInWithWebUIPluginOptions? options]) {
3031
Uri baseUri;
3132
// ignore: invalid_use_of_internal_member
3233
if (this.signInUri != null) {
@@ -35,9 +36,20 @@ extension HostedUiConfig on OAuthOutputs {
3536
} else {
3637
baseUri = _webDomain.replace(path: '/oauth2/authorize');
3738
}
39+
40+
final nonce = options?.nonce;
41+
final language = options?.language;
42+
final loginHint = options?.loginHint;
43+
final prompt = options?.prompt?.map((obj) => obj.value).toList().join(' ');
44+
final resource = options?.resource;
3845
return baseUri.replace(
3946
queryParameters: <String, String>{
4047
if (provider != null) 'identity_provider': provider.uriParameter,
48+
if (nonce != null) 'nonce': nonce,
49+
if (language != null) 'lang': language,
50+
if (loginHint != null) 'login_hint': loginHint,
51+
if (prompt != null) 'prompt': prompt,
52+
if (resource != null) 'resource': resource,
4153
// ignore: invalid_use_of_internal_member
4254
...?signInUriQueryParameters,
4355
},

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ abstract class HostedUiPlatform implements Closeable {
102102
@protected
103103
@visibleForTesting
104104
@nonVirtual
105-
Future<Uri> getSignInUri({Uri? redirectUri, AuthProvider? provider}) async {
105+
Future<Uri> getSignInUri({Uri? redirectUri, AuthProvider? provider, CognitoSignInWithWebUIPluginOptions? options}) async {
106106
final state = generateState();
107107
final codeVerifier = createCodeVerifier();
108108

@@ -124,6 +124,7 @@ abstract class HostedUiPlatform implements Closeable {
124124
codeVerifier: codeVerifier,
125125
httpClient: httpClient,
126126
provider: provider,
127+
options: options
127128
);
128129
final uri = _authCodeGrant!.getAuthorizationUrl(
129130
redirectUri ?? signInRedirectUri,
@@ -162,12 +163,13 @@ abstract class HostedUiPlatform implements Closeable {
162163
String userPoolClientId, {
163164
String? appClientSecret,
164165
AuthProvider? provider,
166+
CognitoSignInWithWebUIPluginOptions? options,
165167
String? codeVerifier,
166168
http.Client? httpClient,
167169
}) {
168170
return oauth2.AuthorizationCodeGrant(
169171
userPoolClientId,
170-
HostedUiConfig(authOutputs.oauth!).signInUri(provider),
172+
HostedUiConfig(authOutputs.oauth!).signInUri(provider, options),
171173
HostedUiConfig(authOutputs.oauth!).tokenUri,
172174
secret: appClientSecret,
173175
httpClient: httpClient,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class HostedUiPlatformImpl extends HostedUiPlatform {
5454
required CognitoSignInWithWebUIPluginOptions options,
5555
AuthProvider? provider,
5656
}) async {
57-
final signInUrl = (await getSignInUri(provider: provider)).toString();
57+
final signInUrl = (await getSignInUri(provider: provider, options: options)).toString();
5858
await launchUrl(signInUrl);
5959
}
6060

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class HostedUiPlatformImpl extends HostedUiPlatform {
216216
try {
217217
final signInUrl = (await getSignInUri(
218218
provider: provider,
219+
options: options,
219220
redirectUri: localServer.uri,
220221
)).toString();
221222
await launchUrl(signInUrl);

packages/auth/amplify_auth_cognito_dart/lib/src/model/signin/cognito_sign_in_with_web_ui_plugin_options.dart

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import 'package:amplify_auth_cognito_dart/src/model/signin/cognito_sign_in_with_web_ui_plugin_options_prompt.dart';
45
import 'package:amplify_core/amplify_core.dart';
56

67
part 'cognito_sign_in_with_web_ui_plugin_options.g.dart';
@@ -14,6 +15,11 @@ class CognitoSignInWithWebUIPluginOptions extends SignInWithWebUIPluginOptions {
1415
const CognitoSignInWithWebUIPluginOptions({
1516
this.isPreferPrivateSession = false,
1617
this.browserPackageName,
18+
this.nonce,
19+
this.language,
20+
this.loginHint,
21+
this.prompt,
22+
this.resource
1723
});
1824

1925
/// {@macro amplify_auth_cognito.model.cognito_sign_in_with_web_ui_plugin_options}
@@ -40,8 +46,42 @@ class CognitoSignInWithWebUIPluginOptions extends SignInWithWebUIPluginOptions {
4046
/// {@endtemplate}
4147
final String? browserPackageName;
4248

49+
/// {@template amplify_auth_cognito.model.cognito_sign_in_with_web_ui_options.nonce}
50+
/// A random value that you can add to the request. The nonce value that you provide is included in the ID token
51+
/// that Amazon Cognito issues. To guard against replay attacks, your app can inspect the nonce claim in the ID
52+
/// token and compare it to the one you generated.
53+
/// {@endtemplate}
54+
final String? nonce;
55+
56+
/// {@template amplify_auth_cognito.model.cognito_sign_in_with_web_ui_options.language}
57+
/// The language that you want to display user-interactive pages in
58+
/// For more information, see Managed login localization -
59+
/// https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-managed-login.html#managed-login-localization
60+
/// {@endtemplate}
61+
final String? language;
62+
63+
/// {@template amplify_auth_cognito.model.cognito_sign_in_with_web_ui_options.loginHint}
64+
/// A username prompt that you want to pass to the authorization server. You can collect a username, email
65+
/// address or phone number from your user and allow the destination provider to pre-populate the user's
66+
/// sign-in name.
67+
/// {@endtemplate}
68+
final String? loginHint;
69+
70+
/// {@template amplify_auth_cognito.model.cognito_sign_in_with_web_ui_options.prompt}
71+
/// An OIDC parameter that controls authentication behavior for existing sessions.
72+
/// {@endtemplate}
73+
final List<CognitoSignInWithWebUIPrompt>? prompt;
74+
75+
/// {@template amplify_auth_cognito.model.cognito_sign_in_with_web_ui_options.resource}
76+
/// The identifier of a resource that you want to bind to the access token in the `aud` claim. When you include
77+
/// this parameter, Amazon Cognito validates that the value is a URL and sets the audience of the resulting
78+
/// access token to the requested resource. Values for this parameter must begin with "https://", "http://localhost",
79+
/// or a custom URL scheme like "myapp://".
80+
/// {@endtemplate}
81+
final String? resource;
82+
4383
@override
44-
List<Object?> get props => [isPreferPrivateSession, browserPackageName];
84+
List<Object?> get props => [isPreferPrivateSession, browserPackageName, nonce, language, loginHint, prompt, resource];
4585

4686
@override
4787
String get runtimeTypeName => 'CognitoSignInWithWebUIPluginOptions';
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'package:json_annotation/json_annotation.dart';
2+
3+
/// {@macro amplify_auth_cognito.model.cognito_sign_in_with_web_ui_options.prompt}
4+
enum CognitoSignInWithWebUIPrompt {
5+
/// Amazon Cognito silently continues authentication for users who have a valid authenticated session.
6+
/// With this prompt, users can silently authenticate between different app clients in your user pool.
7+
/// If the user is not already authenticated, the authorization server returns a login_required error.
8+
@JsonValue('none')
9+
none('none'),
10+
11+
/// Amazon Cognito requires users to re-authenticate even if they have an existing session. Send this
12+
/// value when you want to verify the user's identity again. Authenticated users who have an existing
13+
/// session can return to sign-in without invalidating that session. When a user who has an existing
14+
/// session signs in again, Amazon Cognito assigns them a new session cookie. This parameter can also
15+
/// be forwarded to your IdPs. IdPs that accept this parameter also request a new authentication
16+
/// attempt from the user.
17+
@JsonValue('login')
18+
login('login'),
19+
20+
/// This value has no effect on local sign-in and must be submitted in requests that redirect to IdPs.
21+
/// When included in your authorization request, this parameter adds prompt=select_account to the URL
22+
/// path for the IdP redirect destination. When IdPs support this parameter, they request that users
23+
/// select the account that they want to log in with.
24+
@JsonValue('select_account')
25+
selectAccount('select_account'),
26+
27+
/// This value has no effect on local sign-in and must be submitted in requests that redirect to IdPs.
28+
/// When included in your authorization request, this parameter adds prompt=consent to the URL path for
29+
/// the IdP redirect destination. When IdPs support this parameter, they request user consent before
30+
/// they redirect back to your user pool.
31+
@JsonValue('consent')
32+
consent('consent');
33+
34+
const CognitoSignInWithWebUIPrompt(this.value);
35+
36+
/// String value for the enumeration
37+
final String value;
38+
}

0 commit comments

Comments
 (0)