choonkeat/elm-totp/1.1.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)

Accessors

keyIssuer : Key -> String
import TOTP.Algorithm


keyResult : Result String Key
keyResult =
    init
        { issuer = "123issuer"
        , user = "123user"
        , rawSecret = "123rawSecret"
        , outputLength = Just 123
        , periodSeconds = Just 456
        , algorithm = TOTP.Algorithm.SHA512
        }


Result.map keyIssuer keyResult
--> Ok "123issuer"
keyUser : Key -> String
import TOTP.Algorithm


keyResult : Result String Key
keyResult =
    init
        { issuer = "123issuer"
        , user = "123user"
        , rawSecret = "123rawSecret"
        , outputLength = Just 123
        , periodSeconds = Just 456
        , algorithm = TOTP.Algorithm.SHA512
        }


Result.map keyUser keyResult
--> Ok "123user"
keyRawSecret : Key -> Result String String
import TOTP.Algorithm


keyResult : Result String Key
keyResult =
    init
        { issuer = "123issuer"
        , user = "123user"
        , rawSecret = "123rawSecret"
        , outputLength = Just 123
        , periodSeconds = Just 456
        , algorithm = TOTP.Algorithm.SHA512
        }


Result.andThen keyRawSecret keyResult
--> Ok "123rawSecret"
keyOutputLength : Key -> Maybe Int
import TOTP.Algorithm


keyResult : Result String Key
keyResult =
    init
        { issuer = "123issuer"
        , user = "123user"
        , rawSecret = "123rawSecret"
        , outputLength = Just 123
        , periodSeconds = Just 456
        , algorithm = TOTP.Algorithm.SHA512
        }


Result.map keyOutputLength keyResult
--> Ok (Just 123)
keyPeriodSeconds : Key -> Maybe Int
import TOTP.Algorithm


keyResult : Result String Key
keyResult =
    init
        { issuer = "123issuer"
        , user = "123user"
        , rawSecret = "123rawSecret"
        , outputLength = Just 123
        , periodSeconds = Just 456
        , algorithm = TOTP.Algorithm.SHA512
        }


Result.map keyPeriodSeconds keyResult
--> Ok (Just 456)
import TOTP.Algorithm


keyResult : Result String Key
keyResult =
    init
        { issuer = "123issuer"
        , user = "123user"
        , rawSecret = "123rawSecret"
        , outputLength = Just 123
        , periodSeconds = Just 456
        , algorithm = TOTP.Algorithm.SHA512
        }


Result.map keyAlgorithm keyResult
--> Ok TOTP.Algorithm.SHA512