JwtAttributeMapperTest.java revision e440587b940248554524993d31d3e3f22997e62b
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms of the Common Development and
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Distribution License (the License). You may not use this file except in compliance with the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * specific language governing permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Software, include this CDDL Header Notice in each file and include
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header, with the fields enclosed by brackets [] replaced by your own identifying
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * information: "Portions copyright [year] [name of copyright owner]".
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright 2014 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage org.forgerock.openam.authentication.modules.oidc;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport org.forgerock.json.jose.jwt.JwtClaimsSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport org.testng.annotations.BeforeTest;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport org.testng.annotations.Test;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.HashMap;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
d5a35ee0e81c6fb618b79309f71177dc876cbd65Neil Maddenimport static org.testng.Assert.assertTrue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/*
8af80418ba1ec431c8027fa9668e5678658d3611Allan FosterNot testing the lookupPrincipal method, as the AMIdentityRespository is final, and thus requires PowerMock. Introducing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterbyte-code engineering to test the simple consumption of the AMIdentityRepository in the DefaultPrincipalMapper not
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunningtonworth the downsides of PowerMock.
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class JwtAttributeMapperTest {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String EMAIL = "email";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String AM_EMAIL = "mail";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String EMAIL_VALUE = "bobo@bobo.com";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String SUB = "sub";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String UID = "uid";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String ISS = "iss";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String SUBJECT_VALUE = "112403712094132422537";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static final String ISSUER = "accounts.google.com";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map<String, Object> jwtMappings;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private Map<String, String> attributeMappings;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private JwtClaimsSet claimsSet;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private JwtAttributeMapper defaultPrincipalMapper;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster @BeforeTest
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void initialize() {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster jwtMappings = new HashMap<String, Object>();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster jwtMappings.put(SUB, SUBJECT_VALUE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster jwtMappings.put(ISS, ISSUER);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster jwtMappings.put(EMAIL, EMAIL_VALUE);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attributeMappings = new HashMap<String, String>();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attributeMappings.put(UID, SUB);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attributeMappings.put(AM_EMAIL, EMAIL);
d5a35ee0e81c6fb618b79309f71177dc876cbd65Neil Madden
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster claimsSet = new JwtClaimsSet(jwtMappings);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster defaultPrincipalMapper = new JwtAttributeMapper();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster @Test
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void testBasicJwtMapping() {
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington final Map<String, Set<String>> attrs =
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington defaultPrincipalMapper.getAttributes(attributeMappings, claimsSet);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster assertTrue(SUBJECT_VALUE.equals(attrs.get(UID).iterator().next()));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster assertTrue(EMAIL_VALUE.equals(attrs.get(AM_EMAIL).iterator().next()));
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster