cmd/example-app: check all errors, pass claims as string to renderToken

Signed-off-by: Stephan Renatus <srenatus@chef.io>
This commit is contained in:
Stephan Renatus 2019-07-24 12:26:51 +02:00
parent c4e0587df1
commit d7c7d42466
No known key found for this signature in database
GPG key ID: 811376EBA81C2C59
2 changed files with 12 additions and 6 deletions

View file

@ -314,7 +314,7 @@ func (a *app) handleCallback(w http.ResponseWriter, r *http.Request) {
idToken, err := a.verifier.Verify(r.Context(), rawIDToken) idToken, err := a.verifier.Verify(r.Context(), rawIDToken)
if err != nil { if err != nil {
http.Error(w, fmt.Sprintf("Failed to verify ID token: %v", err), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("failed to verify ID token: %v", err), http.StatusInternalServerError)
return return
} }
@ -325,10 +325,16 @@ func (a *app) handleCallback(w http.ResponseWriter, r *http.Request) {
} }
var claims json.RawMessage var claims json.RawMessage
idToken.Claims(&claims) if err := idToken.Claims(&claims); err != nil {
http.Error(w, fmt.Sprintf("error decoding ID token claims: %v", err), http.StatusInternalServerError)
return
}
buff := new(bytes.Buffer) buff := new(bytes.Buffer)
json.Indent(buff, []byte(claims), "", " ") if err := json.Indent(buff, []byte(claims), "", " "); err != nil {
http.Error(w, fmt.Sprintf("error indenting ID token claims: %v", err), http.StatusInternalServerError)
return
}
renderToken(w, a.redirectURI, rawIDToken, accessToken, token.RefreshToken, buff.Bytes()) renderToken(w, a.redirectURI, rawIDToken, accessToken, token.RefreshToken, buff.String())
} }

View file

@ -66,13 +66,13 @@ pre {
</html> </html>
`)) `))
func renderToken(w http.ResponseWriter, redirectURL, idToken, accessToken, refreshToken string, claims []byte) { func renderToken(w http.ResponseWriter, redirectURL, idToken, accessToken, refreshToken, claims string) {
renderTemplate(w, tokenTmpl, tokenTmplData{ renderTemplate(w, tokenTmpl, tokenTmplData{
IDToken: idToken, IDToken: idToken,
AccessToken: accessToken, AccessToken: accessToken,
RefreshToken: refreshToken, RefreshToken: refreshToken,
RedirectURL: redirectURL, RedirectURL: redirectURL,
Claims: string(claims), Claims: claims,
}) })
} }