|
| 1 | +/* Copyright 2025-2025 the original author or authors. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package grails.plugin.springsecurity.oauth2.oidc |
| 17 | + |
| 18 | +import com.auth0.jwk.JwkProviderBuilder |
| 19 | +import com.auth0.jwt.JWT |
| 20 | +import com.auth0.jwt.JWTVerifier |
| 21 | +import com.auth0.jwt.algorithms.Algorithm |
| 22 | +import com.auth0.jwt.exceptions.JWTVerificationException |
| 23 | +import com.auth0.jwt.interfaces.DecodedJWT |
| 24 | +import com.auth0.jwt.interfaces.RSAKeyProvider |
| 25 | +import com.github.scribejava.core.builder.ServiceBuilder |
| 26 | +import com.github.scribejava.core.builder.api.DefaultApi20 |
| 27 | +import com.github.scribejava.core.model.OAuth2AccessToken |
| 28 | +import com.github.scribejava.core.oauth.OAuth20Service |
| 29 | +import grails.plugin.springsecurity.oauth2.SpringSecurityOauth2BaseService |
| 30 | +import grails.plugin.springsecurity.oauth2.exception.OAuth2Exception |
| 31 | +import grails.plugin.springsecurity.oauth2.service.OAuth2AbstractProviderService |
| 32 | +import grails.plugin.springsecurity.oauth2.token.OAuth2SpringToken |
| 33 | +import grails.plugin.springsecurity.oauth2.util.OAuth2ProviderConfiguration |
| 34 | +import groovy.json.JsonSlurper |
| 35 | +import groovy.transform.CompileStatic |
| 36 | +import org.springframework.beans.factory.InitializingBean |
| 37 | + |
| 38 | +import java.security.interfaces.RSAPrivateKey |
| 39 | +import java.security.interfaces.RSAPublicKey |
| 40 | +import java.util.concurrent.TimeUnit |
| 41 | + |
| 42 | +@CompileStatic |
| 43 | +class OidcAuth2Service extends OAuth2AbstractProviderService implements InitializingBean{ |
| 44 | + |
| 45 | + SpringSecurityOauth2BaseService springSecurityOauth2BaseService |
| 46 | + |
| 47 | + OidcOauth2Config OidcOauth2Config |
| 48 | + |
| 49 | + final String providerID = OidcOauth2Api.PROVIDER_ID |
| 50 | + final Class apiClass = OidcOauth2Api |
| 51 | + final String scopeSeparator = ' ' |
| 52 | + private JWTVerifier jwtVerifier |
| 53 | + |
| 54 | + String getProfileScope() { |
| 55 | + "${OidcOauth2Config.domain}/userinfo" |
| 56 | + } |
| 57 | + String getScopes() { |
| 58 | + OidcOauth2Config.scope |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Override |
| 63 | + OAuth2SpringToken createSpringAuthToken(OAuth2AccessToken accessToken) { |
| 64 | + DecodedJWT decoded = decodeJWT(accessToken) |
| 65 | + verifyJWT(decoded) |
| 66 | + return new OidcOauth2SpringToken(accessToken, decoded.claims) |
| 67 | + } |
| 68 | + |
| 69 | + DecodedJWT decodeJWT(OAuth2AccessToken accessToken) { |
| 70 | + def raw = new JsonSlurper().parseText(accessToken.rawResponse as String) as Map |
| 71 | + String idToken = raw?.id_token as String |
| 72 | + if (!idToken) throw new IllegalStateException("Missing id_token from OIDC response") |
| 73 | + |
| 74 | + DecodedJWT decoded = new JWT().decodeJwt(idToken) |
| 75 | + return decoded |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + OAuth20Service buildScribeService(OAuth2ProviderConfiguration providerConfiguration) { |
| 80 | + DefaultApi20 api = new OidcOauth2Api(OidcOauth2Config.domain) |
| 81 | + |
| 82 | + return new ServiceBuilder(OidcOauth2Config.apiKey) |
| 83 | + .apiSecret(OidcOauth2Config.apiSecret) |
| 84 | + .defaultScope(OidcOauth2Config.scope) |
| 85 | + .callback(providerConfiguration.callbackUrl) |
| 86 | + .build(api) |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + @Override |
| 91 | + void afterPropertiesSet() throws Exception { |
| 92 | + def jwkProvider = new JwkProviderBuilder(OidcOauth2Config.domain) |
| 93 | + .cached(10, 24, TimeUnit.HOURS) |
| 94 | + .rateLimited(10, 1, TimeUnit.MINUTES) |
| 95 | + .build() |
| 96 | + |
| 97 | + RSAKeyProvider keyProvider = new RSAKeyProvider() { |
| 98 | + @Override |
| 99 | + RSAPublicKey getPublicKeyById(String kid) { |
| 100 | + return (RSAPublicKey) jwkProvider.get(kid).getPublicKey() |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + RSAPrivateKey getPrivateKey() { |
| 105 | + // return the private key used |
| 106 | + return null |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + String getPrivateKeyId() { |
| 111 | + return null |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + def algorithm = Algorithm.RSA256(keyProvider) |
| 116 | + String issuerDomain = OidcOauth2Config.domain.endsWith('/') ? OidcOauth2Config.domain : OidcOauth2Config.domain + '/' |
| 117 | + jwtVerifier = JWT.require(algorithm) |
| 118 | + .withIssuer(issuerDomain) |
| 119 | + .withAnyOfAudience(OidcOauth2Config.apiKey) |
| 120 | + .build() |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + private void verifyJWT(DecodedJWT decoded) { |
| 125 | + try { |
| 126 | + jwtVerifier.verify(decoded) |
| 127 | + } catch (JWTVerificationException e) { |
| 128 | + throw new OAuth2Exception("OIDC token is invalid", e) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments