Skip to content

Commit 694dac0

Browse files
authored
feat(auth): add support for cognito oidc parameters in managed login (#3158)
1 parent 56c2469 commit 694dac0

File tree

8 files changed

+312
-6
lines changed

8 files changed

+312
-6
lines changed

aws-auth-cognito/api/aws-auth-cognito.api

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,11 @@ public final class com/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUI
611611
public fun equals (Ljava/lang/Object;)Z
612612
public fun getBrowserPackage ()Ljava/lang/String;
613613
public fun getIdpIdentifier ()Ljava/lang/String;
614+
public fun getLanguage ()Ljava/lang/String;
615+
public fun getLoginHint ()Ljava/lang/String;
616+
public fun getNonce ()Ljava/lang/String;
617+
public fun getPrompt ()Ljava/util/List;
618+
public fun getResource ()Ljava/lang/String;
614619
public fun hashCode ()I
615620
public fun toString ()Ljava/lang/String;
616621
}
@@ -623,6 +628,11 @@ public final class com/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUI
623628
public fun getThis ()Lcom/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUISignInOptions$CognitoBuilder;
624629
public synthetic fun getThis ()Lcom/amplifyframework/auth/options/AuthWebUISignInOptions$Builder;
625630
public fun idpIdentifier (Ljava/lang/String;)Lcom/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUISignInOptions$CognitoBuilder;
631+
public fun language (Ljava/lang/String;)Lcom/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUISignInOptions$CognitoBuilder;
632+
public fun loginHint (Ljava/lang/String;)Lcom/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUISignInOptions$CognitoBuilder;
633+
public fun nonce (Ljava/lang/String;)Lcom/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUISignInOptions$CognitoBuilder;
634+
public fun prompt ([Lcom/amplifyframework/auth/cognito/options/AuthWebUIPrompt;)Lcom/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUISignInOptions$CognitoBuilder;
635+
public fun resource (Ljava/lang/String;)Lcom/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUISignInOptions$CognitoBuilder;
626636
}
627637

