This repository has been archived on 2022-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
dex/cmd/example-app/templates.go

94 lines
2.6 KiB
Go
Raw Normal View History

2016-08-09 05:02:42 +05:30
package main
import (
"html/template"
"log"
"net/http"
)
var indexTmpl = template.Must(template.New("index.html").Parse(`<html>
<body>
2016-12-23 06:28:21 +05:30
<form action="/login" method="post">
2016-08-09 05:02:42 +05:30
<p>
Authenticate for:<input type="text" name="cross_client" placeholder="list of client-ids">
</p>
<p>
Extra scopes:<input type="text" name="extra_scopes" placeholder="list of scopes">
</p>
<p>
Request offline access:<input type="checkbox" name="offline_access" value="yes" checked>
2016-08-09 05:02:42 +05:30
</p>
<input type="submit" value="Login">
</form>
</body>
</html>`))
func renderIndex(w http.ResponseWriter) {
renderTemplate(w, indexTmpl, nil)
}
type tokenTmplData struct {
IDToken string
2019-02-01 13:09:35 +05:30
AccessToken string
2016-08-09 05:02:42 +05:30
RefreshToken string
RedirectURL string
Claims string
}
var tokenTmpl = template.Must(template.New("token.html").Parse(`<html>
<head>
<style>
/* make pre wrap */
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
</style>
</head>
<body>
2019-02-01 13:09:35 +05:30
<p> ID Token: <pre><code>{{ .IDToken }}</code></pre></p>
<p> Access Token: <pre><code>{{ .AccessToken }}</code></pre></p>
2016-08-09 05:02:42 +05:30
<p> Claims: <pre><code>{{ .Claims }}</code></pre></p>
2016-12-23 06:28:21 +05:30
{{ if .RefreshToken }}
2016-08-09 05:02:42 +05:30
<p> Refresh Token: <pre><code>{{ .RefreshToken }}</code></pre></p>
2016-12-23 06:28:21 +05:30
<form action="{{ .RedirectURL }}" method="post">
<input type="hidden" name="refresh_token" value="{{ .RefreshToken }}">
<input type="submit" value="Redeem refresh token">
</form>
{{ end }}
2016-08-09 05:02:42 +05:30
</body>
</html>
`))
2019-02-01 13:09:35 +05:30
func renderToken(w http.ResponseWriter, redirectURL, idToken, accessToken, refreshToken string, claims []byte) {
2016-08-09 05:02:42 +05:30
renderTemplate(w, tokenTmpl, tokenTmplData{
IDToken: idToken,
2019-02-01 13:09:35 +05:30
AccessToken: accessToken,
2016-08-09 05:02:42 +05:30
RefreshToken: refreshToken,
RedirectURL: redirectURL,
Claims: string(claims),
})
}
func renderTemplate(w http.ResponseWriter, tmpl *template.Template, data interface{}) {
err := tmpl.Execute(w, data)
if err == nil {
return
}
switch err := err.(type) {
case *template.Error:
// An ExecError guarantees that Execute has not written to the underlying reader.
2016-08-09 05:02:42 +05:30
log.Printf("Error rendering template %s: %s", tmpl.Name(), err)
// TODO(ericchiang): replace with better internal server error.
http.Error(w, "Internal server error", http.StatusInternalServerError)
default:
// An error with the underlying write, such as the connection being
// dropped. Ignore for now.
}
}