138 lines
3.3 KiB
Go
138 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/gob"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
_ "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"
|
|
)
|
|
|
|
var config = oauth2.Config{
|
|
ClientID: "dccb4b93-3f75-4775-a94a-da39216d7daf",
|
|
ClientSecret: "XN98Q~Wrp1RfakkihA1BaTKfokOSX9fuB01unanr",
|
|
Endpoint: microsoft.AzureADEndpoint("ceeae22e-f163-4ac9-b7c2-45972d3aed4f"),
|
|
RedirectURL: "https://alias.spamasaurus.com/callback",
|
|
Scopes: []string{"email", "openid", "profile", "user.read"},
|
|
}
|
|
|
|
var sessionStore = sessions.NewFilesystemStore("", []byte("xDDBjhYwyndZty3exGNq2ahE8wHRCR4DfdCJCSoWXAYncfWw2UQDH63QcJ9CkrGx"))
|
|
|
|
func init() {
|
|
// Register the oauth2.Token type with gob
|
|
gob.Register(&oauth2.Token{})
|
|
}
|
|
|
|
func main() {
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/", rootHandler)
|
|
r.HandleFunc("/health", healthHandler)
|
|
r.HandleFunc("/callback", callbackHandler)
|
|
r.HandleFunc("/readiness", readinessHandler)
|
|
|
|
srv := &http.Server{
|
|
Handler: r,
|
|
Addr: ":8080",
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 10 * time.Second,
|
|
}
|
|
|
|
// Start Server
|
|
go func() {
|
|
log.Println("Starting Server")
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}()
|
|
|
|
// Graceful Shutdown
|
|
waitForShutdown(srv)
|
|
}
|
|
|
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
|
session, err := sessionStore.Get(r, "spamasaurusRex")
|
|
if err != nil {
|
|
log.Println(spew.Sdump(err))
|
|
return
|
|
}
|
|
|
|
if token, ok := session.Values["token"]; ok {
|
|
log.Println(spew.Sdump(token))
|
|
w.Write([]byte("Token retrieved from session"))
|
|
} else {
|
|
log.Println(spew.Sdump(session))
|
|
url := config.AuthCodeURL("state", oauth2.AccessTypeOffline)
|
|
http.Redirect(w, r, url, http.StatusFound)
|
|
}
|
|
}
|
|
|
|
func callbackHandler(w http.ResponseWriter, r *http.Request) {
|
|
// Handle the callback after successful authentication
|
|
token, err := config.Exchange(r.Context(), r.URL.Query().Get("code"))
|
|
if err != nil {
|
|
if retrieveErr, ok := err.(*oauth2.RetrieveError); ok {
|
|
log.Println(retrieveErr.ErrorDescription + " (" + retrieveErr.ErrorCode + ")")
|
|
}
|
|
http.Error(w, "Error exchanging code for token", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Store the token in the session
|
|
session, err := sessionStore.Get(r, "spamasaurusRex")
|
|
if err != nil {
|
|
log.Println(spew.Sdump(err))
|
|
return
|
|
}
|
|
session.Values["token"] = token
|
|
|
|
err = session.Save(r, w)
|
|
if err != nil {
|
|
log.Println(spew.Sdump(err))
|
|
return
|
|
}
|
|
|
|
log.Println(spew.Sdump(session))
|
|
|
|
// w.Write([]byte("Authentication successful!"))
|
|
|
|
url := "https://alias.spamasaurus.com"
|
|
http.Redirect(w, r, url, http.StatusSeeOther)
|
|
}
|
|
|
|
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func readinessHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func waitForShutdown(srv *http.Server) {
|
|
interruptChan := make(chan os.Signal, 1)
|
|
signal.Notify(interruptChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
// Block until we receive our signal.
|
|
<-interruptChan
|
|
|
|
// create a deadline to wait for.
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
defer cancel()
|
|
srv.Shutdown(ctx)
|
|
|
|
log.Println("Shutting down")
|
|
os.Exit(0)
|
|
}
|