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