save dynamically registered clients to the server's database, closes #799

pull/866/head
Justin Richer 2015-07-08 14:35:20 -04:00
parent 662b0cd098
commit e96eda0990
7 changed files with 260 additions and 11 deletions

View File

@ -0,0 +1,97 @@
/*******************************************************************************
* Copyright 2015 The MITRE Corporation
* and the MIT Kerberos and Internet Trust Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package org.mitre.uma.model;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.mitre.oauth2.model.RegisteredClient;
import org.mitre.uma.model.convert.RegisteredClientStringConverter;
/**
* @author jricher
*
*/
@Entity
@Table(name = "saved_registered_client")
public class SavedRegisteredClient {
private Long id;
private String issuer;
private RegisteredClient registeredClient;
/**
* @return the id
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public Long getId() {
return id;
}
/**
*
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the issuer
*/
@Basic
@Column(name = "issuer")
public String getIssuer() {
return issuer;
}
/**
* @param issuer the issuer to set
*/
public void setIssuer(String issuer) {
this.issuer = issuer;
}
/**
* @return the registeredClient
*/
@Basic
@Column(name = "registered_client")
@Convert(converter = RegisteredClientStringConverter.class)
public RegisteredClient getRegisteredClient() {
return registeredClient;
}
/**
* @param registeredClient the registeredClient to set
*/
public void setRegisteredClient(RegisteredClient registeredClient) {
this.registeredClient = registeredClient;
}
}

View File

@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright 2015 The MITRE Corporation
* and the MIT Kerberos and Internet Trust Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package org.mitre.uma.model.convert;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import org.mitre.oauth2.model.RegisteredClient;
import org.mitre.openid.connect.ClientDetailsEntityJsonProcessor;
import com.google.common.base.Strings;
/**
* @author jricher
*
*/
@Converter
public class RegisteredClientStringConverter implements AttributeConverter<RegisteredClient, String>{
/* (non-Javadoc)
* @see javax.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.Object)
*/
@Override
public String convertToDatabaseColumn(RegisteredClient attribute) {
if (attribute == null || attribute.getSource() == null) {
return null;
} else {
return attribute.getSource().toString();
}
}
/* (non-Javadoc)
* @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object)
*/
@Override
public RegisteredClient convertToEntityAttribute(String dbData) {
if (Strings.isNullOrEmpty(dbData)) {
return null;
} else {
return ClientDetailsEntityJsonProcessor.parseRegistered(dbData);
}
}
}

View File

@ -345,3 +345,9 @@ CREATE TABLE IF NOT EXISTS claim_issuer (
owner_id BIGINT NOT NULL,
issuer VARCHAR(1024)
);
CREATE TABLE IF NOT EXISTS saved_registered_client (
id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY,
issuer VARCHAR(1024),
registered_client VARCHAR(8192)
);

View File

@ -345,3 +345,9 @@ CREATE TABLE IF NOT EXISTS claim_issuer (
owner_id BIGINT NOT NULL,
issuer VARCHAR(1024)
);
CREATE TABLE IF NOT EXISTS saved_registered_client (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
issuer VARCHAR(1024),
registered_client VARCHAR(8192)
);

View File

@ -345,3 +345,9 @@ CREATE TABLE IF NOT EXISTS claim_issuer (
owner_id BIGINT NOT NULL,
issuer VARCHAR(1024)
);
CREATE TABLE IF NOT EXISTS saved_registered_client (
id SERIAL PRIMARY KEY,
issuer VARCHAR(1024),
registered_client VARCHAR(8192)
);

View File

@ -98,19 +98,9 @@
</property>
</bean>
</property>
<!--
Registered Client Service. Uncomment this to save dynamically registered clients out to a
file on disk (indicated by the filename property) or replace this with another implementation
of RegisteredClientService. This defaults to an in-memory implementation of RegisteredClientService
which will forget and re-register all clients on restart.
-->
<!--
<property name="registeredClientService">
<bean class="org.mitre.openid.connect.client.service.impl.JsonFileRegisteredClientService">
<constructor-arg name="filename" value="/tmp/simple-web-app-clients.json" />
</bean>
<bean class="org.mitre.uma.service.impl.JpaRegisteredClientService" />
</property>
-->
</bean>
<bean class="org.mitre.openid.connect.client.service.impl.StaticAuthRequestOptionsService" id="staticAuthRequestOptionsService" />

View File

@ -0,0 +1,84 @@
/*******************************************************************************
* Copyright 2015 The MITRE Corporation
* and the MIT Kerberos and Internet Trust Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package org.mitre.uma.service.impl;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.mitre.oauth2.model.RegisteredClient;
import org.mitre.openid.connect.client.service.RegisteredClientService;
import org.mitre.uma.model.SavedRegisteredClient;
import org.mitre.util.jpa.JpaUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @author jricher
*
*/
@Service
public class JpaRegisteredClientService implements RegisteredClientService {
@PersistenceContext
private EntityManager em;
/* (non-Javadoc)
* @see org.mitre.openid.connect.client.service.RegisteredClientService#getByIssuer(java.lang.String)
*/
@Override
public RegisteredClient getByIssuer(String issuer) {
SavedRegisteredClient saved = getSavedRegisteredClientFromStorage(issuer);
if (saved == null) {
return null;
} else {
return saved.getRegisteredClient();
}
}
/* (non-Javadoc)
* @see org.mitre.openid.connect.client.service.RegisteredClientService#save(java.lang.String, org.mitre.oauth2.model.RegisteredClient)
*/
@Override
@Transactional
public void save(String issuer, RegisteredClient client) {
SavedRegisteredClient saved = getSavedRegisteredClientFromStorage(issuer);
if (saved == null) {
saved = new SavedRegisteredClient();
saved.setIssuer(issuer);
}
saved.setRegisteredClient(client);
em.persist(saved);
}
private SavedRegisteredClient getSavedRegisteredClientFromStorage(String issuer) {
TypedQuery<SavedRegisteredClient> query = em.createQuery("SELECT c from SavedRegisteredClient c where c.issuer = :issuer", SavedRegisteredClient.class);
query.setParameter("issuer", issuer);
SavedRegisteredClient saved = JpaUtil.getSingleResult(query.getResultList());
return saved;
}
}