2024-02-19 00:35:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2024-03-01 06:15:06 +00:00
|
|
|
"pkg/spamasaurusrex/pkg/graphhelper"
|
2024-02-19 00:35:04 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2024-03-08 01:31:17 +00:00
|
|
|
_ "github.com/breml/rootcerts"
|
2024-02-19 00:35:04 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
2024-03-01 06:15:06 +00:00
|
|
|
graphHelper := graphhelper.NewGraphHelper()
|
|
|
|
initializeGraph(graphHelper)
|
|
|
|
|
2024-03-08 00:50:00 +00:00
|
|
|
user, err := graphHelper.GetUser()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Error getting user: %v\n", err)
|
2024-02-19 00:35:04 +00:00
|
|
|
}
|
2024-03-08 00:50:00 +00:00
|
|
|
|
|
|
|
fmt.Printf("Hello, %s!\n", *user.GetDisplayName())
|
|
|
|
w.Write([]byte(fmt.Sprintf("Hello, %s\n", *user.GetDisplayName())))
|
|
|
|
|
|
|
|
// query := r.URL.Query()
|
|
|
|
// name := query.Get("name")
|
|
|
|
// w.Write([]byte(fmt.Sprintf("Hello, %s\n", name)))
|
2024-02-19 00:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-03-08 00:50:00 +00:00
|
|
|
func loginHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clientId := "dccb4b93-3f75-4775-a94a-da39216d7daf"
|
|
|
|
tenantId := "ceeae22e-f163-4ac9-b7c2-45972d3aed4f"
|
|
|
|
redirectURI := "https://alias.spamasaurus.com/"
|
|
|
|
|
|
|
|
http.Redirect(w, r,
|
|
|
|
"https://login.microsoftonline.com/"+tenantId+"/oauth2/v2.0/authorize?client_id="+clientId+"&response_type=code&redirect_uri="+redirectURI+"&scope=openid profile offline_access", http.StatusMovedPermanently)
|
|
|
|
}
|
|
|
|
|
2024-02-19 00:35:04 +00:00
|
|
|
func readinessHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
r.HandleFunc("/", handler)
|
|
|
|
r.HandleFunc("/health", healthHandler)
|
2024-03-08 00:50:00 +00:00
|
|
|
r.HandleFunc("/login", loginHandler)
|
2024-02-19 00:35:04 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-03-01 06:15:06 +00:00
|
|
|
|
|
|
|
func initializeGraph(graphHelper *graphhelper.GraphHelper) {
|
|
|
|
err := graphHelper.InitializeGraphForUserAuth()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Error initializing Graph for user auth: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func displayAccessToken(graphHelper *graphhelper.GraphHelper) {
|
|
|
|
token, err := graphHelper.GetUserToken()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Error getting user token: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("User token: %s", *token)
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeGraphCall(graphHelper *graphhelper.GraphHelper) {
|
|
|
|
// TODO
|
|
|
|
}
|