Add a temporary hard-coded notice for the extended token validity period. This should...
[pithos] / gss / src / gr / ebs / gss / server / domain / Nonce.java
1 /*
2  * Copyright 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.server.domain;
20
21 import java.io.UnsupportedEncodingException;
22 import java.net.URLEncoder;
23 import java.security.SecureRandom;
24 import java.util.Calendar;
25 import java.util.Date;
26
27 import javax.persistence.Entity;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.Id;
30 import javax.persistence.Temporal;
31 import javax.persistence.TemporalType;
32
33 import org.apache.commons.codec.binary.Base64;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 /**
38  * The class that holds an issued nonce for a user.
39  *
40  * @author past
41  */
42 @Entity
43 public class Nonce {
44
45         /**
46          * The logger.
47          */
48         private static Log logger = LogFactory.getLog(Nonce.class);
49
50         /**
51          * The nonce size in bytes.
52          */
53         private static final int NONCE_SIZE = 20;
54
55         /**
56          * The persistence ID of the object.
57          */
58         @Id
59         @GeneratedValue
60         private Long id;
61
62         /**
63          * The nonce issued for logging in this user.
64          */
65         private byte[] nonce;
66
67         /**
68          * The nonce encoded in Base64.
69          */
70         private String encodedNonce;
71
72         /**
73          * The time that the user's issued nonce
74          * will expire.
75          */
76         @Temporal(TemporalType.TIMESTAMP)
77         private Date nonceExpiryDate;
78
79
80         /**
81          * The ID of the user for whom this nonce was issued.
82          */
83         private Long userId;
84
85         /**
86          * Retrieve the id.
87          *
88          * @return the id
89          */
90         public Long getId() {
91                 return id;
92         }
93
94         /**
95          * Retrieve the nonce. If it is not valid or non-existent,
96          * this method returns null. Therefore, call sites must
97          * request a regeneration of the authentication token in
98          * both cases.
99          *
100          * @return the nonce
101          */
102         public byte[] getNonce() {
103                 if (isNonceValid())
104                         return nonce;
105                 return null;
106         }
107
108         /**
109          * Return true if the nonce is usable, or false
110          * if a new one must be regenerated.
111          *
112          * @return true if the nonce is valid
113          */
114         private boolean isNonceValid() {
115                 if (nonce == null)
116                         return false;
117                 if (nonceExpiryDate == null)
118                         return false;
119                 if (nonceExpiryDate.before(new Date()))
120                         return false;
121                 return true;
122         }
123
124         /**
125          * Creates a new nonce and resets its expiry date.
126          *
127          * @param userId the ID of the associated user
128          * @return a new nonce
129          */
130         public static Nonce createNonce(Long userId) {
131                 Nonce n = new Nonce();
132                 n.userId = userId;
133                 SecureRandom random = new SecureRandom();
134                 n.nonce = new byte[NONCE_SIZE];
135                 random.nextBytes(n.nonce);
136                 Calendar cal = Calendar.getInstance();
137                 // Set nonce time-to-live to 5 minutes.
138                 cal.add(Calendar.MINUTE, 5);
139                 n.nonceExpiryDate = cal.getTime();
140                 try {
141                         n.encodedNonce = URLEncoder.encode(new String(Base64.encodeBase64(n.nonce), "US-ASCII"), "US-ASCII");
142                 } catch (UnsupportedEncodingException e) {
143                         logger.error(e);
144                 }
145                 return n;
146         }
147
148         /**
149          * Retrieve the userId.
150          *
151          * @return the userId
152          */
153         public Long getUserId() {
154                 return userId;
155         }
156
157         /**
158          * Modify the nonce.
159          *
160          * @param aNonce the nonce to set
161          */
162         public void setNonce(byte[] aNonce) {
163                 nonce = aNonce;
164         }
165
166         /**
167          * Modify the nonceExpiryDate.
168          *
169          * @param aNonceExpiryDate the nonceExpiryDate to set
170          */
171         public void setNonceExpiryDate(Date aNonceExpiryDate) {
172                 nonceExpiryDate = aNonceExpiryDate;
173         }
174
175         /**
176          * Retrieve the encodedNonce.
177          *
178          * @return the encodedNonce
179          */
180         public String getEncodedNonce() {
181                 return encodedNonce;
182         }
183
184         /**
185          * Retrieve the nonceExpiryDate.
186          *
187          * @return the nonceExpiryDate
188          */
189         public Date getNonceExpiryDate() {
190                 return nonceExpiryDate;
191         }
192
193 }