ApprovedSite.java

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

  19. import java.util.Date;
  20. import java.util.Set;

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

  36. @Entity
  37. @Table(name="approved_site")
  38. @NamedQueries({
  39.     @NamedQuery(name = ApprovedSite.QUERY_ALL, query = "select a from ApprovedSite a"),
  40.     @NamedQuery(name = ApprovedSite.QUERY_BY_USER_ID, query = "select a from ApprovedSite a where a.userId = :" + ApprovedSite.PARAM_USER_ID),
  41.     @NamedQuery(name = ApprovedSite.QUERY_BY_CLIENT_ID, query = "select a from ApprovedSite a where a.clientId = :" + ApprovedSite.PARAM_CLIENT_ID),
  42.     @NamedQuery(name = ApprovedSite.QUERY_BY_CLIENT_ID_AND_USER_ID, query = "select a from ApprovedSite a where a.clientId = :" + ApprovedSite.PARAM_CLIENT_ID + " and a.userId = :" + ApprovedSite.PARAM_USER_ID)
  43. })
  44. public class ApprovedSite {

  45.     public static final String QUERY_BY_CLIENT_ID_AND_USER_ID = "ApprovedSite.getByClientIdAndUserId";
  46.     public static final String QUERY_BY_CLIENT_ID = "ApprovedSite.getByClientId";
  47.     public static final String QUERY_BY_USER_ID = "ApprovedSite.getByUserId";
  48.     public static final String QUERY_ALL = "ApprovedSite.getAll";

  49.     public static final String PARAM_CLIENT_ID = "clientId";
  50.     public static final String PARAM_USER_ID = "userId";

  51.     // unique id
  52.     private Long id;

  53.     // which user made the approval
  54.     private String userId;

  55.     // which OAuth2 client is this tied to
  56.     private String clientId;

  57.     // when was this first approved?
  58.     private Date creationDate;

  59.     // when was this last accessed?
  60.     private Date accessDate;

  61.     // if this is a time-limited access, when does it run out?
  62.     private Date timeoutDate;

  63.     // what scopes have been allowed
  64.     // this should include all information for what data to access
  65.     private Set<String> allowedScopes;

  66.     /**
  67.      * Empty constructor
  68.      */
  69.     public ApprovedSite() {

  70.     }

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

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

  86.     /**
  87.      * @return the userInfo
  88.      */
  89.     @Basic
  90.     @Column(name="user_id")
  91.     public String getUserId() {
  92.         return userId;
  93.     }

  94.     /**
  95.      * @param userInfo the userInfo to set
  96.      */
  97.     public void setUserId(String userId) {
  98.         this.userId = userId;
  99.     }

  100.     /**
  101.      * @return the clientId
  102.      */
  103.     @Basic
  104.     @Column(name="client_id")
  105.     public String getClientId() {
  106.         return clientId;
  107.     }

  108.     /**
  109.      * @param clientId the clientId to set
  110.      */
  111.     public void setClientId(String clientId) {
  112.         this.clientId = clientId;
  113.     }

  114.     /**
  115.      * @return the creationDate
  116.      */
  117.     @Basic
  118.     @Temporal(javax.persistence.TemporalType.TIMESTAMP)
  119.     @Column(name="creation_date")
  120.     public Date getCreationDate() {
  121.         return creationDate;
  122.     }

  123.     /**
  124.      * @param creationDate the creationDate to set
  125.      */
  126.     public void setCreationDate(Date creationDate) {
  127.         this.creationDate = creationDate;
  128.     }

  129.     /**
  130.      * @return the accessDate
  131.      */
  132.     @Basic
  133.     @Temporal(javax.persistence.TemporalType.TIMESTAMP)
  134.     @Column(name="access_date")
  135.     public Date getAccessDate() {
  136.         return accessDate;
  137.     }

  138.     /**
  139.      * @param accessDate the accessDate to set
  140.      */
  141.     public void setAccessDate(Date accessDate) {
  142.         this.accessDate = accessDate;
  143.     }

  144.     /**
  145.      * @return the allowedScopes
  146.      */
  147.     @ElementCollection(fetch = FetchType.EAGER)
  148.     @CollectionTable(
  149.             name="approved_site_scope",
  150.             joinColumns=@JoinColumn(name="owner_id")
  151.             )
  152.     @Column(name="scope")
  153.     public Set<String> getAllowedScopes() {
  154.         return allowedScopes;
  155.     }

  156.     /**
  157.      * @param allowedScopes the allowedScopes to set
  158.      */
  159.     public void setAllowedScopes(Set<String> allowedScopes) {
  160.         this.allowedScopes = allowedScopes;
  161.     }

  162.     /**
  163.      * @return the timeoutDate
  164.      */
  165.     @Basic
  166.     @Temporal(javax.persistence.TemporalType.TIMESTAMP)
  167.     @Column(name="timeout_date")
  168.     public Date getTimeoutDate() {
  169.         return timeoutDate;
  170.     }

  171.     /**
  172.      * @param timeoutDate the timeoutDate to set
  173.      */
  174.     public void setTimeoutDate(Date timeoutDate) {
  175.         this.timeoutDate = timeoutDate;
  176.     }

  177.     /**
  178.      * Has this approval expired?
  179.      * @return
  180.      */
  181.     @Transient
  182.     public boolean isExpired() {
  183.         if (getTimeoutDate() != null) {
  184.             Date now = new Date();
  185.             if (now.after(getTimeoutDate())) {
  186.                 return true;
  187.             } else {
  188.                 return false;
  189.             }
  190.         } else {
  191.             return false;
  192.         }
  193.     }

  194. }