hydra-passwordless/internal/route/consent.go

52 lines
1.3 KiB
Go
Raw Normal View History

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
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"))
}
2020-05-26 11:11:53 +02:00
consentRes, err := hydr.ConsentRequest(challenge)
2020-05-20 11:13:14 +02:00
if err != nil {
panic(errors.Wrap(err, "could not retrieve hydra consent response"))
}
2020-05-26 11:11:53 +02:00
scopes := []string{"email"}
scopes = append(scopes, consentRes.RequestedScope...)
acceptConsentReq := &hydra.AcceptConsentRequest{
GrantScope: scopes,
GrantAccessTokenAudience: consentRes.RequestedAccessTokenAudience,
Session: hydra.AcceptConsentSession{
IDToken: map[string]interface{}{
"email": consentRes.Context["email"],
"email_verified": true,
},
},
2020-04-08 08:56:42 +02:00
}
2020-05-20 11:13:14 +02:00
2020-05-26 11:11:53 +02:00
acceptRes, err := hydr.AcceptConsentRequest(challenge, acceptConsentReq)
2020-05-20 11:13:14 +02:00
if err != nil {
panic(errors.Wrap(err, "could not accept hydra consent request"))
}
2020-05-26 11:11:53 +02:00
http.Redirect(w, r, acceptRes.RedirectTo, http.StatusTemporaryRedirect)
2020-04-08 08:56:42 +02:00
}