|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2018 The MIT Internet Trust Consortium |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + *******************************************************************************/ |
| 16 | +package org.mitre.openid.connect.service.impl; |
| 17 | + |
| 18 | +import java.util.Date; |
| 19 | + |
| 20 | +import org.mitre.jwt.signer.service.JWTSigningAndValidationService; |
| 21 | +import org.mitre.oauth2.model.ClientDetailsEntity; |
| 22 | +import org.mitre.oauth2.model.OAuth2AccessTokenEntity; |
| 23 | +import org.mitre.openid.connect.config.ConfigurationPropertiesBean; |
| 24 | +import org.springframework.security.oauth2.provider.OAuth2Request; |
| 25 | + |
| 26 | +import com.nimbusds.jose.JWSAlgorithm; |
| 27 | +import com.nimbusds.jwt.JWT; |
| 28 | +import com.nimbusds.jwt.JWTClaimsSet; |
| 29 | + |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.mockito.InjectMocks; |
| 35 | +import org.mockito.Mock; |
| 36 | +import org.mockito.Mockito; |
| 37 | +import org.mockito.runners.MockitoJUnitRunner; |
| 38 | + |
| 39 | +@RunWith(MockitoJUnitRunner.class) |
| 40 | +public class TestDefaultOIDCTokenService { |
| 41 | + private static final String CLIENT_ID = "client"; |
| 42 | + private static final String KEY_ID = "key"; |
| 43 | + |
| 44 | + private ConfigurationPropertiesBean configBean = new ConfigurationPropertiesBean(); |
| 45 | + private ClientDetailsEntity client = new ClientDetailsEntity(); |
| 46 | + private OAuth2AccessTokenEntity accessToken = new OAuth2AccessTokenEntity(); |
| 47 | + private OAuth2Request request = new OAuth2Request(CLIENT_ID) { }; |
| 48 | + |
| 49 | + @Mock |
| 50 | + private JWTSigningAndValidationService jwtService; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void prepare() { |
| 54 | + configBean.setIssuer("https://auth.example.org/"); |
| 55 | + |
| 56 | + client.setClientId(CLIENT_ID); |
| 57 | + Mockito.when(jwtService.getDefaultSigningAlgorithm()).thenReturn(JWSAlgorithm.RS256); |
| 58 | + Mockito.when(jwtService.getDefaultSignerKeyId()).thenReturn(KEY_ID); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void invokesCustomClaimsHook() throws java.text.ParseException { |
| 63 | + DefaultOIDCTokenService s = new DefaultOIDCTokenService() { |
| 64 | + @Override |
| 65 | + protected void addCustomIdTokenClaims(JWTClaimsSet.Builder idClaims, ClientDetailsEntity client, OAuth2Request request, |
| 66 | + String sub, OAuth2AccessTokenEntity accessToken) { |
| 67 | + idClaims.claim("test", "foo"); |
| 68 | + } |
| 69 | + }; |
| 70 | + configure(s); |
| 71 | + |
| 72 | + JWT token = s.createIdToken(client, request, new Date(), "sub", accessToken); |
| 73 | + Assert.assertEquals("foo", token.getJWTClaimsSet().getClaim("test")); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + private void configure(DefaultOIDCTokenService s) { |
| 78 | + s.setConfigBean(configBean); |
| 79 | + s.setJwtService(jwtService); |
| 80 | + } |
| 81 | +} |
0 commit comments