Some renaming

This commit is contained in:
Daniel 2022-11-20 12:51:26 -05:00
parent f3e17e149f
commit d34814f050
5 changed files with 78 additions and 56 deletions

View file

@ -18,15 +18,43 @@ type AuthFlowManager struct {
type LoginFlow struct { type LoginFlow struct {
flow.FlowHandlerBase flow.FlowHandlerBase
ClientID common.ClientID
FlowContext
}
type FlowContext struct {
IPAddr string
CredentialOnly bool
RedirectURI string
}
type LoginFlowRequest struct {
ClientID common.ClientID `json:"client_id"`
Handler []*string `json:"handler"`
RedirectURI string `json:"redirect_uri"`
Type *string `json:"type"`
ip string `json:"-"`
}
func (r *LoginFlowRequest) FlowContext() FlowContext {
return FlowContext{
IPAddr: r.ip,
RedirectURI: r.RedirectURI,
CredentialOnly: r.Type != nil && *r.Type == "link_user",
}
} }
func NewAuthFlowManager() *AuthFlowManager { func NewAuthFlowManager() *AuthFlowManager {
return &AuthFlowManager{FlowManager: flow.NewFlowManager()} return &AuthFlowManager{FlowManager: flow.NewFlowManager()}
} }
func (afm *AuthFlowManager) NewLoginFlow(f *flow.FlowRequest, prov provider.AuthProvider) *LoginFlow { func (afm *AuthFlowManager) NewLoginFlow(req *LoginFlowRequest, prov provider.AuthProvider) *LoginFlow {
lf := &LoginFlow{ lf := &LoginFlow{
FlowHandlerBase: flow.NewFlowHandlerBase(f, prov, prov.ProviderType()), FlowHandlerBase: flow.NewFlowHandlerBase(prov, prov.ProviderType()),
ClientID: req.ClientID,
FlowContext: req.FlowContext(),
} }
afm.Register(lf) afm.Register(lf)
@ -34,7 +62,7 @@ func (afm *AuthFlowManager) NewLoginFlow(f *flow.FlowRequest, prov provider.Auth
return lf return lf
} }
func (a *Authenticator) NewFlow(r *flow.FlowRequest) *flow.FlowResult { func (a *Authenticator) NewFlow(r *LoginFlowRequest) *flow.Result {
var prov provider.AuthProvider var prov provider.AuthProvider
for _, h := range r.Handler { for _, h := range r.Handler {
@ -42,7 +70,7 @@ func (a *Authenticator) NewFlow(r *flow.FlowRequest) *flow.FlowResult {
break break
} }
prov = a.Provider(h.String()) prov = a.Provider(*h)
if prov != nil { if prov != nil {
break break
} }
@ -79,7 +107,7 @@ func (f *LoginFlow) progress(a *Authenticator, c echo.Context) error {
user, err := a.Check(f, c.Request(), rm) user, err := a.Check(f, c.Request(), rm)
switch err { switch err {
case nil: case nil:
finishedFlow := flow.FlowResult{} finishedFlow := flow.Result{}
a.flows.Remove(f) a.flows.Remove(f)
copier.Copy(&finishedFlow, f) copier.Copy(&finishedFlow, f)
finishedFlow.Type = flow.TypeCreateEntry finishedFlow.Type = flow.TypeCreateEntry
@ -95,13 +123,15 @@ func (f *LoginFlow) progress(a *Authenticator, c echo.Context) error {
case ErrInvalidAuth: case ErrInvalidAuth:
fallthrough fallthrough
default: default:
fr := f.ShowForm(map[string]interface{}{ return c.JSON(http.StatusOK, f.ShowForm(map[string]interface{}{
"base": "invalid_auth", "base": "invalid_auth",
}) }))
return c.JSON(http.StatusOK, fr)
} }
default: default:
return c.String(http.StatusBadRequest, "unknown flow step") return c.JSON(http.StatusOK, f.ShowForm(map[string]interface{}{
"base": "unknown_flow_step",
}))
} }
} }
@ -127,12 +157,14 @@ func setJSON(c echo.Context) {
func (a *Authenticator) BeginLoginFlowHandler(c echo.Context) error { func (a *Authenticator) BeginLoginFlowHandler(c echo.Context) error {
setJSON(c) setJSON(c)
var flowReq flow.FlowRequest var flowReq LoginFlowRequest
err := c.Bind(&flowReq) err := c.Bind(&flowReq)
if err != nil { if err != nil {
return c.String(http.StatusBadRequest, err.Error()) return c.String(http.StatusBadRequest, err.Error())
} }
flowReq.ip = c.Request().RemoteAddr
resp := a.NewFlow(&flowReq) resp := a.NewFlow(&flowReq)
if resp == nil { if resp == nil {

View file

@ -127,8 +127,8 @@ func (hap *HomeAssistantProvider) NewCredData() interface{} {
return &HAUser{} return &HAUser{}
} }
func (hap *HomeAssistantProvider) FlowSchema() flow.FlowSchema { func (hap *HomeAssistantProvider) FlowSchema() flow.Schema {
return []flow.FlowSchemaItem{ return []flow.SchemaItem{
{ {
Type: "string", Type: "string",
Name: "username", Name: "username",

View file

@ -14,7 +14,7 @@ var Providers = make(map[string]Constructor)
type AuthProvider interface { // TODO: this should include stepping type AuthProvider interface { // TODO: this should include stepping
AuthProviderMetadata AuthProviderMetadata
ProviderBase() AuthProviderBase ProviderBase() AuthProviderBase
FlowSchema() flow.FlowSchema FlowSchema() flow.Schema
NewCredData() interface{} NewCredData() interface{}
ValidateCreds(r *http.Request, reqMap map[string]interface{}) (user ProviderUser, success bool) ValidateCreds(r *http.Request, reqMap map[string]interface{}) (user ProviderUser, success bool)
Lookup(ProviderUser) ProviderUser Lookup(ProviderUser) ProviderUser

View file

@ -62,8 +62,8 @@ func (hap *TrustedNetworksProvider) NewCredData() interface{} {
return &UserData{} return &UserData{}
} }
func (hap *TrustedNetworksProvider) FlowSchema() flow.FlowSchema { func (hap *TrustedNetworksProvider) FlowSchema() flow.Schema {
return []flow.FlowSchemaItem{ return []flow.SchemaItem{
{ {
Type: "string", Type: "string",
Name: "username", Name: "username",

View file

@ -5,16 +5,15 @@ import (
"fmt" "fmt"
"time" "time"
"dynatron.me/x/blasphem/internal/common"
"dynatron.me/x/blasphem/internal/generate" "dynatron.me/x/blasphem/internal/generate"
) )
type ( type (
FlowResultType string ResultType string
FlowID string FlowID string
Step string Step string
HandlerKey string HandlerKey string
Errors interface{} Errors interface{}
Context interface{} Context interface{}
@ -24,14 +23,14 @@ type (
flows FlowStore flows FlowStore
} }
FlowResult struct { Result struct {
Type FlowResultType `json:"type"` Type ResultType `json:"type"`
ID FlowID `json:"flow_id"` ID FlowID `json:"flow_id"`
Handler []*HandlerKey `json:"handler"` Handler []*HandlerKey `json:"handler"`
Title *string `json:"title,omitempty"` Title *string `json:"title,omitempty"`
Data map[string]interface{} `json:"data,omitempty"` Data map[string]interface{} `json:"data,omitempty"`
StepID *Step `json:"step_id,omitempty"` StepID *Step `json:"step_id,omitempty"`
Schema []FlowSchemaItem `json:"data_schema"` Schema []SchemaItem `json:"data_schema"`
Extra *string `json:"extra,omitempty"` Extra *string `json:"extra,omitempty"`
Required *bool `json:"required,omitempty"` Required *bool `json:"required,omitempty"`
Errors interface{} `json:"errors"` Errors interface{} `json:"errors"`
@ -46,29 +45,24 @@ type (
Version *int `json:"version,omitempty"` Version *int `json:"version,omitempty"`
} }
FlowSchemaItem struct { SchemaItem struct {
Type string `json:"type"` Type string `json:"type"`
Name string `json:"name"` Name string `json:"name"`
Required bool `json:"required"` Required bool `json:"required"`
} }
FlowSchema []FlowSchemaItem Schema []SchemaItem
FlowRequest struct {
ClientID common.ClientID `json:"client_id"`
Handler []*HandlerKey `json:"handler"`
RedirectURI string `json:"redirect_uri"`
}
) )
type ( type (
Schemer interface { Schemer interface {
FlowSchema() FlowSchema FlowSchema() Schema
} }
Handler interface { Handler interface {
Base() FlowHandlerBase Base() FlowHandlerBase
FlowID() FlowID FlowID() FlowID
flowCtime() time.Time flowCtime() time.Time
} }
) )
@ -77,7 +71,7 @@ const (
StepInit Step = "init" StepInit Step = "init"
) )
func (fs *FlowSchema) CheckRequired(rm map[string]interface{}) error { func (fs *Schema) CheckRequired(rm map[string]interface{}) error {
for _, si := range *fs { for _, si := range *fs {
if si.Required { if si.Required {
if _, ok := rm[si.Name]; !ok { if _, ok := rm[si.Name]; !ok {
@ -98,12 +92,10 @@ func NewFlowManager() *FlowManager {
func stepPtr(s Step) *Step { return &s } func stepPtr(s Step) *Step { return &s }
type FlowHandlerBase struct { type FlowHandlerBase struct {
ID FlowID // ID is the FlowID ID FlowID // ID is the FlowID
Handler HandlerKey // Handler key Handler HandlerKey // Handler key
Context Context // flow Context Context Context // flow Context
ClientID common.ClientID Schema Schema
RedirectURI string
Schema FlowSchema
// curStep is the current step set by the flow manager // curStep is the current step set by the flow manager
curStep Step curStep Step
@ -121,13 +113,11 @@ func (f *FlowHandlerBase) FlowID() FlowID {
func (f *FlowHandlerBase) flowCtime() time.Time { return f.ctime } func (f *FlowHandlerBase) flowCtime() time.Time { return f.ctime }
func NewFlowHandlerBase(f *FlowRequest, sch Schemer, hand string) FlowHandlerBase { func NewFlowHandlerBase(sch Schemer, hand string) FlowHandlerBase {
return FlowHandlerBase{ return FlowHandlerBase{
ID: FlowID(generate.UUID()), ID: FlowID(generate.UUID()),
Handler: HandlerKey(hand), Handler: HandlerKey(hand),
ClientID: f.ClientID, Schema: sch.FlowSchema(),
RedirectURI: f.RedirectURI,
Schema: sch.FlowSchema(),
curStep: StepInit, curStep: StepInit,
ctime: time.Now(), ctime: time.Now(),
@ -150,8 +140,8 @@ func resultErrs(e Errors) Errors {
return e return e
} }
func (fm *FlowHandlerBase) ShowForm(errs Errors) *FlowResult { func (fm *FlowHandlerBase) ShowForm(errs Errors) *Result {
res := &FlowResult{ res := &Result{
Type: TypeForm, Type: TypeForm,
ID: fm.ID, ID: fm.ID,
StepID: stepPtr(fm.curStep), StepID: stepPtr(fm.curStep),
@ -168,14 +158,14 @@ func (fm *FlowManager) Delete(id FlowID) {
} }
const ( const (
TypeForm FlowResultType = "form" TypeForm ResultType = "form"
TypeCreateEntry FlowResultType = "create_entry" TypeCreateEntry ResultType = "create_entry"
TypeAbort FlowResultType = "abort" TypeAbort ResultType = "abort"
TypeExternalStep FlowResultType = "external" TypeExternalStep ResultType = "external"
TypeExternalStepDone FlowResultType = "external_done" TypeExternalStepDone ResultType = "external_done"
TypeShowProgress FlowResultType = "progress" TypeShowProgress ResultType = "progress"
TypeShowProgressDone FlowResultType = "progress_done" TypeShowProgressDone ResultType = "progress_done"
TypeMenu FlowResultType = "menu" TypeMenu ResultType = "menu"
) )
func (f *FlowHandlerBase) touch() { func (f *FlowHandlerBase) touch() {