001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *******************************************************************************/
016
017package org.mitre.uma.service.impl;
018
019import java.util.Collection;
020
021import javax.persistence.EntityManager;
022import javax.persistence.PersistenceContext;
023import javax.persistence.TypedQuery;
024
025import org.mitre.oauth2.model.RegisteredClient;
026import org.mitre.openid.connect.client.service.RegisteredClientService;
027import org.mitre.uma.model.SavedRegisteredClient;
028import org.mitre.uma.service.SavedRegisteredClientService;
029import org.mitre.util.jpa.JpaUtil;
030import org.springframework.stereotype.Service;
031import org.springframework.transaction.annotation.Transactional;
032
033/**
034 * @author jricher
035 *
036 */
037@Service
038public class JpaRegisteredClientService implements RegisteredClientService, SavedRegisteredClientService{
039
040        @PersistenceContext(unitName="defaultPersistenceUnit")
041        private EntityManager em;
042
043        /* (non-Javadoc)
044         * @see org.mitre.openid.connect.client.service.RegisteredClientService#getByIssuer(java.lang.String)
045         */
046        @Override
047        public RegisteredClient getByIssuer(String issuer) {
048                SavedRegisteredClient saved = getSavedRegisteredClientFromStorage(issuer);
049
050                if (saved == null) {
051                        return null;
052                } else {
053                        return saved.getRegisteredClient();
054                }
055        }
056
057        /* (non-Javadoc)
058         * @see org.mitre.openid.connect.client.service.RegisteredClientService#save(java.lang.String, org.mitre.oauth2.model.RegisteredClient)
059         */
060        @Override
061        @Transactional(value="defaultTransactionManager")
062        public void save(String issuer, RegisteredClient client) {
063
064
065                SavedRegisteredClient saved = getSavedRegisteredClientFromStorage(issuer);
066
067                if (saved == null) {
068                        saved = new SavedRegisteredClient();
069                        saved.setIssuer(issuer);
070                }
071
072                saved.setRegisteredClient(client);
073
074                em.persist(saved);
075
076        }
077
078        private SavedRegisteredClient getSavedRegisteredClientFromStorage(String issuer) {
079                TypedQuery<SavedRegisteredClient> query = em.createQuery("SELECT c from SavedRegisteredClient c where c.issuer = :issuer", SavedRegisteredClient.class);
080                query.setParameter("issuer", issuer);
081
082                SavedRegisteredClient saved = JpaUtil.getSingleResult(query.getResultList());
083                return saved;
084        }
085
086        /**
087         * @return
088         */
089        @Override
090        public Collection<SavedRegisteredClient> getAll() {
091                TypedQuery<SavedRegisteredClient> query = em.createQuery("SELECT c from SavedRegisteredClient c", SavedRegisteredClient.class);
092                return query.getResultList();
093        }
094
095}