@@ -8,7 +8,6 @@ import com.atlassian.onetime.model.TOTPSecret
88import java.net.URI
99import java.net.URLEncoder
1010import java.nio.charset.StandardCharsets
11- import java.time.Clock
1211
1312interface TOTPService {
1413
@@ -42,21 +41,32 @@ class DefaultTOTPService(
4241 private val totpConfiguration : TOTPConfiguration = TOTPConfiguration ()
4342) : TOTPService {
4443
44+ companion object {
45+ private const val SCHEME = " otpauth"
46+ private const val TYPE = " totp"
47+ private const val SECRET_QUERY_PARAM = " secret"
48+ private const val ISSUER_QUERY_PARAM = " issuer"
49+ private const val ALGORITHM_QUERY_PARAM = " algorithm"
50+ private const val DIGITS_QUERY_PARAM = " digits"
51+ private const val PERIOD_QUERY_PARAM = " period"
52+
53+ }
4554 override suspend fun generateTotpSecret (): TOTPSecret = totpConfiguration.secretProvider.generateSecret()
4655
4756 override suspend fun generateTOTPUrl (
4857 totpSecret : TOTPSecret ,
4958 emailAddress : EmailAddress ,
5059 issuer : Issuer
5160 ): URI {
61+
5262 val encodedEmailAddress: String = URLEncoder .encode(emailAddress.value, StandardCharsets .UTF_8 )
5363 val encodedIssuer: String = URLEncoder .encode(issuer.value, StandardCharsets .UTF_8 )
54- val template = " otpauth ://totp /$encodedIssuer :$encodedEmailAddress ?" +
55- " secret =${totpSecret.base32Encoded} " +
56- " &issuer =$encodedIssuer " +
57- " &algorithm =${totpGenerator.digest.toQueryParam()} " +
58- " &digits =${totpGenerator.otpLength.value} " +
59- " &period =${totpGenerator.timeStepSeconds} "
64+ val template = " $SCHEME ://$TYPE /$encodedIssuer :$encodedEmailAddress ?" +
65+ " $SECRET_QUERY_PARAM =${totpSecret.base32Encoded} " +
66+ " &$ISSUER_QUERY_PARAM =$encodedIssuer " +
67+ " &$ALGORITHM_QUERY_PARAM =${totpGenerator.digest.toQueryParam()} " +
68+ " &$DIGITS_QUERY_PARAM =${totpGenerator.otpLength.value} " +
69+ " &$PERIOD_QUERY_PARAM =${totpGenerator.timeStepSeconds} "
6070 return URI (template)
6171 }
6272
0 commit comments