blasphem/pkg/auth/session.go

177 lines
3.7 KiB
Go
Raw Normal View History

2022-10-26 19:13:50 -04:00
package auth
import (
2022-11-12 15:56:17 -05:00
"encoding/json"
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-11-12 15:56:17 -05:00
"dynatron.me/x/blasphem/pkg/auth/provider"
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
2022-11-12 15:56:17 -05:00
user provider.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-12 15:56:17 -05:00
func (ss *SessionStore) verify(tr *TokenRequest, r *http.Request) (provider.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 {
2022-11-12 15:56:17 -05:00
ID CredID `json:"id"`
UserID UserID `json:"user_id"`
AuthProviderType string `json:"auth_provider_type"`
AuthProviderID *string `json:"auth_provider_id"`
DataRaw json.RawMessage `json:"data,omitempty"`
user provider.ProviderUser
}
func (cred *Credential) MarshalJSON() ([]byte, error) {
rm := map[string]interface{}{
"id": cred.ID,
"user_id": cred.UserID,
"auth_provider_type": cred.user.ProviderType(),
"auth_provider_id": cred.user.ProviderID(),
}
providerData := cred.user.ProviderUserData()
if providerData != nil {
rm["data"] = providerData
}
return json.Marshal(rm)
2022-11-11 18:02:52 -05:00
}
func (ss *SessionStore) verifyAndGetCredential(tr *TokenRequest, r *http.Request) *Credential {
user, success := ss.verify(tr, r)
if !success {
return nil
}
return &Credential{user: user}
}
2022-10-26 19:43:51 -04:00
const defaultExpiration = 2 * time.Hour
2022-11-12 15:56:17 -05:00
func (a *Authenticator) NewToken(r *http.Request, user provider.ProviderUser, f *Flow) TokenID {
2022-10-26 19:13:50 -04:00
id := TokenID(genUUID())
2022-11-12 17:42:36 -05:00
now := time.Now()
2022-10-26 19:13:50 -04:00
t := &Token{
2022-10-26 19:43:51 -04:00
ID: id,
2022-11-12 17:42:36 -05:00
Ctime: now,
Expires: now.Add(defaultExpiration),
2022-10-26 19:43:51 -04:00
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"
2022-11-12 13:34:39 -05:00
GTRefreshToken GrantType = "refresh_token"
2022-11-11 18:02:52 -05:00
)
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-12 12:45:25 -05:00
ClientID ClientID `form:"client_id"`
2022-11-12 13:34:39 -05:00
Code TokenID `form:"code"`
GrantType GrantType `form:"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
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"})
}
2022-11-12 13:34:39 -05:00
if err := user.allowedToAuth(); err != nil {
2022-11-11 18:02:52 -05:00
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")
}