001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Portions copyright 2011-2013 The MITRE Corporation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *******************************************************************************/
018package org.mitre.openid.connect.model;
019
020import java.util.Date;
021import java.util.Set;
022
023import javax.persistence.Basic;
024import javax.persistence.CollectionTable;
025import javax.persistence.Column;
026import javax.persistence.ElementCollection;
027import javax.persistence.Entity;
028import javax.persistence.FetchType;
029import javax.persistence.GeneratedValue;
030import javax.persistence.GenerationType;
031import javax.persistence.Id;
032import javax.persistence.JoinColumn;
033import javax.persistence.NamedQueries;
034import javax.persistence.NamedQuery;
035import javax.persistence.Table;
036import javax.persistence.Temporal;
037import javax.persistence.Transient;
038
039@Entity
040@Table(name="approved_site")
041@NamedQueries({
042        @NamedQuery(name = ApprovedSite.QUERY_ALL, query = "select a from ApprovedSite a"),
043        @NamedQuery(name = ApprovedSite.QUERY_BY_USER_ID, query = "select a from ApprovedSite a where a.userId = :" + ApprovedSite.PARAM_USER_ID),
044        @NamedQuery(name = ApprovedSite.QUERY_BY_CLIENT_ID, query = "select a from ApprovedSite a where a.clientId = :" + ApprovedSite.PARAM_CLIENT_ID),
045        @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)
046})
047public class ApprovedSite {
048
049        public static final String QUERY_BY_CLIENT_ID_AND_USER_ID = "ApprovedSite.getByClientIdAndUserId";
050        public static final String QUERY_BY_CLIENT_ID = "ApprovedSite.getByClientId";
051        public static final String QUERY_BY_USER_ID = "ApprovedSite.getByUserId";
052        public static final String QUERY_ALL = "ApprovedSite.getAll";
053
054        public static final String PARAM_CLIENT_ID = "clientId";
055        public static final String PARAM_USER_ID = "userId";
056
057        // unique id
058        private Long id;
059
060        // which user made the approval
061        private String userId;
062
063        // which OAuth2 client is this tied to
064        private String clientId;
065
066        // when was this first approved?
067        private Date creationDate;
068
069        // when was this last accessed?
070        private Date accessDate;
071
072        // if this is a time-limited access, when does it run out?
073        private Date timeoutDate;
074
075        // what scopes have been allowed
076        // this should include all information for what data to access
077        private Set<String> allowedScopes;
078
079        /**
080         * Empty constructor
081         */
082        public ApprovedSite() {
083
084        }
085
086        /**
087         * @return the id
088         */
089        @Id
090        @GeneratedValue(strategy = GenerationType.IDENTITY)
091        @Column(name = "id")
092        public Long getId() {
093                return id;
094        }
095
096        /**
097         * @param id the id to set
098         */
099        public void setId(Long id) {
100                this.id = id;
101        }
102
103        /**
104         * @return the userInfo
105         */
106        @Basic
107        @Column(name="user_id")
108        public String getUserId() {
109                return userId;
110        }
111
112        /**
113         * @param userInfo the userInfo to set
114         */
115        public void setUserId(String userId) {
116                this.userId = userId;
117        }
118
119        /**
120         * @return the clientId
121         */
122        @Basic
123        @Column(name="client_id")
124        public String getClientId() {
125                return clientId;
126        }
127
128        /**
129         * @param clientId the clientId to set
130         */
131        public void setClientId(String clientId) {
132                this.clientId = clientId;
133        }
134
135        /**
136         * @return the creationDate
137         */
138        @Basic
139        @Temporal(javax.persistence.TemporalType.TIMESTAMP)
140        @Column(name="creation_date")
141        public Date getCreationDate() {
142                return creationDate;
143        }
144
145        /**
146         * @param creationDate the creationDate to set
147         */
148        public void setCreationDate(Date creationDate) {
149                this.creationDate = creationDate;
150        }
151
152        /**
153         * @return the accessDate
154         */
155        @Basic
156        @Temporal(javax.persistence.TemporalType.TIMESTAMP)
157        @Column(name="access_date")
158        public Date getAccessDate() {
159                return accessDate;
160        }
161
162        /**
163         * @param accessDate the accessDate to set
164         */
165        public void setAccessDate(Date accessDate) {
166                this.accessDate = accessDate;
167        }
168
169        /**
170         * @return the allowedScopes
171         */
172        @ElementCollection(fetch = FetchType.EAGER)
173        @CollectionTable(
174                        name="approved_site_scope",
175                        joinColumns=@JoinColumn(name="owner_id")
176                        )
177        @Column(name="scope")
178        public Set<String> getAllowedScopes() {
179                return allowedScopes;
180        }
181
182        /**
183         * @param allowedScopes the allowedScopes to set
184         */
185        public void setAllowedScopes(Set<String> allowedScopes) {
186                this.allowedScopes = allowedScopes;
187        }
188
189        /**
190         * @return the timeoutDate
191         */
192        @Basic
193        @Temporal(javax.persistence.TemporalType.TIMESTAMP)
194        @Column(name="timeout_date")
195        public Date getTimeoutDate() {
196                return timeoutDate;
197        }
198
199        /**
200         * @param timeoutDate the timeoutDate to set
201         */
202        public void setTimeoutDate(Date timeoutDate) {
203                this.timeoutDate = timeoutDate;
204        }
205
206        /**
207         * Has this approval expired?
208         * @return
209         */
210        @Transient
211        public boolean isExpired() {
212                if (getTimeoutDate() != null) {
213                        Date now = new Date();
214                        if (now.after(getTimeoutDate())) {
215                                return true;
216                        } else {
217                                return false;
218                        }
219                } else {
220                        return false;
221                }
222        }
223
224}