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

add support for returning authenticated user - direct comments to #173 #298

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Godeps
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ github.com/bitly/go-simplejson 3378bdcb5cebedcbf8b5750edee28010f128fe2
github.com/mreiferson/go-options 33795234b6f327f1be2d78a541893012362a4e06
github.com/bmizerany/assert e17e99893cb6509f428e1728281c2ad60a6b31e3
gopkg.in/fsnotify.v1 v1.2.0
golang.org/x/oauth2 397fe7649477ff2e8ced8fc0b2696f781e53745a
golang.org/x/oauth2/google 397fe7649477ff2e8ced8fc0b2696f781e53745a
golang.org/x/oauth2 04e1573abc896e70388bd387a69753c378d46466
golang.org/x/oauth2/google 04e1573abc896e70388bd387a69753c378d46466
google.golang.org/api/admin/directory/v1 a5c3e2a4792aff40e59840d9ecdff0542a202a80
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func main() {
flagSet.String("tls-key", "", "path to private key file")
flagSet.String("redirect-url", "", "the OAuth Redirect URL. ie: \"https://internalapp.yourcompany.com/oauth2/callback\"")
flagSet.Var(&upstreams, "upstream", "the http url(s) of the upstream endpoint or file:// paths for static files. Routing is based on the path")
flagSet.Bool("return-authenticated-email", false, "return X-Authorized-Email header (useful when used as authenticator only)")
flagSet.Bool("pass-basic-auth", true, "pass HTTP Basic Auth, X-Forwarded-User and X-Forwarded-Email information to upstream")
flagSet.String("basic-auth-password", "", "the password to set when passing the HTTP Basic Auth header")
flagSet.Bool("pass-access-token", false, "pass OAuth access_token to upstream via X-Forwarded-Access-Token header")
Expand Down
5 changes: 5 additions & 0 deletions oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type OAuthProxy struct {
serveMux http.Handler
PassBasicAuth bool
SkipProviderButton bool
ReturnAuthenticatedEmail bool
BasicAuthPassword string
PassAccessToken bool
CookieCipher *cookie.Cipher
Expand Down Expand Up @@ -194,6 +195,7 @@ func NewOAuthProxy(opts *Options, validator func(string) bool) *OAuthProxy {
skipAuthRegex: opts.SkipAuthRegex,
compiledRegex: opts.CompiledRegex,
PassBasicAuth: opts.PassBasicAuth,
ReturnAuthenticatedEmail: opts.ReturnAuthenticatedEmail,
BasicAuthPassword: opts.BasicAuthPassword,
PassAccessToken: opts.PassAccessToken,
SkipProviderButton: opts.SkipProviderButton,
Expand Down Expand Up @@ -602,6 +604,9 @@ func (p *OAuthProxy) Authenticate(rw http.ResponseWriter, req *http.Request) int
req.Header["X-Forwarded-Email"] = []string{session.Email}
}
}
if p.ReturnAuthenticatedEmail {
rw.Header().Set("X-Authenticated-Email", session.Email)
}
if p.PassAccessToken && session.AccessToken != "" {
req.Header["X-Forwarded-Access-Token"] = []string{session.AccessToken}
}
Expand Down
29 changes: 29 additions & 0 deletions oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,35 @@ func TestAuthOnlyEndpointUnauthorizedOnEmailValidationFailure(t *testing.T) {
assert.Equal(t, "unauthorized request\n", string(bodyBytes))
}

func TestAuthOnlyEndpointReturnAuthenticatedEmail(t *testing.T) {
var pc_test ProcessCookieTest

pc_test.opts = NewOptions()
pc_test.opts.ReturnAuthenticatedEmail = true
pc_test.opts.Validate()

pc_test.proxy = NewOAuthProxy(pc_test.opts, func(email string) bool {
return pc_test.validate_user
})
pc_test.proxy.provider = &TestProvider{
ValidToken: true,
}

pc_test.validate_user = true

pc_test.rw = httptest.NewRecorder()
pc_test.req, _ = http.NewRequest("GET",
pc_test.opts.ProxyPrefix+"/auth", nil)

startSession := &providers.SessionState{
Email: "[email protected]", AccessToken: "my_access_token"}
pc_test.SaveSession(startSession, time.Now())

pc_test.proxy.ServeHTTP(pc_test.rw, pc_test.req)
assert.Equal(t, http.StatusAccepted, pc_test.rw.Code)
assert.Equal(t, "[email protected]", pc_test.rw.HeaderMap["X-Authenticated-Email"][0])
}

type SignatureAuthenticator struct {
auth hmacauth.HmacAuth
}
Expand Down
2 changes: 2 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Options struct {

Upstreams []string `flag:"upstream" cfg:"upstreams"`
SkipAuthRegex []string `flag:"skip-auth-regex" cfg:"skip_auth_regex"`
ReturnAuthenticatedEmail bool `flag:"return-authenticated-email" cfg:"return_authenticated_email"`
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 Expand Up @@ -94,6 +95,7 @@ func NewOptions() *Options {
CookieHttpOnly: true,
CookieExpire: time.Duration(168) * time.Hour,
CookieRefresh: time.Duration(0),
ReturnAuthenticatedEmail: false,
PassBasicAuth: true,
PassAccessToken: false,
PassHostHeader: true,
Expand Down