Debian Codegraph @

contribs/jersey-oauth/oauth-server/src/main/java/com/sun/jersey/oauth/server/NonceManager.java

raw
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common Development
8 * and Distribution License("CDDL") (collectively, the "License"). You
9 * may not use this file except in compliance with the License. You can
10 * obtain a copy of the License at
11 * http://glassfish.java.net/public/CDDL+GPL_1_1.html
12 * or packager/legal/LICENSE.txt. See the License for the specific
13 * language governing permissions and limitations under the License.
14 *
15 * When distributing the software, include this License Header Notice in each
16 * file and include the License file at packager/legal/LICENSE.txt.
17 *
18 * GPL Classpath Exception:
19 * Oracle designates this particular file as subject to the "Classpath"
20 * exception as provided by Oracle in the GPL Version 2 section of the License
21 * file that accompanied this code.
22 *
23 * Modifications:
24 * If applicable, add the following below the License Header, with the fields
25 * enclosed by brackets [] replaced by your own identifying information:
26 * "Portions Copyright [year] [name of copyright owner]"
27 *
28 * Contributor(s):
29 * If you wish your version of this file to be governed by only the CDDL or
30 * only the GPL Version 2, indicate your decision by adding "[Contributor]
31 * elects to include this software in this distribution under the [CDDL or GPL
32 * Version 2] license." If you don't indicate a single choice of license, a
33 * recipient has the option to distribute your version of this file under
34 * either the CDDL, the GPL Version 2 or to extend the choice of license to
35 * its licensees as provided above. However, if you add GPL Version 2 code
36 * and therefore, elected the GPL Version 2 license, then the option applies
37 * only if the new code is made subject to such option by the copyright
38 * holder.
39 */
40
41 package com.sun.jersey.oauth.server;
42
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.Map;
46 import java.util.Set;
47 import java.util.SortedMap;
48 import java.util.TreeMap;
49
50 /**
51 * Tracks the nonces for a given consumer key and/or token. Automagically
52 * ensures timestamp is monotonically increasing and tracks all nonces
53 * for a given timestamp.
54 *
55 * @author Paul C. Bryan
56 * @author Martin Matula (martin.matula at oracle.com)
57 * @author Thomas Meire
58 */
59 public final class NonceManager {
60 /**
61 * The maximum valid age of a nonce timestamp, in milliseconds.
62 */
63 private final long maxAge;
64
65 /**
66 * Verifications to perform on average before performing garbage collection.
67 */
68 private final int gcPeriod;
69
70 /**
71 * Counts number of verification requests performed to schedule garbage collection.
72 */
73 private int gcCounter = 0;
74
75 /**
76 * Maps timestamps to key-nonce pairs.
77 */
78 private final SortedMap<Long, Map<String, Set<String>>> tsToKeyNoncePairs = new TreeMap<Long, Map<String, Set<String>>>();
79
80 /**
81 * TODO: Description.
82 *
83 * @param maxAge the maximum valid age of a nonce timestamp, in milliseconds.
84 * @param gcPeriod verifications to perform on average before performing garbage collection.
85 */
86 public NonceManager(long maxAge, int gcPeriod) {
87 if (maxAge <= 0 || gcPeriod <= 0) {
88 throw new IllegalArgumentException();
89 }
90
91 this.maxAge = maxAge;
92 this.gcPeriod = gcPeriod;
93 }
94
95 /**
96 * Evaluates the timestamp/nonce combination for validity, storing and/or
97 * clearing nonces as required.
98 *
99 * @param key the oauth_consumer_key value for a given consumer request
100 * @param timestamp the oauth_timestamp value for a given consumer request.
101 * @param nonce the oauth_nonce value for a given consumer request.
102 * @return true if the timestamp/nonce are valid.
103 */
104 public synchronized boolean verify(String key, String timestamp, String nonce) {
105 long now = System.currentTimeMillis();
106
107 // convert timestamp to milliseconds since epoch to deal with uniformly
108 long stamp = longValue(timestamp) * 1000;
109
110 // invalid timestamp supplied; automatically invalid
111 if (stamp + maxAge < now) {
112 return false;
113 }
114
115 Map<String, Set<String>> keyToNonces = tsToKeyNoncePairs.get(stamp);
116 if (keyToNonces == null) {
117 keyToNonces = new HashMap<String, Set<String>>();
118 tsToKeyNoncePairs.put(stamp, keyToNonces);
119 }
120
121 Set<String> nonces = keyToNonces.get(key);
122 if (nonces == null) {
123 nonces = new HashSet<String>();
124 keyToNonces.put(key, nonces);
125 }
126
127 boolean result = nonces.add(nonce);
128
129 // perform garbage collection if counter is up to established number of passes
130 if (++gcCounter >= gcPeriod) {
131 gc(now);
132 }
133
134 // returns false if nonce already encountered for given timestamp
135 return result;
136 }
137
138 /**
139 * Deletes all nonces older than maxAge.
140 * This method is package private (instead of private) for testability purposes.
141 *
142 * @param now milliseconds since epoch representing "now"
143 */
144 void gc(long now) {
145 gcCounter = 0;
146 tsToKeyNoncePairs.headMap(now - maxAge).clear();
147 }
148
149 /**
150 * Returns number of currently tracked timestamp-key-nonce tuples. The method is used by tests.
151 * @return number of currently tracked timestamp-key-nonce tuples.
152 */
153 long size() {
154 long size = 0;
155 for (Map<String, Set<String>> keyToNonces : tsToKeyNoncePairs.values()) {
156 size += keyToNonces.values().size();
157 }
158 return size;
159 }
160
161 private static long longValue(String value) {
162 try {
163 return Long.valueOf(value);
164 } catch (NumberFormatException nfe) {
165 return -1;
166 }
167 }
168 }
169