|  |  |  | @@ -2,6 +2,7 @@ package main | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  | import ( | 
		
	
		
			
				|  |  |  |  | 	"context" | 
		
	
		
			
				|  |  |  |  | 	"encoding/gob" | 
		
	
		
			
				|  |  |  |  | 	"log" | 
		
	
		
			
				|  |  |  |  | 	"net/http" | 
		
	
		
			
				|  |  |  |  | 	"os" | 
		
	
	
		
			
				
					
					|  |  |  | @@ -29,52 +30,9 @@ var config = oauth2.Config{ | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  | var sessionStore = sessions.NewCookieStore([]byte("xDDBjhYwyndZty3exGNq2ahE8wHRCR4DfdCJCSoWXAYncfWw2UQDH63QcJ9CkrGx")) | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  | func rootHandler(w http.ResponseWriter, r *http.Request) { | 
		
	
		
			
				|  |  |  |  | 	session, _ := sessionStore.Get(r, "spamasaurusRex") | 
		
	
		
			
				|  |  |  |  | 	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 | 
		
	
		
			
				|  |  |  |  | 	session.Save(r, w) | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  | 	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 init() { | 
		
	
		
			
				|  |  |  |  | 	// Register the oauth2.Token type with gob | 
		
	
		
			
				|  |  |  |  | 	gob.Register(&oauth2.Token{}) | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  | func main() { | 
		
	
	
		
			
				
					
					|  |  |  | @@ -104,6 +62,64 @@ func main() { | 
		
	
		
			
				|  |  |  |  | 	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) | 
		
	
	
		
			
				
					
					|  |  |  |   |