From e99a0bc7714a2041d7759cda17f474b094f61d1c Mon Sep 17 00:00:00 2001 From: KKlochko Date: Fri, 1 Dec 2023 14:04:47 +0200 Subject: [PATCH] Add factories to create a token as a string. --- pom.xml | 11 ++++++ .../security/tokens/SimpleRandomToken.java | 17 +++++++++ .../security/tokens/SimpleToken.java | 8 ++++ .../security/tokens/StringTokenFactory.java | 6 +++ .../tokens/SimpleRandomTokenSpec.groovy | 38 +++++++++++++++++++ .../security/tokens/SimpleTokenSpec.groovy | 24 ++++++++++++ 6 files changed, 104 insertions(+) create mode 100644 src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleRandomToken.java create mode 100644 src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleToken.java create mode 100644 src/main/java/space/kklochko/spring_rest_example/security/tokens/StringTokenFactory.java create mode 100644 src/test/groovy/space/kklochko/spring_rest_example/security/tokens/SimpleRandomTokenSpec.groovy create mode 100644 src/test/groovy/space/kklochko/spring_rest_example/security/tokens/SimpleTokenSpec.groovy diff --git a/pom.xml b/pom.xml index 86d0bae..91c413d 100644 --- a/pom.xml +++ b/pom.xml @@ -100,6 +100,17 @@ + + commons-codec + commons-codec + 1.16.0 + + + org.apache.commons + commons-lang3 + 3.13.0 + + org.springframework spring-core diff --git a/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleRandomToken.java b/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleRandomToken.java new file mode 100644 index 0000000..c2a0f21 --- /dev/null +++ b/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleRandomToken.java @@ -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); + } +} + diff --git a/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleToken.java b/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleToken.java new file mode 100644 index 0000000..520f9c1 --- /dev/null +++ b/src/main/java/space/kklochko/spring_rest_example/security/tokens/SimpleToken.java @@ -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); + } +} + diff --git a/src/main/java/space/kklochko/spring_rest_example/security/tokens/StringTokenFactory.java b/src/main/java/space/kklochko/spring_rest_example/security/tokens/StringTokenFactory.java new file mode 100644 index 0000000..41f03a1 --- /dev/null +++ b/src/main/java/space/kklochko/spring_rest_example/security/tokens/StringTokenFactory.java @@ -0,0 +1,6 @@ +package space.kklochko.spring_rest_example.security.tokens; + +abstract public class StringTokenFactory { + public abstract String create(String input); +} + diff --git a/src/test/groovy/space/kklochko/spring_rest_example/security/tokens/SimpleRandomTokenSpec.groovy b/src/test/groovy/space/kklochko/spring_rest_example/security/tokens/SimpleRandomTokenSpec.groovy new file mode 100644 index 0000000..99e1586 --- /dev/null +++ b/src/test/groovy/space/kklochko/spring_rest_example/security/tokens/SimpleRandomTokenSpec.groovy @@ -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 + } +} + diff --git a/src/test/groovy/space/kklochko/spring_rest_example/security/tokens/SimpleTokenSpec.groovy b/src/test/groovy/space/kklochko/spring_rest_example/security/tokens/SimpleTokenSpec.groovy new file mode 100644 index 0000000..ad2ac72 --- /dev/null +++ b/src/test/groovy/space/kklochko/spring_rest_example/security/tokens/SimpleTokenSpec.groovy @@ -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" + } +} +