Use UUID
This commit is contained in:
parent
fb9ecb0509
commit
c033dced54
4 changed files with 33 additions and 24 deletions
1
go.mod
1
go.mod
|
@ -11,6 +11,7 @@ require (
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||||
|
github.com/google/uuid v1.3.0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||||
github.com/jinzhu/copier v0.3.5 // indirect
|
github.com/jinzhu/copier v0.3.5 // indirect
|
||||||
github.com/labstack/gommon v0.3.1 // indirect
|
github.com/labstack/gommon v0.3.1 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -7,6 +7,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||||
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||||
|
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||||
|
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
@ -19,9 +21,24 @@ var (
|
||||||
|
|
||||||
type Authenticator struct {
|
type Authenticator struct {
|
||||||
Flows FlowStore
|
Flows FlowStore
|
||||||
|
Sessions SessionStore
|
||||||
Providers map[string]AuthProvider
|
Providers map[string]AuthProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Authenticator) InstallRoutes(e *echo.Echo) {
|
||||||
|
authG := e.Group("/auth")
|
||||||
|
authG.GET("/authorize", a.AuthorizeHandler)
|
||||||
|
authG.GET("/providers", a.ProvidersHandler)
|
||||||
|
authG.POST("/token", a.TokenHandler)
|
||||||
|
|
||||||
|
authG.POST("/login_flow", a.BeginLoginFlowHandler)
|
||||||
|
|
||||||
|
loginFlow := authG.Group("/login_flow") // TODO: add IP address affinity middleware
|
||||||
|
loginFlow.POST("/:flow_id", a.LoginFlowHandler)
|
||||||
|
loginFlow.DELETE("/:flow_id", a.LoginFlowDeleteHandler)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (a *Authenticator) Provider(name string) AuthProvider {
|
func (a *Authenticator) Provider(name string) AuthProvider {
|
||||||
p, ok := a.Providers[name]
|
p, ok := a.Providers[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -33,6 +50,7 @@ func (a *Authenticator) Provider(name string) AuthProvider {
|
||||||
|
|
||||||
func (a *Authenticator) InitAuth(s storage.Store) error {
|
func (a *Authenticator) InitAuth(s storage.Store) error {
|
||||||
a.Flows = make(FlowStore)
|
a.Flows = make(FlowStore)
|
||||||
|
a.Sessions = make(SessionStore)
|
||||||
hap, err := NewHAProvider(s)
|
hap, err := NewHAProvider(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -118,3 +136,10 @@ func (a *Authenticator) Check(f *Flow, rm map[string]interface{}) error {
|
||||||
|
|
||||||
return ErrInvalidAuth
|
return ErrInvalidAuth
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func genUUID() string {
|
||||||
|
// must be addressable
|
||||||
|
u := uuid.New()
|
||||||
|
|
||||||
|
return hex.EncodeToString(u[:])
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -59,15 +57,6 @@ func (f *Flow) touch() {
|
||||||
f.ctime = time.Now()
|
f.ctime = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
func genFlowID() FlowID {
|
|
||||||
b := make([]byte, 16)
|
|
||||||
if _, err := rand.Read(b); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return FlowID(hex.EncodeToString(b))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fs FlowStore) register(f *Flow) {
|
func (fs FlowStore) register(f *Flow) {
|
||||||
fs.cull()
|
fs.cull()
|
||||||
fs[f.ID] = f
|
fs[f.ID] = f
|
||||||
|
@ -116,7 +105,7 @@ func (a *Authenticator) NewFlow(r *FlowRequest) *Flow {
|
||||||
|
|
||||||
flow := &Flow{
|
flow := &Flow{
|
||||||
Type: TypeForm,
|
Type: TypeForm,
|
||||||
ID: genFlowID(),
|
ID: FlowID(genUUID()),
|
||||||
StepID: stepPtr(StepInit),
|
StepID: stepPtr(StepInit),
|
||||||
Schema: sch,
|
Schema: sch,
|
||||||
Handler: r.Handler,
|
Handler: r.Handler,
|
||||||
|
@ -164,7 +153,7 @@ func (f *Flow) progress(a *Authenticator, c echo.Context) error {
|
||||||
var finishedFlow struct {
|
var finishedFlow struct {
|
||||||
ID FlowID `json:"flow_id"`
|
ID FlowID `json:"flow_id"`
|
||||||
Handler []*string `json:"handler"`
|
Handler []*string `json:"handler"`
|
||||||
Result string `json:"result"`
|
Result TokenID `json:"result"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Type FlowType `json:"type"`
|
Type FlowType `json:"type"`
|
||||||
Version int `json:"version"`
|
Version int `json:"version"`
|
||||||
|
@ -175,6 +164,7 @@ func (f *Flow) progress(a *Authenticator, c echo.Context) error {
|
||||||
finishedFlow.Type = TypeCreateEntry
|
finishedFlow.Type = TypeCreateEntry
|
||||||
finishedFlow.Title = common.AppName
|
finishedFlow.Title = common.AppName
|
||||||
finishedFlow.Version = 1
|
finishedFlow.Version = 1
|
||||||
|
finishedFlow.Result = a.NewToken(c.Request(), f)
|
||||||
|
|
||||||
f.redirect(c)
|
f.redirect(c)
|
||||||
|
|
||||||
|
@ -250,15 +240,6 @@ func (a *Authenticator) LoginFlowHandler(c echo.Context) error {
|
||||||
return flow.progress(a, c)
|
return flow.progress(a, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Authenticator) InstallRoutes(e *echo.Echo) {
|
func (a *Authenticator) TokenHandler(c echo.Context) error {
|
||||||
authG := e.Group("/auth")
|
return c.String(http.StatusOK, "token good I guess")
|
||||||
authG.GET("/authorize", a.AuthorizeHandler)
|
|
||||||
authG.GET("/providers", a.ProvidersHandler)
|
|
||||||
|
|
||||||
authG.POST("/login_flow", a.BeginLoginFlowHandler)
|
|
||||||
|
|
||||||
loginFlow := authG.Group("/login_flow") // TODO: add IP address affinity middleware
|
|
||||||
loginFlow.POST("/:flow_id", a.LoginFlowHandler)
|
|
||||||
loginFlow.DELETE("/:flow_id", a.LoginFlowDeleteHandler)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue