org.apigw.authserver.svc.impl.CertifiedClientDetailsServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.authserver.svc.impl.CertifiedClientDetailsServiceImpl.java

Source

/**
 *   Copyright 2013 Stockholm County Council
 *
 *   This file is part of APIGW
 *
 *   APIGW is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   APIGW is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with APIGW; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 *
 */

package org.apigw.authserver.svc.impl;

import org.apigw.authserver.svc.CertifiedClientDetailsService;
import org.apigw.authserver.svc.repository.AuthorizationGrantRepository;
import org.apigw.authserver.svc.repository.CertifiedClientPermissionRepository;
import org.apigw.authserver.svc.repository.CertifiedClientRepository;
import org.apigw.authserver.types.domain.AuthorizationGrant;
import org.apigw.authserver.types.domain.CertifiedClient;
import org.apigw.authserver.types.domain.CertifiedClientIcon;
import org.apigw.authserver.types.domain.CertifiedClientPermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.List;

@Service
public class CertifiedClientDetailsServiceImpl implements CertifiedClientDetailsService {

    private static final Logger log = LoggerFactory.getLogger(CertifiedClientDetailsServiceImpl.class);

    private CertifiedClientRepository certifiedClientRepository;
    @Autowired
    private CertifiedClientPermissionRepository certifiedClientPermissionRepository;
    @Autowired
    private AuthorizationGrantRepository authorizationGrantRepository;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ClientDetails loadClientByClientId(String clientId) throws NoSuchClientException {
        log.debug("loadClientByClientId(clientId:{})", clientId);
        // client id - query
        CertifiedClient client = certifiedClientRepository.findClientByClientIdEager(clientId);

        if (client == null) {
            log.warn("No client with id {} found", clientId);
            throw new UsernameNotFoundException("No client with id " + clientId + " found");
        } else {
            log.debug("returning from loadClientByClientId with client");
            return client;
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public CertifiedClient loadClientByX509Cert(String issuerDN, String subjectDN) {
        log.trace("loadClientByX509Cert(issuerDN:{}, subjectDN:{})", issuerDN, subjectDN);
        CertifiedClient certifiedClient = certifiedClientRepository.findClientByIssuerDnAndSubjectDnEager(issuerDN,
                subjectDN);
        if (certifiedClient == null) {
            log.warn("No client with issuer DN {} and subject DN {} found", issuerDN, subjectDN);
            throw new UsernameNotFoundException("No client for the provided certificate found");
        }
        log.trace("returning CertifiedClient from loadClientByX509Cert");
        return certifiedClient;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<CertifiedClient> findAllClients() {
        return certifiedClientRepository.findAll(new Sort(Sort.Direction.ASC, "clientId"));
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public CertifiedClient findClientByClientId(String clientId) {
        return certifiedClientRepository.findClientByClientIdEager(clientId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public CertifiedClientIcon findClientIconByClientId(String clientId) {
        return certifiedClientRepository.findCertifiedClientIconByClientId(clientId);
    }

    @Override
    public CertifiedClient store(CertifiedClient client) {
        return certifiedClientRepository.save(client);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(CertifiedClient client) {
        certifiedClientRepository.delete(client);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deletePermission(CertifiedClientPermission permission) {
        // JPA CascadeType.REMOVE doesn't work as expected so we have to remove all deps. from the owner side
        List<AuthorizationGrant> authorizationGrants = authorizationGrantRepository.findByClientIdAndPermissionId(
                permission.getCertifiedClient().getClientId(), permission.getPermission().getId());
        for (AuthorizationGrant grant : authorizationGrants) {
            grant.getGrantedPermissions().remove(permission);
            authorizationGrantRepository.save(grant);
        }
        certifiedClientPermissionRepository.delete(permission);
    }

    @Override
    public List<AuthorizationGrant> findAuthorizationGrantsByClientIdAndPermissionId(String clientId,
            Long permissionId) {
        return authorizationGrantRepository.findByClientIdAndPermissionId(clientId, permissionId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void storePermission(CertifiedClientPermission permission) {
        certifiedClientPermissionRepository.save(permission);
    }

    @Autowired
    public void setCertifiedClientRepository(CertifiedClientRepository certifiedClientRepository) {
        this.certifiedClientRepository = certifiedClientRepository;
    }

}