Skip to content

Commit

Permalink
oauthproxy: support use-javascript-redirect
Browse files Browse the repository at this point in the history
Support setting the redirect to the requested href based on js.
  • Loading branch information
stephen committed Oct 29, 2018
1 parent 9691907 commit 8427c4a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 51 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {
flagSet.String("scope", "", "OAuth scope specification")
flagSet.String("approval-prompt", "force", "OAuth approval_prompt")
flagSet.String("allowed-url", "", "Regexp for allowed redirect URLs")
flagSet.Bool("use-javascript-redirect", false, "Use javascript on the signin page to set the redirect url")

flagSet.String("signature-key", "", "GAP-Signature request signature key (algorithm:secretkey)")

Expand Down
106 changes: 55 additions & 51 deletions oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,27 @@ type OAuthProxy struct {
OAuthCallbackPath string
AuthOnlyPath string

redirectURL *url.URL // the url to receive requests at
allowedURL string
provider providers.Provider
ProxyPrefix string
SignInMessage string
HtpasswdFile *HtpasswdFile
DisplayHtpasswdForm bool
serveMux http.Handler
SetXAuthRequest bool
PassBasicAuth bool
SkipProviderButton bool
PassUserHeaders bool
BasicAuthPassword string
PassAccessToken bool
CookieCipher *cookie.Cipher
skipAuthRegex []string
skipAuthPreflight bool
compiledRegex []*regexp.Regexp
templates *template.Template
Footer string
redirectURL *url.URL // the url to receive requests at
allowedURL string
UseJavascriptRedirect bool
provider providers.Provider
ProxyPrefix string
SignInMessage string
HtpasswdFile *HtpasswdFile
DisplayHtpasswdForm bool
serveMux http.Handler
SetXAuthRequest bool
PassBasicAuth bool
SkipProviderButton bool
PassUserHeaders bool
BasicAuthPassword string
PassAccessToken bool
CookieCipher *cookie.Cipher
skipAuthRegex []string
skipAuthPreflight bool
compiledRegex []*regexp.Regexp
templates *template.Template
Footer string
}

type UpstreamProxy struct {
Expand Down Expand Up @@ -230,23 +231,24 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
OAuthCallbackPath: fmt.Sprintf("%s/callback", opts.ProxyPrefix),
AuthOnlyPath: fmt.Sprintf("%s/auth", opts.ProxyPrefix),

ProxyPrefix: opts.ProxyPrefix,
provider: opts.provider,
serveMux: serveMux,
redirectURL: redirectURL,
allowedURL: opts.AllowedURL,
skipAuthRegex: opts.SkipAuthRegex,
skipAuthPreflight: opts.SkipAuthPreflight,
compiledRegex: opts.CompiledRegex,
SetXAuthRequest: opts.SetXAuthRequest,
PassBasicAuth: opts.PassBasicAuth,
PassUserHeaders: opts.PassUserHeaders,
BasicAuthPassword: opts.BasicAuthPassword,
PassAccessToken: opts.PassAccessToken,
SkipProviderButton: opts.SkipProviderButton,
CookieCipher: cipher,
templates: loadTemplates(opts.CustomTemplatesDir),
Footer: opts.Footer,
ProxyPrefix: opts.ProxyPrefix,
provider: opts.provider,
serveMux: serveMux,
redirectURL: redirectURL,
allowedURL: opts.AllowedURL,
skipAuthRegex: opts.SkipAuthRegex,
skipAuthPreflight: opts.SkipAuthPreflight,
compiledRegex: opts.CompiledRegex,
SetXAuthRequest: opts.SetXAuthRequest,
PassBasicAuth: opts.PassBasicAuth,
PassUserHeaders: opts.PassUserHeaders,
BasicAuthPassword: opts.BasicAuthPassword,
PassAccessToken: opts.PassAccessToken,
SkipProviderButton: opts.SkipProviderButton,
CookieCipher: cipher,
templates: loadTemplates(opts.CustomTemplatesDir),
Footer: opts.Footer,
UseJavascriptRedirect: opts.UseJavascriptRedirect,
}
}

Expand Down Expand Up @@ -424,21 +426,23 @@ func (p *OAuthProxy) SignInPage(rw http.ResponseWriter, req *http.Request, code
}

t := struct {
ProviderName string
SignInMessage string
CustomLogin bool
Redirect string
Version string
ProxyPrefix string
Footer template.HTML
ProviderName string
SignInMessage string
CustomLogin bool
Redirect string
Version string
ProxyPrefix string
Footer template.HTML
UseJavascriptRedirect bool
}{
ProviderName: p.provider.Data().ProviderName,
SignInMessage: p.SignInMessage,
CustomLogin: p.displayCustomLoginForm(),
Redirect: redirect_url,
Version: VERSION,
ProxyPrefix: p.ProxyPrefix,
Footer: template.HTML(p.Footer),
ProviderName: p.provider.Data().ProviderName,
SignInMessage: p.SignInMessage,
CustomLogin: p.displayCustomLoginForm(),
Redirect: redirect_url,
Version: VERSION,
ProxyPrefix: p.ProxyPrefix,
Footer: template.HTML(p.Footer),
UseJavascriptRedirect: p.UseJavascriptRedirect,
}
p.templates.ExecuteTemplate(rw, "sign_in.html", t)
}
Expand Down
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Options struct {
CustomTemplatesDir string `flag:"custom-templates-dir" cfg:"custom_templates_dir"`
Footer string `flag:"footer" cfg:"footer"`
AllowedURL string `flag:"allowed-url" cfg:"allowed-url"`
UseJavascriptRedirect bool `flag:"use-javascript-redirect" cfg:"use-javascript-redirect"`

CookieName string `flag:"cookie-name" cfg:"cookie_name" env:"OAUTH2_PROXY_COOKIE_NAME"`
CookieSecret string `flag:"cookie-secret" cfg:"cookie_secret" env:"OAUTH2_PROXY_COOKIE_SECRET"`
Expand Down
6 changes: 6 additions & 0 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ func getTemplates() *template.Template {
})();
}
</script>
{{if .UseJavascriptRedirect}}
<script>
var selectors = Array.prototype.slice.call(document.querySelectorAll('input[name="rd"]'));
selectors.forEach((input) => input.value = window.location.href);
</script>
{{end}}
<footer>
{{ if eq .Footer "-" }}
{{ else if eq .Footer ""}}
Expand Down

0 comments on commit 8427c4a

Please sign in to comment.