68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
|
package ui
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/services/approval"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
ApprovalRoute = "/approval"
|
||
|
rememberKey = "remember"
|
||
|
)
|
||
|
|
||
|
type ApprovalController struct {
|
||
|
l *logrus.Logger
|
||
|
downstreamController http.Handler
|
||
|
srv approval.ApprovalService
|
||
|
}
|
||
|
|
||
|
func NewApprovalController(l *logrus.Logger, downstream http.Handler, srv approval.ApprovalService) *ApprovalController {
|
||
|
return &ApprovalController{
|
||
|
l: l,
|
||
|
downstreamController: downstream,
|
||
|
srv: srv,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (ac *ApprovalController) handleGetApproval(r *http.Request) {
|
||
|
ac.l.Debug("Checking if approval is remembered")
|
||
|
|
||
|
remembered, err := ac.srv.IsRemembered(r.Context(), approval.Claim{Email: "kilgore@kilgore.trout", ClientID: "9854d42da9cd91369a293758d514178c73d2b9774971d8965945ab2b81e83e69"})
|
||
|
if err != nil {
|
||
|
ac.l.Errorf("Failed to check if approval is remembered: %s", err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if remembered {
|
||
|
ac.l.Info("Approval is remembered, skipping approval page")
|
||
|
return
|
||
|
} else {
|
||
|
ac.l.Info("Approval is not remembered, continuing")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (ac *ApprovalController) handlePostApproval(r *http.Request) {
|
||
|
ac.l.Debug("Handling POST approval request")
|
||
|
if err := r.ParseForm(); err != nil {
|
||
|
ac.l.Errorf("Failed to parse request form: %s", err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if remember := r.Form.Get(rememberKey); remember == "on" {
|
||
|
if err := ac.srv.Remember(r.Context(), approval.Claim{Email: "kilgore@kilgore.trout", ClientID: "9854d42da9cd91369a293758d514178c73d2b9774971d8965945ab2b81e83e69"}); err != nil {
|
||
|
ac.l.Errorf("Failed to remember approval request: %s", err.Error())
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (ac *ApprovalController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||
|
if r.Method == http.MethodPost {
|
||
|
ac.handlePostApproval(r)
|
||
|
} else if r.Method == http.MethodGet {
|
||
|
ac.handleGetApproval(r)
|
||
|
}
|
||
|
ac.downstreamController.ServeHTTP(w, r)
|
||
|
}
|