2022-03-06 05:47:40 +00:00
|
|
|
package model
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
import (
|
2022-10-20 02:16:36 +00:00
|
|
|
"database/sql"
|
2021-12-03 00:04:11 +00:00
|
|
|
"net"
|
2021-11-23 09:45:38 +00:00
|
|
|
"time"
|
2021-11-30 06:58:21 +00:00
|
|
|
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
"github.com/google/uuid"
|
2021-11-23 09:45:38 +00:00
|
|
|
)
|
|
|
|
|
2021-11-30 06:58:21 +00:00
|
|
|
// NewIdentityVerification creates a new IdentityVerification from a given username and action.
|
2023-04-23 10:59:15 +00:00
|
|
|
func NewIdentityVerification(jti uuid.UUID, username, action string, ip net.IP, expiration time.Duration) (verification IdentityVerification) {
|
2021-11-30 06:58:21 +00:00
|
|
|
return IdentityVerification{
|
2021-12-04 04:48:22 +00:00
|
|
|
JTI: jti,
|
2021-11-30 06:58:21 +00:00
|
|
|
IssuedAt: time.Now(),
|
2023-04-23 10:59:15 +00:00
|
|
|
ExpiresAt: time.Now().Add(expiration),
|
2021-11-30 06:58:21 +00:00
|
|
|
Action: action,
|
|
|
|
Username: username,
|
2021-12-03 00:04:11 +00:00
|
|
|
IssuedIP: NewIP(ip),
|
2021-11-30 06:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
// IdentityVerification represents an identity verification row in the database.
|
|
|
|
type IdentityVerification struct {
|
2022-10-20 02:16:36 +00:00
|
|
|
ID int `db:"id"`
|
|
|
|
JTI uuid.UUID `db:"jti"`
|
|
|
|
IssuedAt time.Time `db:"iat"`
|
|
|
|
IssuedIP IP `db:"issued_ip"`
|
|
|
|
ExpiresAt time.Time `db:"exp"`
|
|
|
|
Action string `db:"action"`
|
|
|
|
Username string `db:"username"`
|
|
|
|
Consumed sql.NullTime `db:"consumed"`
|
|
|
|
ConsumedIP NullIP `db:"consumed_ip"`
|
2021-11-30 06:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToIdentityVerificationClaim converts the IdentityVerification into a IdentityVerificationClaim.
|
|
|
|
func (v IdentityVerification) ToIdentityVerificationClaim() (claim *IdentityVerificationClaim) {
|
|
|
|
return &IdentityVerificationClaim{
|
|
|
|
RegisteredClaims: jwt.RegisteredClaims{
|
|
|
|
ID: v.JTI.String(),
|
|
|
|
Issuer: "Authelia",
|
|
|
|
IssuedAt: jwt.NewNumericDate(v.IssuedAt),
|
|
|
|
ExpiresAt: jwt.NewNumericDate(v.ExpiresAt),
|
|
|
|
},
|
|
|
|
Action: v.Action,
|
|
|
|
Username: v.Username,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdentityVerificationClaim custom claim for specifying the action claim.
|
|
|
|
// The action can be to register a TOTP device, a U2F device or reset one's password.
|
|
|
|
type IdentityVerificationClaim struct {
|
|
|
|
jwt.RegisteredClaims
|
|
|
|
|
|
|
|
// The action this token has been crafted for.
|
|
|
|
Action string `json:"action"`
|
|
|
|
// The user this token has been crafted for.
|
|
|
|
Username string `json:"username"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToIdentityVerification converts the IdentityVerificationClaim into a IdentityVerification.
|
|
|
|
func (v IdentityVerificationClaim) ToIdentityVerification() (verification *IdentityVerification, err error) {
|
|
|
|
jti, err := uuid.Parse(v.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &IdentityVerification{
|
|
|
|
JTI: jti,
|
|
|
|
Username: v.Username,
|
|
|
|
Action: v.Action,
|
|
|
|
ExpiresAt: v.ExpiresAt.Time,
|
|
|
|
}, nil
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|