lavender/server/flow.go

135 lines
3.6 KiB
Go
Raw Normal View History

2023-10-01 21:44:49 +01:00
package server
import (
"context"
2023-10-01 21:44:49 +01:00
_ "embed"
2023-10-04 14:51:38 +01:00
"encoding/json"
"fmt"
"github.com/google/uuid"
2023-10-01 21:44:49 +01:00
"github.com/julienschmidt/httprouter"
"golang.org/x/oauth2"
2023-10-01 21:44:49 +01:00
"html/template"
"log"
"net/http"
2023-10-04 14:51:38 +01:00
"strings"
2023-10-03 23:20:28 +01:00
"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
2023-10-04 14:51:38 +01:00
//go:embed flow-callback.go.html
flowCallbackHtml string
flowCallbackTemplate *template.Template
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
2023-10-04 14:51:38 +01:00
pageParse, err = template.New("pages").Parse(flowCallbackHtml)
if err != nil {
log.Fatal("flow.go: Failed to parse flow callback HTML:", err)
}
flowCallbackTemplate = pageParse
2023-10-01 21:44:49 +01:00
}
func (h *HttpServer) flowPopup(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) {
err := flowPopupTemplate.Execute(rw, map[string]any{
"ServiceName": h.serviceName,
2023-10-04 14:51:38 +01:00
"Origin": req.URL.Query().Get("origin"),
2023-10-01 21:44:49 +01:00
})
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) {
loginName := req.PostFormValue("loginname")
login := h.manager.FindServiceFromLogin(loginName)
2023-10-01 21:44:49 +01:00
if login == nil {
http.Error(rw, "No login service defined for this username", http.StatusBadRequest)
return
}
2023-10-04 14:51:38 +01:00
targetOrigin := req.PostFormValue("origin")
if _, found := h.services[targetOrigin]; !found {
http.Error(rw, "Invalid target origin", http.StatusBadRequest)
2023-10-03 23:20:28 +01:00
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,
2023-10-04 14:51:38 +01:00
targetOrigin,
2023-10-03 23:20:28 +01:00
}, 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
2023-10-04 14:51:38 +01:00
oa2conf := login.OAuth2Config
oa2conf.RedirectURL = h.baseUrl + "/callback"
nextUrl := oa2conf.AuthCodeURL(state, oauth2.SetAuthURLParam("login_name", loginName))
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")
2023-10-04 14:51:38 +01:00
n := strings.IndexByte(state, '%')
if !h.manager.CheckNamespace(state[:n]) {
2023-10-03 23:20:28 +01:00
http.Error(rw, "Invalid state", http.StatusBadRequest)
return
}
v, found := h.flowState.Get(state)
if !found {
http.Error(rw, "Invalid state", http.StatusBadRequest)
return
}
oa2conf := v.sso.OAuth2Config
oa2conf.RedirectURL = h.baseUrl + "/callback"
exchange, err := oa2conf.Exchange(context.Background(), q.Get("code"))
2023-10-04 14:51:38 +01:00
if err != nil {
fmt.Println("Failed exchange:", err)
2023-10-04 14:51:38 +01:00
http.Error(rw, "Failed to exchange code", http.StatusInternalServerError)
return
}
client := v.sso.OAuth2Config.Client(req.Context(), exchange)
v2, err := client.Get(v.sso.UserInfoEndpoint)
if err != nil {
fmt.Println("Failed to get userinfo:", err)
2023-10-04 14:51:38 +01:00
http.Error(rw, "Failed to get userinfo", http.StatusInternalServerError)
return
}
defer v2.Body.Close()
if v2.StatusCode != http.StatusOK {
http.Error(rw, "Failed to get userinfo", http.StatusInternalServerError)
return
}
var v3 any
if json.NewDecoder(v2.Body).Decode(&v3) != nil {
fmt.Println("Failed to decode userinfo:", err)
2023-10-04 14:51:38 +01:00
http.Error(rw, "Failed to decode userinfo JSON", http.StatusInternalServerError)
return
}
// TODO: generate signed mjwt object
2023-10-04 14:51:38 +01:00
_ = flowCallbackTemplate.Execute(rw, map[string]any{
"ServiceName": h.serviceName,
2023-10-04 14:51:38 +01:00
"TargetOrigin": v.targetOrigin,
"TargetMessage": v3,
})
2023-10-01 21:44:49 +01:00
}