View Javadoc
1   /*
2    * Copyright 2012-2013 the original author or authors.
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    *      https://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  
17  package org.springframework.security.oauth2.provider.approval;
18  
19  import java.util.Calendar;
20  import java.util.Date;
21  
22  import com.fasterxml.jackson.annotation.JsonIgnore;
23  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24  import com.fasterxml.jackson.databind.annotation.JsonSerialize;
25  
26  import org.springframework.security.oauth2.common.util.JsonDateDeserializer;
27  import org.springframework.security.oauth2.common.util.JsonDateSerializer;
28  
29  /**
30   * @author Dave Syer
31   * @author Vidya Val
32   *
33   */
34  @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
35  public class Approval {
36  
37  	private String userId;
38  
39  	private String clientId;
40  
41  	private String scope;
42  
43  	public enum ApprovalStatus {
44  		APPROVED,
45  		DENIED;
46  	}
47  
48  	private ApprovalStatus status;
49  	
50  	private Date expiresAt;
51  
52  	private Date lastUpdatedAt;
53  
54  	public Approval(String userId, String clientId, String scope, int expiresIn, ApprovalStatus status) {
55  		this(userId, clientId, scope, new Date(), status, new Date());
56  		Calendar expiresAt = Calendar.getInstance();
57  		expiresAt.add(Calendar.MILLISECOND, expiresIn);
58  		setExpiresAt(expiresAt.getTime());
59  	}
60  
61  	public Approval(String userId, String clientId, String scope, Date expiresAt, ApprovalStatus status) {
62  		this(userId, clientId, scope, expiresAt, status, new Date());
63  	}
64  
65  	public Approval(String userId, String clientId, String scope, Date expiresAt, ApprovalStatus status, Date lastUpdatedAt) {
66  		setUserId(userId);
67  		setClientId(clientId);
68  		setScope(scope);
69  		setExpiresAt(expiresAt);
70  		this.status = status;
71  		this.lastUpdatedAt = lastUpdatedAt;
72  	}
73  
74  	protected Approval() { }
75  
76  	public String getUserId() {
77  		return userId;
78  	}
79  
80  	public void setUserId(String userId) {
81  		this.userId = userId == null ? "" : userId;
82  	}
83  
84  	public String getClientId() {
85  		return clientId;
86  	}
87  
88  	public void setClientId(String clientId) {
89  		this.clientId = clientId == null ? "" : clientId;
90  	}
91  
92  	public String getScope() {
93  		return scope;
94  	}
95  
96  	public void setScope(String scope) {
97  		this.scope = scope == null ? "" : scope;
98  	}
99  
100 	@JsonSerialize(using = JsonDateSerializer.class, include = JsonSerialize.Inclusion.NON_NULL)
101 	public Date getExpiresAt() {
102 		return expiresAt;
103 	}
104 
105 	@JsonDeserialize(using = JsonDateDeserializer.class)
106 	public void setExpiresAt(Date expiresAt) {
107 		if (expiresAt == null) {
108 			Calendar thirtyMinFromNow = Calendar.getInstance();
109 			thirtyMinFromNow.add(Calendar.MINUTE, 30);
110 			expiresAt = thirtyMinFromNow.getTime();
111 		}
112 		this.expiresAt = expiresAt;
113 	}
114 
115 	@JsonSerialize(using = JsonDateSerializer.class, include = JsonSerialize.Inclusion.NON_NULL)
116 	public Date getLastUpdatedAt() {
117 		return lastUpdatedAt;
118 	}
119 
120 	@JsonDeserialize(using = JsonDateDeserializer.class)
121 	public void setLastUpdatedAt(Date lastUpdatedAt) {
122 		this.lastUpdatedAt = lastUpdatedAt;
123 	}
124 
125 	@JsonIgnore
126 	public boolean isCurrentlyActive() {
127 		return expiresAt != null && expiresAt.after(new Date());
128 	}
129 
130 	@JsonIgnore
131 	public boolean isApproved() {
132 		return isCurrentlyActive() && status==ApprovalStatus.APPROVED;
133 	}
134 
135 	public void setStatus(ApprovalStatus status) {
136 		this.status = status;
137 	}
138 
139 	public ApprovalStatus getStatus() {
140 		return status;
141 	}
142 
143 	@Override
144 	public int hashCode() {
145 		final int prime = 31;
146 		int result = 1;
147 		result = prime * result + userId.hashCode();
148 		result = prime * result + clientId.hashCode();
149 		result = prime * result + scope.hashCode();
150 		result = prime * result + status.hashCode();
151 		return result;
152 	}
153 
154 	@Override
155 	public boolean equals(Object o) {
156 		if (o == null || !(o instanceof Approval)) {
157 			return false;
158 		}
159 		Approval other = (Approval) o;
160 		return userId.equals(other.userId) && clientId.equals(other.clientId) && scope.equals(other.scope) && status == other.status;
161 	}
162 
163 	@Override
164 	public String toString() {
165 		return String.format("[%s, %s, %s, %s, %s, %s]", userId, scope, clientId, expiresAt, status.toString(), lastUpdatedAt);
166 	}
167 
168 }