74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
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() {
|
|
openidConnect, err := openidConnect.New(os.Getenv("OPENID_CONNECT_KEY"), os.Getenv("OPENID_CONNECT_SECRET"), os.Getenv("OPENID_CONNECT_CALLBACK_URL"), os.Getenv("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))
|
|
}
|