blasphem/pkg/auth/flow.go

237 lines
4.8 KiB
Go
Raw Permalink Normal View History

2022-09-30 23:54:21 -04:00
package auth
import (
"net/http"
"strings"
"time"
2022-10-26 18:27:24 -04:00
"github.com/jinzhu/copier"
2022-09-30 23:54:21 -04:00
"github.com/labstack/echo/v4"
2022-10-26 18:27:24 -04:00
"dynatron.me/x/blasphem/internal/common"
2022-11-12 15:56:17 -05:00
"dynatron.me/x/blasphem/pkg/auth/provider"
2022-09-30 23:54:21 -04:00
)
type FlowStore map[FlowID]*Flow
type FlowRequest struct {
2022-11-12 12:45:25 -05:00
ClientID ClientID `json:"client_id"`
2022-09-30 23:54:21 -04:00
Handler []*string `json:"handler"`
RedirectURI string `json:"redirect_uri"`
}
type FlowType string
const (
2022-10-26 18:27:24 -04:00
TypeForm FlowType = "form"
TypeCreateEntry FlowType = "create_entry"
2022-09-30 23:54:21 -04:00
)
type FlowID string
type Step string
const (
StepInit Step = "init"
)
type Flow struct {
Type FlowType `json:"type"`
ID FlowID `json:"flow_id"`
Handler []*string `json:"handler"`
2022-10-26 18:27:24 -04:00
StepID *Step `json:"step_id,omitempty"`
2022-11-12 15:56:17 -05:00
Schema []provider.FlowSchemaItem `json:"data_schema"`
2022-10-25 00:16:29 -04:00
Errors interface{} `json:"errors"`
2022-09-30 23:54:21 -04:00
DescPlace *string `json:"description_placeholders"`
LastStep *string `json:"last_step"`
request *FlowRequest
2022-10-26 18:27:24 -04:00
ctime time.Time
2022-09-30 23:54:21 -04:00
}
func (f *Flow) touch() {
2022-10-26 18:27:24 -04:00
f.ctime = time.Now()
2022-09-30 23:54:21 -04:00
}
func (fs FlowStore) register(f *Flow) {
fs.cull()
fs[f.ID] = f
}
2022-10-25 21:18:50 -04:00
func (fs FlowStore) Remove(f *Flow) {
delete(fs, f.ID)
}
2022-09-30 23:54:21 -04:00
const cullAge = time.Minute * 30
func (fs FlowStore) cull() {
for k, v := range fs {
2022-10-26 18:27:24 -04:00
if time.Now().Sub(v.ctime) > cullAge {
2022-09-30 23:54:21 -04:00
delete(fs, k)
}
}
}
func (fs FlowStore) Get(id FlowID) *Flow {
f, ok := fs[id]
if ok {
return f
}
return nil
}
func (a *Authenticator) NewFlow(r *FlowRequest) *Flow {
2022-11-12 15:56:17 -05:00
var sch []provider.FlowSchemaItem
2022-10-25 00:16:29 -04:00
for _, h := range r.Handler {
if h == nil {
break
}
if hand := a.Provider(*h); hand != nil {
sch = hand.FlowSchema()
break
}
}
if sch == nil {
return nil
}
2022-09-30 23:54:21 -04:00
flow := &Flow{
2022-10-25 00:16:29 -04:00
Type: TypeForm,
2022-10-26 19:01:45 -04:00
ID: FlowID(genUUID()),
2022-10-26 18:27:24 -04:00
StepID: stepPtr(StepInit),
2022-10-25 00:16:29 -04:00
Schema: sch,
2022-09-30 23:54:21 -04:00
Handler: r.Handler,
Errors: []string{},
request: r,
}
flow.touch()
2022-10-27 09:51:11 -04:00
a.flows.register(flow)
2022-09-30 23:54:21 -04:00
return flow
}
2022-10-26 18:27:24 -04:00
func stepPtr(s Step) *Step { return &s }
func (f *Flow) redirect(c echo.Context) {
c.Request().Header.Set("Location", f.request.RedirectURI)
}
2022-10-25 00:16:29 -04:00
func (f *Flow) progress(a *Authenticator, c echo.Context) error {
2022-10-26 18:27:24 -04:00
if f.StepID == nil {
c.Logger().Error("stepID is nil")
return c.String(http.StatusInternalServerError, "No Step ID")
}
switch *f.StepID {
2022-09-30 23:54:21 -04:00
case StepInit:
2022-10-25 00:16:29 -04:00
rm := make(map[string]interface{})
2022-09-30 23:54:21 -04:00
err := c.Bind(&rm)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
for _, si := range f.Schema {
if si.Required {
if _, ok := rm[si.Name]; !ok {
return c.String(http.StatusBadRequest, "missing required param "+si.Name)
}
}
}
2022-11-11 18:02:52 -05:00
user, err := a.Check(f, rm)
2022-10-25 00:16:29 -04:00
switch err {
case nil:
2022-10-26 18:27:24 -04:00
var finishedFlow struct {
ID FlowID `json:"flow_id"`
Handler []*string `json:"handler"`
2022-10-26 19:01:45 -04:00
Result TokenID `json:"result"`
2022-10-26 18:27:24 -04:00
Title string `json:"title"`
Type FlowType `json:"type"`
Version int `json:"version"`
}
2022-11-11 18:02:52 -05:00
2022-10-27 09:51:11 -04:00
a.flows.Remove(f)
2022-10-26 18:27:24 -04:00
copier.Copy(&finishedFlow, f)
finishedFlow.Type = TypeCreateEntry
finishedFlow.Title = common.AppName
finishedFlow.Version = 1
2022-11-11 18:02:52 -05:00
finishedFlow.Result = a.NewToken(c.Request(), user, f)
2022-10-26 18:27:24 -04:00
f.redirect(c)
return c.JSON(http.StatusCreated, &finishedFlow)
2022-10-25 00:16:29 -04:00
case ErrInvalidHandler:
return c.String(http.StatusNotFound, err.Error())
case ErrInvalidAuth:
fallthrough
default:
f.Errors = map[string]interface{}{
"base": "invalid_auth",
}
return c.JSON(http.StatusOK, f)
}
2022-09-30 23:54:21 -04:00
default:
return c.String(http.StatusBadRequest, "unknown flow step")
}
}
2022-10-02 08:52:48 -04:00
func (a *Authenticator) LoginFlowDeleteHandler(c echo.Context) error {
flowID := c.Param("flow_id")
if flowID == "" {
return c.String(http.StatusBadRequest, "empty flow ID")
}
2022-10-27 09:51:11 -04:00
delete(a.flows, FlowID(flowID))
2022-10-02 08:52:48 -04:00
return c.String(http.StatusOK, "deleted")
}
2022-10-25 21:18:50 -04:00
func setJSON(c echo.Context) {
2022-09-30 23:54:21 -04:00
if c.Request().Method == http.MethodPost && strings.HasPrefix(c.Request().Header.Get(echo.HeaderContentType), "text/plain") {
// hack around the content-type, Context.JSON refuses to work otherwise
2022-10-25 00:16:29 -04:00
c.Request().Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
2022-09-30 23:54:21 -04:00
}
2022-10-25 21:18:50 -04:00
}
2022-09-30 23:54:21 -04:00
2022-10-25 21:18:50 -04:00
func (a *Authenticator) BeginLoginFlowHandler(c echo.Context) error {
setJSON(c)
2022-09-30 23:54:21 -04:00
2022-10-25 21:18:50 -04:00
var flowReq FlowRequest
err := c.Bind(&flowReq)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
2022-09-30 23:54:21 -04:00
2022-10-25 21:18:50 -04:00
resp := a.NewFlow(&flowReq)
2022-09-30 23:54:21 -04:00
2022-10-25 21:18:50 -04:00
if resp == nil {
return c.String(http.StatusBadRequest, "no such handler")
}
2022-10-25 00:16:29 -04:00
2022-10-25 21:18:50 -04:00
return c.JSON(http.StatusOK, resp)
}
func (a *Authenticator) LoginFlowHandler(c echo.Context) error {
setJSON(c)
flowID := c.Param("flow_id")
2022-09-30 23:54:21 -04:00
2022-10-27 09:51:11 -04:00
flow := a.flows.Get(FlowID(flowID))
2022-10-25 21:18:50 -04:00
if flow == nil {
return c.String(http.StatusNotFound, "no such flow")
2022-09-30 23:54:21 -04:00
}
2022-10-25 21:18:50 -04:00
2022-10-26 18:27:24 -04:00
if time.Now().Sub(flow.ctime) > cullAge {
2022-10-27 09:51:11 -04:00
a.flows.Remove(flow)
2022-10-26 18:27:24 -04:00
return c.String(http.StatusGone, "flow timed out")
}
2022-10-25 21:18:50 -04:00
return flow.progress(a, c)
}