Refactor user authentication

This commit is contained in:
Marcos Lilljedahl
2020-10-02 00:22:52 -03:00
parent 2d1515d12f
commit 02804c4b58
3 changed files with 27 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package pwd
import (
"context"
"errors"
"fmt"
"log"
"math"
@@ -19,6 +20,18 @@ import (
var preparedSessions = map[string]bool{}
type AccessDeniedError struct {
Err error
}
func (u *AccessDeniedError) Error() string {
return fmt.Sprintf("Acess denied error: %s", u.Err.Error())
}
func (u *AccessDeniedError) Unwrap() error {
return u.Err
}
type sessionBuilderWriter struct {
sessionId string
event event.EventApi
@@ -48,8 +61,10 @@ type SessionSetupInstanceConf struct {
func (p *pwd) SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) {
defer observeAction("SessionNew", time.Now())
if u, err := p.storage.UserGet(config.UserId); err == nil && u.IsBanned {
return nil, fmt.Errorf("User %s is banned\n", config.UserId)
if _, err := p.UserGet(config.UserId); errors.Is(err, userBannedError) {
return nil, &AccessDeniedError{err}
} else if err != nil {
return nil, err
}
s := &types.Session{}