package main import ( "fmt" "html/template" "log" "net/http" "os" "github.com/gorilla/pat" "github.com/markbates/goth" "github.com/markbates/goth/gothic" "github.com/markbates/goth/providers/openidConnect" ) type UserInfo struct { Username string OpenIdInfo goth.User } func main() { OPENID_CONNECT_KEY := os.Getenv("OPENID_CONNECT_KEY") if OPENID_CONNECT_KEY == "" { panic("OPENID_CONNECT_KEY not set") } OPENID_CONNECT_SECRET := os.Getenv("OPENID_CONNECT_SECRET") if OPENID_CONNECT_SECRET == "" { panic("OPENID_CONNECT_SECRET not set") } OPENID_CONNECT_DISCOVERY_URL := os.Getenv("OPENID_CONNECT_DISCOVERY_URL") if OPENID_CONNECT_DISCOVERY_URL == "" { panic("OPENID_CONNECT_DISCOVERY_URL not set") } OPENID_CONNECT_CALLBACK_DOMAIN := os.Getenv("OPENID_CONNECT_CALLBACK_DOMAIN") if OPENID_CONNECT_CALLBACK_DOMAIN == "" { panic("OPENID_CONNECT_CALLBACK_DOMAIN not set") } OPENID_CONNECT_CALLBACK_URL := OPENID_CONNECT_CALLBACK_DOMAIN + "/auth/openid-connect/callback" openidConnect, err := openidConnect.New(OPENID_CONNECT_KEY, OPENID_CONNECT_SECRET, OPENID_CONNECT_CALLBACK_URL, OPENID_CONNECT_DISCOVERY_URL) if err != nil { panic(err) } if openidConnect != nil { goth.UseProviders(openidConnect) } p := pat.New() user_template, err := template.ParseFiles("tmpl/user.html") if err != nil { panic(err) } home_template, err := template.ParseFiles("tmpl/index.html") if err != nil { panic(err) } p.Get("/auth/{provider}/callback", func(res http.ResponseWriter, req *http.Request) { user, err := gothic.CompleteUserAuth(res, req) if err != nil { fmt.Fprintln(res, err) return } user_template.Execute(res, UserInfo{"TODO", user}) }) p.Get("/logout/{provider}", func(res http.ResponseWriter, req *http.Request) { gothic.Logout(res, req) res.Header().Set("Location", "/") res.WriteHeader(http.StatusTemporaryRedirect) }) p.Get("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) { // try to get the user without re-authenticating if gothUser, err := gothic.CompleteUserAuth(res, req); err == nil { user_template.Execute(res, UserInfo{"TODO", gothUser}) //user_template.Execute(res, gothUser) } else { gothic.BeginAuthHandler(res, req) } }) p.Get("/", func(w http.ResponseWriter, r *http.Request) { home_template.Execute(w, r) }) log.Println("Listening on localhost:3003") log.Fatal(http.ListenAndServe(":3003", p)) }