2015-01-03 22:03:57 +02:00
|
|
|
package authboss
|
|
|
|
|
2018-02-26 01:20:57 +02:00
|
|
|
// A concious decision was made to put all storer
|
|
|
|
// and user types into this file despite them truly
|
|
|
|
// belonging to outside modules. The reason for this
|
|
|
|
// is because documentation-wise, it was previously
|
|
|
|
// difficult to find what you had to implement or even
|
|
|
|
// what you could implement.
|
|
|
|
|
2015-01-03 22:03:57 +02:00
|
|
|
import (
|
2017-02-21 00:28:38 +02:00
|
|
|
"context"
|
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)
|
|
|
|
|
2018-02-26 01:20:57 +02:00
|
|
|
// Save persists the user in the database, this should never
|
|
|
|
// create a user and instead return ErrUserNotFound if the user
|
|
|
|
// does not exist.
|
2018-02-01 03:07:11 +02:00
|
|
|
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-26 01:20:57 +02:00
|
|
|
// CreatingServerStorer is used for creating new users
|
|
|
|
// like when Registration is being done.
|
|
|
|
type CreatingServerStorer interface {
|
2018-03-06 03:47:11 +02:00
|
|
|
ServerStorer
|
|
|
|
|
2018-02-26 01:20:57 +02:00
|
|
|
// New creates a blank user, it is not yet persisted in the database
|
|
|
|
// but is just for storing data
|
|
|
|
New(ctx context.Context) User
|
|
|
|
// Create the user in storage, it should not overwrite a user
|
|
|
|
// and should return ErrUserFound if it currently exists.
|
|
|
|
Create(ctx context.Context, user User) error
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:32:27 +02:00
|
|
|
// ConfirmingServerStorer can find a user by a confirm token
|
|
|
|
type ConfirmingServerStorer interface {
|
2018-03-06 03:47:11 +02:00
|
|
|
ServerStorer
|
|
|
|
|
|
|
|
// LoadByConfirmToken finds a user by his confirm token field
|
|
|
|
// and should return ErrUserNotFound if that user cannot be found.
|
|
|
|
LoadByConfirmToken(ctx context.Context, token string) (ConfirmableUser, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecoveringServerStorer allows users to be recovered by a token
|
|
|
|
type RecoveringServerStorer interface {
|
|
|
|
ServerStorer
|
|
|
|
|
|
|
|
// LoadByRecoverToken finds a user by his recover token field
|
|
|
|
// and should return ErrUserNotFound if that user cannot be found.
|
|
|
|
LoadByRecoverToken(ctx context.Context, token string) (RecoverableUser, error)
|
2018-02-26 01:32:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-26 01:20:57 +02:00
|
|
|
// EnsureCanCreate makes sure the server storer supports create operations
|
|
|
|
func EnsureCanCreate(storer ServerStorer) CreatingServerStorer {
|
|
|
|
s, ok := storer.(CreatingServerStorer)
|
|
|
|
if !ok {
|
|
|
|
panic("could not upgrade serverstorer to creatingserverstorer, check your struct")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-02-26 01:32:27 +02:00
|
|
|
// EnsureCanConfirm makes sure the server storer supports confirm-lookup operations
|
|
|
|
func EnsureCanConfirm(storer ServerStorer) ConfirmingServerStorer {
|
|
|
|
s, ok := storer.(ConfirmingServerStorer)
|
|
|
|
if !ok {
|
|
|
|
panic("could not upgrade serverstorer to confirmingserverstorer, check your struct")
|
2018-02-05 07:24:55 +02:00
|
|
|
}
|
2018-02-26 01:32:27 +02:00
|
|
|
|
|
|
|
return s
|
2018-02-05 07:24:55 +02:00
|
|
|
}
|
2018-03-06 03:47:11 +02:00
|
|
|
|
|
|
|
// EnsureCanRecover makes sure the server storer supports confirm-lookup operations
|
|
|
|
func EnsureCanRecover(storer ServerStorer) RecoveringServerStorer {
|
|
|
|
s, ok := storer.(RecoveringServerStorer)
|
|
|
|
if !ok {
|
|
|
|
panic("could not upgrade serverstorer to recoveringserverstorer, check your struct")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|