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
code : Time.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 -> Time.Posix -> Basics.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
init : { issuer : String, user : String, rawSecret : String, outputLength : Maybe Basics.Int, periodSeconds : Maybe Basics.Int, algorithm : TOTP.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 = "john@example.com"
, 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:john@example.com?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)
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 Basics.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 Basics.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)
keyAlgorithm : Key -> TOTP.Algorithm.Algorithm
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