From 8ba8fbebb4bbd0d4d01f1ff6c08ef56bfa5b8d06 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Wed, 13 Mar 2019 08:15:10 -0700 Subject: idp: i18n --- idp/main.go | 66 +++++++++++++++++++++++++++++++----------- idp/translations/en_US.json | 3 +- idp/translations/jbo_latn.json | 7 +++-- 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/idp/main.go b/idp/main.go index 349bd5c..c22206f 100644 --- a/idp/main.go +++ b/idp/main.go @@ -10,6 +10,7 @@ import ( "text/template" "time" + "github.com/Xe/x/i18n" "github.com/Xe/x/idp/idpmiddleware" "github.com/Xe/x/internal" "github.com/pborman/uuid" @@ -19,11 +20,13 @@ import ( ) var ( - domain = flag.String("domain", "idp.christine.website", "domain to be hosted from") - otpSecret = flag.String("otp-secret", "", "OTP secret") - port = flag.String("port", "5484", "TCP port to listen on for HTTP") - owner = flag.String("owner", "https://christine.website/", "the me=that is required") - secretGen = flag.Int("secret-gen", 0, "generate a secret of len if set") + domain = flag.String("domain", "idp.christine.website", "domain to be hosted from") + otpSecret = flag.String("otp-secret", "", "OTP secret") + port = flag.String("port", "5484", "TCP port to listen on for HTTP") + owner = flag.String("owner", "https://christine.website/", "the me=that is required") + secretGen = flag.Int("secret-gen", 0, "generate a secret of len if set") + defaultLang = flag.String("default-language", "en_US", "default language if none is set") + gitRev = flag.String("git-rev", "", "git revision of runtime (used for dokku detection)") ) func main() { @@ -33,8 +36,16 @@ func main() { log.Fatal(gotp.RandomSecret(*secretGen)) } + translationPath := "./translations" + if *gitRev != "" { + translationPath = "/app/translations" + } + + l := i18n.New(*defaultLang, translationPath) + i := &idp{ t: gotp.NewDefaultTOTP(*otpSecret), + l: l, bearer2me: map[string]string{}, } @@ -42,15 +53,30 @@ func main() { def := idpmiddleware.XeProtect("https://" + *domain + "/")(http.DefaultServeMux) mux := http.NewServeMux() + mux.HandleFunc("/lang", func(w http.ResponseWriter, r *http.Request) { + locales := i18n.GetLocales(r) + + json.NewEncoder(w).Encode(locales) + }) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/auth/challenge" { - r.URL.Path = "/.within/x/idpmiddleware/challenge" - http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect) + tr := i.l.TranslationsForRequest(r) + fm := template.FuncMap{ + "T": tr.Value, + } + + t, err := template.New("root").Funcs(fm).Parse(rootPageTemplate) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/html") - w.Write([]byte(rootPageTemplate)) + err = t.Execute(w, nil) + if err != nil { + log.Printf("%v", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } }) mux.HandleFunc("/auth", i.auth) mux.HandleFunc("/challenge", i.challenge) @@ -62,6 +88,7 @@ func main() { type idp struct { t *gotp.TOTP + l *i18n.L sync.Mutex bearer2me map[string]string } @@ -126,7 +153,12 @@ func (i *idp) auth(w http.ResponseWriter, r *http.Request) { return } - t, err := template.New("auth").Parse(authPageTemplate) + tr := i.l.TranslationsForRequest(r) + fm := template.FuncMap{ + "T": tr.Value, + } + + t, err := template.New("auth").Funcs(fm).Parse(authPageTemplate) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -202,7 +234,7 @@ func (i *idp) challenge(w http.ResponseWriter, r *http.Request) { const rootPageTemplate = ` -Forbidden +{{ T "errors.forbidden" }}