628638
public final class com/amplifyframework/auth/cognito/options/AuthFlowType : java/lang/Enum {
@@ -636,6 +646,17 @@ public final class com/amplifyframework/auth/cognito/options/AuthFlowType : java
636646
public static fun values ()[Lcom/amplifyframework/auth/cognito/options/AuthFlowType;
637647
}
638648

649+
public final class com/amplifyframework/auth/cognito/options/AuthWebUIPrompt : java/lang/Enum {
650+
public static final field CONSENT Lcom/amplifyframework/auth/cognito/options/AuthWebUIPrompt;
651+
public static final field LOGIN Lcom/amplifyframework/auth/cognito/options/AuthWebUIPrompt;
652+
public static final field NONE Lcom/amplifyframework/auth/cognito/options/AuthWebUIPrompt;
653+
public static final field SELECT_ACCOUNT Lcom/amplifyframework/auth/cognito/options/AuthWebUIPrompt;
654+
public static fun getEntries ()Lkotlin/enums/EnumEntries;
655+
public final fun getValue ()Ljava/lang/String;
656+
public static fun valueOf (Ljava/lang/String;)Lcom/amplifyframework/auth/cognito/options/AuthWebUIPrompt;
657+
public static fun values ()[Lcom/amplifyframework/auth/cognito/options/AuthWebUIPrompt;
658+
}
659+
639660
public final class com/amplifyframework/auth/cognito/options/FederateToIdentityPoolOptions {
640661
public static final field Companion Lcom/amplifyframework/auth/cognito/options/FederateToIdentityPoolOptions$Companion;
641662
public static final fun builder ()Lcom/amplifyframework/auth/cognito/options/FederateToIdentityPoolOptions$CognitoBuilder;

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/HostedUIClient.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,31 @@ internal class HostedUIClient private constructor(
164164
builder.appendQueryParameter("scope", it)
165165
}
166166

167+
// check if nonce is set as param.
168+
hostedUIOptions.nonce?.takeIf { it.isNotEmpty() }?.let {
169+
builder.appendQueryParameter("nonce", it)
170+
}
171+
172+
// check if language is set as param.
173+
hostedUIOptions.language?.takeIf { it.isNotEmpty() }?.let {
174+
builder.appendQueryParameter("lang", it)
175+
}
176+
177+
// check if loginHint is set as param.
178+
hostedUIOptions.loginHint?.takeIf { it.isNotEmpty() }?.let {
179+
builder.appendQueryParameter("login_hint", it)
180+
}
181+
182+
// check if prompt is set as param.
183+
hostedUIOptions.prompt?.joinToString(" ") { it.value }.let {
184+
builder.appendQueryParameter("prompt", it)
185+
}
186+
187+
// check if resource is set as param.
188+
hostedUIOptions.resource?.takeIf { it.isNotEmpty() }?.let {
189+
builder.appendQueryParameter("resource", it)
190+
}
191+
167192
return builder.build()
168193
}
169194

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/helpers/HostedUIHelper.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ internal object HostedUIHelper {
3636
idpIdentifier = (options as? AWSCognitoAuthWebUISignInOptions)?.idpIdentifier
3737
),
3838
browserPackage = (options as? AWSCognitoAuthWebUISignInOptions)?.browserPackage,
39-
preferPrivateSession = options.preferPrivateSession
39+
preferPrivateSession = options.preferPrivateSession,
40+
nonce = (options as? AWSCognitoAuthWebUISignInOptions)?.nonce,
41+
language = (options as? AWSCognitoAuthWebUISignInOptions)?.language,
42+
loginHint = (options as? AWSCognitoAuthWebUISignInOptions)?.loginHint,
43+
prompt = (options as? AWSCognitoAuthWebUISignInOptions)?.prompt,
44+
resource = (options as? AWSCognitoAuthWebUISignInOptions)?.resource
4045
)
4146

4247
/**

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/options/AWSCognitoAuthWebUISignInOptions.java

Lines changed: 168 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.amplifyframework.auth.options.AuthWebUISignInOptions;
2323
import com.amplifyframework.util.Immutable;
2424

25+
import java.util.ArrayList;
2526
import java.util.List;
2627

2728
/**
@@ -30,6 +31,11 @@
3031
public final class AWSCognitoAuthWebUISignInOptions extends AuthWebUISignInOptions {
3132
private final String idpIdentifier;
3233
private final String browserPackage;
34+
private final String nonce;
35+
private final String language;
36+
private final String loginHint;
37+
private final List<AuthWebUIPrompt> prompt;
38+
private final String resource;
3339

3440
/**
3541
* Advanced options for signing in via a hosted web ui.
@@ -38,16 +44,33 @@ public final class AWSCognitoAuthWebUISignInOptions extends AuthWebUISignInOptio
3844
* @param browserPackage Specify which browser package should be used for web sign in (e.g. "org.mozilla.firefox").
3945
* Defaults to the Chrome package if not specified.
4046
* @param preferPrivateSession specifying whether or not to launch web ui in an ephemeral CustomTab.
47+
* @param nonce random value that can be added to the request, which is included in the ID token
48+
* that Amazon Cognito issues.
49+
* @param language language displayed in user-interactive page
50+
* @param loginHint username prompt passed to the authorization server
51+
* @param prompt a list of OIDC parameters that controls authentication behavior for existing sessions.
52+
* @param resource identifier of a resource that you want to bind to the access token in the `aud` claim.
4153
*/
54+
@SuppressWarnings("checkstyle:all")
4255
protected AWSCognitoAuthWebUISignInOptions(
4356
List<String> scopes,
4457
String idpIdentifier,
4558
String browserPackage,
46-
Boolean preferPrivateSession
59+
Boolean preferPrivateSession,
60+
String nonce,
61+
String language,
62+
String loginHint,
63+
List<AuthWebUIPrompt> prompt,
64+
String resource
4765
) {
4866
super(scopes, preferPrivateSession);
4967
this.idpIdentifier = idpIdentifier;
5068
this.browserPackage = browserPackage;
69+
this.nonce = nonce;
70+
this.language = language;
71+
this.loginHint = loginHint;
72+
this.prompt = prompt;
73+
this.resource = resource;
5174
}
5275

5376
/**
@@ -68,6 +91,58 @@ public String getBrowserPackage() {
6891
return browserPackage;
6992
}
7093

94+
/**
95+
* Optional A random value that can be added to the request, which is included in the ID token
96+
* that Amazon Cognito issues. To guard against replay attacks, your app can inspect the nonce claim in the ID
97+
* token and compare it to the one you generated.
98+
* @return the nonce value
99+
*/
100+
@Nullable
101+
public String getNonce() {
102+
return nonce;
103+
}
104+
105+
/** Optional The language displayed in user-interactive page.
106+
* For more information, see Managed login localization
107+
* https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-managed-login.html
108+
* @return the language value
109+
*/
110+
@Nullable
111+
public String getLanguage() {
112+
return language;
113+
}
114+
115+
/** Optional A username prompt passed to the authorization server. You can collect a username, email
116+
* address or phone number from your user and allow the destination provider to pre-populate the user's
117+
* sign-in name.
118+
* @return the login prompt displayed in the username field
119+
*/
120+
@Nullable
121+
public String getLoginHint() {
122+
return loginHint;
123+
}
124+
125+
/**
126+
* Optional An OIDC parameter that controls authentication behavior for existing sessions.
127+
* @return the prompt value
128+
*/
129+
@Nullable
130+
public List<AuthWebUIPrompt> getPrompt() {
131+
return prompt;
132+
}
133+
134+
/**
135+
* Optional The identifier of a resource that you want to bind to the access token in the `aud`
136+
* claim. When this parameter is included, Amazon Cognito validates that the value is a URL and
137+
* sets the audience of the resulting access token to the requested resource. Values for this
138+
* parameter must begin with "https://", "http://localhost" or a custom URL scheme like "myapp://".
139+
* @return the resource value
140+
*/
141+
@Nullable
142+
public String getResource() {
143+
return resource;
144+
}
145+
71146
/**
72147
* Returns a builder for this object.
73148
* @return a builder for this object.
@@ -83,7 +158,12 @@ public int hashCode() {
83158
getScopes(),
84159
getIdpIdentifier(),
85160
getBrowserPackage(),
86-
getPreferPrivateSession()
161+
getPreferPrivateSession(),
162+
getNonce(),
163+
getLanguage(),
164+
getLoginHint(),
165+
getPrompt(),
166+
getResource()
87167
);
88168
}
89169

@@ -98,7 +178,12 @@ public boolean equals(Object obj) {
98178
return ObjectsCompat.equals(getScopes(), webUISignInOptions.getScopes()) &&
99179
ObjectsCompat.equals(getIdpIdentifier(), webUISignInOptions.getIdpIdentifier()) &&
100180
ObjectsCompat.equals(getBrowserPackage(), webUISignInOptions.getBrowserPackage()) &&
101-
ObjectsCompat.equals(getPreferPrivateSession(), webUISignInOptions.getPreferPrivateSession());
181+
ObjectsCompat.equals(getPreferPrivateSession(), webUISignInOptions.getPreferPrivateSession()) &&
182+
ObjectsCompat.equals(getNonce(), webUISignInOptions.getNonce()) &&
183+
ObjectsCompat.equals(getLanguage(), webUISignInOptions.getLanguage()) &&
184+
ObjectsCompat.equals(getLoginHint(), webUISignInOptions.getLoginHint()) &&
185+
ObjectsCompat.equals(getPrompt(), webUISignInOptions.getPrompt()) &&
186+
ObjectsCompat.equals(getResource(), webUISignInOptions.getResource());
102187
}
103188
}
104189

@@ -109,6 +194,11 @@ public String toString() {
109194
", idpIdentifier=" + getIdpIdentifier() +
110195
", browserPackage=" + getBrowserPackage() +
111196
", preferPrivateSession=" + getPreferPrivateSession() +
197+
", nonce=" + getNonce() +
198+
", language=" + getLanguage() +
199+
", loginHint=" + getLoginHint() +
200+
", prompt=" + getPrompt() +
201+
", resource=" + getResource() +
112202
'}';
113203
}
114204

@@ -118,6 +208,11 @@ public String toString() {
118208
public static final class CognitoBuilder extends Builder<CognitoBuilder> {
119209
private String idpIdentifier;
120210
private String browserPackage;
211+
private String nonce;
212+
private String language;
213+
private String loginHint;
214+
private List<AuthWebUIPrompt> prompt;
215+
private String resource;
121216

122217
/**
123218
* Constructs the builder.
@@ -146,6 +241,70 @@ public CognitoBuilder idpIdentifier(@NonNull String idpIdentifier) {
146241
return getThis();
147242
}
148243

244+
/**
245+
* A random value that can be added to the request, which is included in the ID token
246+
* that Amazon Cognito issues.
247+
* @param nonce a random value to be added to the request
248+
* @return the instance of the builder.
249+
*/
250+
@NonNull
251+
public CognitoBuilder nonce(@NonNull String nonce) {
252+
this.nonce = nonce;
253+
return getThis();
254+
}
255+
256+
/** The language displayed in user-interactive page.
257+
* For more information, see Managed login localization
258+
* https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-managed-login.html
259+
* @param language language value
260+
* @return the instance of the builder.
261+
*/
262+
@NonNull
263+
public CognitoBuilder language(@NonNull String language) {
264+
this.language = language;
265+
return getThis();
266+
}
267+
268+
/** A username prompt passed to the authorization server. You can collect a username, email
269+
* address or phone number from your user and allow the destination provider to pre-populate the user's
270+
* sign-in name.
271+
* @param loginHint login prompt to pass to authorization server
272+
* @return the instance of the builder.
273+
*/
274+
@NonNull
275+
public CognitoBuilder loginHint(@NonNull String loginHint) {
276+
this.loginHint = loginHint;
277+
return getThis();
278+
}
279+
280+
/**
281+
* Optional An OIDC parameter that controls authentication behavior for existing sessions.
282+
* @param prompt list of AuthWebUIPrompt values
283+
* @return the instance of the builder.
284+
*/
285+
@NonNull
286+
public CognitoBuilder prompt(AuthWebUIPrompt... prompt) {
287+
this.prompt = new ArrayList<>();
288+
for (AuthWebUIPrompt value : prompt) {
289+
this.prompt.add(value);
290+
}
291+
return getThis();
292+
}
293+
294+
/**
295+
* Optional The identifier of a resource that you want to bind to the access token in the `aud`
296+
* claim. When this parameter is included, Amazon Cognito validates that the value is a URL and
297+
* sets the audience of the resulting access token to the requested resource. Values for this
298+
* parameter must begin with "https://", "http://localhost" or a custom URL scheme like "myapp://".
299+
* @param resource resource value
300+
* @return the instance of the builder.
301+
*/
302+
@NonNull
303+
public CognitoBuilder resource(@NonNull String resource) {
304+
this.resource = resource;
305+
return getThis();
306+
}
307+
149308
/**
150309
* This can optionally be set to specify which browser package should perform the sign in action
151310
* (e.g. "org.mozilla.firefox"). Defaults to the Chrome package if not set.
@@ -168,7 +327,12 @@ public AWSCognitoAuthWebUISignInOptions build() {
168327
Immutable.of(super.getScopes()),
169328
idpIdentifier,
170329
browserPackage,
171-
super.getPreferPrivateSession()
330+
super.getPreferPrivateSession(),
331+
nonce,
332+
language,
333+
loginHint,
334+
prompt,
335+
resource
172336
);
173337
}
174338
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.amplifyframework.auth.cognito.options
2+
3+
/**
4+
* An OIDC parameter that controls authentication behavior for existing sessions.
5+
*/
6+
public enum class AuthWebUIPrompt(val value: String) {
7+
/**
8+
* Amazon Cognito silently continues authentication for users who have a valid authenticated session.
9+
* With this prompt, users can silently authenticate between different app clients in your user pool.
10+
* If the user is not already authenticated, the authorization server returns a login_required error.
11+
*/
12+
NONE(value = "none"),
13+
14+
/**
15+
* Amazon Cognito requires users to re-authenticate even if they have an existing session. Send this
16+
* value when you want to verify the user's identity again. Authenticated users who have an existing
17+
* session can return to sign-in without invalidating that session. When a user who has an existing
18+
* session signs in again, Amazon Cognito assigns them a new session cookie. This parameter can also
19+
* be forwarded to your IdPs. IdPs that accept this parameter also request a new authentication
20+
* attempt from the user.
21+
*/
22+
LOGIN(value = "login"),
23+
24+
/**
25+
* This value has no effect on local sign-in and must be submitted in requests that redirect to IdPs.
26+
* When included in your authorization request, this parameter adds prompt=select_account to the URL
27+
* path for the IdP redirect destination. When IdPs support this parameter, they request that users
28+
* select the account that they want to log in with.
29+
*/
30+
SELECT_ACCOUNT(value = "select_account"),
31+
32+
/**
33+
* This value has no effect on local sign-in and must be submitted in requests that redirect to IdPs.
34+
* When included in your authorization request, this parameter adds prompt=consent to the URL path for
35+
* the IdP redirect destination. When IdPs support this parameter, they request user consent before
36+
* they redirect back to your user pool.
37+
*/
38+
CONSENT(value = "consent")
39+
}

aws-auth-cognito/src/main/java/com/amplifyframework/statemachine/codegen/data/HostedUIOptions.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
package com.amplifyframework.statemachine.codegen.data
1717

1818
import android.app.Activity
19+
import com.amplifyframework.auth.cognito.options.AuthWebUIPrompt
1920

2021
internal data class HostedUIOptions(
2122
val callingActivity: Activity,
2223
val scopes: List<String>?,
2324
val providerInfo: HostedUIProviderInfo,
2425
val browserPackage: String?,
25-
val preferPrivateSession: Boolean?
26+
val preferPrivateSession: Boolean?,
27+
val nonce: String?,
28+
val language: String?,
29+
val loginHint: String?,
30+
val prompt: List<AuthWebUIPrompt>?,
31+
val resource: String?
2632
)

0 commit comments

Comments
 (0)