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:
@@ -730,10 +730,21 @@ func main() {
|
|||||||
Shard = flag.String("s", "", "Shard ID")
|
Shard = flag.String("s", "", "Shard ID")
|
||||||
ShardCount = flag.String("c", "", "Number of shards")
|
ShardCount = flag.String("c", "", "Number of shards")
|
||||||
Owner = flag.String("o", "", "Owner ID")
|
Owner = flag.String("o", "", "Owner ID")
|
||||||
|
Port = flag.Int("p", 0, "Web server port")
|
||||||
|
Ci = flag.Int("ci", 0, "ClientID")
|
||||||
|
Cs = flag.String("cs", "", "ClientSecret")
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// Start Webserver if a valid port is provided and if ClientID and ClientSecret are set
|
||||||
|
if *Port != 0 && *Port >= 1 && *Ci != 0 && *Cs != "" {
|
||||||
|
log.Infoln("Starting web server on port " + strconv.Itoa(*Port))
|
||||||
|
go startWebServer(strconv.Itoa(*Port), strconv.Itoa(*Ci), *Cs)
|
||||||
|
} else {
|
||||||
|
log.Infoln("Required web server arguments missing or invalid. Skipping web server start.")
|
||||||
|
}
|
||||||
|
|
||||||
if *Owner != "" {
|
if *Owner != "" {
|
||||||
OWNER = *Owner
|
OWNER = *Owner
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user