2015-01-03 22:03:57 +02:00
|
|
|
package authboss
|
|
|
|
|
|
|
|
import (
|
2017-02-21 00:28:38 +02:00
|
|
|
"context"
|
2015-01-03 22:03:57 +02:00
|
|
|
"time"
|
2017-02-22 01:04:30 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-01-03 22:03:57 +02:00
|
|
|
)
|
|
|
|
|
2015-02-10 10:43:45 +02:00
|
|
|
// Data store constants for attribute names.
|
|
|
|
const (
|
2015-02-16 23:27:29 +02:00
|
|
|
StoreEmail = "email"
|
|
|
|
StoreUsername = "username"
|
|
|
|
StorePassword = "password"
|
2015-02-10 10:43:45 +02:00
|
|
|
)
|
|
|
|
|
2015-03-13 04:20:36 +02:00
|
|
|
// Data store constants for OAuth2 attribute names.
|
|
|
|
const (
|
2015-03-14 01:23:43 +02:00
|
|
|
StoreOAuth2UID = "oauth2_uid"
|
|
|
|
StoreOAuth2Provider = "oauth2_provider"
|
|
|
|
StoreOAuth2Token = "oauth2_token"
|
|
|
|
StoreOAuth2Refresh = "oauth2_refresh"
|
|
|
|
StoreOAuth2Expiry = "oauth2_expiry"
|
2015-03-13 04:20:36 +02:00
|
|
|
)
|
|
|
|
|
2015-01-24 01:56:24 +02:00
|
|
|
var (
|
2015-02-02 00:17:18 +02:00
|
|
|
// ErrUserNotFound should be returned from Get when the record is not found.
|
2017-02-22 01:04:30 +02:00
|
|
|
ErrUserNotFound = errors.New("user not found")
|
2015-02-02 00:17:18 +02:00
|
|
|
// ErrTokenNotFound should be returned from UseToken when the record is not found.
|
2017-02-22 01:04:30 +02:00
|
|
|
ErrTokenNotFound = errors.New("token not found")
|
2018-02-01 03:07:11 +02:00
|
|
|
// ErrUserFound should be returned from Create (see ConfirmUser) when the primaryID
|
|
|
|
// of the record is found.
|
2017-02-22 01:04:30 +02:00
|
|
|
ErrUserFound = errors.New("user found")
|
2015-01-24 01:56:24 +02:00
|
|
|
)
|
2015-01-11 08:52:39 +02:00
|
|
|
|
2018-02-01 03:07:11 +02:00
|
|
|
// ServerStorer represents the data store that's capable of loading users
|
2017-02-21 00:28:38 +02:00
|
|
|
// and giving them a context with which to store themselves.
|
2018-02-01 03:07:11 +02:00
|
|
|
type ServerStorer interface {
|
|
|
|
// Load will look up the user based on the passed the PrimaryID
|
|
|
|
Load(ctx context.Context, key string) (User, error)
|
|
|
|
|
|
|
|
// Save persists the user in the database
|
|
|
|
Save(ctx context.Context, user User) error
|
2017-02-21 00:28:38 +02:00
|
|
|
}
|
2015-01-11 08:52:39 +02:00
|
|
|
|
2018-02-01 03:07:11 +02:00
|
|
|
// User has functions for each piece of data it requires.
|
|
|
|
// Data should not be persisted on each function call.
|
2018-02-02 01:42:48 +02:00
|
|
|
// User has a PID (primary ID) that is used on the site as
|
|
|
|
// a single unique identifier to any given user (very typically e-mail
|
|
|
|
// or username).
|
2018-02-15 00:18:03 +02:00
|
|
|
//
|
|
|
|
// User interfaces return no errors or bools to signal that a value was
|
|
|
|
// not present. Instead 0-value = null = not present, this puts the onus
|
|
|
|
// on Authboss code to check for this.
|
2018-02-01 03:07:11 +02:00
|
|
|
type User interface {
|
2018-02-16 20:31:55 +02:00
|
|
|
GetPID() (pid string)
|
|
|
|
PutPID(pid string)
|
2018-02-02 03:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthableUser is identified by a password
|
|
|
|
type AuthableUser interface {
|
|
|
|
User
|
2017-02-21 00:28:38 +02:00
|
|
|
|
2018-02-16 20:31:55 +02:00
|
|
|
GetPassword() (password string)
|
|
|
|
PutPassword(password string)
|
2018-02-02 03:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfirmableUser can be in a state of confirmed or not
|
|
|
|
type ConfirmableUser interface {
|
|
|
|
User
|
|
|
|
|
2018-02-16 20:31:55 +02:00
|
|
|
GetConfirmed() (confirmed bool)
|
|
|
|
GetConfirmToken() (token string)
|
2018-02-02 03:23:31 +02:00
|
|
|
|
2018-02-16 20:31:55 +02:00
|
|
|
PutConfirmed(confirmed bool)
|
|
|
|
PutConfirmToken(token string)
|
2017-02-21 00:28:38 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:07:11 +02:00
|
|
|
// ArbitraryUser allows arbitrary data from the web form through. You should
|
2017-02-25 02:45:47 +02:00
|
|
|
// definitely only pull the keys you want from the map, since this is unfiltered
|
|
|
|
// input from a web request and is an attack vector.
|
2018-02-01 03:07:11 +02:00
|
|
|
type ArbitraryUser interface {
|
|
|
|
User
|
2017-02-21 00:28:38 +02:00
|
|
|
|
|
|
|
// GetArbitrary is used only to display the arbitrary data back to the user
|
|
|
|
// when the form is reset.
|
2018-02-16 20:31:55 +02:00
|
|
|
GetArbitrary() (arbitrary map[string]string)
|
2018-02-02 03:23:31 +02:00
|
|
|
// PutArbitrary allows arbitrary fields defined by the authboss library
|
|
|
|
// consumer to add fields to the user registration piece.
|
2018-02-16 20:31:55 +02:00
|
|
|
PutArbitrary(arbitrary map[string]string)
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:07:11 +02:00
|
|
|
// OAuth2User allows reading and writing values relating to OAuth2
|
|
|
|
type OAuth2User interface {
|
|
|
|
User
|
2017-02-21 00:28:38 +02:00
|
|
|
|
2018-02-01 03:07:11 +02:00
|
|
|
// IsOAuth2User checks to see if a user was registered in the site as an
|
|
|
|
// oauth2 user.
|
2018-02-16 20:31:55 +02:00
|
|
|
IsOAuth2User() bool
|
|
|
|
|
|
|
|
GetUID() (uid string)
|
|
|
|
GetProvider() (provider string)
|
|
|
|
GetToken() (token string)
|
|
|
|
GetRefreshToken() (refreshToken string)
|
|
|
|
GetExpiry() (expiry time.Duration)
|
|
|
|
|
|
|
|
PutUID(uid string)
|
|
|
|
PutProvider(provider string)
|
|
|
|
PutToken(token string)
|
|
|
|
PutRefreshToken(refreshToken string)
|
|
|
|
PutExpiry(expiry time.Duration)
|
2015-03-14 01:23:43 +02:00
|
|
|
}
|
2018-02-05 07:24:55 +02:00
|
|
|
|
|
|
|
// MustBeAuthable forces an upgrade conversion to Authable
|
|
|
|
// or will panic.
|
|
|
|
func MustBeAuthable(u User) AuthableUser {
|
|
|
|
if au, ok := u.(AuthableUser); ok {
|
|
|
|
return au
|
|
|
|
}
|
|
|
|
panic("could not upgrade user to an authable user, check your user struct")
|
|
|
|
}
|