First implementaion of a web server

First implementation of a web server with oauth2 functionality. Nothing fancy, it just returns the username.
Three more arguments: -p for a port the web server shall run on and -ci and -cs for oauth2 ClientID and ClientSecret.
This commit is contained in:
da
2017-01-08 20:15:09 +01:00
parent b04eaf6978
commit 90e628b517
2 changed files with 89 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
package main
import (
"fmt"
"net/http"
"github.com/bwmarrin/discordgo"
"golang.org/x/oauth2"
)
const htmlIndex = `<html><body>
<a href="/discordLogin">Log in with discord</a>
</body></html>
`
var (
discordOauthConfig = &oauth2.Config{
RedirectURL: "",
ClientID: "",
ClientSecret: "",
Scopes: []string{"connections",
"email",
"identify",
"guilds"},
Endpoint: *endp,
}
// Some random string, random for each request
oauthStateString = "random"
endp = &oauth2.Endpoint{
AuthURL: "https://discordapp.com/api/oauth2/authorize",
TokenURL: "https://discordapp.com/api/oauth2/token",
}
)
// startWebServer with the port provided
func startWebServer(port string, ci string, cs string) {
discordOauthConfig.ClientID = ci
discordOauthConfig.ClientSecret = cs
http.HandleFunc("/", handleMain)
http.HandleFunc("/discordLogin", handlediscordLogin)
http.HandleFunc("/discordCallback", handlediscordCallback)
fmt.Println(http.ListenAndServe(":"+port, nil))
}
func handleMain(w http.ResponseWriter, r *http.Request) {
discordOauthConfig.RedirectURL = "http://" + r.Host + "/discordCallback"
fmt.Fprintf(w, htmlIndex)
}
func handlediscordLogin(w http.ResponseWriter, r *http.Request) {
url := discordOauthConfig.AuthCodeURL(oauthStateString)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func handlediscordCallback(w http.ResponseWriter, r *http.Request) {
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
}
code := r.FormValue("code")
token, err := discordOauthConfig.Exchange(oauth2.NoContext, code)
if err != nil {
fmt.Println("Code exchange failed with ", err)
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
return
}
dg, err := discordgo.New("Bearer " + token.AccessToken)
if err != nil {
fmt.Println("Error while creating discord session: ", err)
return
}
user, _ := dg.User("@me")
fmt.Fprintf(w, user.Username)
}