| 
 | 1 | +/*******************************************************************************  | 
 | 2 | + * Copyright 2017 The MIT Internet Trust Consortium  | 
 | 3 | + *  | 
 | 4 | + * Portions copyright 2011-2013 The MITRE Corporation  | 
 | 5 | + *  | 
 | 6 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 7 | + * you may not use this file except in compliance with the License.  | 
 | 8 | + * You may obtain a copy of the License at  | 
 | 9 | + *  | 
 | 10 | + * http://www.apache.org/licenses/LICENSE-2.0  | 
 | 11 | + *  | 
 | 12 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 13 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 15 | + * See the License for the specific language governing permissions and  | 
 | 16 | + * limitations under the License.  | 
 | 17 | + *******************************************************************************/  | 
 | 18 | +package org.mitre.openid.connect.token;  | 
 | 19 | + | 
 | 20 | +import java.text.ParseException;  | 
 | 21 | + | 
 | 22 | +import org.mitre.jwt.signer.service.JWTSigningAndValidationService;  | 
 | 23 | +import org.mitre.oauth2.model.ClientDetailsEntity;  | 
 | 24 | +import org.mitre.oauth2.model.OAuth2AccessTokenEntity;  | 
 | 25 | +import org.mitre.oauth2.service.ClientDetailsEntityService;  | 
 | 26 | +import org.mitre.openid.connect.config.ConfigurationPropertiesBean;  | 
 | 27 | +import org.mitre.openid.connect.model.UserInfo;  | 
 | 28 | +import org.mitre.openid.connect.service.OIDCTokenService;  | 
 | 29 | +import org.mitre.openid.connect.service.UserInfoService;  | 
 | 30 | +import org.springframework.security.oauth2.provider.OAuth2Authentication;  | 
 | 31 | +import org.springframework.security.oauth2.provider.OAuth2Request;  | 
 | 32 | + | 
 | 33 | +import org.junit.Assert;  | 
 | 34 | +import org.junit.Before;  | 
 | 35 | +import org.junit.Test;  | 
 | 36 | +import org.junit.runner.RunWith;  | 
 | 37 | +import org.mockito.InjectMocks;  | 
 | 38 | +import org.mockito.Mock;  | 
 | 39 | +import org.mockito.Mockito;  | 
 | 40 | +import org.mockito.runners.MockitoJUnitRunner;  | 
 | 41 | + | 
 | 42 | +import com.nimbusds.jose.JWSAlgorithm;  | 
 | 43 | +import com.nimbusds.jwt.JWT;  | 
 | 44 | +import com.nimbusds.jwt.JWTClaimsSet.Builder;  | 
 | 45 | + | 
 | 46 | +@RunWith(MockitoJUnitRunner.class)  | 
 | 47 | +public class TestConnectTokenEnhancer {  | 
 | 48 | + | 
 | 49 | +private static final String CLIENT_ID = "client";  | 
 | 50 | +private static final String KEY_ID = "key";  | 
 | 51 | + | 
 | 52 | +private ConfigurationPropertiesBean configBean = new ConfigurationPropertiesBean();  | 
 | 53 | + | 
 | 54 | +@Mock  | 
 | 55 | +private JWTSigningAndValidationService jwtService;  | 
 | 56 | + | 
 | 57 | +@Mock  | 
 | 58 | +private ClientDetailsEntityService clientService;  | 
 | 59 | + | 
 | 60 | +@Mock  | 
 | 61 | +private UserInfoService userInfoService;  | 
 | 62 | + | 
 | 63 | +@Mock  | 
 | 64 | +private OIDCTokenService connectTokenService;  | 
 | 65 | + | 
 | 66 | +@Mock  | 
 | 67 | +private OAuth2Authentication authentication;  | 
 | 68 | + | 
 | 69 | +private OAuth2Request request = new OAuth2Request(CLIENT_ID) { };  | 
 | 70 | + | 
 | 71 | +@InjectMocks  | 
 | 72 | +private ConnectTokenEnhancer enhancer = new ConnectTokenEnhancer();  | 
 | 73 | + | 
 | 74 | +@Before  | 
 | 75 | +public void prepare() {  | 
 | 76 | +configBean.setIssuer("https://auth.example.org/");  | 
 | 77 | +enhancer.setConfigBean(configBean);  | 
 | 78 | + | 
 | 79 | +ClientDetailsEntity client = new ClientDetailsEntity();  | 
 | 80 | +client.setClientId(CLIENT_ID);  | 
 | 81 | +Mockito.when(clientService.loadClientByClientId(Mockito.anyString())).thenReturn(client);  | 
 | 82 | +Mockito.when(authentication.getOAuth2Request()).thenReturn(request);  | 
 | 83 | +Mockito.when(jwtService.getDefaultSigningAlgorithm()).thenReturn(JWSAlgorithm.RS256);  | 
 | 84 | +Mockito.when(jwtService.getDefaultSignerKeyId()).thenReturn(KEY_ID);  | 
 | 85 | +}  | 
 | 86 | + | 
 | 87 | +@Test  | 
 | 88 | +public void invokesCustomClaimsHook() throws ParseException {  | 
 | 89 | +configure(enhancer = new ConnectTokenEnhancer() {  | 
 | 90 | +@Override  | 
 | 91 | +protected void addCustomAccessTokenClaims(Builder builder, OAuth2AccessTokenEntity token,  | 
 | 92 | + OAuth2Authentication authentication) {  | 
 | 93 | +builder.claim("test", "foo");  | 
 | 94 | +}  | 
 | 95 | +});  | 
 | 96 | + | 
 | 97 | +OAuth2AccessTokenEntity token = new OAuth2AccessTokenEntity();  | 
 | 98 | + | 
 | 99 | +OAuth2AccessTokenEntity enhanced = (OAuth2AccessTokenEntity) enhancer.enhance(token, authentication);  | 
 | 100 | +Assert.assertEquals("foo", enhanced.getJwt().getJWTClaimsSet().getClaim("test"));  | 
 | 101 | +}  | 
 | 102 | + | 
 | 103 | +private void configure(ConnectTokenEnhancer e) {  | 
 | 104 | +e.setConfigBean(configBean);  | 
 | 105 | +e.setJwtService(jwtService);  | 
 | 106 | +e.setClientService(clientService);  | 
 | 107 | +}  | 
 | 108 | +}  | 
0 commit comments