package main import ( "context" "fmt" "log" "net/http" "os" "os/signal" "syscall" "time" _ "github.com/breml/rootcerts" "github.com/davecgh/go-spew/spew" "github.com/gorilla/mux" "golang.org/x/oauth2" "golang.org/x/oauth2/microsoft" ) var config = oauth2.Config{ ClientID: "dccb4b93-3f75-4775-a94a-da39216d7daf", ClientSecret: "tiL8Q~qahoaZUck4ZG4sc5w.V_I.1c60bwkW6aYJ", Endpoint: microsoft.AzureADEndpoint("ceeae22e-f163-4ac9-b7c2-45972d3aed4f"), RedirectURL: "https://alias.spamasaurus.com/callback", Scopes: []string{"User.Read"}, } func rootHandler(w http.ResponseWriter, r *http.Request) { 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 code := r.URL.Query().Get("code") token, err := config.Exchange(r.Context(), code) if err != nil { w.Write([]byte(spew.Sdump(err))) http.Error(w, "Error exchanging code for token", http.StatusInternalServerError) return } // Use the token to make MS Graph queries // Example: Fetch user profile information // ... w.Write([]byte(spew.Sdump(token))) fmt.Fprintln(w, "Authentication successful!") } 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 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 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) }