hydra-webauthn/internal/route/hydra/consent.go

51 lines
1.3 KiB
Go

package hydra
import (
"net/http"
"forge.cadoles.com/wpetit/hydra-webauthn/internal/hydra"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
)
func serveConsentPage(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
hydr := hydra.Must(ctn)
challenge, err := hydr.ConsentChallenge(r)
if err != nil {
if err == hydra.ErrChallengeNotFound {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
panic(errors.Wrap(err, "could not retrieve consent challenge"))
}
consentRes, err := hydr.ConsentRequest(challenge)
if err != nil {
panic(errors.Wrap(err, "could not retrieve hydra consent response"))
}
userAttributes, ok := consentRes.Context["userAttributes"].(map[string]any)
if !ok {
userAttributes = make(map[string]any)
}
acceptConsentReq := &hydra.AcceptConsentRequest{
GrantScope: consentRes.RequestedScope,
GrantAccessTokenAudience: consentRes.RequestedAccessTokenAudience,
Session: hydra.AcceptConsentSession{
IDToken: userAttributes,
},
}
acceptRes, err := hydr.AcceptConsentRequest(challenge, acceptConsentReq)
if err != nil {
panic(errors.Wrap(err, "could not accept hydra consent request"))
}
http.Redirect(w, r, acceptRes.RedirectTo, http.StatusTemporaryRedirect)
}