Skip to content

Commit 0f84386

Browse files
authored
Allow configure username-attribute-name for aad b2c (Azure#18657)
1 parent 3718f83 commit 0f84386

File tree

9 files changed

+57
-27
lines changed

9 files changed

+57
-27
lines changed

sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-active-directory-b2c-oidc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ azure:
4747
client-secret: ${your-client-secret}
4848
reply-url: ${your-reply-url} # should be absolute url.
4949
logout-success-url: ${your-logout-success-url}
50+
user-name-attribute-name: ${your-user-name-claim}
5051
user-flows:
5152
sign-up-or-sign-in: ${your-sign-up-or-in-user-flow}
5253
profile-edit: ${your-profile-edit-user-flow} # optional

sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-active-directory-b2c-oidc/src/main/java/com/azure/spring/sample/aad/b2c/controller/WebController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ private void initializeModel(Model model, OAuth2AuthenticationToken token) {
1818

1919
model.addAttribute("grant_type", user.getAuthorities());
2020
model.addAllAttributes(user.getAttributes());
21+
model.addAttribute("name", user.getName());
2122
}
2223
}
2324

sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-active-directory-b2c-oidc/src/main/resources/application.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ azure:
66
client-id: ${your-client-id}
77
client-secret: ${your-client-secret}
88
reply-url: ${your-reply-url-from-aad} # should be absolute url.
9-
logout-success-url: ${you-logout-success-url}
9+
logout-success-url: ${your-logout-success-url}
10+
user-name-attribute-name: ${your-user-name-claim}
1011
user-flows:
1112
sign-up-or-sign-in: ${your-sign-up-or-in-user-flow}
1213
profile-edit: ${your-profile-edit-user-flow} # optional

sdk/spring/azure-spring-boot-starter-active-directory-b2c/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Release History
22

33
## 3.2.0-beta.1 (Unreleased)
4-
4+
### Breaking Changes
5+
- Exposed `userNameAttributeName` to configure the user's name.
56

67
## 3.0.0 (2020-12-30)
78

sdk/spring/azure-spring-boot-starter-active-directory-b2c/README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ while protecting the identities of your customers at the same time.
4848

4949
3. Select **Keys** from your application, click **Generate key** to generate `${your-client-secret}` and then **Save**.
5050

51-
4. Select **User flows** on your left, and then **Click** **New user flow **.
51+
4. Select **User flows** on your left, and then Click **New user flow**.
5252

5353
5. Choose **Sign up or in**, **Profile editing** and **Password reset** to create user flows
5454
respectively. Specify your user flow **Name** and **User attributes and claims**, click **Create**.
@@ -58,6 +58,22 @@ In this documentation, you created a new Java web application using the Azure Ac
5858
configured a new Azure AD B2C tenant and registered a new application in it, and then configured your
5959
application to use the Spring annotations and classes to protect the web app.
6060

61+
### Configurable properties
62+
This starter provides following properties to be customized:
63+
64+
65+
| Parameter | Description |
66+
|---|---|
67+
| `azure.activedirectory.b2c.client-id` | The registered application ID in Azure AD B2C. |
68+
| `azure.activedirectory.b2c.client-secret` | The client secret of a registered application. |
69+
| `azure.activedirectory.b2c.logout-success-url` | The target URL after a successful logout. |
70+
| `azure.activedirectory.b2c.reply-url` | The reply URL of a registered application. It's the same as the **Redirect URI** configured on Azure Portal.|
71+
| `azure.activedirectory.b2c.tenant` | The Azure AD B2C's tenant name. |
72+
| `azure.activedirectory.b2c.user-flows.signUpOrSignIn` | The name of the **sign up and sign in** user flow. |
73+
| `azure.activedirectory.b2c.user-flows.profileEdit` | The name of the **profile editing** user flow. |
74+
| `azure.activedirectory.b2c.user-flows.passwordReset` | The name of the **password reset** user flow. |
75+
| `azure.activedirectory.b2c.user-name-attribute-name` | The the attribute name of the user name.|
76+
6177
## Examples
6278
### Configure and compile your app
6379

