blasphem/pkg/auth/session.go

175 lines
3.4 KiB
Go
Raw Normal View History

2022-10-26 19:13:50 -04:00
package auth
import (
2022-11-11 18:02:52 -05:00
"errors"
2022-10-26 19:13:50 -04:00
"net/http"
"time"
2022-10-26 19:43:51 -04:00
"github.com/labstack/echo/v4"
2022-10-26 19:13:50 -04:00
)
type SessionStore struct {
s map[TokenID]*Token
lastCull time.Time
}
type TokenID string
2022-11-11 18:02:52 -05:00
func (t *TokenID) IsValid() bool {
// TODO: more validation than this
return *t != ""
}
2022-10-26 19:43:51 -04:00
type Token struct { // TODO: jwt bro
2022-10-26 19:13:50 -04:00
ID TokenID
Ctime time.Time
2022-10-26 19:43:51 -04:00
Expires time.Time
2022-10-26 19:13:50 -04:00
Addr string
2022-11-11 18:02:52 -05:00
user ProviderUser `json:"-"`
2022-10-26 19:13:50 -04:00
}
func (ss *SessionStore) init() {
ss.s = make(map[TokenID]*Token)
}
const cullInterval = 5 * time.Minute
func (ss *SessionStore) cull() {
if now := time.Now(); now.Sub(ss.lastCull) > cullInterval {
for k, v := range ss.s {
2022-10-26 19:43:51 -04:00
if now.After(v.Expires) {
2022-10-26 19:13:50 -04:00
delete(ss.s, k)
}
}
}
}
func (ss *SessionStore) register(t *Token) {
ss.cull()
ss.s[t.ID] = t
}
2022-11-11 18:02:52 -05:00
func (ss *SessionStore) verify(tr *TokenRequest, r *http.Request) (ProviderUser, bool) {
2022-10-26 19:43:51 -04:00
if t, hasToken := ss.s[tr.Code]; hasToken {
// TODO: JWT
if t.Expires.After(time.Now()) {
2022-11-11 18:02:52 -05:00
return t.user, true
} else {
delete(ss.s, t.ID)
2022-10-26 19:43:51 -04:00
}
}
2022-11-11 18:02:52 -05:00
return nil, false
}
type Credential struct {
user ProviderUser
}
func (ss *SessionStore) verifyAndGetCredential(tr *TokenRequest, r *http.Request) *Credential {
user, success := ss.verify(tr, r)
if !success {
return nil
}
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) {
panic("not implemented")
return &User{}, nil
2022-10-26 19:43:51 -04:00
}
const defaultExpiration = 2 * time.Hour
2022-11-11 18:02:52 -05:00
func (a *Authenticator) NewToken(r *http.Request, user ProviderUser, f *Flow) TokenID {
2022-10-26 19:13:50 -04:00
id := TokenID(genUUID())
t := &Token{
2022-10-26 19:43:51 -04:00
ID: id,
Ctime: time.Now(),
Expires: time.Now().Add(defaultExpiration),
Addr: r.RemoteAddr,
2022-11-11 18:02:52 -05:00
user: user,
2022-10-26 19:13:50 -04:00
}
2022-10-27 09:51:11 -04:00
a.sessions.register(t)
2022-10-26 19:13:50 -04:00
return id
}
2022-10-26 19:43:51 -04:00
2022-11-11 18:02:52 -05:00
type GrantType string
const (
GTAuthorizationCode GrantType = "authorization_code"
GTRefreshToken GrantType = "refresh_token"
)
type ClientID string
func (c *ClientID) IsValid() bool {
// TODO: || !indieauth.VerifyClientID(rq.ClientID)?
return *c != ""
}
2022-10-26 19:43:51 -04:00
type TokenRequest struct {
2022-11-11 18:02:52 -05:00
ClientID ClientID `query:"client_id"`
2022-10-26 19:43:51 -04:00
Code TokenID `query:"code"`
2022-11-11 18:02:52 -05:00
GrantType GrantType `query:"grant_type"`
2022-10-26 19:43:51 -04:00
}
func (a *Authenticator) TokenHandler(c echo.Context) error {
2022-11-11 18:02:52 -05:00
rq := new(TokenRequest)
err := c.Bind(rq)
2022-10-26 19:43:51 -04:00
if err != nil {
return err
}
2022-11-11 18:02:52 -05:00
if *rq == (TokenRequest{}) {
panic("it didn't bind")
}
switch rq.GrantType {
case GTAuthorizationCode:
if !rq.ClientID.IsValid() {
return c.JSON(http.StatusBadRequest, AuthError{Error: "invalid_request", Description: "invalid client ID"})
}
if !rq.Code.IsValid() {
return c.JSON(http.StatusBadRequest, AuthError{Error: "invalid_request", Description: "invalid code"})
}
if cred := a.sessions.verifyAndGetCredential(rq, c.Request()); cred != nil {
// TODO: success
user, err := a.getOrCreateUser(cred)
if err != nil {
return c.JSON(http.StatusUnauthorized, AuthError{Error: "access_denied", Description: "bad user"})
}
if err := user.allowedToAuth(); err != nil {
return c.JSON(http.StatusUnauthorized, AuthError{Error: "access_denied", Description: err.Error()})
}
return c.String(http.StatusOK, "token good I guess")
}
case GTRefreshToken:
return c.String(http.StatusNotImplemented, "not implemented")
2022-10-26 19:43:51 -04:00
}
return c.String(http.StatusUnauthorized, "token bad I guess")
}