hydra-passwordless/internal/route/login.go

176 lines
4.3 KiB
Go
Raw Normal View History

2020-04-08 08:56:42 +02:00
package route
import (
"fmt"
2020-04-08 08:56:42 +02:00
"net/http"
2020-04-24 09:27:07 +02:00
netMail "net/mail"
"strings"
2020-04-08 08:56:42 +02:00
2020-05-20 11:13:14 +02:00
"forge.cadoles.com/wpetit/hydra-passwordless/internal/command"
"forge.cadoles.com/wpetit/hydra-passwordless/internal/config"
2020-04-08 08:56:42 +02:00
"forge.cadoles.com/wpetit/hydra-passwordless/internal/hydra"
"github.com/gorilla/csrf"
"github.com/pkg/errors"
2020-05-20 11:13:14 +02:00
"gitlab.com/wpetit/goweb/cqrs"
2020-04-08 08:56:42 +02:00
"gitlab.com/wpetit/goweb/middleware/container"
2020-04-24 09:27:07 +02:00
"gitlab.com/wpetit/goweb/service/session"
2020-04-08 08:56:42 +02:00
"gitlab.com/wpetit/goweb/service/template"
)
func serveLoginPage(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
hydr := hydra.Must(ctn)
challenge, err := hydr.LoginChallenge(r)
if err != nil {
if err == hydra.ErrChallengeNotFound {
2020-05-26 11:11:53 +02:00
err := renderErrorPage(
w, r,
http.StatusBadRequest,
"Requête invalide",
"Certaines informations requises afin de réaliser votre requête sont absentes.",
)
if err != nil {
panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path))
}
2020-04-08 08:56:42 +02:00
return
}
panic(errors.Wrap(err, "could not retrieve login challenge"))
}
res, err := hydr.LoginRequest(challenge)
if err != nil {
panic(errors.Wrap(err, "could not retrieve hydra login response"))
}
2020-04-24 09:27:07 +02:00
if res.Skip {
2020-05-26 11:11:53 +02:00
accept := &hydra.AcceptLoginRequest{
Subject: res.Subject,
Context: map[string]interface{}{
"email": res.Subject,
},
}
res, err := hydr.AcceptLoginRequest(challenge, accept)
2020-04-24 09:27:07 +02:00
if err != nil {
2020-05-26 11:11:53 +02:00
panic(errors.Wrap(err, "could not retrieve hydra accept response"))
2020-04-24 09:27:07 +02:00
}
2020-05-26 11:11:53 +02:00
http.Redirect(w, r, res.RedirectTo, http.StatusSeeOther)
2020-04-24 09:27:07 +02:00
return
}
2020-04-08 08:56:42 +02:00
tmpl := template.Must(ctn)
data := extendTemplateData(w, r, template.Data{
csrf.TemplateTag: csrf.TemplateField(r),
2020-04-24 09:27:07 +02:00
"LoginChallenge": challenge,
"Email": "",
2020-05-26 11:11:53 +02:00
"ClientName": res.Client.ClientName,
"ClientURI": res.Client.ClientURI,
2020-04-08 08:56:42 +02:00
})
if err := tmpl.RenderPage(w, "login.html.tmpl", data); err != nil {
panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path))
}
}
func handleLoginForm(w http.ResponseWriter, r *http.Request) {
2020-05-20 11:13:14 +02:00
ctx := r.Context()
ctn := container.Must(ctx)
2020-04-08 08:56:42 +02:00
tmpl := template.Must(ctn)
2020-05-26 11:11:53 +02:00
hydr := hydra.Must(ctn)
2020-05-20 11:13:14 +02:00
bus := cqrs.Must(ctn)
conf := config.Must(ctn)
2020-04-24 09:27:07 +02:00
if err := r.ParseForm(); err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
challenge := r.Form.Get("challenge")
2020-05-26 11:11:53 +02:00
if challenge == "" {
err := renderErrorPage(
w, r,
http.StatusBadRequest,
"Requête invalide",
"Certaines informations requises sont manquantes pour pouvoir réaliser votre requête.",
)
if err != nil {
panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path))
}
return
}
res, err := hydr.LoginRequest(challenge)
if err != nil {
panic(errors.Wrap(err, "could not retrieve hydra login response"))
}
email := r.Form.Get("email")
rememberMe := r.Form.Get("rememberMe")
2020-04-24 09:27:07 +02:00
renderFlashError := func(message string) {
sess, err := session.Must(ctn).Get(w, r)
if err != nil {
panic(errors.Wrap(err, "could not retrieve session"))
}
sess.AddFlash(session.FlashError, message)
if err := sess.Save(w, r); err != nil {
panic(errors.Wrap(err, "could not save session"))
}
data := extendTemplateData(w, r, template.Data{
csrf.TemplateTag: csrf.TemplateField(r),
"LoginChallenge": challenge,
"Email": email,
2020-05-26 11:11:53 +02:00
"ClientName": res.Client.ClientName,
"ClientURI": res.Client.ClientURI,
2020-04-24 09:27:07 +02:00
})
if err := tmpl.RenderPage(w, "login.html.tmpl", data); err != nil {
panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path))
}
}
if _, err := netMail.ParseAddress(email); err != nil {
renderFlashError("Veuillez saisir une adresse courriel valide")
return
}
var baseURL string
if conf.HTTP.BaseURL != "" {
baseURL = strings.TrimSuffix(conf.HTTP.BaseURL, "/")
} else {
baseURL = fmt.Sprintf("%s//%s", r.Host, r.URL.Scheme)
}
2020-05-20 11:13:14 +02:00
cmd := &command.SendConfirmationEmailRequest{
Email: email,
Challenge: challenge,
BaseURL: baseURL,
RememberMe: rememberMe == "on",
ClientName: res.Client.ClientName,
ClientURI: res.Client.ClientURI,
2020-04-24 09:27:07 +02:00
}
2020-05-20 11:13:14 +02:00
if _, err := bus.Exec(ctx, cmd); err != nil {
panic(errors.Wrap(err, "could not execute command"))
2020-04-24 09:27:07 +02:00
}
2020-04-08 08:56:42 +02:00
data := extendTemplateData(w, r, template.Data{})
if err := tmpl.RenderPage(w, "email_sent.html.tmpl", data); err != nil {
panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path))
}
}