mirror of
https://github.com/davidallendj/opaal.git
synced 2025-12-20 03:27:02 -07:00
Removed unused code, wrapped verbose prints, and added error page template
This commit is contained in:
parent
35cf37836d
commit
56ea3290a6
1 changed files with 23 additions and 26 deletions
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/davidallendj/go-utils/httpx"
|
"github.com/davidallendj/go-utils/httpx"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
@ -71,7 +70,9 @@ func (s *Server) Login(buttons string, provider *oidc.IdentityProvider, client *
|
||||||
// get the code from the OIDC provider
|
// get the code from the OIDC provider
|
||||||
if r != nil {
|
if r != nil {
|
||||||
code = r.URL.Query().Get("code")
|
code = r.URL.Query().Get("code")
|
||||||
fmt.Printf("Authorization code: %v\n", code)
|
if params.Verbose {
|
||||||
|
fmt.Printf("Authorization code: %v\n", code)
|
||||||
|
}
|
||||||
|
|
||||||
// use code from response and exchange for bearer token (with ID token)
|
// use code from response and exchange for bearer token (with ID token)
|
||||||
bearerToken, err := client.FetchTokenFromAuthenticationServer(
|
bearerToken, err := client.FetchTokenFromAuthenticationServer(
|
||||||
|
|
@ -120,7 +121,9 @@ func (s *Server) Login(buttons string, provider *oidc.IdentityProvider, client *
|
||||||
http.Redirect(w, r, "/success", http.StatusSeeOther)
|
http.Redirect(w, r, "/success", http.StatusSeeOther)
|
||||||
})
|
})
|
||||||
r.HandleFunc("/success", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/success", func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Printf("Serving success page.\n")
|
if params.Verbose {
|
||||||
|
fmt.Printf("Serving success page.\n")
|
||||||
|
}
|
||||||
template, err := gonja.FromFile("pages/success.html")
|
template, err := gonja.FromFile("pages/success.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
@ -135,7 +138,9 @@ func (s *Server) Login(buttons string, provider *oidc.IdentityProvider, client *
|
||||||
}
|
}
|
||||||
// try and send access code to target if set
|
// try and send access code to target if set
|
||||||
if target != "" {
|
if target != "" {
|
||||||
fmt.Printf("Send access token to target: %s\n", target)
|
if params.Verbose {
|
||||||
|
fmt.Printf("Sending access token to target: %s\n", target)
|
||||||
|
}
|
||||||
_, _, err := httpx.MakeHttpRequest(target, http.MethodPost, nil, httpx.Headers{"access_token": accessToken})
|
_, _, err := httpx.MakeHttpRequest(target, http.MethodPost, nil, httpx.Headers{"access_token": accessToken})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to make request: %v", err)
|
fmt.Printf("failed to make request: %v", err)
|
||||||
|
|
@ -143,30 +148,22 @@ func (s *Server) Login(buttons string, provider *oidc.IdentityProvider, client *
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
r.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
|
r.HandleFunc("/error", func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Printf("Serving error page.")
|
if params.Verbose {
|
||||||
errorPage, err := os.ReadFile("pages/error.html")
|
fmt.Printf("Serving error page.")
|
||||||
if err != nil {
|
}
|
||||||
fmt.Printf("failed to load error page: %v\n", err)
|
template, err := gonja.FromFile("pages/success.html")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
data := exec.NewContext(map[string]interface{}{
|
||||||
|
"index": fmt.Sprintf("<a href=\"%s\">try logging in again?</a>", s.Addr),
|
||||||
|
})
|
||||||
|
if err = template.Execute(w, data); err != nil { // Prints: Hello Bob!
|
||||||
|
panic(err)
|
||||||
}
|
}
|
||||||
w.Write(errorPage)
|
|
||||||
})
|
})
|
||||||
s.Handler = r
|
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("./pages/static"))))
|
||||||
|
|
||||||
return s.ListenAndServe()
|
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
s.Handler = r
|
s.Handler = r
|
||||||
return s.ListenAndServe()
|
return s.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue