DeviceCode.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *******************************************************************************/

  16. package org.mitre.oauth2.model;

  17. import java.util.Date;
  18. import java.util.Map;
  19. import java.util.Set;

  20. import javax.persistence.Basic;
  21. import javax.persistence.CollectionTable;
  22. import javax.persistence.Column;
  23. import javax.persistence.ElementCollection;
  24. import javax.persistence.Entity;
  25. import javax.persistence.FetchType;
  26. import javax.persistence.GeneratedValue;
  27. import javax.persistence.GenerationType;
  28. import javax.persistence.Id;
  29. import javax.persistence.JoinColumn;
  30. import javax.persistence.ManyToOne;
  31. import javax.persistence.MapKeyColumn;
  32. import javax.persistence.NamedQueries;
  33. import javax.persistence.NamedQuery;
  34. import javax.persistence.Table;
  35. import javax.persistence.Temporal;

  36. /**
  37.  * @author jricher
  38.  *
  39.  */
  40. @Entity
  41. @Table(name = "device_code")
  42. @NamedQueries({
  43.     @NamedQuery(name = DeviceCode.QUERY_BY_USER_CODE, query = "select d from DeviceCode d where d.userCode = :" + DeviceCode.PARAM_USER_CODE),
  44.     @NamedQuery(name = DeviceCode.QUERY_BY_DEVICE_CODE, query = "select d from DeviceCode d where d.deviceCode = :" + DeviceCode.PARAM_DEVICE_CODE),
  45.     @NamedQuery(name = DeviceCode.QUERY_EXPIRED_BY_DATE, query = "select d from DeviceCode d where d.expiration <= :" + DeviceCode.PARAM_DATE)
  46. })
  47. public class DeviceCode {

  48.     public static final String QUERY_BY_USER_CODE = "DeviceCode.queryByUserCode";
  49.     public static final String QUERY_BY_DEVICE_CODE = "DeviceCode.queryByDeviceCode";
  50.     public static final String QUERY_EXPIRED_BY_DATE = "DeviceCode.queryExpiredByDate";

  51.     public static final String PARAM_USER_CODE = "userCode";
  52.     public static final String PARAM_DEVICE_CODE = "deviceCode";
  53.     public static final String PARAM_DATE = "date";

  54.     private Long id;
  55.     private String deviceCode;
  56.     private String userCode;
  57.     private Set<String> scope;
  58.     private Date expiration;
  59.     private String clientId;
  60.     private Map<String, String> requestParameters;
  61.     private boolean approved;
  62.     private AuthenticationHolderEntity authenticationHolder;

  63.     public DeviceCode() {

  64.     }

  65.     public DeviceCode(String deviceCode, String userCode, Set<String> scope, String clientId, Map<String, String> params) {
  66.         this.deviceCode = deviceCode;
  67.         this.userCode = userCode;
  68.         this.scope = scope;
  69.         this.clientId = clientId;
  70.         this.requestParameters = params;
  71.     }

  72.     /**
  73.      * @return the id
  74.      */
  75.     @Id
  76.     @GeneratedValue(strategy = GenerationType.IDENTITY)
  77.     @Column(name = "id")
  78.     public Long getId() {
  79.         return id;
  80.     }

  81.     /**
  82.      * @param id the id to set
  83.      */
  84.     public void setId(Long id) {
  85.         this.id = id;
  86.     }

  87.     /**
  88.      * @return the deviceCode
  89.      */
  90.     @Basic
  91.     @Column(name = "device_code")
  92.     public String getDeviceCode() {
  93.         return deviceCode;
  94.     }

  95.     /**
  96.      * @param deviceCode the deviceCode to set
  97.      */
  98.     public void setDeviceCode(String deviceCode) {
  99.         this.deviceCode = deviceCode;
  100.     }

  101.     /**
  102.      * @return the userCode
  103.      */
  104.     @Basic
  105.     @Column(name = "user_code")
  106.     public String getUserCode() {
  107.         return userCode;
  108.     }

  109.     /**
  110.      * @param userCode the userCode to set
  111.      */
  112.     public void setUserCode(String userCode) {
  113.         this.userCode = userCode;
  114.     }

  115.     /**
  116.      * @return the scope
  117.      */
  118.     @ElementCollection(fetch = FetchType.EAGER)
  119.     @CollectionTable(
  120.             name="device_code_scope",
  121.             joinColumns=@JoinColumn(name="owner_id")
  122.             )
  123.     @Column(name="scope")
  124.     public Set<String> getScope() {
  125.         return scope;
  126.     }

  127.     /**
  128.      * @param scope the scope to set
  129.      */
  130.     public void setScope(Set<String> scope) {
  131.         this.scope = scope;
  132.     }

  133.     @Basic
  134.     @Temporal(javax.persistence.TemporalType.TIMESTAMP)
  135.     @Column(name = "expiration")
  136.     public Date getExpiration() {
  137.         return expiration;
  138.     }

  139.     public void setExpiration(Date expiration) {
  140.         this.expiration = expiration;
  141.     }

  142.     /**
  143.      * @return the clientId
  144.      */
  145.     @Basic
  146.     @Column(name = "client_id")
  147.     public String getClientId() {
  148.         return clientId;
  149.     }

  150.     /**
  151.      * @param clientId the clientId to set
  152.      */
  153.     public void setClientId(String clientId) {
  154.         this.clientId = clientId;
  155.     }

  156.     /**
  157.      * @return the params
  158.      */
  159.     @ElementCollection(fetch = FetchType.EAGER)
  160.     @CollectionTable(
  161.             name="device_code_request_parameter",
  162.             joinColumns=@JoinColumn(name="owner_id")
  163.             )
  164.     @Column(name="val")
  165.     @MapKeyColumn(name="param")
  166.     public Map<String, String> getRequestParameters() {
  167.         return requestParameters;
  168.     }

  169.     /**
  170.      * @param params the params to set
  171.      */
  172.     public void setRequestParameters(Map<String, String> params) {
  173.         this.requestParameters = params;
  174.     }

  175.     /**
  176.      * @return the approved
  177.      */
  178.     @Basic
  179.     @Column(name = "approved")
  180.     public boolean isApproved() {
  181.         return approved;
  182.     }

  183.     /**
  184.      * @param approved the approved to set
  185.      */
  186.     public void setApproved(boolean approved) {
  187.         this.approved = approved;
  188.     }

  189.     /**
  190.      * The authentication in place when this token was created.
  191.      * @return the authentication
  192.      */
  193.     @ManyToOne
  194.     @JoinColumn(name = "auth_holder_id")
  195.     public AuthenticationHolderEntity getAuthenticationHolder() {
  196.         return authenticationHolder;
  197.     }

  198.     /**
  199.      * @param authentication the authentication to set
  200.      */
  201.     public void setAuthenticationHolder(AuthenticationHolderEntity authenticationHolder) {
  202.         this.authenticationHolder = authenticationHolder;
  203.     }


  204. }