Skip to content

Commit 5f9e03c

Browse files
committed
verify signature and claims
1 parent c18dd29 commit 5f9e03c

3 files changed

Lines changed: 45 additions & 18 deletions

File tree

‎spring-security-openid/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
<version>1.0.9.RELEASE</version>
4747
</dependency>
4848

49+
<dependency>
50+
<groupId>com.auth0</groupId>
51+
<artifactId>jwks-rsa</artifactId>
52+
<version>0.3.0</version>
53+
</dependency>
4954
</dependencies>
5055

5156
<repositories>

‎spring-security-openid/src/main/java/org/baeldung/security/OpenIdConnectFilter.java‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package org.baeldung.security;
22

33
import java.io.IOException;
4-
import java.math.BigInteger;
4+
import java.net.URL;
5+
import java.security.interfaces.RSAPublicKey;
6+
import java.util.Date;
57
import java.util.Map;
68

79
import javax.servlet.ServletException;
810
import javax.servlet.http.HttpServletRequest;
911
import javax.servlet.http.HttpServletResponse;
1012

11-
import org.apache.commons.codec.binary.Base64;
13+
import org.springframework.beans.factory.annotation.Value;
1214
import org.springframework.security.authentication.AuthenticationManager;
1315
import org.springframework.security.authentication.BadCredentialsException;
1416
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -20,13 +22,24 @@
2022
import org.springframework.security.oauth2.client.OAuth2RestOperations;
2123
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
2224
import org.springframework.security.oauth2.common.OAuth2AccessToken;
23-
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
2425
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
2526
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
2627

28+
import com.auth0.jwk.Jwk;
29+
import com.auth0.jwk.JwkProvider;
30+
import com.auth0.jwk.UrlJwkProvider;
2731
import com.fasterxml.jackson.databind.ObjectMapper;
2832

2933
public class OpenIdConnectFilter extends AbstractAuthenticationProcessingFilter {
34+
@Value("${google.clientId}")
35+
private String clientId;
36+
37+
@Value("${google.issuer}")
38+
private String issuer;
39+
40+
@Value("${google.jwkUrl}")
41+
private String jwkUrl;
42+
3043
public OAuth2RestOperations restTemplate;
3144

3245
public OpenIdConnectFilter(String defaultFilterProcessesUrl) {
@@ -45,31 +58,38 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
4558
}
4659
try {
4760
final String idToken = accessToken.getAdditionalInformation().get("id_token").toString();
48-
final Jwt tokenDecoded = JwtHelper.decodeAndVerify(idToken, verifier());
49-
System.out.println("===== : " + tokenDecoded.getClaims());
50-
61+
String kid = JwtHelper.headers(idToken)
62+
.get("kid");
63+
final Jwt tokenDecoded = JwtHelper.decodeAndVerify(idToken, verifier(kid));
5164
final Map<String, String> authInfo = new ObjectMapper().readValue(tokenDecoded.getClaims(), Map.class);
52-
65+
verifyClaims(authInfo);
5366
final OpenIdConnectUserDetails user = new OpenIdConnectUserDetails(authInfo, accessToken);
5467
return new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
55-
} catch (final InvalidTokenException e) {
68+
} catch (final Exception e) {
5669
throw new BadCredentialsException("Could not obtain user details from token", e);
5770
}
5871

5972
}
6073

61-
public void setRestTemplate(OAuth2RestTemplate restTemplate2) {
62-
restTemplate = restTemplate2;
74+
public void verifyClaims(Map claims) {
75+
int exp = (int) claims.get("exp");
76+
Date expireDate = new Date(exp * 1000L);
77+
Date now = new Date();
78+
if (expireDate.before(now) || !claims.get("iss").equals(issuer) || !claims.get("aud").equals(clientId)) {
79+
throw new RuntimeException("Invalid claims");
80+
}
81+
}
82+
6383

84+
private RsaVerifier verifier(String kid) throws Exception {
85+
JwkProvider provider = new UrlJwkProvider(new URL(jwkUrl));
86+
Jwk jwk = provider.get(kid);
87+
return new RsaVerifier((RSAPublicKey) jwk.getPublicKey());
6488
}
6589

66-
// details can be found at https://www.googleapis.com/oauth2/v2/certs
67-
private RsaVerifier verifier() {
68-
byte[] nbytes = Base64.decodeBase64("vmyoDT6ND_YJa1ItdvULuTJr2pw4MvN3Z5kmSiJBm9glVoakcDEBGF4b5crKiPW7WDh2PZ0_yXY9ikDaTux7hxtgUtmm96KjmdBn_FYwv3SlsBRnzZw1oAG-2OdjlFWvlx4rXOhAzZ04ngPb3ELywwtKoO90hCy2DrNOMMSCuSu8zrFLw5oREawPcUFEQReipy_KRFf02VxFbK4Tj2FHVdBPPLW3W1KJD4S-NNwPnoeDrI6zWMv7WWAeSLAT0hX36r5FM9dM2uXTxPRCZzs-nqrUiHxn4duFIGgzuxCVbyigDrnfsmHx-B5tG1m7ts74xwf2P_PJwNNJ8qRihMsS2Q==");
69-
byte[] ebytes = Base64.decodeBase64("AQAB");
70-
BigInteger n = new BigInteger(1, nbytes);
71-
BigInteger e = new BigInteger(1, ebytes);
72-
return new RsaVerifier(n, e);
90+
public void setRestTemplate(OAuth2RestTemplate restTemplate2) {
91+
restTemplate = restTemplate2;
92+
7393
}
7494

7595
private static class NoopAuthenticationManager implements AuthenticationManager {

‎spring-security-openid/src/main/resources/application.properties.sample.properties‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ google.clientId=TODO
33
google.clientSecret=TODO
44
google.accessTokenUri=https://www.googleapis.com/oauth2/v3/token
55
google.userAuthorizationUri=https://accounts.google.com/o/oauth2/auth
6-
google.redirectUri=http://localhost:8081/google-login
6+
google.redirectUri=http://localhost:8081/google-login
7+
google.issuer=accounts.google.com
8+
google.jwkUrl=https://www.googleapis.com/oauth2/v2/certs

0 commit comments

Comments
 (0)