001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Portions copyright 2011-2013 The MITRE Corporation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *******************************************************************************/
018package org.mitre.openid.connect.service.impl;
019
020import java.util.HashSet;
021import java.util.Set;
022
023import org.mitre.openid.connect.service.ScopeClaimTranslationService;
024import org.springframework.stereotype.Service;
025
026import com.google.common.collect.HashMultimap;
027import com.google.common.collect.SetMultimap;
028
029/**
030 * Service to map scopes to claims, and claims to Java field names
031 *
032 * @author Amanda Anganes
033 *
034 */
035@Service("scopeClaimTranslator")
036public class DefaultScopeClaimTranslationService implements ScopeClaimTranslationService {
037
038        private SetMultimap<String, String> scopesToClaims = HashMultimap.create();
039
040        /**
041         * Default constructor; initializes scopesToClaims map
042         */
043        public DefaultScopeClaimTranslationService() {
044                scopesToClaims.put("openid", "sub");
045
046                scopesToClaims.put("profile", "name");
047                scopesToClaims.put("profile", "preferred_username");
048                scopesToClaims.put("profile", "given_name");
049                scopesToClaims.put("profile", "family_name");
050                scopesToClaims.put("profile", "middle_name");
051                scopesToClaims.put("profile", "nickname");
052                scopesToClaims.put("profile", "profile");
053                scopesToClaims.put("profile", "picture");
054                scopesToClaims.put("profile", "website");
055                scopesToClaims.put("profile", "gender");
056                scopesToClaims.put("profile", "zoneinfo");
057                scopesToClaims.put("profile", "locale");
058                scopesToClaims.put("profile", "updated_at");
059                scopesToClaims.put("profile", "birthdate");
060
061                scopesToClaims.put("email", "email");
062                scopesToClaims.put("email", "email_verified");
063
064                scopesToClaims.put("phone", "phone_number");
065                scopesToClaims.put("phone", "phone_number_verified");
066
067                scopesToClaims.put("address", "address");
068        }
069
070        /* (non-Javadoc)
071         * @see org.mitre.openid.connect.service.ScopeClaimTranslationService#getClaimsForScope(java.lang.String)
072         */
073        @Override
074        public Set<String> getClaimsForScope(String scope) {
075                if (scopesToClaims.containsKey(scope)) {
076                        return scopesToClaims.get(scope);
077                } else {
078                        return new HashSet<>();
079                }
080        }
081
082        /* (non-Javadoc)
083         * @see org.mitre.openid.connect.service.ScopeClaimTranslationService#getClaimsForScopeSet(java.util.Set)
084         */
085        @Override
086        public Set<String> getClaimsForScopeSet(Set<String> scopes) {
087                Set<String> result = new HashSet<>();
088                for (String scope : scopes) {
089                        result.addAll(getClaimsForScope(scope));
090                }
091                return result;
092        }
093
094}