Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions handlers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,18 @@ func AuthStateHandler(w http.ResponseWriter, r *http.Request) {
return
}

// get the originally requested URL so we can send them on their way
requestedURL := session.Values["requestedURL"].(string)

var responsesOptions []responses.Option
if requestedURL != "" {
responsesOptions = append(responsesOptions, responses.WithPrevURLOption(requestedURL))
}

// is the nonce "state" valid?
queryState := r.URL.Query().Get("state")
if session.Values["state"] != queryState {
responses.Error400(w, r, fmt.Errorf("/auth Invalid session state: stored %s, returned %s", session.Values["state"], queryState))
responses.Error400(w, r, fmt.Errorf("/auth Invalid session state: stored %s, returned %s", session.Values["state"], queryState), responsesOptions...)
return
}

Expand All @@ -85,7 +93,7 @@ func AuthStateHandler(w http.ResponseWriter, r *http.Request) {
}

if err := getUserInfo(r, &user, &customClaims, &ptokens, authCodeOptions...); err != nil {
responses.Error400(w, r, fmt.Errorf("/auth Error while retrieving user info after successful login at the OAuth provider: %w", err))
responses.Error400(w, r, fmt.Errorf("/auth Error while retrieving user info after successful login at the OAuth provider: %w", err), responsesOptions...)
return
}
log.Debugf("/auth/{state}/ Claims from userinfo: %+v", customClaims)
Expand All @@ -98,9 +106,6 @@ func AuthStateHandler(w http.ResponseWriter, r *http.Request) {

// SUCCESS!! they are authorized

// get the originally requested URL so we can send them on their way
requestedURL := session.Values["requestedURL"].(string)

// issue the jwt
var tokenstring string
if requestedURL == "" {
Expand Down
11 changes: 11 additions & 0 deletions pkg/responses/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package responses

// Option is an option type for responses package
type Option func(idx *Index)

// WithPrevURLOption sets previous URL to be used in response template
func WithPrevURLOption(prevURL string) Option {
return func(idx *Index) {
idx.PrevURL = prevURL
}
}
15 changes: 11 additions & 4 deletions pkg/responses/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Index struct {
TestURLs []string
Testing bool
DocumentRoot string
PrevURL string
}

var (
Expand Down Expand Up @@ -56,12 +57,18 @@ func RenderIndex(w http.ResponseWriter, msg string) {

// renderError html error page
// something terse for the end user
func renderError(w http.ResponseWriter, msg string, status int) {
func renderError(w http.ResponseWriter, msg string, status int, opts ...Option) {
log.Debugf("rendering error for user: %s", msg)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(status)
if err := indexTemplate.Execute(w, &Index{Msg: msg, DocumentRoot: cfg.Cfg.DocumentRoot}); err != nil {

idx := &Index{Msg: msg, DocumentRoot: cfg.Cfg.DocumentRoot}
for _, o := range opts {
o(idx)
}

if err := indexTemplate.Execute(w, idx); err != nil {
log.Error(err)
}
}
Expand All @@ -85,9 +92,9 @@ func Redirect302(w http.ResponseWriter, r *http.Request, rURL string) {
}

// Error400 Bad Request
func Error400(w http.ResponseWriter, r *http.Request, e error) {
func Error400(w http.ResponseWriter, r *http.Request, e error, opts ...Option) {
cancelClearSetError(w, r, e)
renderError(w, "400 Bad Request", http.StatusBadRequest)
renderError(w, "400 Bad Request", http.StatusBadRequest, opts...)
}

// Error401 Unauthorized, the standard error returned when failing /validate
Expand Down
5 changes: 5 additions & 0 deletions templates/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ All 302 redirects will be captured and presented as links here
{{ end }}
</ul>
{{ end }}
{{ if .PrevURL }}
<div>
<p>Try revisiting this <a href="{{ .PrevURL}}">previous URL</a></p>
</div>
{{ end }}
<div class="bottom">
For support, please contact your network administrator or whomever configured Nginx to use Vouch Proxy.
<p/>
Expand Down