2021-12-01 19:11:29 +07:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TOTPConfiguration represents a users TOTP configuration row in the database.
|
|
|
|
type TOTPConfiguration struct {
|
|
|
|
ID int `db:"id" json:"-"`
|
|
|
|
Username string `db:"username" json:"-"`
|
|
|
|
Issuer string `db:"issuer" json:"-"`
|
|
|
|
Algorithm string `db:"algorithm" json:"-"`
|
|
|
|
Digits uint `db:"digits" json:"digits"`
|
2021-12-02 08:24:10 +07:00
|
|
|
Period uint `db:"period" json:"period"`
|
2021-12-01 19:11:29 +07:00
|
|
|
Secret []byte `db:"secret" json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// URI shows the configuration in the URI representation.
|
|
|
|
func (c TOTPConfiguration) URI() (uri string) {
|
|
|
|
v := url.Values{}
|
|
|
|
v.Set("secret", string(c.Secret))
|
|
|
|
v.Set("issuer", c.Issuer)
|
|
|
|
v.Set("period", strconv.FormatUint(uint64(c.Period), 10))
|
|
|
|
v.Set("algorithm", c.Algorithm)
|
|
|
|
v.Set("digits", strconv.Itoa(int(c.Digits)))
|
|
|
|
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: "otpauth",
|
|
|
|
Host: "totp",
|
|
|
|
Path: "/" + c.Issuer + ":" + c.Username,
|
|
|
|
RawQuery: v.Encode(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.String()
|
|
|
|
}
|