@@ -97,6 +113,7 @@ application to use the Spring annotations and classes to protect the web app.
97113
client-secret: ${your-client-secret}
98114
reply-url: ${your-reply-url-from-aad} # should be absolute url.
99115
logout-success-url: ${you-logout-success-url}
116+
user-name-attribute-name: ${your-user-name-attribute-name}
100117
user-flows:
101118
sign-up-or-sign-in: ${your-sign-up-or-in-user-flow}
102119
profile-edit: ${your-profile-edit-user-flow} # optional
@@ -106,17 +123,6 @@ application to use the Spring annotations and classes to protect the web app.
106123
login_hint: xxxxxxxxx # optional
107124
domain_hint: xxxxxxxxx # optional
108125
```
109-
Where:
110-
111-
| Parameter | Description |
112-
|---|---|
113-
| `azure.activedirectory.b2c.tenant` | Contains your AD B2C's `${your-tenant-name` from earlier. |
114-
| `azure.activedirectory.b2c.client-id` | Contains the `${your-client-id}` from your application that you completed earlier. |
115-
| `azure.activedirectory.b2c.client-secret` | Contains the `${your-client-secret}` from your application that you completed earlier. |
116-
| `azure.activedirectory.b2c.reply-url` | Contains one of the **Reply URL** from your application that you completed earlier. |
117-
| `azure.activedirectory.b2c.logout-success-url` | Specify the URL when your application logout successfully. |
118-
| `azure.activedirectory.b2c.user-flows` | Contains the name of the user flows that you completed earlier.
119-
120126
7. Save and close the *application.yml* file.
121127
122128
8. Create a folder named *controller* in the Java source folder for your application.

sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/b2c/AADB2CAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ private ClientRegistration b2cClientRegistration(String userFlow) {
148148
.authorizationUri(AADB2CURL.getAuthorizationUrl(properties.getTenant()))
149149
.tokenUri(AADB2CURL.getTokenUrl(properties.getTenant(), userFlow))
150150
.jwkSetUri(AADB2CURL.getJwkSetUrl(properties.getTenant(), userFlow))
151-
.userNameAttributeName("name")
151+
.userNameAttributeName(properties.getUserNameAttributeName())
152152
.clientName(userFlow)
153153
.build();
154154
}

sdk/spring/azure-spring-boot/src/main/java/com/azure/spring/autoconfigure/b2c/AADB2CProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public class AADB2CProperties {
6868

6969
private Map<String, Object> authenticateAdditionalParameters;
7070

71+
/**
72+
* User name attribute name
73+
*/
74+
private String userNameAttributeName;
75+
7176
/**
7277
* The all user flows which is created under b2c tenant.
7378
*/
@@ -210,4 +215,12 @@ public boolean isAllowTelemetry() {
210215
public void setAllowTelemetry(boolean allowTelemetry) {
211216
this.allowTelemetry = allowTelemetry;
212217
}
218+
219+
public String getUserNameAttributeName() {
220+
return userNameAttributeName;
221+
}
222+
223+
public void setUserNameAttributeName(String userNameAttributeName) {
224+
this.userNameAttributeName = userNameAttributeName;
225+
}
213226
}

sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/b2c/AADB2CAutoConfigurationTest.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
public class AADB2CAutoConfigurationTest {
1313

1414
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
15-
.withConfiguration(AutoConfigurations.of(AADB2CAutoConfiguration.class))
16-
.withPropertyValues(
17-
String.format("%s=%s", AADB2CConstants.TENANT, AADB2CConstants.TEST_TENANT),
18-
String.format("%s=%s", AADB2CConstants.CLIENT_ID, AADB2CConstants.TEST_CLIENT_ID),
19-
String.format("%s=%s", AADB2CConstants.CLIENT_SECRET, AADB2CConstants.TEST_CLIENT_SECRET),
20-
String.format("%s=%s", AADB2CConstants.REPLY_URL, AADB2CConstants.TEST_REPLY_URL),
21-
String.format("%s=%s", AADB2CConstants.LOGOUT_SUCCESS_URL, AADB2CConstants.TEST_LOGOUT_SUCCESS_URL),
22-
String.format("%s=%s", AADB2CConstants.SIGN_UP_OR_SIGN_IN, AADB2CConstants.TEST_SIGN_UP_OR_IN_NAME),
23-
String.format("%s=%s", AADB2CConstants.CONFIG_PROMPT, AADB2CConstants.TEST_PROMPT),
24-
String.format("%s=%s", AADB2CConstants.CONFIG_LOGIN_HINT, AADB2CConstants.TEST_LOGIN_HINT)
25-
);
15+
.withConfiguration(AutoConfigurations.of(AADB2CAutoConfiguration.class))
16+
.withPropertyValues(
17+
String.format("%s=%s", AADB2CConstants.TENANT, AADB2CConstants.TEST_TENANT),
18+
String.format("%s=%s", AADB2CConstants.CLIENT_ID, AADB2CConstants.TEST_CLIENT_ID),
19+
String.format("%s=%s", AADB2CConstants.CLIENT_SECRET, AADB2CConstants.TEST_CLIENT_SECRET),
20+
String.format("%s=%s", AADB2CConstants.REPLY_URL, AADB2CConstants.TEST_REPLY_URL),
21+
String.format("%s=%s", AADB2CConstants.LOGOUT_SUCCESS_URL, AADB2CConstants.TEST_LOGOUT_SUCCESS_URL),
22+
String.format("%s=%s", AADB2CConstants.SIGN_UP_OR_SIGN_IN, AADB2CConstants.TEST_SIGN_UP_OR_IN_NAME),
23+
String.format("%s=%s", AADB2CConstants.CONFIG_PROMPT, AADB2CConstants.TEST_PROMPT),
24+
String.format("%s=%s", AADB2CConstants.CONFIG_LOGIN_HINT, AADB2CConstants.TEST_LOGIN_HINT),
25+
String.format("%s=%s", AADB2CConstants.USER_NAME_ATTRIBUTE_NAME, AADB2CConstants.TEST_ATTRIBUTE_NAME)
26+
);
2627

2728
@Test
2829
public void testAutoConfigurationBean() {
@@ -43,10 +44,12 @@ public void testPropertiesBean() {
4344
assertThat(properties.getClientId()).isEqualTo(AADB2CConstants.TEST_CLIENT_ID);
4445
assertThat(properties.getClientSecret()).isEqualTo(AADB2CConstants.TEST_CLIENT_SECRET);
4546
assertThat(properties.getReplyUrl()).isEqualTo(AADB2CConstants.TEST_REPLY_URL);
47+
assertThat(properties.getUserNameAttributeName()).isEqualTo(AADB2CConstants.TEST_ATTRIBUTE_NAME);
4648

4749
final String signUpOrSignIn = properties.getUserFlows().getSignUpOrSignIn();
4850
final Object prompt = properties.getAuthenticateAdditionalParameters().get(AADB2CConstants.PROMPT);
49-
final String loginHint = String.valueOf(properties.getAuthenticateAdditionalParameters().get(AADB2CConstants.LOGIN_HINT));
51+
final String loginHint =
52+
String.valueOf(properties.getAuthenticateAdditionalParameters().get(AADB2CConstants.LOGIN_HINT));
5053

5154
assertThat(signUpOrSignIn).isEqualTo(AADB2CConstants.TEST_SIGN_UP_OR_IN_NAME);
5255
assertThat(prompt).isEqualTo(AADB2CConstants.TEST_PROMPT);

sdk/spring/azure-spring-boot/src/test/java/com/azure/spring/autoconfigure/b2c/AADB2CConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public class AADB2CConstants {
3434

3535
public static final String TENANT = String.format("%s.%s", PREFIX, "tenant");
3636

37+
public static final String TEST_ATTRIBUTE_NAME = String.format("%s.%s", PREFIX, "name");
38+
39+
public static final String USER_NAME_ATTRIBUTE_NAME = String.format("%s.%s", PREFIX, "user-name-attribute-name");
40+
3741
public static final String CLIENT_ID = String.format("%s.%s", PREFIX, "client-id");
3842

3943
public static final String CLIENT_SECRET = String.format("%s.%s", PREFIX, "client-secret");

0 commit comments

Comments
 (0)