Not sure, what I missed in the various hints?
I have an action:
It is triggered:
In the Action Details it is logged:
But there are no Claims when I decode the token in go?
func (app *application) CallbackHandler(w http.ResponseWriter, r *http.Request) {
// Retrieve the authorization code from the URL
code := r.URL.Query().Get("code")
if code == "" {
http.Error(w, "Authorization code not found", http.StatusBadRequest)
return
}
// Exchange the authorization code for an OAuth2 token
token, err := app.authenticator.Exchange(context.Background(), code)
if err != nil {
http.Error(w, "Failed to exchange authorization code for token", http.StatusUnauthorized)
return
}
// Verify the ID token to ensure its validity
idToken, err := app.authenticator.VerifyIDToken(context.Background(), token)
if err != nil {
http.Error(w, "Failed to verify ID token", http.StatusInternalServerError)
return
}
// Extract all claims from the ID token, including custom claims
var claims map[string]interface{}
if err := idToken.Claims(&claims); err != nil {
http.Error(w, "Failed to parse token claims: "+err.Error(), http.StatusInternalServerError)
return
}
// Marshal the claims to JSON for debugging purposes
claimsJSON, err := json.MarshalIndent(claims, "", " ")
if err != nil {
http.Error(w, "Error marshaling claims to JSON", http.StatusInternalServerError)
return
}
// Log all claims, including custom claims
log.Printf("All claims: %s\n", string(claimsJSON))
// For debug purposes, you can also print specific custom claims if needed
// For example, if you have a custom claim like "https://example.com/roles"
if roles, ok := claims["https://example.com/roles"].([]interface{}); ok {
log.Printf("Custom roles claim: %v\n", roles)
}
// URL-encode the JSON string before storing it in the cookie
encodedClaims := url.QueryEscape(string(claimsJSON))
// Set the profile as a URL-encoded JSON string in a cookie
http.SetCookie(w, &http.Cookie{
Name: "profile",
Value: encodedClaims,
Path: "/",
})
// Set the access token in a cookie (not URL-encoded, assuming no special chars)
http.SetCookie(w, &http.Cookie{
Name: "access_token",
Value: token.AccessToken,
Path: "/",
})
// Redirect the user to the /user page
http.Redirect(w, r, "/user", http.StatusSeeOther)
}