Skip to content

Commit 4d09be2

Browse files
authored
Support unauthenticated user in JwtUserInfoExtractor (#356)
1 parent 91cde12 commit 4d09be2

File tree

14 files changed

+48
-21
lines changed

14 files changed

+48
-21
lines changed

calendar-helper/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Inspired by: https://github.com/fabric8io/ipaas-quickstarts/
2222
<parent>
2323
<groupId>org.entur.ror.helpers</groupId>
2424
<artifactId>helper</artifactId>
25-
<version>5.17.0-SNAPSHOT</version>
25+
<version>5.19.0-SNAPSHOT</version>
2626
<relativePath>..</relativePath>
2727
</parent>
2828

entur-google-pubsub/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Inspired by: https://github.com/fabric8io/ipaas-quickstarts/
2323
<parent>
2424
<groupId>org.entur.ror.helpers</groupId>
2525
<artifactId>helper</artifactId>
26-
<version>5.17.0-SNAPSHOT</version>
26+
<version>5.19.0-SNAPSHOT</version>
2727
</parent>
2828

2929
<artifactId>entur-google-pubsub</artifactId>

hazelcast4-helper/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Inspired by: https://github.com/fabric8io/ipaas-quickstarts/
2222
<parent>
2323
<groupId>org.entur.ror.helpers</groupId>
2424
<artifactId>helper</artifactId>
25-
<version>5.17.0-SNAPSHOT</version>
25+
<version>5.19.0-SNAPSHOT</version>
2626
<relativePath>..</relativePath>
2727
</parent>
2828

oauth2/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>helper</artifactId>
55
<groupId>org.entur.ror.helpers</groupId>
6-
<version>5.17.0-SNAPSHOT</version>
6+
<version>5.19.0-SNAPSHOT</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99

oauth2/src/main/java/org/entur/oauth2/user/JwtUserInfoExtractor.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.entur.oauth2.user;
22

3+
import javax.annotation.Nullable;
34
import org.rutebanken.helper.organisation.user.UserInfoExtractor;
45
import org.springframework.security.core.Authentication;
56
import org.springframework.security.core.context.SecurityContextHolder;
@@ -17,11 +18,13 @@ public class JwtUserInfoExtractor implements UserInfoExtractor {
1718
"https://ror.entur.io/preferred_username";
1819

1920
@Override
21+
@Nullable
2022
public String getPreferredName() {
2123
return getClaim(CLAIM_ROR_PREFERRED_NAME);
2224
}
2325

2426
@Override
27+
@Nullable
2528
public String getPreferredUsername() {
2629
return getClaim(CLAIM_ROR_PREFERRED_USERNAME);
2730
}
@@ -30,9 +33,12 @@ private String getClaim(String claim) {
3033
Authentication auth = SecurityContextHolder
3134
.getContext()
3235
.getAuthentication();
33-
JwtAuthenticationToken jwtAuthenticationToken =
34-
(JwtAuthenticationToken) auth;
35-
Jwt jwt = (Jwt) jwtAuthenticationToken.getPrincipal();
36-
return jwt.getClaimAsString(claim);
36+
37+
if (auth instanceof JwtAuthenticationToken jwtAuthenticationToken) {
38+
Jwt jwt = (Jwt) jwtAuthenticationToken.getPrincipal();
39+
return jwt.getClaimAsString(claim);
40+
} else {
41+
return null;
42+
}
3743
}
3844
}

organisation/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Inspired by: https://github.com/fabric8io/ipaas-quickstarts/
2222
<parent>
2323
<groupId>org.entur.ror.helpers</groupId>
2424
<artifactId>helper</artifactId>
25-
<version>5.17.0-SNAPSHOT</version>
25+
<version>5.19.0-SNAPSHOT</version>
2626
<relativePath>..</relativePath>
2727
</parent>
2828

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package org.rutebanken.helper.organisation.user;
22

3+
import javax.annotation.Nullable;
4+
35
/**
46
* Retrieve user information of the current user.
57
*/
68
public interface UserInfoExtractor {
9+
/**
10+
* Return the preferred name of the current user, or null if no user is authenticated.
11+
*/
12+
@Nullable
713
String getPreferredName();
814

15+
/**
16+
* Return the preferred username of the current user, or null if no user is authenticated.
17+
*/
18+
@Nullable
919
String getPreferredUsername();
1020
}

permission-store-proxy/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<groupId>org.entur.ror.helpers</groupId>
2121
<artifactId>helper</artifactId>
22-
<version>5.17.0-SNAPSHOT</version>
22+
<version>5.19.0-SNAPSHOT</version>
2323
</parent>
2424

2525
<properties>
@@ -38,13 +38,13 @@
3838
<dependency>
3939
<groupId>org.entur.ror.helpers</groupId>
4040
<artifactId>organisation</artifactId>
41-
<version>5.17.0-SNAPSHOT</version>
41+
<version>5.19.0-SNAPSHOT</version>
4242
</dependency>
4343

4444
<dependency>
4545
<groupId>org.entur.ror.helpers</groupId>
4646
<artifactId>oauth2</artifactId>
47-
<version>5.17.0-SNAPSHOT</version>
47+
<version>5.19.0-SNAPSHOT</version>
4848
</dependency>
4949

5050
<!--test-->

permission-store-proxy/src/main/java/org/entur/ror/permission/RemoteBabaUserInfoExtractor.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.time.Duration;
44
import java.util.List;
55
import java.util.function.Predicate;
6+
import javax.annotation.Nullable;
67
import org.rutebanken.helper.organisation.user.UserInfoExtractor;
78
import org.springframework.http.MediaType;
89
import org.springframework.security.access.AccessDeniedException;
@@ -36,13 +37,23 @@ public RemoteBabaUserInfoExtractor(WebClient webClient, String uri) {
3637
}
3738

3839
@Override
40+
@Nullable
3941
public String getPreferredName() {
40-
BabaContactDetails babaContactDetails = getBabaUser().contactDetails;
42+
BabaUser babaUser = getBabaUser();
43+
if (babaUser == null) {
44+
return null;
45+
}
46+
BabaContactDetails babaContactDetails = babaUser.contactDetails;
4147
return babaContactDetails.firstName + " " + babaContactDetails.lastName;
4248
}
4349

4450
@Override
51+
@Nullable
4552
public String getPreferredUsername() {
53+
BabaUser babaUser = getBabaUser();
54+
if (babaUser == null) {
55+
return null;
56+
}
4657
return getBabaUser().username;
4758
}
4859

@@ -53,7 +64,7 @@ private BabaUser getBabaUser() {
5364
if (
5465
!(authentication instanceof JwtAuthenticationToken jwtAuthenticationToken)
5566
) {
56-
throw new AccessDeniedException("Not authenticated with token");
67+
return null;
5768
}
5869

5970
AuthenticatedUser authenticatedUser = AuthenticatedUser.of(

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
<groupId>org.entur.ror.helpers</groupId>
2929
<artifactId>helper</artifactId>
30-
<version>5.17.0-SNAPSHOT</version>
30+
<version>5.19.0-SNAPSHOT</version>
3131
<packaging>pom</packaging>
3232

3333
<name>entur-helper</name>

0 commit comments

Comments
 (0)