choonkeat/elm-totp/1.0.0

NOTE: the latest version is 1.1.2

TOTP.Key

Type

type Key

An opaque type to hold the configuration value for a TOTP.

This is the value to store in your database, by calling toString

https://github.com/google/google-authenticator/wiki/Key-Uri-Format#algorithm

Helper functions

code : Posix -> Key -> Result String String

Attempt to return the expected OTP code at the given time.

Compare this value against the user input to verify if their OTP is correct.

expiresIn : Key -> Posix -> Int

Return the number of seconds the OTP from code is valid for

fromString : String -> Maybe Key

Attempt to parse a String representation of Key back into a Key value

{ issuer : String
, user : String
, rawSecret : String
, outputLength : Maybe Int
, periodSeconds : Maybe Int
, algorithm : Algorithm
}
-> Result String Key

Builds a Key value from the parameters

toString : Key -> String

https://rootprojects.org/authenticator/

import Base32
import String.Extra
import TOTP.Algorithm
import TOTP


keyResult : Result String Key
keyResult =
    init
        { issuer = "ACME Co"
        , user = "[email protected]"
        , rawSecret = "HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ"
            |> Base32.decode
            |> Result.map String.Extra.fromCodePoints
            |> Result.withDefault "<fail>"
        , outputLength = Just 6
        , periodSeconds = Just 30
        , algorithm = TOTP.Algorithm.SHA1
        }


-- expected String output
Result.map toString keyResult
--> Ok "otpauth://totp/ACME%20Co:[email protected]?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30"




-- toString and fromString are reversible
Result.map toString keyResult
|> Result.map fromString
--> Ok (Result.toMaybe keyResult)