Java tutorial
/** * 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.admin; import java.util.Arrays; import java.util.EnumSet; import java.util.List; import org.apigw.authserver.svc.AdministrationServices; import org.apigw.authserver.types.domain.CertifiedClientRole; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import static org.apigw.authserver.types.domain.CertifiedClientRole.ROLE_ADMIN; import static org.apigw.authserver.types.domain.CertifiedClientRole.ROLE_CLIENT; import static org.apigw.authserver.types.domain.CertifiedClientRole.ROLE_TRUSTED_CLIENT; /** * Executed when running locally to fill the database with data. * Only to be used locally, i.e. when running profile "local" * * @author Christian Hilmersson */ @Service public class LocalDataAdministration { private static final Logger log = LoggerFactory.getLogger(LocalDataAdministration.class); @Autowired private AdministrationServices administrationServices; /** * Registers a role if it doesn't already exists. * * @param name the name of the role * @param description a description of the role */ private void registerRole(String name, String description) { if (administrationServices.findPermission(name) == null) { administrationServices.registerPermission(name, description); } else { log.info("A role with name {} already exist, no role was registered.", name); } } /** * Registers an application if it doesn't already exist. * * @param clientId the client id * @param applicationName the application name * @param description A textual description of the application * @param accessTokenValiditySeconds access token validity seconds, 0 for infinity * @param refreshTokenValiditySeconds refresh token validity seconds, 0 for infinity * @param x509CertificateIssuerDN * @param x509CertificateSubjectDN * @param x509CertificateOrganization organization from the certificate * @param assignedRoles roles assigned to this application * @param secret the application secret, only used in local development instead of x509 authentication * @param clientUrl the url of the client */ private void registerApplication(String clientId, String applicationName, String description, long accessTokenValiditySeconds, long refreshTokenValiditySeconds, String x509CertificateIssuerDN, String x509CertificateSubjectDN, String x509CertificateOrganization, List<String> assignedRoles, String secret, EnumSet<CertifiedClientRole> certifiedClientRoles, String clientUrl) { if (administrationServices.findCertifiedClientByClientId(clientId) == null) { administrationServices.registerApplication(clientId, applicationName, description, accessTokenValiditySeconds, refreshTokenValiditySeconds, x509CertificateIssuerDN, x509CertificateSubjectDN, x509CertificateOrganization, assignedRoles, secret, certifiedClientRoles, clientUrl); } else { log.info("An application with client id {} already exist, no application was registered.", clientId); } } /** * Populates the database with some base data for local testing purposes. * This method runs upon spring context initialization when running locally, * i.e. when using loginform-authserver-context.xml as context */ public void populateWithDataForLocalTesting() { log.info("Populating local usage data..."); registerRole("CRM_SCHEDULING_READ", "Lsa tidbokningar"); registerRole("CRM_SCHEDULING_WRITE", "Skriva tidbokningar"); registerRole("CRM_REQUESTSTATUS_READ", "Lsa remiss-status"); registerRole("CRM_CARELISTING_READ", "Lsa listningar"); registerRole("CLINICALPROCESS_HEALTHCOND_DESCRIPTION_READ", "Lsa vrddokumentation"); registerRole("CLINICALPROCESS_LOGISTICS_LOGISTICS_READ", "Lsa vrdkontakter"); registerRole("CLINICALPROCESS_ACTIVITYPRESCRIPTION_ACTOUTCOME_READ", "Lsa vaccinationer"); registerRole("CLINICALPROCESS_HEALTHCOND_ACTOUTCOME_READ", "Lsa mdravrd"); registerRole("CLINICALPROCESS_HEALTHCOND_RHEUMA_READ", "Lsa reumatismdata"); registerRole("INFRASTRUCTURE_SUPPORTSERVICES_FORMINTERACTION_WRITE", "Skriva formulrdata"); registerRole("INFRASTRUCTURE_SUPPORTSERVICES_FORMINTERACTION_READ", "Lsa formulrdata"); registerRole("CLINICALPROCESS_HEALTHCOND_BASIC_READ", ""); registerApplication("vardpumpen", "Vrdpumpen", "Application description", 0L, 0L, "CA_2", "VARDPUMPEN_DUMMY_SUBJ_DN", "Callista Enterprise", Arrays.asList("CRM_SCHEDULING_READ", "CLINICALPROCESS_HEALTHCOND_DESCRIPTION_READ", "CLINICALPROCESS_LOGISTICS_LOGISTICS_READ", "CLINICALPROCESS_ACTIVITYPRESCRIPTION_ACTOUTCOME_READ", "CLINICALPROCESS_HEALTHCOND_ACTOUTCOME_READ", "CLINICALPROCESS_HEALTHCOND_RHEUMA_READ", "CRM_CARELISTING_READ"), "secret", EnumSet.of(ROLE_CLIENT, ROLE_TRUSTED_CLIENT), "http://localhost:8080/anotherClient"); registerApplication("clientA", "API Test", "Description of the API Test application", 0L, 0L, "CN=clientA, OU=Callista Software, O=Callista Enterprise AB, L=Gothenburg, ST=, C=SE", "CN=clientA, OU=Callista Software, O=Callista Enterprise AB, L=Gothenburg, ST=, C=SE", "Callista Enterprise", Arrays.asList("CRM_SCHEDULING_READ", "CRM_SCHEDULING_WRITE", "CRM_REQUESTSTATUS_READ", "CRM_CARELISTING_READ", "CLINICALPROCESS_HEALTHCOND_DESCRIPTION_READ", "CLINICALPROCESS_LOGISTICS_LOGISTICS_READ", "CLINICALPROCESS_ACTIVITYPRESCRIPTION_ACTOUTCOME_READ", "CLINICALPROCESS_HEALTHCOND_ACTOUTCOME_READ", "CLINICALPROCESS_HEALTHCOND_RHEUMA_READ", "INFRASTRUCTURE_SUPPORTSERVICES_FORMINTERACTION_READ", "INFRASTRUCTURE_SUPPORTSERVICES_FORMINTERACTION_WRITE", "CLINICALPROCESS_HEALTHCOND_BASIC_READ"), "secret", EnumSet.of(ROLE_CLIENT, ROLE_TRUSTED_CLIENT, ROLE_ADMIN), "http://localhost:9090"); } }