|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Google Inc. |
| 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 | + |
| 17 | +package org.oidc.service.oidc; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Date; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.junit.Assert; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.Test; |
| 26 | +import org.oidc.common.MissingRequiredAttributeException; |
| 27 | +import org.oidc.common.ValueException; |
| 28 | +import org.oidc.msg.InvalidClaimException; |
| 29 | +import org.oidc.msg.oidc.RegistrationRequest; |
| 30 | +import org.oidc.msg.oidc.RegistrationResponse; |
| 31 | +import org.oidc.service.BaseServiceTest; |
| 32 | +import org.oidc.service.base.HttpArguments; |
| 33 | +import org.oidc.service.base.ServiceContext; |
| 34 | + |
| 35 | +/** |
| 36 | + * Unit tests for {@link Registration} service. |
| 37 | + */ |
| 38 | +public class RegistrationTest extends BaseServiceTest<Registration> { |
| 39 | + |
| 40 | + ServiceContext serviceContext; |
| 41 | + String clientId; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void init() { |
| 45 | + serviceContext = new ServiceContext(); |
| 46 | + service = new Registration(serviceContext, null, null); |
| 47 | + clientId = "mock_rp"; |
| 48 | + } |
| 49 | + |
| 50 | + @Test(expected = InvalidClaimException.class) |
| 51 | + public void testNoRedirectUris() throws Exception { |
| 52 | + service.setEndpoint("https://www.example.com/registration"); |
| 53 | + HttpArguments httpArguments = service.getRequestParameters(null); |
| 54 | + RegistrationRequest request = new RegistrationRequest(); |
| 55 | + request.fromJson(httpArguments.getBody()); |
| 56 | + request.verify(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test(expected = MissingRequiredAttributeException.class) |
| 60 | + public void testHttpParametersNoUrl() throws Exception { |
| 61 | + RegistrationResponse behaviour = new RegistrationResponse(); |
| 62 | + serviceContext.setBehavior(behaviour); |
| 63 | + service.getRequestParameters(null); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testUpdateContextMinimal() |
| 68 | + throws MissingRequiredAttributeException, ValueException, InvalidClaimException { |
| 69 | + service.updateServiceContext(buildMinimalResponse()); |
| 70 | + Assert.assertNotNull(serviceContext.getClientId()); |
| 71 | + Assert.assertEquals(clientId, serviceContext.getClientId()); |
| 72 | + Map<String, Object> claims = ((Registration) service).getResponseMessage().getClaims(); |
| 73 | + Assert.assertEquals(3, claims.size()); |
| 74 | + assertMinimal(claims); |
| 75 | + Assert.assertEquals("client_secret_basic", claims.get("token_endpoint_auth_method")); |
| 76 | + } |
| 77 | + |
| 78 | + @Test(expected = InvalidClaimException.class) |
| 79 | + public void testUpdateContextWrongResponseMsgContents() |
| 80 | + throws MissingRequiredAttributeException, ValueException, InvalidClaimException { |
| 81 | + RegistrationResponse response = buildMinimalResponse(); |
| 82 | + response.addClaim("client_id_issued_at", "should be a Date"); |
| 83 | + service.updateServiceContext(response); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testUpdateContextSpec() |
| 88 | + throws MissingRequiredAttributeException, ValueException, InvalidClaimException { |
| 89 | + service.updateServiceContext(buildSpecExampleResponse()); |
| 90 | + Assert.assertNotNull(serviceContext.getClientId()); |
| 91 | + Assert.assertEquals("s6BhdRkqt3", serviceContext.getClientId()); |
| 92 | + Assert.assertEquals("ZJYCqe3GGRvdrudKyZS0XhGv_Z45DuKhCUk0gBR1vZk", |
| 93 | + serviceContext.getClientSecret()); |
| 94 | + Assert.assertEquals(new Date(1577858400000L), serviceContext.getClientSecretExpiresAt()); |
| 95 | + Assert.assertEquals("this.is.an.access.token.value.ffx83", |
| 96 | + serviceContext.getRegistrationAccessToken()); |
| 97 | + } |
| 98 | + |
| 99 | + protected void assertMinimal(Map<String, Object> claims) { |
| 100 | + Assert.assertEquals(Arrays.asList("https://rp.example.org"), claims.get("redirect_uris")); |
| 101 | + Assert.assertEquals(clientId, claims.get("client_id")); |
| 102 | + } |
| 103 | + |
| 104 | + protected RegistrationResponse buildMinimalResponse() { |
| 105 | + RegistrationResponse response = new RegistrationResponse(); |
| 106 | + response.addClaim("redirect_uris", Arrays.asList("https://rp.example.org")); |
| 107 | + response.addClaim("client_id", clientId); |
| 108 | + return response; |
| 109 | + } |
| 110 | + |
| 111 | + protected RegistrationResponse buildSpecExampleResponse() throws InvalidClaimException { |
| 112 | + String json = "{\n" + " \"client_id\": \"s6BhdRkqt3\",\n" + " \"client_secret\":\n" |
| 113 | + + " \"ZJYCqe3GGRvdrudKyZS0XhGv_Z45DuKhCUk0gBR1vZk\",\n" |
| 114 | + + " \"client_secret_expires_at\": 1577858400,\n" + " \"registration_access_token\":\n" |
| 115 | + + " \"this.is.an.access.token.value.ffx83\",\n" + " \"registration_client_uri\":\n" |
| 116 | + + " \"https://server.example.com/connect/register?client_id=s6BhdRkqt3\",\n" |
| 117 | + + " \"token_endpoint_auth_method\":\n" + " \"client_secret_basic\",\n" |
| 118 | + + " \"application_type\": \"web\",\n" + " \"redirect_uris\":\n" |
| 119 | + + " [\"https://client.example.org/callback\",\n" |
| 120 | + + " \"https://client.example.org/callback2\"],\n" |
| 121 | + + " \"client_name\": \"My Example\",\n" |
| 122 | + + " \"logo_uri\": \"https://client.example.org/logo.png\",\n" |
| 123 | + + " \"subject_type\": \"pairwise\",\n" + " \"sector_identifier_uri\":\n" |
| 124 | + + " \"https://other.example.net/file_of_redirect_uris.json\",\n" |
| 125 | + + " \"jwks_uri\": \"https://client.example.org/my_public_keys.jwks\",\n" |
| 126 | + + " \"userinfo_encrypted_response_alg\": \"RSA1_5\",\n" |
| 127 | + + " \"userinfo_encrypted_response_enc\": \"A128CBC-HS256\",\n" |
| 128 | + + " \"contacts\": [\"[email protected]\", \"[email protected]\"],\n" |
| 129 | + + " \"request_uris\":\n" |
| 130 | + + " [\"https://client.example.org/rf.txt#qpXaRLh_n93TTR9F252ValdatUQvQiJi5BDub2BeznA\"]\n" |
| 131 | + + "}"; |
| 132 | + RegistrationResponse response = new RegistrationResponse(); |
| 133 | + response.fromJson(json); |
| 134 | + return response; |
| 135 | + } |
| 136 | +} |
0 commit comments