-
Notifications
You must be signed in to change notification settings - Fork 4
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 #238 from aivanovski/feature/otp-support
Add TOTP/HOTP support
- Loading branch information
Showing
66 changed files
with
2,291 additions
and
195 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,14 @@ import app.keemobile.kotpass.database.encode | |
import com.ivanovsky.passnotes.data.entity.PropertyType | ||
import com.ivanovsky.passnotes.data.repository.file.databaseDsl.EntryEntity | ||
import com.ivanovsky.passnotes.data.repository.file.databaseDsl.GroupEntity | ||
import com.ivanovsky.passnotes.data.repository.file.databaseDsl.KotpassTreeDsl | ||
import com.ivanovsky.passnotes.data.repository.file.databaseDsl.KotpassTreeDsl.tree | ||
import java.io.ByteArrayOutputStream | ||
import java.util.UUID | ||
|
||
class FakeFileContentFactory { | ||
|
||
fun createDefaultLocalDatabase(): ByteArray { | ||
return KotpassTreeDsl.tree(ROOT) { | ||
return tree(ROOT) { | ||
group(GROUP_EMAIL) | ||
group(GROUP_INTERNET) { | ||
group(GROUP_CODING) { | ||
|
@@ -37,7 +37,7 @@ class FakeFileContentFactory { | |
} | ||
|
||
fun createDefaultRemoteDatabase(): ByteArray { | ||
return KotpassTreeDsl.tree(ROOT) { | ||
return tree(ROOT) { | ||
group(GROUP_EMAIL) | ||
group(GROUP_INTERNET) { | ||
group(GROUP_CODING) { | ||
|
@@ -64,6 +64,14 @@ class FakeFileContentFactory { | |
.toByteArray() | ||
} | ||
|
||
fun createDatabaseWithOtpData(): ByteArray { | ||
return tree(ROOT) { | ||
entry(ENTRY_TOTP) | ||
entry(ENTRY_HOTP) | ||
} | ||
.toByteArray() | ||
} | ||
|
||
private fun KeePassDatabase.toByteArray(): ByteArray { | ||
return ByteArrayOutputStream().use { out -> | ||
this.encode(out) | ||
|
@@ -80,6 +88,16 @@ class FakeFileContentFactory { | |
private val GROUP_SHOPPING = GroupEntity(title = "Shopping", uuid = UUID(100L, 5L)) | ||
private val GROUP_SOCIAL = GroupEntity(title = "Social", uuid = UUID(100L, 7L)) | ||
|
||
private val TOTP_URL = """ | ||
otpauth://totp/Example:john.doe?secret=AAAABBBBCCCCDDDD&period=30 | ||
&digits=6&issuer=Example&algorithm=SHA1 | ||
""".trimIndent() | ||
|
||
private val HOTP_URL = """ | ||
otpauth://hotp/Example:john.doe?secret=AAAABBBBCCCCDDDD&digits=6 | ||
&issuer=Example&algorithm=SHA1&counter=1 | ||
""".trimIndent() | ||
|
||
private val ENTRY_NAS_LOGIN = EntryEntity( | ||
title = "NAS Login", | ||
username = "john.doe", | ||
|
@@ -193,5 +211,27 @@ class FakeFileContentFactory { | |
PropertyType.URL.propertyName to "https://amazon.com" | ||
) | ||
) | ||
|
||
private val ENTRY_TOTP = EntryEntity( | ||
title = "TOTP Entry", | ||
username = "[email protected]", | ||
password = "", | ||
created = parseDate("2020-01-09"), | ||
modified = parseDate("2020-01-09"), | ||
custom = mapOf( | ||
"otp" to TOTP_URL | ||
) | ||
) | ||
|
||
private val ENTRY_HOTP = EntryEntity( | ||
title = "HOTP Entry", | ||
username = "[email protected]", | ||
password = "", | ||
created = parseDate("2020-01-09"), | ||
modified = parseDate("2020-01-09"), | ||
custom = mapOf( | ||
"otp" to HOTP_URL | ||
) | ||
) | ||
} | ||
} |
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
44 changes: 0 additions & 44 deletions
44
app/src/main/java/com/ivanovsky/passnotes/data/entity/PropertyType.java
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
app/src/main/java/com/ivanovsky/passnotes/data/entity/PropertyType.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,29 @@ | ||
package com.ivanovsky.passnotes.data.entity | ||
|
||
enum class PropertyType(val propertyName: String) { | ||
TITLE("Title"), | ||
PASSWORD("Password"), | ||
USER_NAME("UserName"), | ||
URL("URL"), | ||
NOTES("Notes"), | ||
OTP("otp"); | ||
|
||
companion object { | ||
val DEFAULT_TYPES = setOf(TITLE, PASSWORD, USER_NAME, URL, NOTES) | ||
|
||
private val TYPES_MAP = mapOf( | ||
TITLE.propertyName.lowercase() to TITLE, | ||
PASSWORD.propertyName.lowercase() to PASSWORD, | ||
USER_NAME.propertyName.lowercase() to USER_NAME, | ||
URL.propertyName.lowercase() to URL, | ||
NOTES.propertyName.lowercase() to NOTES, | ||
OTP.propertyName.lowercase() to OTP | ||
) | ||
|
||
fun getByName(name: String?): PropertyType? { | ||
val loweredName = name?.lowercase() ?: return null | ||
|
||
return TYPES_MAP[loweredName] | ||
} | ||
} | ||
} |
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
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
12 changes: 12 additions & 0 deletions
12
app/src/main/kotlin/com/ivanovsky/passnotes/domain/otp/DataConverters.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,12 @@ | ||
package com.ivanovsky.passnotes.domain.otp | ||
|
||
import com.ivanovsky.passnotes.domain.otp.model.HashAlgorithmType | ||
import dev.robinohs.totpkt.otp.HashAlgorithm | ||
|
||
fun HashAlgorithmType.toExternalAlgorithm(): HashAlgorithm { | ||
return when (this) { | ||
HashAlgorithmType.SHA1 -> HashAlgorithm.SHA1 | ||
HashAlgorithmType.SHA256 -> HashAlgorithm.SHA256 | ||
HashAlgorithmType.SHA512 -> HashAlgorithm.SHA512 | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/kotlin/com/ivanovsky/passnotes/domain/otp/HotpGenerator.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,18 @@ | ||
package com.ivanovsky.passnotes.domain.otp | ||
|
||
import com.ivanovsky.passnotes.domain.otp.model.OtpToken | ||
import dev.robinohs.totpkt.otp.hotp.HotpGenerator as ExternalHotpGenerator | ||
|
||
class HotpGenerator( | ||
override val token: OtpToken | ||
) : OtpGenerator { | ||
|
||
private val generator = ExternalHotpGenerator( | ||
algorithm = token.algorithm.toExternalAlgorithm(), | ||
codeLength = token.digits | ||
) | ||
|
||
override fun generateCode(): String { | ||
return generator.generateCode(token.secret.toByteArray(), token.counter ?: 0) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/kotlin/com/ivanovsky/passnotes/domain/otp/OtpCodeFormatter.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,18 @@ | ||
package com.ivanovsky.passnotes.domain.otp | ||
|
||
import com.ivanovsky.passnotes.util.StringUtils.splitIntoWords | ||
|
||
object OtpCodeFormatter { | ||
|
||
fun format(code: String): String { | ||
val length = code.length | ||
|
||
return when { | ||
length < 6 -> code | ||
length % 3 == 0 -> splitIntoWords(code, wordLength = 3) | ||
length % 4 == 0 -> splitIntoWords(code, wordLength = 4) | ||
length == 7 -> splitIntoWords(code, wordLength = 4) | ||
else -> splitIntoWords(code, wordLength = 4) | ||
} | ||
} | ||
} |
Oops, something went wrong.