mirror of
https://github.com/davidallendj/opaal.git
synced 2025-12-20 03:27:02 -07:00
Added flow initialization and completed implementation
This commit is contained in:
parent
a3f0caf4ff
commit
1de4d3a5d5
6 changed files with 262 additions and 41 deletions
|
|
@ -5,10 +5,13 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
http.Server
|
||||
*http.Server
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
}
|
||||
|
|
@ -17,16 +20,16 @@ func NewServerWithConfig(config *Config) *Server {
|
|||
host := config.Server.Host
|
||||
port := config.Server.Port
|
||||
server := &Server{
|
||||
Server: &http.Server{
|
||||
Addr: fmt.Sprintf("%s:%d", host, port),
|
||||
},
|
||||
Host: host,
|
||||
Port: port,
|
||||
}
|
||||
server.Addr = fmt.Sprintf("%s:%d", host, port)
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *Server) SetListenAddr(host string, port int) {
|
||||
s.Host = host
|
||||
s.Port = port
|
||||
s.Addr = s.GetListenAddr()
|
||||
}
|
||||
|
||||
|
|
@ -36,34 +39,69 @@ func (s *Server) GetListenAddr() string {
|
|||
|
||||
func (s *Server) WaitForAuthorizationCode(loginUrl string) (string, error) {
|
||||
var code string
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.RedirectSlashes)
|
||||
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
})
|
||||
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
// show login page with notice to redirect
|
||||
loginPage, err := os.ReadFile("pages/index.html")
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load login page: %v\n", err)
|
||||
}
|
||||
loginPage = []byte(strings.ReplaceAll(string(loginPage), "{{loginUrl}}", loginUrl))
|
||||
w.WriteHeader(http.StatusSeeOther)
|
||||
w.Write(loginPage)
|
||||
})
|
||||
http.HandleFunc("/oidc/callback", func(w http.ResponseWriter, r *http.Request) {
|
||||
r.HandleFunc("/oidc/callback", func(w http.ResponseWriter, r *http.Request) {
|
||||
// get the code from the OIDC provider
|
||||
if r != nil {
|
||||
code = r.URL.Query().Get("code")
|
||||
fmt.Printf("Authorization code: %v\n", code)
|
||||
}
|
||||
http.Redirect(w, r, s.Addr+"/success", http.StatusSeeOther)
|
||||
s.Close()
|
||||
http.Redirect(w, r, "/redirect", http.StatusSeeOther)
|
||||
})
|
||||
r.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
|
||||
err := s.Close()
|
||||
if err != nil {
|
||||
fmt.Printf("failed to close server: %v\n", err)
|
||||
}
|
||||
})
|
||||
s.Handler = r
|
||||
|
||||
return code, s.ListenAndServe()
|
||||
}
|
||||
|
||||
func (s *Server) ShowSuccessPage() error {
|
||||
http.HandleFunc("/success", func(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) Serve(data chan []byte) error {
|
||||
output, ok := <-data
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to receive data")
|
||||
}
|
||||
|
||||
fmt.Printf("Received data: %v\n", string(output))
|
||||
// http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// })
|
||||
r := chi.NewRouter()
|
||||
r.HandleFunc("/success", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("Serving success page.")
|
||||
successPage, err := os.ReadFile("pages/success.html")
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load success page: %v\n", err)
|
||||
}
|
||||
successPage = []byte(strings.ReplaceAll(string(successPage), "{{access_token}}", string(output)))
|
||||
w.Write(successPage)
|
||||
})
|
||||
r.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("Serving error page.")
|
||||
errorPage, err := os.ReadFile("pages/success.html")
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load success page: %v\n", err)
|
||||
}
|
||||
// errorPage = []byte(strings.ReplaceAll(string(errorPage), "{{access_token}}", output))
|
||||
w.Write(errorPage)
|
||||
})
|
||||
|
||||
s.Handler = r
|
||||
return s.ListenAndServe()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue