Skip to content

Commit 417ad40

Browse files
ThomasVitalejgrandja
authored andcommitted
Add generic getClaim() method in ClaimAccessor
Fixes gh-6947
1 parent 59dcc36 commit 417ad40

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/ClaimAccessor.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ public interface ClaimAccessor {
3939
*/
4040
Map<String, Object> getClaims();
4141

42+
/**
43+
* Returns the claim value as a {@code T} type.
44+
* The claim value is expected to be of type {@code T}.
45+
*
46+
* @since 5.2
47+
* @param claim the name of the claim
48+
* @param <T> the type of the claim value
49+
* @return the claim value
50+
*/
51+
@SuppressWarnings("unchecked")
52+
default <T> T getClaim(String claim) {
53+
return !containsClaim(claim) ? null : (T) getClaims().get(claim);
54+
}
55+
4256
/**
4357
* Returns {@code true} if the claim exists in {@link #getClaims()}, otherwise {@code false}.
4458
*

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,11 +19,14 @@
1919
import org.junit.Test;
2020

2121
import java.time.Instant;
22+
import java.util.Arrays;
2223
import java.util.Date;
2324
import java.util.HashMap;
25+
import java.util.List;
2426
import java.util.Map;
2527

2628
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.assertj.core.api.Assertions.catchThrowable;
2730

2831
/**
2932
* Tests for {@link ClaimAccessor}.
@@ -101,4 +104,44 @@ public void getClaimAsStringWhenValueIsNullThenReturnNull() {
101104

102105
assertThat(this.claimAccessor.getClaimAsString(claimName)).isNull();
103106
}
107+
108+
@Test
109+
public void getClaimWhenNotExistingThenReturnNull() {
110+
String claimName = "list";
111+
List<String> actualClaimValue = this.claimAccessor.getClaim(claimName);
112+
assertThat(actualClaimValue).isNull();
113+
}
114+
115+
@Test
116+
public void getClaimWhenValueIsConvertedThenReturnList() {
117+
List<String> expectedClaimValue = Arrays.asList("item1", "item2");
118+
String claimName = "list";
119+
this.claims.put(claimName, expectedClaimValue);
120+
121+
List<String> actualClaimValue = this.claimAccessor.getClaim(claimName);
122+
123+
assertThat(actualClaimValue).containsOnlyElementsOf(expectedClaimValue);
124+
}
125+
126+
@Test
127+
public void getClaimWhenValueIsConvertedThenReturnBoolean() {
128+
boolean expectedClaimValue = true;
129+
String claimName = "boolean";
130+
this.claims.put(claimName, expectedClaimValue);
131+
132+
boolean actualClaimValue = this.claimAccessor.getClaim(claimName);
133+
134+
assertThat(actualClaimValue).isEqualTo(expectedClaimValue);
135+
}
136+
137+
@Test
138+
public void getClaimWhenValueIsNotConvertedThenThrowClassCastException() {
139+
String expectedClaimValue = "true";
140+
String claimName = "boolean";
141+
this.claims.put(claimName, expectedClaimValue);
142+
143+
Throwable thrown = catchThrowable(() -> { boolean actualClaimValue = this.claimAccessor.getClaim(claimName); });
144+
145+
assertThat(thrown).isInstanceOf(ClassCastException.class);
146+
}
104147
}

0 commit comments

Comments
 (0)