Skip to content

Commit e06a72f

Browse files
authored
Merge pull request #56 from atlassian/noissue/extracting-constants-for-URI-building
2 parents 89ebee1 + 15b2b2c commit e06a72f

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ Java/Kotlin lightweight implementation of RFC-6238 and RFC-4226 to generate and
55

66
## Maven / gradle dependency
77

8-
Check the latest package at [https://github.com/atlassian/1time/packages/](https://github.com/atlassian/1time/packages/)
8+
Check the latest package at [https://github.com/atlassian/1time/packages/](https://github.com/atlassian/1time/packages/).
9+
10+
Also check Maven central on: https://mvnrepository.com/artifact/com.atlassian/onetime
911

1012
## Quick start
1113

src/main/kotlin/com/atlassian/onetime/service/TOTPService.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.atlassian.onetime.model.TOTPSecret
88
import java.net.URI
99
import java.net.URLEncoder
1010
import java.nio.charset.StandardCharsets
11-
import java.time.Clock
1211

1312
interface 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

Comments
 (0)