Skip to content

Commit 13320d0

Browse files
committed
feat(backend): Personalize private info message using UserInfo claims
1 parent b1e3592 commit 13320d0

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

backend/src/main/java/ai/bluefields/oidcauthdemo/service/PrivateInfoService.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,32 @@ public Mono<PrivateInfoResponse> getInfo(Authentication authentication) {
5353
.bodyToMono(Map.class) // Assuming response is a JSON object
5454
.map(
5555
userInfoMap -> {
56-
// Extract email, handle potential null or incorrect type
56+
// Extract claims, handle potential null or incorrect type
5757
Object emailObj = userInfoMap.get("email");
5858
String email = (emailObj instanceof String) ? (String) emailObj : "Email not found";
59-
return new PrivateInfoResponse("Hello AUTH (from UserInfo)", email);
59+
60+
Object givenNameObj = userInfoMap.get("given_name");
61+
String givenName = (givenNameObj instanceof String) ? (String) givenNameObj : "User";
62+
63+
Object familyNameObj = userInfoMap.get("family_name");
64+
String familyName =
65+
(familyNameObj instanceof String)
66+
? (String) familyNameObj
67+
: ""; // Default to empty if missing
68+
69+
// Construct message carefully to avoid double spaces if familyName is empty
70+
String fullName = givenName + (familyName.isEmpty() ? "" : " " + familyName);
71+
String message = String.format("Hello %s (from UserInfo)", fullName.trim());
72+
73+
return new PrivateInfoResponse(message, email);
6074
})
6175
.onErrorResume(
6276
error -> {
6377
// Log the error appropriately in a real application
6478
System.err.println("Error fetching UserInfo: " + error.getMessage());
6579
return Mono.just(
66-
new PrivateInfoResponse("Hello AUTH (UserInfo Error)", "Error fetching email"));
80+
new PrivateInfoResponse(
81+
"Hello User (UserInfo Error)", "Error fetching user details"));
6782
});
6883
}
6984
}

backend/src/test/java/ai/bluefields/oidcauthdemo/service/PrivateInfoServiceTest.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,17 @@ void setUp() throws MalformedURLException {
6666
}
6767

6868
@Test
69-
void getInfo_shouldReturnResponseWithEmailFromUserInfo_whenSuccessful() {
69+
void getInfo_shouldReturnPersonalizedResponse_whenUserInfoSuccessful() {
7070
// Arrange
7171
String testEmail = "userinfo@example.com";
72+
String testGivenName = "John";
73+
String testFamilyName = "Doe";
7274
String expectedUserInfoUrl = "http://mock-issuer.com/oidc/v1/userinfo";
73-
Map<String, Object> userInfoResponseMap = Map.of("email", testEmail);
75+
Map<String, Object> userInfoResponseMap =
76+
Map.of(
77+
"email", testEmail,
78+
"given_name", testGivenName,
79+
"family_name", testFamilyName);
7480

7581
// Mock the final bodyToMono call for success
7682
when(responseSpec.bodyToMono(Map.class)).thenReturn(Mono.just(userInfoResponseMap));
@@ -83,7 +89,8 @@ void getInfo_shouldReturnResponseWithEmailFromUserInfo_whenSuccessful() {
8389
.assertNext(
8490
response -> {
8591
assertThat(response).isNotNull();
86-
assertThat(response.message()).isEqualTo("Hello AUTH (from UserInfo)");
92+
assertThat(response.message())
93+
.isEqualTo("Hello John Doe (from UserInfo)"); // Updated assertion
8794
assertThat(response.email()).isEqualTo(testEmail);
8895
})
8996
.verifyComplete();
@@ -113,8 +120,10 @@ void getInfo_shouldReturnErrorResponse_whenUserInfoCallFails() {
113120
.assertNext(
114121
response -> {
115122
assertThat(response).isNotNull();
116-
assertThat(response.message()).isEqualTo("Hello AUTH (UserInfo Error)");
117-
assertThat(response.email()).isEqualTo("Error fetching email");
123+
assertThat(response.message())
124+
.isEqualTo("Hello User (UserInfo Error)"); // Updated assertion
125+
assertThat(response.email())
126+
.isEqualTo("Error fetching user details"); // Updated assertion
118127
})
119128
.verifyComplete(); // onErrorResume handles the error, so the Mono completes
120129

@@ -149,11 +158,11 @@ void getInfo_shouldReturnErrorMono_whenAuthenticationIsNotJwt() {
149158
}
150159

151160
@Test
152-
void getInfo_shouldReturnDefaultEmail_whenEmailMissingInUserInfo() {
161+
void getInfo_shouldReturnDefaultValues_whenClaimsMissingInUserInfo() { // Renamed test
153162
// Arrange
154-
Map<String, Object> userInfoResponseMap = Collections.emptyMap(); // No email claim
163+
Map<String, Object> userInfoResponseMap = Collections.emptyMap(); // No email or name claims
155164

156-
// Mock the final bodyToMono call for success with missing email
165+
// Mock the final bodyToMono call for success with missing claims
157166
when(responseSpec.bodyToMono(Map.class)).thenReturn(Mono.just(userInfoResponseMap));
158167

159168
// Act
@@ -164,8 +173,9 @@ void getInfo_shouldReturnDefaultEmail_whenEmailMissingInUserInfo() {
164173
.assertNext(
165174
response -> {
166175
assertThat(response).isNotNull();
167-
assertThat(response.message()).isEqualTo("Hello AUTH (from UserInfo)");
168-
assertThat(response.email()).isEqualTo("Email not found"); // Default value
176+
assertThat(response.message())
177+
.isEqualTo("Hello User (from UserInfo)"); // Updated assertion for default message
178+
assertThat(response.email()).isEqualTo("Email not found"); // Default email value
169179
})
170180
.verifyComplete();
171181
}

0 commit comments

Comments
 (0)