Skip to content

Commit 683102d

Browse files
authored
support signin and signup userflow (Azure#18690)
- add signIn and signUp flow for aadb2c - b2c code refactor
1 parent 513dbec commit 683102d

File tree

9 files changed

+69
-86
lines changed

9 files changed

+69
-86
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,10 @@ azure:
5252
sign-up-or-sign-in: ${your-sign-up-or-in-user-flow}
5353
profile-edit: ${your-profile-edit-user-flow} # optional
5454
password-reset: ${your-password-reset-user-flow} # optional
55+
sign-in: ${your-sign-in-user-flow} # optional
56+
sign-up: ${your-sign-up-user-flow} # optional
5557
```
5658
57-
#### Templates greeting.html and home.html
58-
1. Fill in the `${your-profile-edit-user-flow}` and `${your-password-reset-user-flow}` from the portal `User flows`.
59-
Please make sure that these two placeholders should be the same as `application.yml` respectively.
60-
6159
### Run with Maven
6260
```
6361
cd azure-spring-boot-samples/azure-spring-boot-sample-active-directory-b2c-oidc

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: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package com.azure.spring.sample.aad.b2c.controller;
55

6+
import com.azure.spring.autoconfigure.b2c.AADB2CProperties;
7+
import org.springframework.beans.factory.annotation.Autowired;
68
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
79
import org.springframework.security.oauth2.core.user.OAuth2User;
810
import org.springframework.stereotype.Controller;
@@ -12,6 +14,9 @@
1214
@Controller
1315
public class WebController {
1416

17+
@Autowired
18+
AADB2CProperties aadb2CProperties;
19+
1520
private void initializeModel(Model model, OAuth2AuthenticationToken token) {
1621
if (token != null) {
1722
final OAuth2User user = token.getPrincipal();
@@ -20,26 +25,14 @@ private void initializeModel(Model model, OAuth2AuthenticationToken token) {
2025
model.addAllAttributes(user.getAttributes());
2126
model.addAttribute("name", user.getName());
2227
}
28+
model.addAttribute("aadb2c_profileedit", aadb2CProperties.getUserFlows().getProfileEdit());
29+
model.addAttribute("aadb2c_passwordreset", aadb2CProperties.getUserFlows().getPasswordReset());
2330
}
2431

25-
@GetMapping(value = "/")
32+
@GetMapping(value = { "/", "/home", "/greeting" })
2633
public String index(Model model, OAuth2AuthenticationToken token) {
2734
initializeModel(model, token);
28-
2935
return "home";
3036
}
3137

32-
@GetMapping(value = "/greeting")
33-
public String greeting(Model model, OAuth2AuthenticationToken token) {
34-
initializeModel(model, token);
35-
36-
return "greeting";
37-
}
38-
39-
@GetMapping(value = "/home")
40-
public String home(Model model, OAuth2AuthenticationToken token) {
41-
initializeModel(model, token);
42-
43-
return "home";
44-
}
4538
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ azure:
1212
sign-up-or-sign-in: ${your-sign-up-or-in-user-flow}
1313
profile-edit: ${your-profile-edit-user-flow} # optional
1414
password-reset: ${your-password-reset-user-flow} # optional
15+
sign-in: ${your-sign-in-user-flow} # optional
16+
sign-up: ${your-sign-up-user-flow} # optional

sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-active-directory-b2c-oidc/src/main/resources/templates/greeting.html

Lines changed: 0 additions & 62 deletions
This file was deleted.

sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-active-directory-b2c-oidc/src/main/resources/templates/home.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ <h1>Home Page</h1>
1919
<button class="btn btn-md btn-primary btn-block" type="submit">Log out</button>
2020
</form>
2121

22-
<form style="display:inline" method="get" action="/oauth2/authorization/${your-profile-edit-user-flow}">
22+
<form th:if="${aadb2c_profileedit != null}" style="display:inline" method="get"
23+
th:action="${'/oauth2/authorization/' + aadb2c_profileedit}">
2324
<button class="btn btn-md btn-primary btn-block" type="submit">Profile edit</button>
2425
</form>
2526

26-
<form style="display:inline" method="get" action="/oauth2/authorization/${your-password-reset-user-flow}">
27+
<form th:if="${aadb2c_passwordreset != null}" style="display:inline" method="get"
28+
th:action="${'/oauth2/authorization/' + aadb2c_passwordreset}">
2729
<button class="btn btn-md btn-primary btn-block" type="submit">Password reset</button>
2830
</form>
2931
</div>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ private void addB2CClientRegistration(@NonNull List<ClientRegistration> registra
124124
@Bean
125125
@ConditionalOnMissingBean
126126
public ClientRegistrationRepository clientRegistrationRepository() {
127-
final List<ClientRegistration> signUpOrSignInRegistrations = new ArrayList<>(1);
127+
final List<ClientRegistration> signUpOrSignInRegistrations = new ArrayList<>(3);
128128
final List<ClientRegistration> otherRegistrations = new ArrayList<>();
129129

130-
131130
addB2CClientRegistration(signUpOrSignInRegistrations, properties.getUserFlows().getSignUpOrSignIn());
131+
addB2CClientRegistration(signUpOrSignInRegistrations, properties.getUserFlows().getSignIn());
132+
addB2CClientRegistration(signUpOrSignInRegistrations, properties.getUserFlows().getSignUp());
132133
addB2CClientRegistration(otherRegistrations, properties.getUserFlows().getProfileEdit());
133134
addB2CClientRegistration(otherRegistrations, properties.getUserFlows().getPasswordReset());
134135

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public class AADB2CProperties {
3232

3333
public static final String USER_FLOW_SIGN_UP_OR_SIGN_IN = USER_FLOWS + ".sign-up-or-sign-in";
3434

35+
public static final String USER_FLOW_SIGN_UP = USER_FLOWS + ".sign-up";
36+
37+
public static final String USER_FLOW_SIGN_IN = USER_FLOWS + ".sign-in";
38+
3539
public static final String DEFAULT_LOGOUT_SUCCESS_URL = "http://localhost:8080/login";
3640

3741
public static final String PREFIX = "azure.activedirectory.b2c";
@@ -96,8 +100,11 @@ public String getLoginProcessingUrl() {
96100
return getReplyURLPath(replyUrl);
97101
}
98102

103+
/**
104+
* UserFlows
105+
*/
99106
@Validated
100-
protected static class UserFlows {
107+
public static class UserFlows {
101108

102109
protected UserFlows() {
103110

@@ -119,6 +126,32 @@ protected UserFlows() {
119126
*/
120127
private String passwordReset;
121128

129+
/**
130+
* The sign-up user flow which is created under b2c tenant.
131+
*/
132+
private String signUp;
133+
134+
/**
135+
* The sign-in user flow which is created under b2c tenant.
136+
*/
137+
private String signIn;
138+
139+
public String getSignUp() {
140+
return signUp;
141+
}
142+
143+
public void setSignUp(String signUp) {
144+
this.signUp = signUp;
145+
}
146+
147+
public String getSignIn() {
148+
return signIn;
149+
}
150+
151+
public void setSignIn(String signIn) {
152+
this.signIn = signIn;
153+
}
154+
122155
public String getSignUpOrSignIn() {
123156
return signUpOrSignIn;
124157
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class AADB2CAutoConfigurationTest {
2020
String.format("%s=%s", AADB2CConstants.REPLY_URL, AADB2CConstants.TEST_REPLY_URL),
2121
String.format("%s=%s", AADB2CConstants.LOGOUT_SUCCESS_URL, AADB2CConstants.TEST_LOGOUT_SUCCESS_URL),
2222
String.format("%s=%s", AADB2CConstants.SIGN_UP_OR_SIGN_IN, AADB2CConstants.TEST_SIGN_UP_OR_IN_NAME),
23+
String.format("%s=%s", AADB2CConstants.SIGN_UP, AADB2CConstants.TEST_SIGN_UP_NAME),
24+
String.format("%s=%s", AADB2CConstants.SIGN_IN, AADB2CConstants.TEST_SIGN_IN_NAME),
2325
String.format("%s=%s", AADB2CConstants.CONFIG_PROMPT, AADB2CConstants.TEST_PROMPT),
2426
String.format("%s=%s", AADB2CConstants.CONFIG_LOGIN_HINT, AADB2CConstants.TEST_LOGIN_HINT),
2527
String.format("%s=%s", AADB2CConstants.USER_NAME_ATTRIBUTE_NAME, AADB2CConstants.TEST_ATTRIBUTE_NAME)
@@ -47,11 +49,15 @@ public void testPropertiesBean() {
4749
assertThat(properties.getUserNameAttributeName()).isEqualTo(AADB2CConstants.TEST_ATTRIBUTE_NAME);
4850

4951
final String signUpOrSignIn = properties.getUserFlows().getSignUpOrSignIn();
52+
final String signIn = properties.getUserFlows().getSignIn();
53+
final String signUp = properties.getUserFlows().getSignUp();
5054
final Object prompt = properties.getAuthenticateAdditionalParameters().get(AADB2CConstants.PROMPT);
5155
final String loginHint =
5256
String.valueOf(properties.getAuthenticateAdditionalParameters().get(AADB2CConstants.LOGIN_HINT));
5357

5458
assertThat(signUpOrSignIn).isEqualTo(AADB2CConstants.TEST_SIGN_UP_OR_IN_NAME);
59+
assertThat(signIn).isEqualTo(AADB2CConstants.TEST_SIGN_IN_NAME);
60+
assertThat(signUp).isEqualTo(AADB2CConstants.TEST_SIGN_UP_NAME);
5561
assertThat(prompt).isEqualTo(AADB2CConstants.TEST_PROMPT);
5662
assertThat(loginHint).isEqualTo(AADB2CConstants.TEST_LOGIN_HINT);
5763
});

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55

66
import static com.azure.spring.autoconfigure.b2c.AADB2CProperties.PREFIX;
7+
import static com.azure.spring.autoconfigure.b2c.AADB2CProperties.USER_FLOW_SIGN_IN;
8+
import static com.azure.spring.autoconfigure.b2c.AADB2CProperties.USER_FLOW_SIGN_UP;
79
import static com.azure.spring.autoconfigure.b2c.AADB2CProperties.USER_FLOW_SIGN_UP_OR_SIGN_IN;
810

911
public class AADB2CConstants {
@@ -30,6 +32,10 @@ public class AADB2CConstants {
3032

3133
public static final String TEST_SIGN_UP_OR_IN_NAME = "fake-sign-in-or-up";
3234

35+
public static final Object TEST_SIGN_IN_NAME = "fake-sign-in";
36+
37+
public static final Object TEST_SIGN_UP_NAME = "fake-sign-up";
38+
3339
public static final String TEST_LOGOUT_SUCCESS_URL = "https://fake-logout-success-url";
3440

3541
public static final String TENANT = String.format("%s.%s", PREFIX, "tenant");
@@ -48,6 +54,10 @@ public class AADB2CConstants {
4854

4955
public static final String SIGN_UP_OR_SIGN_IN = String.format("%s.%s", PREFIX, USER_FLOW_SIGN_UP_OR_SIGN_IN);
5056

57+
public static final String SIGN_UP = String.format("%s.%s", PREFIX, USER_FLOW_SIGN_UP);
58+
59+
public static final String SIGN_IN = String.format("%s.%s", PREFIX, USER_FLOW_SIGN_IN);
60+
5161
public static final Object CONFIG_PROMPT = String.format("%s.%s", PREFIX,
5262
AUTHENTICATE_ADDITIONAL_PARAMETERS_PROMPT);
5363

0 commit comments

Comments
 (0)