skillz_bff/auth/user.go

284 lines
8.2 KiB
Go

package auth
import (
"time"
"github.com/volatiletech/authboss/v3"
)
/*
User holds the representation of a user account, using a single Email field as
the reference user identifier (Pid for authboss).
This code is derived from Authboss v3, in authboss-sample/storer.go, under its MIT license.
Main changes: no "name" field, no TOTP or SMS 2FA support.
*/
type User struct {
ID int
// Authboss: auth
Email string
Password string
// Confirm
ConfirmSelector string
ConfirmVerifier string
Confirmed bool
// Lock
AttemptCount int
LastAttempt time.Time
Locked time.Time
// Recover
RecoverSelector string
RecoverVerifier string
RecoverTokenExpiry time.Time
// OAuth2
OAuth2UID string
OAuth2Provider string
OAuth2AccessToken string
OAuth2RefreshToken string
OAuth2Expiry time.Time
}
/*
GetArbitrary returns the arbitrary string fields from the user, as part of the
authboss.ArbitraryUser interface.
It is used to access fields not belonging to the various authboss.*User interfaces.
Be aware this data is unfiltered, hence unsafe if any date comes from a tainted
source, like user input in any form.
*/
func (u *User) GetArbitrary() map[string]string {
return map[string]string{}
}
/*
GetAttemptCount returns the user auth attempt count, as part of the
authboss.LockableUser interface.
*/
func (u *User) GetAttemptCount() int { return u.AttemptCount }
/*
GetConfirmed returns the user confirmation status, as part of the
authboss.ConfirmableUser interface.
*/
func (u *User) GetConfirmed() bool { return u.Confirmed }
/*
GetConfirmSelector returns the user confirmation selector, as part of the
authboss.ConfirmableUser interface
*/
func (u *User) GetConfirmSelector() string { return u.ConfirmSelector }
/*
GetConfirmVerifier returns the user confirmation verifier, as part of the
authboss.ConfirmableUser interface
*/
func (u *User) GetConfirmVerifier() string { return u.ConfirmVerifier }
/*
GetLastAttempt returns the last auth attempt for the user, as part of the
authboss.LockableUser interface
*/
func (u *User) GetLastAttempt() time.Time { return u.LastAttempt }
/*
GetLocked returns the timestamp the user was last locked, as part of the
authboss.LockableUser interface.
*/
func (u *User) GetLocked() time.Time { return u.Locked }
/*
GetPassword returns the user password, as part of the authboss.ConfirmableUser
and authboss.RecoverableUser interfaces.
*/
func (u *User) GetEmail() string { return u.Password }
/*
GetOAuth2UID returns the OAUth 2 ID from user.
*/
func (u User) GetOAuth2UID() (uid string) { return u.OAuth2UID }
/*
GetOAuth2Provider returns the OAUth 2 provider from user.
*/
func (u User) GetOAuth2Provider() (provider string) { return u.OAuth2Provider }
/*
GetOAuth2AccessToken returns the OAuth 2 access token from user, as part of the
authboss.OAuth2User interface.
*/
func (u User) GetOAuth2AccessToken() (token string) { return u.OAuth2AccessToken }
/*
GetOAuth2RefreshToken returns the refresh token from user, as part of the
authboss.OAuth2User interface.
*/
func (u User) GetOAuth2RefreshToken() (refreshToken string) { return u.OAuth2RefreshToken }
/*
GetOAuth2Expiry reeturns the OAuth 2 expiry from user, as part of the
authboss.OAuth2User interface
*/
func (u User) GetOAuth2Expiry() (expiry time.Time) { return u.OAuth2Expiry }
/*
GetPassword returns the user password, as part of the authboss.AuthableUser interface.
*/
func (u *User) GetPassword() string { return u.Password }
/*
PutConfirmed puts confirmed into user, as part of the authboss.ConfirmableUser interface.
*/
func (u *User) PutConfirmed(confirmed bool) { u.Confirmed = confirmed }
/*
PutConfirmSelector puts the confirmation selector into user, as part of the authboss.ConfirmableUser interface.
*/
func (u *User) PutConfirmSelector(selector string) { u.ConfirmSelector = selector }
/*
PutConfirmVerifier puts the confirmation verifier into user, as part of the
authboss.ConfirmableUser interface.
*/
func (u *User) PutConfirmVerifier(selector string) { u.ConfirmSelector = selector }
/*
PutOAuth2UID puts the OAuth 2 UID into user, as part of the
authboss.OAuth2User interface.
*/
func (u *User) PutOAuth2UID(uid string) { u.OAuth2UID = uid }
/*
PutOAuth2Provider puts the OAuth 2 provider into user, as part of the
authboss.OAuth2User interface.
*/
func (u *User) PutOAuth2Provider(provider string) { u.OAuth2Provider = provider }
/*
PutOAuth2AccessToken puts the OAuth 2 access token into user, as part of the
authboss.OAuth2User interface.
*/
func (u *User) PutOAuth2AccessToken(token string) { u.OAuth2AccessToken = token }
/*
PutOAuth2RefreshToken puts the OAuth 2 refresh token into user, as part of the
authboss.OAuth2User interface.
*/
func (u *User) PutOAuth2RefreshToken(refreshToken string) { u.OAuth2RefreshToken = refreshToken }
/*
PutOAuth2Expiry puts the OAuth 2 expiry into user, as part of the
authboss.OAuth2User interface.
*/
func (u *User) PutOAuth2Expiry(expiry time.Time) { u.OAuth2Expiry = expiry }
/*
PutPassword puts password into user, as part of the authboss.AuthableUser interface.
*/
func (u *User) PutPassword(password string) { u.Password = password }
/*
GetPID returns the user pid, as part of the authboss.User interface.
*/
func (u *User) GetPID() string { return u.Email }
/*
GetRecoverExpiry returns the recovery token expiry timestamp, as part of the
authboss.RecoverableUser interface.
*/
func (u *User) GetRecoverExpiry() time.Time { return u.RecoverTokenExpiry }
/*
GetRecoverSelector returns the recovery selector, as part of the
authboss.RecoverableUser interface.
*/
func (u *User) GetRecoverSelector() string { return u.RecoverSelector }
/*
GetRecoverVerifier returns the recovery verifier, as part of the
authboss.RecoverableUser interface.
*/
func (u *User) GetRecoverVerifier() string { return u.RecoverVerifier }
/*
IsOAuth2User returns true if the user was created with OAUth 2
*/
func (u *User) IsOAuth2User() bool { return len(u.OAuth2UID) != 0 }
/*
PutArbitrary puts a set of arbitrary data into user, as part of the
authboss.ArbitraryUser interface. Be aware this data is unfiltered, hence unsafe
if any date comes from a tainted source, like user input in any form.
*/
func (u *User) PutArbitrary(map[string]string) {
// There are currently no expected arbitrary data.
// In the authboss example, the "name" field was such a field.
}
/*
PutAttemptCount puts the auth attempt counter into user, as part of the
authboss.LockableUser interface.
*/
func (u *User) PutAttemptCount(count int) { u.AttemptCount = count }
/*
PutEmail puts email into user, as part of the authboss.ConfirmableUser and
authboss.RecoverableUser interfaces.
*/
func (u *User) PutEmail(email string) { u.Email = email }
/*
PutLastAttempt puts the timestamp of the last auth attempt into user, as part of
the authboss.LockableUser interface.
*/
func (u *User) PutLastAttempt(last time.Time) { u.LastAttempt = last }
/*
PutLocked puts the lock timestamp into user, as part of the
authboss.LockableUser interface.
*/
func (u *User) PutLocked(ts time.Time) { u.Locked = ts }
/*
PutPID puts pid into user, as part of the authboss.User interface.
*/
func (u *User) PutPID(pid string) { u.Email = pid }
/*
PutRecoverExpiry puts the recovery token expiry into user, as part of the
authboss.RecoverableUser interface.
*/
func (u *User) PutRecoverExpiry(expiry time.Time) { u.RecoverTokenExpiry = expiry }
/*
PutRecoverSelector puts the recovery selector into user, as part of the
authboss.RecoverableUser interface.
*/
func (u *User) PutRecoverSelector(selector string) { u.RecoverSelector = selector }
/*
PutRecoverVerifier puts the recovery verifier into user, as part of the
authboss.RecoverableUser interface.
*/
func (u *User) PutRecoverVerifier(verifier string) { u.RecoverVerifier = verifier }
/*
These blank vars allow compile-time verification of the fact that User actually
implements the expected interfaces.
*/
var (
assertUser = &User{}
_ authboss.User = assertUser
_ authboss.AuthableUser = assertUser
_ authboss.ConfirmableUser = assertUser
_ authboss.LockableUser = assertUser
_ authboss.RecoverableUser = assertUser
_ authboss.ArbitraryUser = assertUser
_ authboss.OAuth2User = assertUser
)