001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *******************************************************************************/
016
017package org.mitre.oauth2.model;
018
019import java.util.Date;
020import java.util.Map;
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.ManyToOne;
034import javax.persistence.MapKeyColumn;
035import javax.persistence.NamedQueries;
036import javax.persistence.NamedQuery;
037import javax.persistence.Table;
038import javax.persistence.Temporal;
039
040/**
041 * @author jricher
042 *
043 */
044@Entity
045@Table(name = "device_code")
046@NamedQueries({
047        @NamedQuery(name = DeviceCode.QUERY_BY_USER_CODE, query = "select d from DeviceCode d where d.userCode = :" + DeviceCode.PARAM_USER_CODE),
048        @NamedQuery(name = DeviceCode.QUERY_BY_DEVICE_CODE, query = "select d from DeviceCode d where d.deviceCode = :" + DeviceCode.PARAM_DEVICE_CODE),
049        @NamedQuery(name = DeviceCode.QUERY_EXPIRED_BY_DATE, query = "select d from DeviceCode d where d.expiration <= :" + DeviceCode.PARAM_DATE)
050})
051public class DeviceCode {
052
053        public static final String QUERY_BY_USER_CODE = "DeviceCode.queryByUserCode";
054        public static final String QUERY_BY_DEVICE_CODE = "DeviceCode.queryByDeviceCode";
055        public static final String QUERY_EXPIRED_BY_DATE = "DeviceCode.queryExpiredByDate";
056
057        public static final String PARAM_USER_CODE = "userCode";
058        public static final String PARAM_DEVICE_CODE = "deviceCode";
059        public static final String PARAM_DATE = "date";
060
061        private Long id;
062        private String deviceCode;
063        private String userCode;
064        private Set<String> scope;
065        private Date expiration;
066        private String clientId;
067        private Map<String, String> requestParameters;
068        private boolean approved;
069        private AuthenticationHolderEntity authenticationHolder;
070
071        public DeviceCode() {
072
073        }
074
075        public DeviceCode(String deviceCode, String userCode, Set<String> scope, String clientId, Map<String, String> params) {
076                this.deviceCode = deviceCode;
077                this.userCode = userCode;
078                this.scope = scope;
079                this.clientId = clientId;
080                this.requestParameters = params;
081        }
082
083        /**
084         * @return the id
085         */
086        @Id
087        @GeneratedValue(strategy = GenerationType.IDENTITY)
088        @Column(name = "id")
089        public Long getId() {
090                return id;
091        }
092
093        /**
094         * @param id the id to set
095         */
096        public void setId(Long id) {
097                this.id = id;
098        }
099
100        /**
101         * @return the deviceCode
102         */
103        @Basic
104        @Column(name = "device_code")
105        public String getDeviceCode() {
106                return deviceCode;
107        }
108
109        /**
110         * @param deviceCode the deviceCode to set
111         */
112        public void setDeviceCode(String deviceCode) {
113                this.deviceCode = deviceCode;
114        }
115
116        /**
117         * @return the userCode
118         */
119        @Basic
120        @Column(name = "user_code")
121        public String getUserCode() {
122                return userCode;
123        }
124
125        /**
126         * @param userCode the userCode to set
127         */
128        public void setUserCode(String userCode) {
129                this.userCode = userCode;
130        }
131
132        /**
133         * @return the scope
134         */
135        @ElementCollection(fetch = FetchType.EAGER)
136        @CollectionTable(
137                        name="device_code_scope",
138                        joinColumns=@JoinColumn(name="owner_id")
139                        )
140        @Column(name="scope")
141        public Set<String> getScope() {
142                return scope;
143        }
144
145        /**
146         * @param scope the scope to set
147         */
148        public void setScope(Set<String> scope) {
149                this.scope = scope;
150        }
151
152        @Basic
153        @Temporal(javax.persistence.TemporalType.TIMESTAMP)
154        @Column(name = "expiration")
155        public Date getExpiration() {
156                return expiration;
157        }
158
159        public void setExpiration(Date expiration) {
160                this.expiration = expiration;
161        }
162
163        /**
164         * @return the clientId
165         */
166        @Basic
167        @Column(name = "client_id")
168        public String getClientId() {
169                return clientId;
170        }
171
172        /**
173         * @param clientId the clientId to set
174         */
175        public void setClientId(String clientId) {
176                this.clientId = clientId;
177        }
178
179        /**
180         * @return the params
181         */
182        @ElementCollection(fetch = FetchType.EAGER)
183        @CollectionTable(
184                        name="device_code_request_parameter",
185                        joinColumns=@JoinColumn(name="owner_id")
186                        )
187        @Column(name="val")
188        @MapKeyColumn(name="param")
189        public Map<String, String> getRequestParameters() {
190                return requestParameters;
191        }
192
193        /**
194         * @param params the params to set
195         */
196        public void setRequestParameters(Map<String, String> params) {
197                this.requestParameters = params;
198        }
199
200        /**
201         * @return the approved
202         */
203        @Basic
204        @Column(name = "approved")
205        public boolean isApproved() {
206                return approved;
207        }
208
209        /**
210         * @param approved the approved to set
211         */
212        public void setApproved(boolean approved) {
213                this.approved = approved;
214        }
215
216        /**
217         * The authentication in place when this token was created.
218         * @return the authentication
219         */
220        @ManyToOne
221        @JoinColumn(name = "auth_holder_id")
222        public AuthenticationHolderEntity getAuthenticationHolder() {
223                return authenticationHolder;
224        }
225
226        /**
227         * @param authentication the authentication to set
228         */
229        public void setAuthenticationHolder(AuthenticationHolderEntity authenticationHolder) {
230                this.authenticationHolder = authenticationHolder;
231        }
232
233
234}