Skip to content
This repository was archived by the owner on Jun 18, 2023. It is now read-only.

Commit 8246a19

Browse files
author
pavlyi1
authored
Add files via upload
1 parent 4a65767 commit 8246a19

31 files changed

+7045
-0
lines changed

src/main/java/com/warrenstrange/googleauth/GoogleAuthenticator.java

Lines changed: 673 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
/*
2+
* Copyright (c) 2014-2018 Enrico M. Crisostomo
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* * Neither the name of the author nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
package com.warrenstrange.googleauth;
32+
33+
import java.util.concurrent.TimeUnit;
34+
35+
public class GoogleAuthenticatorConfig
36+
{
37+
private long timeStepSizeInMillis = TimeUnit.SECONDS.toMillis(30);
38+
private int windowSize = 3;
39+
private int codeDigits = 6;
40+
private int numberOfScratchCodes = 5;
41+
private int keyModulus = (int) Math.pow(10, codeDigits);
42+
private int secretBits = 160;
43+
private KeyRepresentation keyRepresentation = KeyRepresentation.BASE32;
44+
private HmacHashFunction hmacHashFunction = HmacHashFunction.HmacSHA1;
45+
46+
/**
47+
* Returns the key module.
48+
*
49+
* @return the key module.
50+
*/
51+
public int getKeyModulus()
52+
{
53+
return keyModulus;
54+
}
55+
56+
/**
57+
* Returns the key representation.
58+
*
59+
* @return the key representation.
60+
*/
61+
public KeyRepresentation getKeyRepresentation()
62+
{
63+
return keyRepresentation;
64+
}
65+
66+
/**
67+
* Returns the number of digits in the generated code.
68+
*
69+
* @return the number of digits in the generated code.
70+
*/
71+
public int getCodeDigits()
72+
{
73+
return codeDigits;
74+
}
75+
76+
/**
77+
* Returns the number of scratch codes to generate. We are using Google's default of providing 5 scratch codes.
78+
*
79+
* @return the number of scratch codes to generate.
80+
*/
81+
public int getNumberOfScratchCodes()
82+
{
83+
return numberOfScratchCodes;
84+
}
85+
86+
/**
87+
* Returns the time step size, in milliseconds, as specified by RFC 6238.
88+
* The default value is 30.000.
89+
*
90+
* @return the time step size in milliseconds.
91+
*/
92+
public long getTimeStepSizeInMillis()
93+
{
94+
return timeStepSizeInMillis;
95+
}
96+
97+
/**
98+
* Returns an integer value representing the number of windows of size
99+
* timeStepSizeInMillis that are checked during the validation process,
100+
* to account for differences between the server and the client clocks.
101+
* The bigger the window, the more tolerant the library code is about
102+
* clock skews.
103+
* <p>
104+
* We are using Google's default behaviour of using a window size equal
105+
* to 3. The limit on the maximum window size, present in older
106+
* versions of this library, has been removed.
107+
*
108+
* @return the window size.
109+
* @see #timeStepSizeInMillis
110+
*/
111+
public int getWindowSize()
112+
{
113+
return windowSize;
114+
}
115+
116+
/**
117+
* Returns the number of bits of the secret keys to generate. The length
118+
* should always be a multiple of 8. The default value is 160 bits, and
119+
* a value smaller than 128 is disallowed, as recommended by RFC 4226 §4.
120+
*
121+
* @return the secret size in bits.
122+
*/
123+
public int getSecretBits() {
124+
return secretBits;
125+
}
126+
127+
/**
128+
* Returns the cryptographic hash function used to calculate the HMAC (Hash-based
129+
* Message Authentication Code). This implementation uses the SHA1 hash
130+
* function by default.
131+
* <p>
132+
*
133+
* @return the HMAC hash function.
134+
*/
135+
public HmacHashFunction getHmacHashFunction()
136+
{
137+
return hmacHashFunction;
138+
}
139+
140+
public static class GoogleAuthenticatorConfigBuilder
141+
{
142+
private GoogleAuthenticatorConfig config = new GoogleAuthenticatorConfig();
143+
144+
public GoogleAuthenticatorConfig build()
145+
{
146+
return config;
147+
}
148+
149+
public GoogleAuthenticatorConfigBuilder setCodeDigits(int codeDigits)
150+
{
151+
if (codeDigits <= 0)
152+
{
153+
throw new IllegalArgumentException("Code digits must be positive.");
154+
}
155+
156+
if (codeDigits < 6)
157+
{
158+
throw new IllegalArgumentException("The minimum number of digits is 6.");
159+
}
160+
161+
if (codeDigits > 8)
162+
{
163+
throw new IllegalArgumentException("The maximum number of digits is 8.");
164+
}
165+
166+
config.codeDigits = codeDigits;
167+
config.keyModulus = (int) Math.pow(10, codeDigits);
168+
return this;
169+
}
170+
171+
public GoogleAuthenticatorConfigBuilder setNumberOfScratchCodes(int numberOfScratchCodes)
172+
{
173+
if (numberOfScratchCodes < 0)
174+
{
175+
throw new IllegalArgumentException("The number of scratch codes must not be negative");
176+
}
177+
178+
if (numberOfScratchCodes > 1_000)
179+
{
180+
throw new IllegalArgumentException("The maximum number of scratch codes is 1000");
181+
}
182+
183+
config.numberOfScratchCodes = numberOfScratchCodes;
184+
return this;
185+
}
186+
187+
public GoogleAuthenticatorConfigBuilder setTimeStepSizeInMillis(long timeStepSizeInMillis)
188+
{
189+
if (timeStepSizeInMillis <= 0)
190+
{
191+
throw new IllegalArgumentException("Time step size must be positive.");
192+
}
193+
194+
config.timeStepSizeInMillis = timeStepSizeInMillis;
195+
return this;
196+
}
197+
198+
public GoogleAuthenticatorConfigBuilder setWindowSize(int windowSize)
199+
{
200+
if (windowSize <= 0)
201+
{
202+
throw new IllegalArgumentException("Window number must be positive.");
203+
}
204+
205+
config.windowSize = windowSize;
206+
return this;
207+
}
208+
209+
public GoogleAuthenticatorConfigBuilder setSecretBits(int secretBits)
210+
{
211+
if (secretBits < 128)
212+
{
213+
throw new IllegalArgumentException("Secret bits must be greater than or equal to 128.");
214+
}
215+
216+
if (secretBits % 8 != 0)
217+
{
218+
throw new IllegalArgumentException("Secret bits must be a multiple of 8.");
219+
}
220+
221+
config.secretBits = secretBits;
222+
return this;
223+
}
224+
225+
public GoogleAuthenticatorConfigBuilder setKeyRepresentation(KeyRepresentation keyRepresentation)
226+
{
227+
if (keyRepresentation == null)
228+
{
229+
throw new IllegalArgumentException("Key representation cannot be null.");
230+
}
231+
232+
config.keyRepresentation = keyRepresentation;
233+
return this;
234+
}
235+
236+
public GoogleAuthenticatorConfigBuilder setHmacHashFunction(HmacHashFunction hmacHashFunction)
237+
{
238+
if (hmacHashFunction == null)
239+
{
240+
throw new IllegalArgumentException("HMAC Hash Function cannot be null.");
241+
}
242+
243+
config.hmacHashFunction = hmacHashFunction;
244+
return this;
245+
}
246+
}
247+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2014-2017 Enrico M. Crisostomo
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* * Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* * Neither the name of the author nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
package com.warrenstrange.googleauth;
32+
33+
/**
34+
* Date: 12/02/14
35+
* Time: 13:36
36+
*
37+
* @author Enrico M. Crisostomo
38+
*/
39+
@SuppressWarnings("serial")
40+
public class GoogleAuthenticatorException extends RuntimeException
41+
{
42+
43+
/**
44+
* Builds an exception with the provided error message.
45+
*
46+
* @param message the error message.
47+
*/
48+
public GoogleAuthenticatorException(String message)
49+
{
50+
super(message);
51+
}
52+
53+
/**
54+
* Builds an exception with the provided error mesasge and
55+
* the provided cuase.
56+
*
57+
* @param message the error message.
58+
* @param cause the cause.
59+
*/
60+
public GoogleAuthenticatorException(String message, Throwable cause)
61+
{
62+
super(message, cause);
63+
}
64+
}

0 commit comments

Comments
 (0)