goweb-oidc/internal/route/profile.go

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

2020-05-20 13:06:04 +02:00
package route
import (
"encoding/json"
"net/http"
oidc "forge.cadoles.com/cadoles/goweb-oidc"
2020-05-20 13:06:04 +02:00
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service/template"
)
func serveProfilePage(w http.ResponseWriter, r *http.Request) {
ctn := container.Must(r.Context())
tmpl := template.Must(ctn)
idToken, err := oidc.IDToken(w, r)
if err != nil {
panic(errors.Wrap(err, "could not retrieve idToken"))
}
2020-05-26 11:17:16 +02:00
claims := make(map[string]interface{})
if err := idToken.Claims(&claims); err != nil {
panic(errors.Wrap(err, "could not decode claims"))
}
2020-05-20 13:06:04 +02:00
jsonIDToken, err := json.MarshalIndent(idToken, "", " ")
if err != nil {
panic(errors.Wrap(err, "could not encode idToken"))
}
2020-05-26 11:17:16 +02:00
jsonClaims, err := json.MarshalIndent(claims, "", " ")
if err != nil {
panic(errors.Wrap(err, "could not encode claims"))
}
2020-05-20 13:06:04 +02:00
data := extendTemplateData(w, r, template.Data{
"IDToken": idToken,
2020-05-26 11:17:16 +02:00
"Claims": claims,
"JSONClaims": string(jsonClaims),
2020-05-20 13:06:04 +02:00
"JSONIDToken": string(jsonIDToken),
})
if err := tmpl.RenderPage(w, "profile.html.tmpl", data); err != nil {
panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path))
}
}