lavender/server/flow.go

97 lines
2.4 KiB
Go
Raw Normal View History

2023-10-01 21:44:49 +01:00
package server
import (
_ "embed"
"github.com/google/uuid"
2023-10-01 21:44:49 +01:00
"github.com/julienschmidt/httprouter"
"html/template"
"log"
"net/http"
2023-10-03 23:20:28 +01:00
"net/url"
"regexp"
"time"
2023-10-01 21:44:49 +01:00
)
var (
//go:embed flow-popup.go.html
flowPopupHtml string
flowPopupTemplate *template.Template
2023-10-03 23:20:28 +01:00
isValidState = regexp.MustCompile("^[a-z.]+%[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
2023-10-01 21:44:49 +01:00
)
func init() {
pageParse, err := template.New("pages").Parse(flowPopupHtml)
if err != nil {
log.Fatal("flow.go: Failed to parse flow popup HTML:", err)
}
flowPopupTemplate = pageParse
}
func (h *HttpServer) flowPopup(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
err := flowPopupTemplate.Execute(rw, map[string]any{
"ServiceName": flowPopupTemplate,
"Return": req.URL.Query().Get("return"),
})
if err != nil {
log.Printf("Failed to render page: %s\n", err)
}
}
2023-10-03 23:20:28 +01:00
func (h *HttpServer) flowPopupPost(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
2023-10-01 21:44:49 +01:00
login := h.manager.FindServiceFromLogin(req.PostFormValue("username"))
if login == nil {
http.Error(rw, "No login service defined for this username", http.StatusBadRequest)
return
}
2023-10-03 23:20:28 +01:00
returnUrl, err := url.Parse(req.PostFormValue("return"))
if err != nil {
http.Error(rw, "Invalid return URL", http.StatusBadRequest)
return
}
if !login.ValidReturnUrl(returnUrl) {
http.Error(rw, "Invalid return URL for this application", http.StatusBadRequest)
return
}
// save state for use later
state := login.Config.Namespace + "%" + uuid.NewString()
2023-10-03 23:20:28 +01:00
h.flowState.Set(state, flowStateData{
login,
returnUrl,
}, time.Now().Add(15*time.Minute))
2023-10-01 21:44:49 +01:00
2023-10-03 23:20:28 +01:00
// generate oauth2 config and redirect to authorize URL
oa2conf := login.Oauth2Config()
oa2conf.RedirectURL = h.baseUrl + "/callback"
nextUrl := oa2conf.AuthCodeURL(state)
http.Redirect(rw, req, nextUrl, http.StatusFound)
2023-10-01 21:44:49 +01:00
}
2023-10-03 23:20:28 +01:00
func (h *HttpServer) flowCallback(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
err := req.ParseForm()
if err != nil {
http.Error(rw, "Error parsing form", http.StatusBadRequest)
return
}
2023-10-01 21:44:49 +01:00
2023-10-03 23:20:28 +01:00
q := req.URL.Query()
state := q.Get("state")
if !isValidState.MatchString(state) {
http.Error(rw, "Invalid state", http.StatusBadRequest)
return
}
if !h.manager.CheckIssuer(state) {
http.Error(rw, "Invalid state", http.StatusBadRequest)
return
}
v, found := h.flowState.Get(state)
if !found {
http.Error(rw, "Invalid state", http.StatusBadRequest)
return
}
// TODO: process flow callback
2023-10-01 21:44:49 +01:00
}