Need a login check

This commit is contained in:
Matthew Olker committed 2024-05-06 14:02:25 -04:00
1 parent a087234aa8
commit 9bc8cd2b28
7 files changed
+190 -3

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
package api
import (
"github.com/gorilla/sessions"
"github.com/markbates/goth"
"github.com/markbates/goth/gothic"
"github.com/markbates/goth/providers/discord"
)
// TODO: actual randomized key for sessions
const (
key = "randomString"
MaxAge = 86400 * 30 // 30 days
IsProd = false
)
func InitAuth(discordClientId string, discordClientSecret string, discordCallbackURI string) {
store := sessions.NewCookieStore([]byte(key))
store.MaxAge(MaxAge)
store.Options.Path = "/"
store.Options.HttpOnly = true
store.Options.Secure = IsProd
gothic.Store = store
goth.UseProviders(
discord.New(discordClientId, discordClientSecret, discordCallbackURI, discord.ScopeIdentify),
)
}
+6 -1
View File
@@ -54,6 +54,11 @@ func Init(mux *http.ServeMux) {
mux.HandleFunc("GET /daily/seed", handleDailySeed)
mux.HandleFunc("GET /daily/rankings", handleDailyRankings)
mux.HandleFunc("GET /daily/rankingpagecount", handleDailyRankingPageCount)
// auth
mux.HandleFunc("/auth/{provider}/callback", handleProviderCallback)
mux.HandleFunc("/auth/{provider}/link", handleProviderLink)
mux.HandleFunc("/auth/{provider}/logout", handleProviderLogout)
}
func tokenFromRequest(r *http.Request) ([]byte, error) {
@@ -90,4 +95,4 @@ func uuidFromRequest(r *http.Request) ([]byte, error) {
func httpError(w http.ResponseWriter, r *http.Request, err error, code int) {
log.Printf("%s: %s\n", r.URL.Path, err)
http.Error(w, err.Error(), code)
}
}
+57
View File
@@ -20,9 +20,11 @@ package api
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/markbates/goth/gothic"
"github.com/pagefaultgames/rogueserver/api/account"
"github.com/pagefaultgames/rogueserver/api/daily"
"github.com/pagefaultgames/rogueserver/api/savedata"
@@ -36,6 +38,10 @@ import (
Handlers should not return serialized JSON, instead return the struct itself.
*/
var (
user = string("")
)
// account
func handleAccountInfo(w http.ResponseWriter, r *http.Request) {
@@ -383,3 +389,54 @@ func handleDailyRankingPageCount(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(strconv.Itoa(count)))
}
// redirect link after authorizing application link
func handleProviderCallback(w http.ResponseWriter, r *http.Request) {
gothic.GetProviderName = func(r *http.Request) (string, error) { return r.PathValue("provider"), nil }
// called again with code after authorization
code := r.URL.Query().Get("code")
if code != "" {
userId, err := db.FetchDiscordIdByUsername(user)
if err != nil {
}
defer http.Redirect(w, r, "http://localhost:8000", http.StatusSeeOther)
}
gothUser, err := gothic.CompleteUserAuth(w, r)
if err != nil {
log.Println("callback err", w, r)
return
} else {
err := db.AddDiscordAuthByUsername(gothUser.UserID, user)
if err != nil {
log.Println("error adding Discord Auth to database")
return
}
}
log.Println("user", gothUser.UserID)
}
func handleProviderLink(w http.ResponseWriter, r *http.Request) {
gothic.GetProviderName = func(r *http.Request) (string, error) { return r.PathValue("provider"), nil }
username := r.URL.Query().Get("username")
// username recorded prior to authorization
if username != "" {
user = username
}
// try to get the user without re-authenticating
if gothUser, err := gothic.CompleteUserAuth(w, r); err == nil {
log.Print("gothUser:", gothUser.Name)
} else {
gothic.BeginAuthHandler(w, r)
}
}
func handleProviderLogout(w http.ResponseWriter, r *http.Request) {
gothic.GetProviderName = func(r *http.Request) (string, error) { return r.PathValue("provider"), nil }
gothic.Logout(w, r)
w.Header().Set("Location", "/")
w.WriteHeader(http.StatusTemporaryRedirect)
}