Package com.authlete.common.util
Class UserCodeGenerator
- java.lang.Object
-
- com.authlete.common.util.UserCodeGenerator
-
public class UserCodeGenerator extends Object
Generator for end-user verification codes (user_code
) in Device Flow. See "6.1. User Code Recommendations" of RFC 8628 OAuth 2.0 Device Authorization Grant for recommendations for user code values.// The simplest example String userCode = new UserCodeGenerator().
generate()
; // The longest example String userCode = new UserCodeGenerator() .setCharacters
(UserCodeCharset
.BASE20
) .setLength
(8) .setRandom
(new SecureRandom()) .generate()
;- Since:
- 2.43
- See Also:
- RFC 8628 OAuth 2.0 Device Authorization Grant
-
-
Constructor Summary
Constructors Constructor Description UserCodeGenerator()
Constructor.UserCodeGenerator(int length)
Constructor.UserCodeGenerator(UserCodeCharset charset)
Constructor.UserCodeGenerator(UserCodeCharset charset, int length)
Constructor.UserCodeGenerator(UserCodeCharset charset, int length, Random random)
Constructor.UserCodeGenerator(String characters)
Constructor.UserCodeGenerator(String characters, int length)
Constructor.UserCodeGenerator(String characters, int length, Random random)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
generate()
Generate a user code.String
getCharacters()
Get the characters that may appear in generated user codes.int
getLength()
Get the length of generated user codes.Random
getRandom()
Get theRandom
instance used for user code generation.UserCodeGenerator
setCharacters(UserCodeCharset charset)
Set characters that may appear in generated user codes.UserCodeGenerator
setCharacters(String characters)
Set characters that may appear in generated user codes.UserCodeGenerator
setLength(int length)
Set length of generated user codes.UserCodeGenerator
setRandom(Random random)
Set aRandom
instance used for user code generation.
-
-
-
Constructor Detail
-
UserCodeGenerator
public UserCodeGenerator()
Constructor.
-
UserCodeGenerator
public UserCodeGenerator(String characters)
Constructor.- Parameters:
characters
- Characters that may appear in generated user codes.
-
UserCodeGenerator
public UserCodeGenerator(int length)
Constructor.- Parameters:
length
- Length of generated user codes.
-
UserCodeGenerator
public UserCodeGenerator(String characters, int length)
Constructor.- Parameters:
characters
- Characters that may appear in generated user codes.length
- Length of generated user codes.
-
UserCodeGenerator
public UserCodeGenerator(String characters, int length, Random random)
Constructor.- Parameters:
characters
- Characters that may appear in generated user codes.length
- Length of generated user codes.random
- ARandom
instance used for user code generation.
-
UserCodeGenerator
public UserCodeGenerator(UserCodeCharset charset)
Constructor.- Parameters:
charset
- A character set for user codes. The value returned fromcharset.
getCharacters()
is used unlesscharset
isnull
.
-
UserCodeGenerator
public UserCodeGenerator(UserCodeCharset charset, int length)
Constructor.- Parameters:
charset
- A character set for user codes. The value returned fromcharset.
getCharacters()
is used unlesscharset
isnull
.length
- Length of generated user codes.
-
UserCodeGenerator
public UserCodeGenerator(UserCodeCharset charset, int length, Random random)
Constructor.- Parameters:
charset
- A character set for user codes. The value returned fromcharset.
getCharacters()
is used unlesscharset
isnull
.length
- Length of generated user codes.random
- ARandom
instance used for user code generation.
-
-
Method Detail
-
getCharacters
public String getCharacters()
Get the characters that may appear in generated user codes.- Returns:
- The characters that may appear in generated user codes.
-
setCharacters
public UserCodeGenerator setCharacters(String characters)
Set characters that may appear in generated user codes.- Parameters:
characters
- Characters that may appear in generated user codes.- Returns:
this
object.
-
setCharacters
public UserCodeGenerator setCharacters(UserCodeCharset charset)
Set characters that may appear in generated user codes. The value returned fromcharset.
getCharacters()
is used as characters for generated user codes unlesscharset
isnull
.- Parameters:
charset
- A character set for user codes.- Returns:
this
object.
-
getLength
public int getLength()
Get the length of generated user codes.- Returns:
- The length of generated user codes.
-
setLength
public UserCodeGenerator setLength(int length)
Set length of generated user codes.- Parameters:
length
- Length of generated user codes.- Returns:
this
object.
-
getRandom
public Random getRandom()
Get theRandom
instance used for user code generation.- Returns:
- The
Random
instance user for user code generation.
-
setRandom
public UserCodeGenerator setRandom(Random random)
Set aRandom
instance used for user code generation.- Parameters:
random
- ARandom
instance used for user code generation.- Returns:
this
object.
-
generate
public String generate()
Generate a user code.If characters for user codes are not set, the value returned from
UserCodeCharset
.
BASE20
.
getCharacters()
(namely,"BCDFGHJKLMNPQRSTVWXZ"
) is used.If length is not set or its value is 0 or less, proper length is computed based on the characters and used.
If a
Random
instance is not set, a new instance ofSecureRandom
is created and used.- Returns:
- A newly-generated user code.
-
-