fix: Check for existing session w/ token

This commit is contained in:
2024-03-10 17:34:20 +11:00
parent 6d5b17515a
commit ef817dc650
3 changed files with 24 additions and 6 deletions

View File

@ -12,7 +12,9 @@ import (
_ "github.com/breml/rootcerts"
"github.com/davecgh/go-spew/spew"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"golang.org/x/oauth2"
"golang.org/x/oauth2/microsoft"
@ -26,9 +28,16 @@ var config = oauth2.Config{
Scopes: []string{"User.Read", "Profile"},
}
var sessionStore = sessions.NewCookieStore([]byte("xDDBjhYwyndZty3exGNq2ahE8wHRCR4DfdCJCSoWXAYncfWw2UQDH63QcJ9CkrGx"))
func rootHandler(w http.ResponseWriter, r *http.Request) {
url := config.AuthCodeURL("state", oauth2.AccessTypeOffline)
http.Redirect(w, r, url, http.StatusFound)
session, _ := sessionStore.Get(r, "spamasaurusRex")
if token, ok := session.Values["token"]; ok {
w.Write([]byte(spew.Sdump(token)))
} else {
url := config.AuthCodeURL("state", oauth2.AccessTypeOffline)
http.Redirect(w, r, url, http.StatusFound)
}
}
func callbackHandler(w http.ResponseWriter, r *http.Request) {
@ -42,12 +51,15 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Use the token to make MS Graph queries
// Example: Fetch user profile information
// ...
w.Write([]byte(spew.Sdump(token)))
// Store the token in the session
session, _ := sessionStore.Get(r, "spamasaurusRex")
session.Values["token"] = token
session.Save(r, w)
fmt.Fprintln(w, "Authentication successful!")
url := "https://alias.spamasaurus.com"
http.Redirect(w, r, url, http.StatusAccepted)
}
func healthHandler(w http.ResponseWriter, r *http.Request) {