2017-01-08 20:15:09 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2017-01-09 21:34:37 +01:00
|
|
|
"crypto/rand"
|
|
|
|
|
"encoding/base64"
|
2017-01-08 20:15:09 +01:00
|
|
|
"fmt"
|
2017-01-09 21:34:37 +01:00
|
|
|
"html/template"
|
2017-01-08 20:15:09 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
2017-01-09 21:34:37 +01:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"github.com/gorilla/sessions"
|
2017-01-08 20:15:09 +01:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
)
|
|
|
|
|
|
2017-01-09 21:34:37 +01:00
|
|
|
const (
|
|
|
|
|
defaultLayout = "templates/layout.html"
|
|
|
|
|
templateDir = "templates/"
|
|
|
|
|
)
|
2017-01-08 20:15:09 +01:00
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
discordOauthConfig = &oauth2.Config{
|
|
|
|
|
RedirectURL: "",
|
|
|
|
|
ClientID: "",
|
|
|
|
|
ClientSecret: "",
|
2017-01-09 21:34:37 +01:00
|
|
|
Scopes: []string{"identify", "guilds"},
|
|
|
|
|
Endpoint: *endp,
|
2017-01-08 20:15:09 +01:00
|
|
|
}
|
|
|
|
|
// Some random string, random for each request
|
2017-01-09 21:34:37 +01:00
|
|
|
oauthStateString string
|
2017-01-08 20:15:09 +01:00
|
|
|
endp = &oauth2.Endpoint{
|
|
|
|
|
AuthURL: "https://discordapp.com/api/oauth2/authorize",
|
|
|
|
|
TokenURL: "https://discordapp.com/api/oauth2/token",
|
|
|
|
|
}
|
2017-01-09 21:34:37 +01:00
|
|
|
tmpls = map[string]*template.Template{}
|
|
|
|
|
// TODO change secret
|
|
|
|
|
store *sessions.CookieStore
|
2017-01-08 20:15:09 +01:00
|
|
|
)
|
|
|
|
|
|
2017-01-09 21:34:37 +01:00
|
|
|
// startWebServer
|
2017-01-08 20:27:51 +01:00
|
|
|
func startWebServer(port string, ci string, cs string, redirectURL string) {
|
2017-01-09 21:34:37 +01:00
|
|
|
tmpls["home.html"] = template.Must(template.ParseFiles(templateDir+"home.html", defaultLayout))
|
|
|
|
|
store = sessions.NewCookieStore([]byte(cs))
|
2017-01-08 20:15:09 +01:00
|
|
|
discordOauthConfig.ClientID = ci
|
|
|
|
|
discordOauthConfig.ClientSecret = cs
|
2017-01-08 20:27:51 +01:00
|
|
|
discordOauthConfig.RedirectURL = redirectURL + "/discordCallback"
|
2017-01-09 21:34:37 +01:00
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
r.HandleFunc("/", handleMain)
|
|
|
|
|
r.HandleFunc("/logout", handleLogout)
|
|
|
|
|
r.HandleFunc("/discordLogin", handlediscordLogin)
|
|
|
|
|
r.HandleFunc("/discordCallback", handlediscordCallback)
|
|
|
|
|
|
|
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
|
|
|
|
http.Handle("/", r)
|
|
|
|
|
http.ListenAndServe(":"+port, nil)
|
2017-01-08 20:15:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleMain(w http.ResponseWriter, r *http.Request) {
|
2017-01-09 21:34:37 +01:00
|
|
|
session, _ := store.Get(r, "session-name")
|
|
|
|
|
if session.Values["discordUsername"] != nil {
|
|
|
|
|
fmt.Fprintf(w, "<html><body>you are logged in as %s. <a href=\"/logout\">logout</a></body></html>", session.Values["discordUsername"])
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
tmpls["home.html"].ExecuteTemplate(w, "base", map[string]interface{}{})
|
|
|
|
|
}
|
|
|
|
|
func handleLogout(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
cookie := &http.Cookie{
|
|
|
|
|
Name: "session-name",
|
|
|
|
|
Value: "",
|
|
|
|
|
Path: "/",
|
|
|
|
|
MaxAge: -1,
|
|
|
|
|
}
|
|
|
|
|
http.SetCookie(w, cookie)
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
2017-01-08 20:15:09 +01:00
|
|
|
}
|
|
|
|
|
func handlediscordLogin(w http.ResponseWriter, r *http.Request) {
|
2017-01-09 21:34:37 +01:00
|
|
|
b := make([]byte, 16)
|
|
|
|
|
rand.Read(b)
|
|
|
|
|
oauthStateString = base64.URLEncoding.EncodeToString(b)
|
|
|
|
|
|
|
|
|
|
session, _ := store.Get(r, "session-name")
|
|
|
|
|
session.Values["state"] = oauthStateString
|
|
|
|
|
session.Save(r, w)
|
|
|
|
|
|
2017-01-08 20:15:09 +01:00
|
|
|
url := discordOauthConfig.AuthCodeURL(oauthStateString)
|
|
|
|
|
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handlediscordCallback(w http.ResponseWriter, r *http.Request) {
|
2017-01-09 21:34:37 +01:00
|
|
|
session, err := store.Get(r, "session-name")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintln(w, "aborted")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if r.URL.Query().Get("state") != session.Values["state"] {
|
|
|
|
|
fmt.Fprintln(w, "no state match; possible csrf OR cookies not enabled")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-08 20:15:09 +01:00
|
|
|
state := r.FormValue("state")
|
|
|
|
|
if state != oauthStateString {
|
|
|
|
|
fmt.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state)
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-09 21:34:37 +01:00
|
|
|
token, err := discordOauthConfig.Exchange(oauth2.NoContext, r.FormValue("code"))
|
2017-01-08 20:15:09 +01:00
|
|
|
if err != nil {
|
2017-01-09 21:34:37 +01:00
|
|
|
fmt.Fprintln(w, "Code exchange (could not get token) failed with ", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !token.Valid() {
|
|
|
|
|
fmt.Fprintln(w, "retrieved invalid token")
|
2017-01-08 20:15:09 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dg, err := discordgo.New("Bearer " + token.AccessToken)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("Error while creating discord session: ", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
user, _ := dg.User("@me")
|
2017-01-09 21:34:37 +01:00
|
|
|
|
|
|
|
|
session.Values["discordUserID"] = user.ID
|
|
|
|
|
session.Values["discordUsername"] = user.Username
|
|
|
|
|
session.Values["accessToken"] = token.AccessToken
|
|
|
|
|
session.Save(r, w)
|
|
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
|
2017-01-08 20:15:09 +01:00
|
|
|
}
|