/** * <p>Creates a random string whose length is between the inclusive minimum and * the exclusive maximum.</p> * * <p>Characters will be chosen from the set of \p{Graph} characters.</p> * * @param minLengthInclusive the inclusive minimum length of the string to generate * @param maxLengthExclusive the exclusive maximum length of the string to generate * @return the random string * @since 3.5 */ public static String randomGraph(final int minLengthInclusive, final int maxLengthExclusive) { return randomGraph(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); }
@Test public void testRandomGraphRange() { final int expectedMinLengthInclusive = 1; final int expectedMaxLengthExclusive = 11; final String pattern = "^\\p{Graph}{" + expectedMinLengthInclusive + ',' + expectedMaxLengthExclusive + "}$"; int maxCreatedLength = expectedMinLengthInclusive; int minCreatedLength = expectedMaxLengthExclusive - 1; for (int i = 0; i < 1000; i++) { final String s = RandomStringUtils.randomGraph(expectedMinLengthInclusive, expectedMaxLengthExclusive); assertThat("within range", s.length(), allOf(greaterThanOrEqualTo(expectedMinLengthInclusive), lessThanOrEqualTo(expectedMaxLengthExclusive - 1))); assertTrue(s, s.matches(pattern)); if (s.length() < minCreatedLength) { minCreatedLength = s.length(); } if (s.length() > maxCreatedLength) { maxCreatedLength = s.length(); } } assertThat("min generated, may fail randomly rarely", minCreatedLength, is(expectedMinLengthInclusive)); assertThat("max generated, may fail randomly rarely", maxCreatedLength, is(expectedMaxLengthExclusive - 1)); }
assertTrue("!r1.equals(r2)", !r1.equals(r2)); r1 = RandomStringUtils.randomGraph(50); assertEquals("randomGraph(50) length", 50, r1.length()); for(int i = 0; i < r1.length(); i++) { assertTrue("char between 33 and 126", r1.charAt(i) >= 33 && r1.charAt(i) <= 126); r2 = RandomStringUtils.randomGraph(50); assertTrue("!r1.equals(r2)", !r1.equals(r2));
/** * <p>Creates a random string whose length is between the inclusive minimum and * the exclusive maximum.</p> * * <p>Characters will be chosen from the set of \p{Graph} characters.</p> * * @param minLengthInclusive the inclusive minimum length of the string to generate * @param maxLengthExclusive the exclusive maximum length of the string to generate * @return the random string * @since 3.5 */ public static String randomGraph(final int minLengthInclusive, final int maxLengthExclusive) { return randomGraph(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); }
/** * <p>Creates a random string whose length is between the inclusive minimum and * the exclusive maximum.</p> * * <p>Characters will be chosen from the set of \p{Graph} characters.</p> * * @param minLengthInclusive the inclusive minimum length of the string to generate * @param maxLengthExclusive the exclusive maximum length of the string to generate * @return the random string * @since 3.5 */ public static String randomGraph(final int minLengthInclusive, final int maxLengthExclusive) { return randomGraph(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); }
/** * <p>Creates a random string whose length is between the inclusive minimum and * the exclusive maximum.</p> * * <p>Characters will be chosen from the set of \p{Graph} characters.</p> * * @param minLengthInclusive the inclusive minimum length of the string to generate * @param maxLengthExclusive the exclusive maximum length of the string to generate * @return the random string * @since 3.5 */ public static String randomGraph(final int minLengthInclusive, final int maxLengthExclusive) { return randomGraph(RandomUtils.nextInt(minLengthInclusive, maxLengthExclusive)); }
private String generateUserMainSecret(UserModel userModel, String secretAttrName, byte[] secretEncryptionPasswordPBKDF2) { String userMainSecretPlain = RandomStringUtils.randomGraph(16); Builder headerBuilder = new JWEHeader.Builder(JWEAlgorithm.DIR, EncryptionMethod.A128GCM); JWEObject jweObj = new JWEObject(headerBuilder.build(), new Payload(userMainSecretPlain)); try { jweObj.encrypt(new DirectEncrypter(secretEncryptionPasswordPBKDF2)); } catch (JOSEException e) { throw new IllegalStateException(e); } String customSecretAttr = jweObj.serialize(); userModel.setAttribute(secretAttrName, Arrays.asList(customSecretAttr)); return userMainSecretPlain; }
private void loadUserCredentials(UserDataRepository userDataService, List<ResourceServerAndSecret> resurceServers, String user, String password){ if(userDataService==null) return; // If Resources are set, we can get or create the corresponding user secrets and have them included in the token. UserCredentials userCredentials = userDataService.loadUserCredentials(user, password); boolean store = false; if(userCredentials==null){ userCredentials = new UserCredentials(); store = true; } for (ResourceServerAndSecret resourceServer : resurceServers) { String credentialForResourceServer = userCredentials.getCredentialForResourceServer(resourceServer.getResourceServer().getAudience()); if(credentialForResourceServer==null){ // create one credentialForResourceServer = RandomStringUtils.randomGraph(16); userCredentials.setCredentialForResourceServer(resourceServer.getResourceServer().getAudience(), credentialForResourceServer); store = true; } resourceServer.setRawSecret(credentialForResourceServer); } if(store){ userDataService.storeUserCredentials(user, password, userCredentials); } }