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 org.mitre.oauth2.model.ClientDetailsEntity;
021import org.mitre.oauth2.model.ClientDetailsEntity.SubjectType;
022import org.mitre.oauth2.service.ClientDetailsEntityService;
023import org.mitre.openid.connect.model.UserInfo;
024import org.mitre.openid.connect.repository.UserInfoRepository;
025import org.mitre.openid.connect.service.PairwiseIdentiferService;
026import org.mitre.openid.connect.service.UserInfoService;
027import org.springframework.beans.factory.annotation.Autowired;
028import org.springframework.stereotype.Service;
029
030/**
031 * Implementation of the UserInfoService
032 *
033 * @author Michael Joseph Walsh, jricher
034 *
035 */
036@Service
037public class DefaultUserInfoService implements UserInfoService {
038
039        @Autowired
040        private UserInfoRepository userInfoRepository;
041
042        @Autowired
043        private ClientDetailsEntityService clientService;
044
045        @Autowired
046        private PairwiseIdentiferService pairwiseIdentifierService;
047
048        @Override
049        public UserInfo getByUsername(String username) {
050                return userInfoRepository.getByUsername(username);
051        }
052
053        @Override
054        public UserInfo getByUsernameAndClientId(String username, String clientId) {
055
056                ClientDetailsEntity client = clientService.loadClientByClientId(clientId);
057
058                UserInfo userInfo = getByUsername(username);
059
060                if (client == null || userInfo == null) {
061                        return null;
062                }
063
064                if (SubjectType.PAIRWISE.equals(client.getSubjectType())) {
065                        String pairwiseSub = pairwiseIdentifierService.getIdentifier(userInfo, client);
066                        userInfo.setSub(pairwiseSub);
067                }
068
069                return userInfo;
070
071        }
072
073        @Override
074        public UserInfo getByEmailAddress(String email) {
075                return userInfoRepository.getByEmailAddress(email);
076        }
077
078}