2020-12-31 09:42:48 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ory/hydra-client-go/client/admin"
|
|
|
|
"github.com/ory/hydra-client-go/models"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type consentHandler struct {
|
|
|
|
adminClient *admin.Client
|
2020-12-31 13:19:21 +01:00
|
|
|
logger *log.Logger
|
2020-12-31 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
func (h *consentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
consentChallenge := r.URL.Query().Get("consent_challenge")
|
|
|
|
consentRequest, err := h.adminClient.AcceptConsentRequest(
|
2020-12-31 09:42:48 +01:00
|
|
|
admin.NewAcceptConsentRequestParams().WithConsentChallenge(consentChallenge).WithBody(
|
|
|
|
&models.AcceptConsentRequest{
|
|
|
|
GrantAccessTokenAudience: nil,
|
|
|
|
GrantScope: []string{"openid", "offline"},
|
|
|
|
HandledAt: models.NullTime(time.Now()),
|
|
|
|
Remember: true,
|
|
|
|
RememberFor: 86400,
|
|
|
|
}).WithTimeout(time.Second * 10))
|
|
|
|
if err != nil {
|
2020-12-31 13:19:21 +01:00
|
|
|
h.logger.Panic(err)
|
2020-12-31 09:42:48 +01:00
|
|
|
}
|
2020-12-31 13:19:21 +01:00
|
|
|
w.Header().Add("Location", *consentRequest.GetPayload().RedirectTo)
|
|
|
|
w.WriteHeader(http.StatusFound)
|
2020-12-31 09:42:48 +01:00
|
|
|
}
|
|
|
|
|
2020-12-31 13:19:21 +01:00
|
|
|
func NewConsentHandler(logger *log.Logger, ctx context.Context) *consentHandler {
|
2020-12-31 09:42:48 +01:00
|
|
|
return &consentHandler{
|
2020-12-31 13:19:21 +01:00
|
|
|
logger: logger,
|
2020-12-31 09:42:48 +01:00
|
|
|
adminClient: ctx.Value(CtxAdminClient).(*admin.Client),
|
|
|
|
}
|
|
|
|
}
|