2020-04-08 08:56:42 +02:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2020-05-20 11:13:14 +02:00
|
|
|
"forge.cadoles.com/wpetit/hydra-passwordless/internal/hydra"
|
2020-04-08 08:56:42 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/middleware/container"
|
|
|
|
)
|
|
|
|
|
|
|
|
func serveConsentPage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctn := container.Must(r.Context())
|
2020-05-20 11:13:14 +02:00
|
|
|
//tmpl := template.Must(ctn)
|
|
|
|
hydr := hydra.Must(ctn)
|
2020-04-08 08:56:42 +02:00
|
|
|
|
2020-05-20 11:13:14 +02:00
|
|
|
challenge, err := hydr.ConsentChallenge(r)
|
|
|
|
if err != nil {
|
|
|
|
if err == hydra.ErrChallengeNotFound {
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
2020-04-08 08:56:42 +02:00
|
|
|
|
2020-05-20 11:13:14 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(errors.Wrap(err, "could not retrieve consent challenge"))
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := hydr.ConsentRequest(challenge)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "could not retrieve hydra consent response"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Skip {
|
|
|
|
res, err := hydr.AcceptConsentRequest(challenge, &hydra.AcceptConsentRequest{
|
|
|
|
GrantScope: res.RequestedScope,
|
|
|
|
GrantAccessTokenAudience: res.RequestedAccessTokenAudience,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "could not accept hydra consent request"))
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, res.RedirectTo, http.StatusTemporaryRedirect)
|
|
|
|
return
|
2020-04-08 08:56:42 +02:00
|
|
|
}
|
2020-05-20 11:13:14 +02:00
|
|
|
|
|
|
|
res2, err := hydr.AcceptConsentRequest(challenge, &hydra.AcceptConsentRequest{
|
|
|
|
GrantScope: res.RequestedScope,
|
|
|
|
GrantAccessTokenAudience: res.RequestedAccessTokenAudience,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "could not accept hydra consent request"))
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, res2.RedirectTo, http.StatusTemporaryRedirect)
|
|
|
|
|
|
|
|
// spew.Dump(res)
|
|
|
|
|
|
|
|
// data := extendTemplateData(w, r, template.Data{
|
|
|
|
// csrf.TemplateTag: csrf.TemplateField(r),
|
|
|
|
// "RequestedScope": res.RequestedScope,
|
|
|
|
// "ConsentChallenge": challenge,
|
|
|
|
// })
|
|
|
|
|
|
|
|
// if err := tmpl.RenderPage(w, "consent.html.tmpl", data); err != nil {
|
|
|
|
// panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path))
|
|
|
|
// }
|
2020-04-08 08:56:42 +02:00
|
|
|
}
|