Compare commits

..

No commits in common. "initwip" and "wsapi" have entirely different histories.

6 changed files with 97 additions and 124 deletions

View file

@ -1,7 +1,6 @@
package auth
import (
"fmt"
"net/http"
"strings"
@ -20,7 +19,6 @@ type AuthFlowManager struct {
type LoginFlow struct {
flow.FlowHandler
prov provider.AuthProvider
ClientID common.ClientID
FlowContext
}
@ -54,10 +52,9 @@ func NewAuthFlowManager() *AuthFlowManager {
func (afm *AuthFlowManager) NewLoginFlow(req *LoginFlowRequest, prov provider.AuthProvider) *LoginFlow {
lf := &LoginFlow{
FlowHandler: flow.NewFlowHandlerBase(prov.ProviderType()),
FlowHandler: flow.NewFlowHandlerBase(prov, prov.ProviderType()),
ClientID: req.ClientID,
FlowContext: req.FlowContext(),
prov: prov,
}
afm.Register(lf)
@ -83,9 +80,9 @@ func (a *authenticator) NewFlow(r *LoginFlowRequest) *flow.Result {
return nil
}
lf := a.flows.NewLoginFlow(r, prov)
flow := a.flows.NewLoginFlow(r, prov)
return lf.ShowForm(lf.WithSchema(prov), lf.WithStep(flow.StepInit))
return flow.ShowForm(nil)
}
func (f *LoginFlow) redirect(c echo.Context) {
@ -102,19 +99,15 @@ func (f *LoginFlow) progress(a *authenticator, c echo.Context) error {
return c.String(http.StatusBadRequest, err.Error())
}
err = f.prov.FlowSchema().CheckRequired(rm)
err = f.Schema.CheckRequired(rm)
if err != nil {
return c.JSON(http.StatusBadRequest, f.ShowForm(f.WithErrors([]string{err.Error()})))
return c.JSON(http.StatusBadRequest, f.ShowForm([]string{err.Error()}))
}
user, clientID, err := a.Check(f, c.Request(), rm)
switch err {
case nil:
creds := a.store.GetCredential(user)
if creds == nil {
return fmt.Errorf("flow progress: no such credential for %v", user.UserData())
}
finishedFlow := flow.Result{}
a.flows.Remove(f)
copier.Copy(&finishedFlow, f)
@ -131,14 +124,14 @@ func (f *LoginFlow) progress(a *authenticator, c echo.Context) error {
case ErrInvalidAuth:
fallthrough
default:
return c.JSON(http.StatusOK, f.ShowForm(f.WithErrors(map[string]interface{}{
return c.JSON(http.StatusOK, f.ShowForm(map[string]interface{}{
"base": "invalid_auth",
})))
}))
}
default:
return c.JSON(http.StatusOK, f.ShowForm(f.WithErrors(map[string]interface{}{
return c.JSON(http.StatusOK, f.ShowForm(map[string]interface{}{
"base": "unknown_flow_step",
})))
}))
}
}

View file

@ -162,7 +162,18 @@ func (hap *HomeAssistantProvider) NewCredData() interface{} {
}
func (hap *HomeAssistantProvider) FlowSchema() flow.Schema {
return flow.NewSchema(flow.RequiredString("username"), flow.RequiredString("password"))
return []flow.SchemaItem{
{
Type: "string",
Name: "username",
Required: true,
},
{
Type: "string",
Name: "password",
Required: true,
},
}
}
func init() {

View file

@ -79,7 +79,18 @@ func (hap *TrustedNetworksProvider) NewCredData() interface{} {
}
func (hap *TrustedNetworksProvider) FlowSchema() flow.Schema {
return nil
return []flow.SchemaItem{
{
Type: "string",
Name: "username",
Required: true,
},
{
Type: "string",
Name: "password",
Required: true,
},
}
}
func init() {

View file

@ -8,53 +8,71 @@ import (
"dynatron.me/x/blasphem/internal/generate"
)
type ResultType string
type FlowID string
type Step string
type HandlerKey string
type Errors interface{}
type (
ResultType string
FlowID string
Step string
HandlerKey string
Errors interface{}
type FlowStore map[FlowID]Handler
Context interface{}
type FlowManager struct {
flows FlowStore
}
FlowStore map[FlowID]Handler
type Result struct {
Type ResultType `json:"type"`
ID FlowID `json:"flow_id"`
Handler []*HandlerKey `json:"handler"`
Title *string `json:"title,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
StepID *Step `json:"step_id,omitempty"`
Schema []SchemaItem `json:"data_schema"`
Extra *string `json:"extra,omitempty"`
Required *bool `json:"required,omitempty"`
Errors interface{} `json:"errors"`
Description *string `json:"description,omitempty"`
DescPlace *string `json:"description_placeholders"`
URL *string `json:"url,omitempty"`
Reason *string `json:"reason,omitempty"`
Context *string `json:"context,omitempty"`
Result interface{} `json:"result,omitempty"`
LastStep *string `json:"last_step"`
Options map[string]interface{} `json:"options,omitempty"`
Version *int `json:"version,omitempty"`
}
FlowManager struct {
flows FlowStore
}
type Handler interface {
BaseHandler() FlowHandler
FlowID() FlowID
Result struct {
Type ResultType `json:"type"`
ID FlowID `json:"flow_id"`
Handler []*HandlerKey `json:"handler"`
Title *string `json:"title,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
StepID *Step `json:"step_id,omitempty"`
Schema []SchemaItem `json:"data_schema"`
Extra *string `json:"extra,omitempty"`
Required *bool `json:"required,omitempty"`
Errors interface{} `json:"errors"`
Description *string `json:"description,omitempty"`
DescPlace *string `json:"description_placeholders"`
URL *string `json:"url,omitempty"`
Reason *string `json:"reason,omitempty"`
Context *string `json:"context,omitempty"`
Result interface{} `json:"result,omitempty"`
LastStep *string `json:"last_step"`
Options map[string]interface{} `json:"options,omitempty"`
Version *int `json:"version,omitempty"`
}
flowCtime() time.Time
}
SchemaItem struct {
Type string `json:"type"`
Name string `json:"name"`
Required bool `json:"required"`
}
Schema []SchemaItem
)
type (
Schemer interface {
FlowSchema() Schema
}
Handler interface {
BaseHandler() FlowHandler
FlowID() FlowID
flowCtime() time.Time
}
)
const (
StepInit Step = "init"
)
func (fs Schema) CheckRequired(rm map[string]interface{}) error {
for _, si := range fs {
func (fs *Schema) CheckRequired(rm map[string]interface{}) error {
for _, si := range *fs {
if si.Required {
if _, ok := rm[si.Name]; !ok {
return fmt.Errorf("missing required param %s", si.Name)
@ -76,6 +94,8 @@ func stepPtr(s Step) *Step { return &s }
type FlowHandler struct {
ID FlowID // ID is the FlowID
Handler HandlerKey // Handler key
Context Context // flow Context
Schema Schema
// curStep is the current step set by the flow manager
curStep Step
@ -93,10 +113,11 @@ func (f *FlowHandler) FlowID() FlowID {
func (f *FlowHandler) flowCtime() time.Time { return f.ctime }
func NewFlowHandlerBase(hand string) FlowHandler {
func NewFlowHandlerBase(sch Schemer, hand string) FlowHandler {
return FlowHandler{
ID: FlowID(generate.UUID()),
Handler: HandlerKey(hand),
Schema: sch.FlowSchema(),
curStep: StepInit,
ctime: time.Now(),
@ -119,39 +140,16 @@ func resultErrs(e Errors) Errors {
return e
}
type FormOption func(*Result)
func (*FlowHandler) WithErrors(e Errors) FormOption {
return func(r *Result) {
r.Errors = e
}
}
func (*FlowHandler) WithStep(s Step) FormOption {
return func(r *Result) {
r.StepID = stepPtr(s)
}
}
func (*FlowHandler) WithSchema(sch Schemer) FormOption {
return func(r *Result) {
r.Schema = sch.FlowSchema()
}
}
func (fm *FlowHandler) ShowForm(opts ...FormOption) *Result {
func (fm *FlowHandler) ShowForm(errs Errors) *Result {
res := &Result{
Type: TypeForm,
ID: fm.ID,
StepID: stepPtr(fm.curStep),
Schema: fm.Schema,
Handler: fm.Handlers(),
Errors: resultErrs(errs),
}
for _, opt := range opts {
opt(res)
}
res.Errors = resultErrs(res.Errors)
return res
}

View file

@ -1,40 +0,0 @@
package flow
type Type string
const (
TypeString Type = "string"
)
func (t Type) IsValid() bool {
switch t {
case TypeString:
return true
}
return false
}
type SchemaItem struct {
Type Type `json:"type"`
Name string `json:"name"`
Required bool `json:"required"`
}
type Schema []SchemaItem
type Schemer interface {
FlowSchema() Schema
}
func NewSchema(items ...SchemaItem) Schema {
return items
}
func RequiredString(name string) SchemaItem {
return SchemaItem{
Type: TypeString,
Name: name,
Required: true,
}
}

View file

@ -104,7 +104,7 @@ func (ws *wsSession) Go(ctx context.Context) error {
}
err = authP.handleMsg(ctx, rdr)
if err != nil || ws.refreshToken == nil {
if err != nil {
return err
}
@ -158,7 +158,7 @@ func (ws *wsSession) Go(ctx context.Context) error {
log.Error().Err(err).Msg("handleMsg")
}
case <-ctx.Done():
close(msgChan) // maybe remove this?
close(msgChan)
return nil
case m := <-write:
err := ws.conn.WriteJSON(m)