Skip to content

Commit

Permalink
Merge pull request #56 from atlassian/noissue/extracting-constants-fo…
Browse files Browse the repository at this point in the history
…r-URI-building
  • Loading branch information
diegoocampoh authored Mar 26, 2023
2 parents 89ebee1 + 15b2b2c commit e06a72f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Java/Kotlin lightweight implementation of RFC-6238 and RFC-4226 to generate and

## Maven / gradle dependency

Check the latest package at [https://github.com/atlassian/1time/packages/](https://github.com/atlassian/1time/packages/)
Check the latest package at [https://github.com/atlassian/1time/packages/](https://github.com/atlassian/1time/packages/).

Also check Maven central on: https://mvnrepository.com/artifact/com.atlassian/onetime

## Quick start

Expand Down
24 changes: 17 additions & 7 deletions src/main/kotlin/com/atlassian/onetime/service/TOTPService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.atlassian.onetime.model.TOTPSecret
import java.net.URI
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
import java.time.Clock

interface TOTPService {

Expand Down Expand Up @@ -42,21 +41,32 @@ class DefaultTOTPService(
private val totpConfiguration: TOTPConfiguration = TOTPConfiguration()
) : TOTPService {

companion object {
private const val SCHEME = "otpauth"
private const val TYPE = "totp"
private const val SECRET_QUERY_PARAM = "secret"
private const val ISSUER_QUERY_PARAM = "issuer"
private const val ALGORITHM_QUERY_PARAM = "algorithm"
private const val DIGITS_QUERY_PARAM = "digits"
private const val PERIOD_QUERY_PARAM = "period"

}
override suspend fun generateTotpSecret(): TOTPSecret = totpConfiguration.secretProvider.generateSecret()

override suspend fun generateTOTPUrl(
totpSecret: TOTPSecret,
emailAddress: EmailAddress,
issuer: Issuer
): URI {

val encodedEmailAddress: String = URLEncoder.encode(emailAddress.value, StandardCharsets.UTF_8)
val encodedIssuer: String = URLEncoder.encode(issuer.value, StandardCharsets.UTF_8)
val template = "otpauth://totp/$encodedIssuer:$encodedEmailAddress?" +
"secret=${totpSecret.base32Encoded}" +
"&issuer=$encodedIssuer" +
"&algorithm=${totpGenerator.digest.toQueryParam()}" +
"&digits=${totpGenerator.otpLength.value}" +
"&period=${totpGenerator.timeStepSeconds}"
val template = "$SCHEME://$TYPE/$encodedIssuer:$encodedEmailAddress?" +
"$SECRET_QUERY_PARAM=${totpSecret.base32Encoded}" +
"&$ISSUER_QUERY_PARAM=$encodedIssuer" +
"&$ALGORITHM_QUERY_PARAM=${totpGenerator.digest.toQueryParam()}" +
"&$DIGITS_QUERY_PARAM=${totpGenerator.otpLength.value}" +
"&$PERIOD_QUERY_PARAM=${totpGenerator.timeStepSeconds}"
return URI(template)
}

Expand Down

0 comments on commit e06a72f

Please sign in to comment.