Sess validate, needs replaced

This commit is contained in:
Daniel Ponte 2022-10-26 19:43:51 -04:00
parent b4b4fa51b2
commit 1c4e5f51a8
4 changed files with 46 additions and 12 deletions

View file

@ -64,7 +64,7 @@ func (a *Authenticator) InitAuth(s storage.Store) error {
return nil
}
type AuthProvider interface {
type AuthProvider interface { // TODO: this should include stepping
ProviderName() string
ProviderID() *string
ProviderType() string

View file

@ -239,7 +239,3 @@ func (a *Authenticator) LoginFlowHandler(c echo.Context) error {
return flow.progress(a, c)
}
func (a *Authenticator) TokenHandler(c echo.Context) error {
return c.String(http.StatusOK, "token good I guess")
}

View file

@ -3,6 +3,7 @@ package auth
import (
"encoding/base64"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/bcrypt"
"dynatron.me/x/blasphem/pkg/storage"
@ -69,7 +70,7 @@ func (hap *HomeAssistantProvider) ValidateCreds(rm map[string]interface{}) bool
var hash []byte
hash, err := base64.StdEncoding.DecodeString(found.Password)
if err != nil {
// XXX: probably log this
log.Error().Err(err).Msg("b64 encode fail")
return false
}

View file

@ -3,6 +3,8 @@ package auth
import (
"net/http"
"time"
"github.com/labstack/echo/v4"
)
type SessionStore struct {
@ -12,10 +14,10 @@ type SessionStore struct {
type TokenID string
type Token struct {
type Token struct { // TODO: jwt bro
ID TokenID
Ctime time.Time
Expires time.Duration
Expires time.Time
Addr string
}
@ -28,7 +30,7 @@ const cullInterval = 5 * time.Minute
func (ss *SessionStore) cull() {
if now := time.Now(); now.Sub(ss.lastCull) > cullInterval {
for k, v := range ss.s {
if now.After(v.Ctime.Add(v.Expires)) {
if now.After(v.Expires) {
delete(ss.s, k)
}
}
@ -40,16 +42,51 @@ func (ss *SessionStore) register(t *Token) {
ss.s[t.ID] = t
}
func (ss *SessionStore) verify(tr *TokenRequest, r *http.Request) bool {
if t, hasToken := ss.s[tr.Code]; hasToken {
// TODO: JWT
if t.Expires.After(time.Now()) {
return true
}
}
return false
}
const defaultExpiration = 2 * time.Hour
func (a *Authenticator) NewToken(r *http.Request, f *Flow) TokenID {
id := TokenID(genUUID())
t := &Token{
ID: id,
Ctime: time.Now(),
Addr: r.RemoteAddr,
ID: id,
Ctime: time.Now(),
Expires: time.Now().Add(defaultExpiration),
Addr: r.RemoteAddr,
}
a.Sessions.register(t)
return id
}
type TokenRequest struct {
ClientID string `query:"client_id"` // TODO: validate this?
Code TokenID `query:"code"`
GrantType string `query:"grant_type"`
}
func (a *Authenticator) TokenHandler(c echo.Context) error {
var rq TokenRequest
err := c.Bind(&rq)
if err != nil {
return err
}
if a.Sessions.verify(&rq, c.Request()) {
// TODO: success
return c.String(http.StatusOK, "token good I guess")
}
return c.String(http.StatusUnauthorized, "token bad I guess")
}