mirror of
https://github.com/volatiletech/authboss.git
synced 2024-11-24 08:42:17 +02:00
Remove context and errors from get/set
I have a feeling that I wrote all this fanciness in when the user was still able to fetch himself from the database. But since that's been dropped I don't think any of this stuff is necessary. In terms of setting without an error, we should do validation before an attempt to save, not every time we set a field. This will just end up being much nicer error handling, and the database is going to do it's own validation and we can handle that error in the same way.
This commit is contained in:
parent
7f97d632d0
commit
c89ca29827
@ -101,7 +101,7 @@ func TestCurrentUser(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if got := user.GetPID(context.TODO()); got != "george-pid" {
|
||||
if got := user.GetPID(); got != "george-pid" {
|
||||
t.Error("got:", got)
|
||||
}
|
||||
}
|
||||
@ -116,7 +116,7 @@ func TestCurrentUserContext(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if got := user.GetPID(context.TODO()); got != "george-pid" {
|
||||
if got := user.GetPID(); got != "george-pid" {
|
||||
t.Error("got:", got)
|
||||
}
|
||||
}
|
||||
@ -194,7 +194,7 @@ func TestLoadCurrentUser(t *testing.T) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if got := user.GetPID(context.TODO()); got != "george-pid" {
|
||||
if got := user.GetPID(); got != "george-pid" {
|
||||
t.Error("got:", got)
|
||||
}
|
||||
|
||||
|
@ -30,78 +30,35 @@ type User struct {
|
||||
OAuthExpiry time.Time
|
||||
}
|
||||
|
||||
func (m User) GetUsername(context.Context) (string, error) { return m.Username, nil }
|
||||
func (m User) GetPID(context.Context) (string, error) { return m.Email, nil }
|
||||
func (m User) GetPassword(context.Context) (string, error) { return m.Password, nil }
|
||||
func (m User) GetRecoverToken(context.Context) (string, error) { return m.RecoverToken, nil }
|
||||
func (m User) GetRecoverTokenExpiry(context.Context) (time.Time, error) {
|
||||
return m.RecoverTokenExpiry, nil
|
||||
}
|
||||
func (m User) GetConfirmToken(context.Context) (string, error) { return m.ConfirmToken, nil }
|
||||
func (m User) GetConfirmed(context.Context) (bool, error) { return m.Confirmed, nil }
|
||||
func (m User) GetLocked(context.Context) (bool, error) { return m.Locked, nil }
|
||||
func (m User) GetAttemptNumber(context.Context) (int, error) { return m.AttemptNumber, nil }
|
||||
func (m User) GetAttemptTime(context.Context) (time.Time, error) {
|
||||
return m.AttemptTime, nil
|
||||
}
|
||||
func (m User) GetOAuthToken(context.Context) (string, error) { return m.OAuthToken, nil }
|
||||
func (m User) GetOAuthRefresh(context.Context) (string, error) { return m.OAuthRefresh, nil }
|
||||
func (m User) GetOAuthExpiry(context.Context) (time.Time, error) {
|
||||
return m.OAuthExpiry, nil
|
||||
}
|
||||
func (m User) GetUsername() string { return m.Username }
|
||||
func (m User) GetPID() string { return m.Email }
|
||||
func (m User) GetPassword() string { return m.Password }
|
||||
func (m User) GetRecoverToken() string { return m.RecoverToken }
|
||||
func (m User) GetRecoverTokenExpiry() time.Time { return m.RecoverTokenExpiry }
|
||||
func (m User) GetConfirmToken() string { return m.ConfirmToken }
|
||||
func (m User) GetConfirmed() bool { return m.Confirmed }
|
||||
func (m User) GetLocked() bool { return m.Locked }
|
||||
func (m User) GetAttemptNumber() int { return m.AttemptNumber }
|
||||
func (m User) GetAttemptTime() time.Time { return m.AttemptTime }
|
||||
func (m User) GetOAuthToken() string { return m.OAuthToken }
|
||||
func (m User) GetOAuthRefresh() string { return m.OAuthRefresh }
|
||||
func (m User) GetOAuthExpiry() time.Time { return m.OAuthExpiry }
|
||||
|
||||
func (m *User) SetUsername(ctx context.Context, username string) error {
|
||||
m.Username = username
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetEmail(ctx context.Context, email string) error {
|
||||
m.Email = email
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetPassword(ctx context.Context, password string) error {
|
||||
m.Password = password
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetRecoverToken(ctx context.Context, recoverToken string) error {
|
||||
m.RecoverToken = recoverToken
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetRecoverTokenExpiry(ctx context.Context, recoverTokenExpiry time.Time) error {
|
||||
func (m *User) SetUsername(username string) { m.Username = username }
|
||||
func (m *User) SetEmail(email string) { m.Email = email }
|
||||
func (m *User) SetPassword(password string) { m.Password = password }
|
||||
func (m *User) SetRecoverToken(recoverToken string) { m.RecoverToken = recoverToken }
|
||||
func (m *User) SetRecoverTokenExpiry(recoverTokenExpiry time.Time) {
|
||||
m.RecoverTokenExpiry = recoverTokenExpiry
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetConfirmToken(ctx context.Context, confirmToken string) error {
|
||||
m.ConfirmToken = confirmToken
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetConfirmed(ctx context.Context, confirmed bool) error {
|
||||
m.Confirmed = confirmed
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetLocked(ctx context.Context, locked bool) error {
|
||||
m.Locked = locked
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetAttemptNumber(ctx context.Context, attemptNumber int) error {
|
||||
m.AttemptNumber = attemptNumber
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetAttemptTime(ctx context.Context, attemptTime time.Time) error {
|
||||
m.AttemptTime = attemptTime
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetOAuthToken(ctx context.Context, oAuthToken string) error {
|
||||
m.OAuthToken = oAuthToken
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetOAuthRefresh(ctx context.Context, oAuthRefresh string) error {
|
||||
m.OAuthRefresh = oAuthRefresh
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetOAuthExpiry(ctx context.Context, oAuthExpiry time.Time) error {
|
||||
m.OAuthExpiry = oAuthExpiry
|
||||
return nil
|
||||
}
|
||||
func (m *User) SetConfirmToken(confirmToken string) { m.ConfirmToken = confirmToken }
|
||||
func (m *User) SetConfirmed(confirmed bool) { m.Confirmed = confirmed }
|
||||
func (m *User) SetLocked(locked bool) { m.Locked = locked }
|
||||
func (m *User) SetAttemptNumber(attemptNumber int) { m.AttemptNumber = attemptNumber }
|
||||
func (m *User) SetAttemptTime(attemptTime time.Time) { m.AttemptTime = attemptTime }
|
||||
func (m *User) SetOAuthToken(oAuthToken string) { m.OAuthToken = oAuthToken }
|
||||
func (m *User) SetOAuthRefresh(oAuthRefresh string) { m.OAuthRefresh = oAuthRefresh }
|
||||
func (m *User) SetOAuthExpiry(oAuthExpiry time.Time) { m.OAuthExpiry = oAuthExpiry }
|
||||
|
||||
// ServerStorer should be valid for any module storer defined in authboss.
|
||||
type ServerStorer struct {
|
||||
@ -220,7 +177,7 @@ func (FailStorer) Load(context.Context) error {
|
||||
return errors.New("fail storer: get")
|
||||
}
|
||||
|
||||
// ClientRW is used for testing the client stores on context
|
||||
// ClientState is used for testing the client stores on context
|
||||
type ClientState struct {
|
||||
Values map[string]string
|
||||
GetShouldFail bool
|
||||
@ -344,16 +301,16 @@ func (m *Mailer) Send(ctx context.Context, email authboss.Email) error {
|
||||
// AfterCallback is a callback that knows if it was called
|
||||
type AfterCallback struct {
|
||||
HasBeenCalled bool
|
||||
Fn authboss.After
|
||||
Fn authboss.EventHandler
|
||||
}
|
||||
|
||||
// NewAfterCallback constructs a new aftercallback.
|
||||
func NewAfterCallback() *AfterCallback {
|
||||
m := AfterCallback{}
|
||||
|
||||
m.Fn = func(context.Context) error {
|
||||
m.Fn = func(http.ResponseWriter, *http.Request, bool) (bool, error) {
|
||||
m.HasBeenCalled = true
|
||||
return nil
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return &m
|
||||
|
@ -27,25 +27,25 @@ func (m mockServerStorer) Load(ctx context.Context, key string) (User, error) {
|
||||
}
|
||||
|
||||
func (m mockServerStorer) Save(ctx context.Context, user User) error {
|
||||
pid := user.GetPID(ctx)
|
||||
pid := user.GetPID()
|
||||
m[pid] = user.(mockUser)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m mockUser) PutPID(ctx context.Context, email string) {
|
||||
func (m mockUser) PutPID(email string) {
|
||||
m.Email = email
|
||||
}
|
||||
|
||||
func (m mockUser) PutPassword(ctx context.Context, password string) {
|
||||
func (m mockUser) PutPassword(password string) {
|
||||
m.Password = password
|
||||
}
|
||||
|
||||
func (m mockUser) GetPID(ctx context.Context) (email string) {
|
||||
func (m mockUser) GetPID() (email string) {
|
||||
return m.Email
|
||||
}
|
||||
|
||||
func (m mockUser) GetPassword(ctx context.Context) (password string) {
|
||||
func (m mockUser) GetPassword() (password string) {
|
||||
return m.Password
|
||||
}
|
||||
|
||||
|
42
storage.go
42
storage.go
@ -53,27 +53,27 @@ type ServerStorer interface {
|
||||
// not present. Instead 0-value = null = not present, this puts the onus
|
||||
// on Authboss code to check for this.
|
||||
type User interface {
|
||||
GetPID(ctx context.Context) (pid string)
|
||||
PutPID(ctx context.Context, pid string)
|
||||
GetPID() (pid string)
|
||||
PutPID(pid string)
|
||||
}
|
||||
|
||||
// AuthableUser is identified by a password
|
||||
type AuthableUser interface {
|
||||
User
|
||||
|
||||
GetPassword(ctx context.Context) (password string)
|
||||
PutPassword(ctx context.Context, password string)
|
||||
GetPassword() (password string)
|
||||
PutPassword(password string)
|
||||
}
|
||||
|
||||
// ConfirmableUser can be in a state of confirmed or not
|
||||
type ConfirmableUser interface {
|
||||
User
|
||||
|
||||
GetConfirmed(ctx context.Context) (confirmed bool)
|
||||
GetConfirmToken(ctx context.Context) (token string)
|
||||
GetConfirmed() (confirmed bool)
|
||||
GetConfirmToken() (token string)
|
||||
|
||||
PutConfirmed(ctx context.Context, confirmed bool)
|
||||
PutConfirmToken(ctx context.Context, token string)
|
||||
PutConfirmed(confirmed bool)
|
||||
PutConfirmToken(token string)
|
||||
}
|
||||
|
||||
// ArbitraryUser allows arbitrary data from the web form through. You should
|
||||
@ -84,10 +84,10 @@ type ArbitraryUser interface {
|
||||
|
||||
// GetArbitrary is used only to display the arbitrary data back to the user
|
||||
// when the form is reset.
|
||||
GetArbitrary(ctx context.Context) (arbitrary map[string]string)
|
||||
GetArbitrary() (arbitrary map[string]string)
|
||||
// PutArbitrary allows arbitrary fields defined by the authboss library
|
||||
// consumer to add fields to the user registration piece.
|
||||
PutArbitrary(ctx context.Context, arbitrary map[string]string)
|
||||
PutArbitrary(arbitrary map[string]string)
|
||||
}
|
||||
|
||||
// OAuth2User allows reading and writing values relating to OAuth2
|
||||
@ -96,19 +96,19 @@ type OAuth2User interface {
|
||||
|
||||
// IsOAuth2User checks to see if a user was registered in the site as an
|
||||
// oauth2 user.
|
||||
IsOAuth2User(ctx context.Context) bool
|
||||
IsOAuth2User() bool
|
||||
|
||||
GetUID(ctx context.Context) (uid string)
|
||||
GetProvider(ctx context.Context) (provider string)
|
||||
GetToken(ctx context.Context) (token string)
|
||||
GetRefreshToken(ctx context.Context) (refreshToken string)
|
||||
GetExpiry(ctx context.Context) (expiry time.Duration)
|
||||
GetUID() (uid string)
|
||||
GetProvider() (provider string)
|
||||
GetToken() (token string)
|
||||
GetRefreshToken() (refreshToken string)
|
||||
GetExpiry() (expiry time.Duration)
|
||||
|
||||
PutUID(ctx context.Context, uid string)
|
||||
PutProvider(ctx context.Context, provider string)
|
||||
PutToken(ctx context.Context, token string)
|
||||
PutRefreshToken(ctx context.Context, refreshToken string)
|
||||
PutExpiry(ctx context.Context, expiry time.Duration)
|
||||
PutUID(uid string)
|
||||
PutProvider(provider string)
|
||||
PutToken(token string)
|
||||
PutRefreshToken(refreshToken string)
|
||||
PutExpiry(expiry time.Duration)
|
||||
}
|
||||
|
||||
// MustBeAuthable forces an upgrade conversion to Authable
|
||||
|
Loading…
Reference in New Issue
Block a user