data structure and services for transactions

pull/1536/head
Justin Richer 2020-05-11 14:08:07 -04:00
parent b299dfcc49
commit a943f1a725
4 changed files with 364 additions and 0 deletions

View File

@ -382,3 +382,22 @@ CREATE TABLE IF NOT EXISTS device_code_request_parameter (
param VARCHAR(2048),
val VARCHAR(2048)
);
CREATE TABLE IF NOT EXISTS transaction (
id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY,
handle VARCHAR(1024),
interaction VARCHAR(1024),
client_id BIGINT NOT NULL,
status VARCHAR(256),
callback_uri VARCHAR(1024),
client_nonce VARCHAR(1024),
server_nonce VARCHAR(1024),
interaction_ref VARCHAR(1024),
hash_method VARCHAR(256),
auth_holder_id BIGINT
);
CREATE TABLE IF NOT EXISTS transaction_scope (
owner_id BIGINT NOT NULL,
scope VARCHAR(256) NOT NULL
);

View File

@ -0,0 +1,57 @@
package org.mitre.xyz;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.mitre.util.jpa.JpaUtil;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
/**
* @author jricher
*
*/
@Repository
@Transactional(value = "defaultTransactionManager")
public class JpaTxService implements TxService {
@PersistenceContext(unitName="defaultPersistenceUnit")
private EntityManager manager;
private TxEntity getById(Long id) {
return manager.find(TxEntity.class, id);
}
@Override
public TxEntity loadByHandle(String handle) {
TypedQuery<TxEntity> query = manager.createNamedQuery(TxEntity.QUERY_BY_HANDLE, TxEntity.class);
query.setParameter(TxEntity.PARAM_HANDLE, handle);
return JpaUtil.getSingleResult(query.getResultList());
}
@Override
public TxEntity loadByInteractUrl(String interaction) {
TypedQuery<TxEntity> query = manager.createNamedQuery(TxEntity.QUERY_BY_INTERACTION, TxEntity.class);
query.setParameter(TxEntity.PARAM_INTERACTION, interaction);
return JpaUtil.getSingleResult(query.getResultList());
}
@Override
public TxEntity save(TxEntity tx) {
return JpaUtil.saveOrUpdate(tx.getId(), manager, tx);
}
@Override
public void delete(TxEntity tx) {
TxEntity found = getById(tx.getId());
if (found != null) {
manager.remove(found);
} else {
throw new IllegalArgumentException("Transaction not found: " + tx);
}
}
}

View File

@ -0,0 +1,271 @@
package org.mitre.xyz;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.mitre.oauth2.model.AuthenticationHolderEntity;
import org.mitre.oauth2.model.ClientDetailsEntity;
import org.mitre.xyz.Hash.Method;
import org.mitre.xyz.TxEndpoint.Status;
/**
* @author jricher
*
*/
@Entity
@Table(name = "transaction")
@NamedQueries({
@NamedQuery(name = TxEntity.QUERY_BY_HANDLE, query = "SELECT t FROM TxEntity t WHERE t.handle = :" + TxEntity.PARAM_HANDLE),
@NamedQuery(name = TxEntity.QUERY_BY_INTERACTION, query = "SELECT t FROM TxEntity t WHERE t.interaction = :" + TxEntity.PARAM_INTERACTION)
})
public class TxEntity {
public static final String QUERY_BY_HANDLE = "TxEntity.getByHandle";
public static final String QUERY_BY_INTERACTION = "TxEntity.getByInteraction";
public static final String PARAM_HANDLE = "handle";
public static final String PARAM_INTERACTION = "interaction";
private Long id;
private String handle;
private String interaction;
private ClientDetailsEntity client;
private Set<String> scope = new HashSet<>();
private Status status;
private String callbackUri;
private String clientNonce;
private String serverNonce;
private String interactionRef;
private Method hashMethod;
private AuthenticationHolderEntity authenticationHolder;
/**
* @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 scope
*/
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(
name="transaction_scope",
joinColumns=@JoinColumn(name="owner_id")
)
@Column(name="scope")
public Set<String> getScope() {
return scope;
}
/**
* @param scope the set of scopes allowed to be issued to this client
*/
public void setScope(Set<String> scope) {
this.scope = scope;
}
/**
* @return the clientId
*/
@ManyToOne
@JoinColumn(name = "client_id")
public ClientDetailsEntity getClient() {
return client;
}
/**
*/
public void setClient(ClientDetailsEntity client) {
this.client = client;
}
/**
* @return the handle
*/
@Basic
@Column(name = "handle")
public String getHandle() {
return handle;
}
/**
* @param handle the handle to set
*/
public void setHandle(String handle) {
this.handle = handle;
}
/**
* @return the interaction
*/
@Basic
@Column(name = "interaction")
public String getInteraction() {
return interaction;
}
/**
* @param interaction the interaction to set
*/
public void setInteraction(String interaction) {
this.interaction = interaction;
}
/**
* @return the status
*/
@Enumerated(EnumType.STRING)
@Column(name = "status")
public Status getStatus() {
return status;
}
/**
* @param status the status to set
*/
public void setStatus(Status status) {
this.status = status;
}
/**
* @return the callbackUri
*/
@Basic
@Column(name = "callback_uri")
public String getCallbackUri() {
return callbackUri;
}
/**
* @param callbackUri the callbackUri to set
*/
public void setCallbackUri(String callbackUri) {
this.callbackUri = callbackUri;
}
/**
* @return the clientNonce
*/
@Basic
@Column(name = "client_nonce")
public String getClientNonce() {
return clientNonce;
}
/**
* @param clientNonce the clientNonce to set
*/
public void setClientNonce(String clientNonce) {
this.clientNonce = clientNonce;
}
/**
* @return the serverNonce
*/
@Basic
@Column(name = "server_nonce")
public String getServerNonce() {
return serverNonce;
}
/**
* @param serverNonce the serverNonce to set
*/
public void setServerNonce(String serverNonce) {
this.serverNonce = serverNonce;
}
/**
* @return the interactionRef
*/
@Basic
@Column(name = "interaction_ref")
public String getInteractionRef() {
return interactionRef;
}
/**
* @param interactionRef the interactionRef to set
*/
public void setInteractionRef(String interactionRef) {
this.interactionRef = interactionRef;
}
/**
* @return the hashMethod
*/
@Enumerated(EnumType.STRING)
@Column(name = "hash_method")
public Method getHashMethod() {
return hashMethod;
}
/**
* @param hashMethod the hashMethod to set
*/
public void setHashMethod(Method hashMethod) {
this.hashMethod = hashMethod;
}
/**
* The authentication in place when this token was created.
* @return the authentication
*/
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "auth_holder_id")
public AuthenticationHolderEntity getAuthenticationHolder() {
return authenticationHolder;
}
/**
* @param authentication the authentication to set
*/
public void setAuthenticationHolder(AuthenticationHolderEntity authenticationHolder) {
this.authenticationHolder = authenticationHolder;
}
}

View File

@ -0,0 +1,17 @@
package org.mitre.xyz;
/**
* @author jricher
*
*/
public interface TxService {
TxEntity loadByHandle(String handle);
TxEntity loadByInteractUrl(String interaction);
TxEntity save(TxEntity tx);
void delete(TxEntity tx);
}