Add factories to create a token as a string.
continuous-integration/drone/push Build is passing Details

main
KKlochko 1 year ago
parent 23bdfd10cf
commit e99a0bc771

@ -100,6 +100,17 @@
</dependencyManagement> </dependencyManagement>
<dependencies> <dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.16.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <artifactId>spring-core</artifactId>

@ -0,0 +1,17 @@
package space.kklochko.spring_rest_example.security.tokens;
import org.apache.commons.lang3.RandomStringUtils;
public class SimpleRandomToken extends StringTokenFactory {
public String create(String input) {
String salt = getRandomSalt(32);
return org.apache.commons.codec.digest.DigestUtils.sha256Hex(input + salt);
}
private String getRandomSalt(int length) {
boolean useLetters = true;
boolean useNumbers = true;
return RandomStringUtils.random(length, useLetters, useNumbers);
}
}

@ -0,0 +1,8 @@
package space.kklochko.spring_rest_example.security.tokens;
public class SimpleToken extends StringTokenFactory {
public String create(String input) {
return org.apache.commons.codec.digest.DigestUtils.sha256Hex(input);
}
}

@ -0,0 +1,6 @@
package space.kklochko.spring_rest_example.security.tokens;
abstract public class StringTokenFactory {
public abstract String create(String input);
}

@ -0,0 +1,38 @@
package space.kklochko.spring_rest_example.security.tokens
import spock.lang.Specification
import spock.lang.Subject
class SimpleRandomTokenSpec extends Specification {
def "Create a token that based on sha256"() {
given: "I have a token factory"
@Subject
def factory = new SimpleRandomToken()
and: "I have a input data"
String input = "hello"
when: "I create a token"
String token = factory.create(input)
then: "The token is the same if the same input data."
token.length() == 64
}
def "Token must be different for the same input data"() {
given: "I have a token factory"
@Subject
def factory = new SimpleRandomToken()
and: "I have a input data"
String input = "hello"
when: "I create tokens"
String token = factory.create(input)
String token2 = factory.create(input)
then: "The tokens are different"
token != token2
}
}

@ -0,0 +1,24 @@
package space.kklochko.spring_rest_example.security.tokens
import spock.lang.Specification
import spock.lang.Subject
class SimpleTokenSpec extends Specification {
def "Create a token that based on sha256"() {
given: "I have a token factory"
@Subject
def factory = new SimpleToken()
when: "I create a token"
String token = factory.create(input)
then: "The token is the same if the same input data."
expectedToken == token
where: "I have the expected data"
input || expectedToken
"abc" || "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
"random numbers232341" || "7f4b80b2b09ae4326f78b1a9782c807af0a7d0194bffb05602a6585466b4c43e"
}
}
Loading…
Cancel
Save