Skip to content

A Mint package providing TOTP code generation for two-factor authentication.

Notifications You must be signed in to change notification settings

mint-lang/mint-totp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

TOTP (Time-Based One-Time Password)

A Mint package providing RFC 6238-compliant TOTP code generation for two-factor authentication.

Features

  • 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)

Installation

Add to your mint.json dependencies:

{
  "dependencies": {
    "totp": {
      "repository": "https://github.com/mint-lang/mint-totp",
      "constraint": "0.0.0 <= v < 1.0.0"
    }
  }
}

Basic Usage

Generate Current Code

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>
  }
}

Advanced Configuration

{
  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")
  }
}

Generate QR Code URL

{
  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
}

Verify User Input

{
  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")
  }
}

Testing

Run tests with RFC 6238 official test vectors:

mint test

All tests validate against the official RFC 6238 test vectors to ensure correctness.

References

About

A Mint package providing TOTP code generation for two-factor authentication.

Resources

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages