A Mint package providing RFC 6238-compliant TOTP code generation for two-factor authentication.
- Support for SHA-1, SHA-256, and SHA-512 hash algorithms
- Configurable code length (6, 8, or any digit count)
- Configurable time period (default 30 seconds)
- Generate
otpauth://URLs for QR code generation - Verify TOTP codes with configurable time window support
- Zero external dependencies (uses Web Crypto API)
Add to your mint.json dependencies:
{
"dependencies": {
"totp": {
"repository": "https://github.com/mint-lang/mint-totp",
"constraint": "0.0.0 <= v < 1.0.0"
}
}
}component AuthenticatorDemo {
fun onGenerate {
case await TOTP.generateWithDefaults("JBSWY3DPEHPK3PXP") {
Ok(code) =>
Debug.log("Current TOTP code: #{code}")
Err(error) =>
Debug.log("Error generating code")
}
}
fun render {
<button onClick={onGenerate}>
"Generate Code"
</button>
}
}{
let config = {
algorithm: Totp.Algorithm.SHA256,
secret: "JBSWY3DPEHPK3PXP",
period: 30,
digits: 8
}
case await Totp.generate(config) {
Ok(code) =>
// Use 8-digit SHA256 code
Debug.log("Code: #{code}")
Err(Totp.Error.InvalidSecret(msg)) =>
// Handle invalid base32 secret
Debug.log("Invalid secret: #{msg}")
Err(error) =>
// Handle other errors
Debug.log("Error")
}
}{
let url = TOTP.URI.generateWithDefaults(
"JBSWY3DPEHPK3PXP",
"MyApp",
"user@example.com"
)
// url = "otpauth://totp/MyApp:user@example.com?secret=JBSWY3DPEHPK3PXP&issuer=MyApp&algorithm=SHA1&digits=6&period=30"
// Convert to QR code and display to user for setup
}{
let config = {
algorithm: Totp.Algorithm.SHA1,
secret: "JBSWY3DPEHPK3PXP",
period: 30,
digits: 6
}
case await Totp.verify(config, userInput, 1) {
Ok(true) =>
// Code is valid - allow login
Debug.log("Authentication successful")
Ok(false) =>
// Code is invalid - reject
Debug.log("Invalid code")
Err(error) =>
// Handle error
Debug.log("Verification error")
}
}Run tests with RFC 6238 official test vectors:
mint testAll tests validate against the official RFC 6238 test vectors to ensure correctness.