Skip to content

Commit c7cea78

Browse files
committed
Use html/template instead of printf for auth page
1 parent 55759f6 commit c7cea78

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

auth_server/server/bindata.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auth_server/server/data/google_auth.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
}
2828
function start() {
2929
gapi.load('auth2', function() {
30-
gapi.auth2.init({client_id: '%s'}).then(checkLogin);
30+
gapi.auth2.init({client_id: '{{.ClientId}}'}).then(checkLogin);
3131
});
3232
}
3333
</script>

auth_server/server/google_auth.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"errors"
2222
"fmt"
23+
"html/template"
2324
"io/ioutil"
2425
"net/http"
2526
"net/url"
@@ -132,6 +133,7 @@ type GoogleAuth struct {
132133
config *GoogleAuthConfig
133134
db *leveldb.DB
134135
client *http.Client
136+
tmpl *template.Template
135137
}
136138

137139
func NewGoogleAuth(c *GoogleAuthConfig) (*GoogleAuth, error) {
@@ -144,6 +146,7 @@ func NewGoogleAuth(c *GoogleAuthConfig) (*GoogleAuth, error) {
144146
config: c,
145147
db: db,
146148
client: &http.Client{Timeout: 10 * time.Second},
149+
tmpl: template.Must(template.New("google_auth").Parse(string(MustAsset("data/google_auth.tmpl")))),
147150
}, nil
148151
}
149152

@@ -173,7 +176,9 @@ func (ga *GoogleAuth) doGoogleAuth(rw http.ResponseWriter, req *http.Request) {
173176
}
174177

175178
func (ga *GoogleAuth) doGoogleAuthPage(rw http.ResponseWriter, req *http.Request) {
176-
fmt.Fprintf(rw, string(MustAsset("data/google_auth.tmpl")), ga.config.ClientId)
179+
if err := ga.tmpl.Execute(rw, struct{ ClientId string }{ClientId: ga.config.ClientId}); err != nil {
180+
http.Error(rw, fmt.Sprintf("Template error: %s", err), http.StatusInternalServerError)
181+
}
177182
}
178183

179184
// https://developers.google.com/identity/protocols/OAuth2WebServer#handlingtheresponse

0 commit comments

Comments
 (0)