Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Allow options method to bypass authentication #183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func main() {
flagSet.Bool("pass-access-token", false, "pass OAuth access_token to upstream via X-Forwarded-Access-Token header")
flagSet.Bool("pass-host-header", true, "pass the request Host Header to upstream")
flagSet.Var(&skipAuthRegex, "skip-auth-regex", "bypass authentication for requests path's that match (may be given multiple times)")
flagSet.Bool("skip-auth-options", false, "bypass authentication for requests using the https OPTIONS method (useful for CORS requests)")

flagSet.Var(&emailDomains, "email-domain", "authenticate emails with the specified domain (may be given multiple times). Use * to authenticate any email")
flagSet.String("github-org", "", "restrict logins to members of this organisation")
Expand Down
6 changes: 6 additions & 0 deletions oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type OAuthProxy struct {
PassAccessToken bool
CookieCipher *cookie.Cipher
skipAuthRegex []string
skipAuthOptions bool
compiledRegex []*regexp.Regexp
templates *template.Template
}
Expand Down Expand Up @@ -191,6 +192,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
serveMux: serveMux,
redirectURL: redirectURL,
skipAuthRegex: opts.SkipAuthRegex,
skipAuthOptions: opts.SkipAuthOptions,
compiledRegex: opts.CompiledRegex,
PassBasicAuth: opts.PassBasicAuth,
BasicAuthPassword: opts.BasicAuthPassword,
Expand Down Expand Up @@ -406,6 +408,10 @@ func getRemoteAddr(req *http.Request) (s string) {
}

func (p *OAuthProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.Method == "OPTIONS" && p.skipAuthOptions {
p.serveMux.ServeHTTP(rw, req)
return
}
switch path := req.URL.Path; {
case path == p.RobotsPath:
p.RobotsTxt(rw)
Expand Down
28 changes: 25 additions & 3 deletions oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package main
import (
"crypto"
"encoding/base64"
"github.com/18F/hmacauth"
"github.com/bitly/oauth2_proxy/providers"
"github.com/bmizerany/assert"
"io"
"io/ioutil"
"log"
Expand All @@ -17,6 +14,10 @@ import (
"strings"
"testing"
"time"

"github.com/18F/hmacauth"
"github.com/bitly/oauth2_proxy/providers"
"github.com/bmizerany/assert"
)

func init() {
Expand Down Expand Up @@ -91,6 +92,27 @@ func TestRobotsTxt(t *testing.T) {
assert.Equal(t, "User-agent: *\nDisallow: /", rw.Body.String())
}

func TestOptionsMethod(t *testing.T) {
provider_server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("pong"))
}))
opts := NewOptions()
opts.Upstreams = append(opts.Upstreams, provider_server.URL)
opts.ClientID = "bazquux"
opts.ClientSecret = "foobar"
opts.CookieSecret = "xyzzyplugh"
opts.SkipAuthOptions = true
opts.Validate()

proxy := NewOAuthProxy(opts, func(string) bool { return true })
rw := httptest.NewRecorder()
req, _ := http.NewRequest("OPTIONS", "/", nil)
proxy.ServeHTTP(rw, req)
assert.Equal(t, 200, rw.Code)
assert.Equal(t, "pong", rw.Body.String())
}

type TestProvider struct {
*providers.ProviderData
EmailAddress string
Expand Down
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Options struct {

Upstreams []string `flag:"upstream" cfg:"upstreams"`
SkipAuthRegex []string `flag:"skip-auth-regex" cfg:"skip_auth_regex"`
SkipAuthOptions bool `flag:"skip-auth-options" cfg:"skip_auth_options"`
PassBasicAuth bool `flag:"pass-basic-auth" cfg:"pass_basic_auth"`
BasicAuthPassword string `flag:"basic-auth-password" cfg:"basic_auth_password"`
PassAccessToken bool `flag:"pass-access-token" cfg:"pass_access_token"`
Expand Down