Split user

This commit is contained in:
Daniel 2022-11-12 13:34:39 -05:00
parent 414654585b
commit 0358eeac53
4 changed files with 43 additions and 25 deletions

2
.gitignore vendored
View file

@ -3,3 +3,5 @@ blas
!cmd/blas/ !cmd/blas/
Session.vim Session.vim
coverage.txt coverage.txt
*.dlv
*.core

View file

@ -20,6 +20,7 @@ var (
) )
type Authenticator struct { type Authenticator struct {
store AuthStore
flows FlowStore flows FlowStore
sessions SessionStore sessions SessionStore
providers map[string]AuthProvider providers map[string]AuthProvider

View file

@ -1,12 +1,10 @@
package auth package auth
import ( import (
"errors"
"net/http" "net/http"
"time" "time"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
) )
type SessionStore struct { type SessionStore struct {
@ -77,25 +75,6 @@ func (ss *SessionStore) verifyAndGetCredential(tr *TokenRequest, r *http.Request
return &Credential{user: user} return &Credential{user: user}
} }
type User struct {
Username string
Active bool
}
func (u *User) allowedToAuth() error {
if !u.Active {
return errors.New("user disabled")
}
return nil
}
func (a *Authenticator) getOrCreateUser(c *Credential) (*User, error) {
log.Debug().Str("user", c.user.ProviderUsername()).Msg("getOrCreateUser")
panic("not implemented")
return &User{}, nil
}
const defaultExpiration = 2 * time.Hour const defaultExpiration = 2 * time.Hour
func (a *Authenticator) NewToken(r *http.Request, user ProviderUser, f *Flow) TokenID { func (a *Authenticator) NewToken(r *http.Request, user ProviderUser, f *Flow) TokenID {
@ -119,7 +98,7 @@ type GrantType string
const ( const (
GTAuthorizationCode GrantType = "authorization_code" GTAuthorizationCode GrantType = "authorization_code"
GTRefreshToken GrantType = "refresh_token" GTRefreshToken GrantType = "refresh_token"
) )
type ClientID string type ClientID string
@ -131,8 +110,8 @@ func (c *ClientID) IsValid() bool {
type TokenRequest struct { type TokenRequest struct {
ClientID ClientID `form:"client_id"` ClientID ClientID `form:"client_id"`
Code TokenID `form:"code"` Code TokenID `form:"code"`
GrantType GrantType `form:"grant_type"` GrantType GrantType `form:"grant_type"`
} }
func (a *Authenticator) TokenHandler(c echo.Context) error { func (a *Authenticator) TokenHandler(c echo.Context) error {
@ -159,7 +138,7 @@ func (a *Authenticator) TokenHandler(c echo.Context) error {
return c.JSON(http.StatusUnauthorized, AuthError{Error: "access_denied", Description: "bad user"}) return c.JSON(http.StatusUnauthorized, AuthError{Error: "access_denied", Description: "bad user"})
} }
if err := user.allowedToAuth(); err != nil { if err := user.allowedToAuth(); err != nil {
return c.JSON(http.StatusUnauthorized, AuthError{Error: "access_denied", Description: err.Error()}) return c.JSON(http.StatusUnauthorized, AuthError{Error: "access_denied", Description: err.Error()})
} }
return c.String(http.StatusOK, "token good I guess") return c.String(http.StatusOK, "token good I guess")

36
pkg/auth/user.go Normal file
View file

@ -0,0 +1,36 @@
package auth
import (
"errors"
"github.com/rs/zerolog/log"
)
const (
AuthKey = "auth"
)
type User struct {
Username string
UserMetadata
}
type UserMetadata struct {
Active bool
}
func (u *User) allowedToAuth() error {
if !u.Active {
return errors.New("user disabled")
}
return nil
}
func (a *Authenticator) getOrCreateUser(c *Credential) (*User, error) {
log.Debug().Str("user", c.user.ProviderUsername()).Msg("getOrCreateUser")
panic("not implemented")
return &User{}, nil
}