-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from VAuthenticator/random-password-generator
Random password generator
Showing
6 changed files
with
259 additions
and
3 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/vauthenticator/server/config/PasswordGeneratorConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.vauthenticator.server.config | ||
|
||
import com.vauthenticator.server.password.PasswordGenerator | ||
import com.vauthenticator.server.password.PasswordGeneratorCriteria | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@EnableConfigurationProperties(PasswordGeneratorCriteria::class) | ||
@Configuration(proxyBeanMethods = false) | ||
class PasswordGeneratorConfig { | ||
|
||
|
||
@Bean | ||
fun passwordGenerator(passwordGeneratorCriteria: PasswordGeneratorCriteria) = PasswordGenerator(passwordGeneratorCriteria) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
src/main/kotlin/com/vauthenticator/server/password/PasswordGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.vauthenticator.server.password | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import kotlin.random.Random | ||
|
||
@ConfigurationProperties(prefix = "password.generator-criteria") | ||
data class PasswordGeneratorCriteria( | ||
val upperCaseCharactersSize: Int, | ||
val lowerCaseCharactersSize: Int, | ||
val specialCharactersSize: Int, | ||
val numberCharactersSize: Int | ||
) { | ||
fun size() = upperCaseCharactersSize + specialCharactersSize + numberCharactersSize | ||
} | ||
|
||
class PasswordGenerator(private val passwordGeneratorCriteria: PasswordGeneratorCriteria) { | ||
|
||
fun generate(): String { | ||
val random = Random | ||
val builder = StringBuilder() | ||
val mutableListOf = mutableListOf<Char>() | ||
|
||
for (index in 1..passwordGeneratorCriteria.specialCharactersSize) { | ||
mutableListOf.add(specialCharactersAlphabet[random.nextInt(specialCharactersAlphabet.size - 1)]) | ||
} | ||
for (index in 1..passwordGeneratorCriteria.numberCharactersSize) { | ||
mutableListOf.add(numberAlphabet[random.nextInt(numberAlphabet.size - 1)]) | ||
} | ||
for (index in 1..passwordGeneratorCriteria.upperCaseCharactersSize) { | ||
mutableListOf.add(upperCaseAlphabet[random.nextInt(upperCaseAlphabet.size - 1)]) | ||
} | ||
for (index in 1..passwordGeneratorCriteria.lowerCaseCharactersSize) { | ||
mutableListOf.add(lowerCaseAlphabet[random.nextInt(lowerCaseAlphabet.size - 1)]) | ||
} | ||
|
||
for (index in 0..mutableListOf.size) { | ||
val nextInt = if (mutableListOf.size > 0) { | ||
random.nextInt(mutableListOf.size) | ||
} else { | ||
0 | ||
} | ||
|
||
if (mutableListOf.isNotEmpty()) { | ||
builder.append(mutableListOf[nextInt]) | ||
mutableListOf.removeAt(nextInt) | ||
} | ||
} | ||
|
||
return builder.toString() | ||
} | ||
} | ||
|
||
data class GeneratedPasswordResponse(val pwd: String) | ||
|
||
@RestController | ||
class PasswordGeneratorEndPoint(private val passwordGenerator: PasswordGenerator) { | ||
|
||
@PostMapping("/api/password") | ||
fun generate() = ResponseEntity.ok(GeneratedPasswordResponse(passwordGenerator.generate())) | ||
} | ||
|
||
val specialCharactersAlphabet = charArrayOf( | ||
'!', | ||
'@', | ||
'#', | ||
'$', | ||
'%', | ||
'^', | ||
'&', | ||
'*', | ||
'(', | ||
')', | ||
'-', | ||
'_', | ||
'=', | ||
'+', | ||
'[', | ||
']', | ||
'{', | ||
'}', | ||
';', | ||
':', | ||
'"', | ||
'\'', | ||
'\\', | ||
'|', | ||
'~', | ||
'`', | ||
',', | ||
'<', | ||
'.', | ||
'>', | ||
'/', | ||
'?', | ||
) | ||
val numberAlphabet = charArrayOf( | ||
'0', | ||
'1', | ||
'2', | ||
'3', | ||
'4', | ||
'5', | ||
'6', | ||
'7', | ||
'8', | ||
'9', | ||
) | ||
val lowerCaseAlphabet = charArrayOf( | ||
'a', | ||
'b', | ||
'c', | ||
'd', | ||
'e', | ||
'f', | ||
'g', | ||
'h', | ||
'i', | ||
'j', | ||
'k', | ||
'l', | ||
'm', | ||
'n', | ||
'o', | ||
'p', | ||
'q', | ||
'r', | ||
's', | ||
't', | ||
'u', | ||
'v', | ||
'w', | ||
'x', | ||
'y', | ||
'z' | ||
) | ||
val upperCaseAlphabet = charArrayOf( | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I', | ||
'J', | ||
'K', | ||
'L', | ||
'M', | ||
'N', | ||
'O', | ||
'P', | ||
'Q', | ||
'R', | ||
'S', | ||
'T', | ||
'U', | ||
'V', | ||
'W', | ||
'X', | ||
'Y', | ||
'Z' | ||
) |
45 changes: 45 additions & 0 deletions
45
src/test/kotlin/com/vauthenticator/server/password/PasswordGeneratorEndPointTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.vauthenticator.server.password | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.mockk.every | ||
import io.mockk.impl.annotations.MockK | ||
import io.mockk.junit5.MockKExtension | ||
import io.mockk.verify | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup | ||
|
||
@ExtendWith(MockKExtension::class) | ||
internal class PasswordGeneratorEndPointTest { | ||
|
||
private val objectMapper = ObjectMapper() | ||
|
||
lateinit var mokMvc: MockMvc | ||
|
||
@MockK | ||
lateinit var passwordGenerator: PasswordGenerator | ||
|
||
@BeforeEach | ||
internal fun setUp() { | ||
mokMvc = standaloneSetup( | ||
PasswordGeneratorEndPoint(passwordGenerator) | ||
).build() | ||
} | ||
|
||
@Test | ||
fun `when a new random password is generated`() { | ||
every { passwordGenerator.generate() } returns "A_RANDOM_PASSWORD" | ||
val expected = GeneratedPasswordResponse("A_RANDOM_PASSWORD") | ||
|
||
mokMvc.perform(post("/api/password")) | ||
.andExpect(content().json(objectMapper.writeValueAsString(expected))) | ||
.andExpect(status().isOk) | ||
|
||
verify { passwordGenerator.generate() } | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/kotlin/com/vauthenticator/server/password/PasswordGeneratorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.vauthenticator.server.password | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.Test | ||
|
||
private const val STRONG_PASSWORD_LENGTH = 20 | ||
|
||
class PasswordGeneratorTest { | ||
|
||
private val uut = PasswordGenerator(PasswordGeneratorCriteria(5, 5, 5, 5)) | ||
|
||
|
||
@Test | ||
fun name() { | ||
|
||
val actual = uut.generate() | ||
assertEquals(STRONG_PASSWORD_LENGTH, actual.length) | ||
assertTrue(actual.length == STRONG_PASSWORD_LENGTH) | ||
} | ||
|
||
} |