Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial attempt to enable JWT auth to look at a configurable header i… #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion oauthproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func buildSessionChain(opts *options.Options, provider providers.Provider, sessi
middlewareapi.CreateTokenToSessionFunc(verifier.Verify))
}

chain = chain.Append(middleware.NewJwtSessionLoader(sessionLoaders))
chain = chain.Append(middleware.NewJwtSessionLoader(sessionLoaders, opts.JWTAuthHeader))
}

if validator != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Options struct {
TrustedIPs []string `flag:"trusted-ip" cfg:"trusted_ips"`
ForceHTTPS bool `flag:"force-https" cfg:"force_https"`
RawRedirectURL string `flag:"redirect-url" cfg:"redirect_url"`
JWTAuthHeader string `flag:"jwt-auth-header" cfg:"jwt_auth_header"`

AuthenticatedEmailsFile string `flag:"authenticated-emails-file" cfg:"authenticated_emails_file"`
EmailDomains []string `flag:"email-domain" cfg:"email_domains"`
Expand Down
10 changes: 8 additions & 2 deletions pkg/middleware/jwt_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (

const jwtRegexFormat = `^ey[IJ][a-zA-Z0-9_-]*\.ey[IJ][a-zA-Z0-9_-]*\.[a-zA-Z0-9_-]+$`

func NewJwtSessionLoader(sessionLoaders []middlewareapi.TokenToSessionFunc) alice.Constructor {
func NewJwtSessionLoader(sessionLoaders []middlewareapi.TokenToSessionFunc, jwtAuthHeader string) alice.Constructor {
js := &jwtSessionLoader{
jwtRegex: regexp.MustCompile(jwtRegexFormat),
jwtAuthHeader: jwtAuthHeader,
sessionLoaders: sessionLoaders,
}
return js.loadSession
Expand All @@ -27,6 +28,7 @@ func NewJwtSessionLoader(sessionLoaders []middlewareapi.TokenToSessionFunc) alic
// Authorization headers.
type jwtSessionLoader struct {
jwtRegex *regexp.Regexp
jwtAuthHeader string
sessionLoaders []middlewareapi.TokenToSessionFunc
}

Expand Down Expand Up @@ -60,7 +62,11 @@ func (j *jwtSessionLoader) loadSession(next http.Handler) http.Handler {
// getJwtSession loads a session based on a JWT token in the authorization header.
// (see the config options skip-jwt-bearer-tokens and extra-jwt-issuers)
func (j *jwtSessionLoader) getJwtSession(req *http.Request) (*sessionsapi.SessionState, error) {
auth := req.Header.Get("Authorization")
authHeader := "Authorization"
if j.jwtAuthHeader == "" {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we ever use the header from the config in this code (:warning: I have no Go knowledge whatsoever…)

I would assume something like this instead.

	authHeader := "Authorization"
	if j.jwtAuthHeader != nil && len(strings.TrimSpace(j.jwtAuthHeader)) > 0 {
		authHeader = j.jwtAuthHeader
	}

authHeader = jwtAuthHeader
}
auth := req.Header.Get(authHeader)
if auth == "" {
// No auth header provided, so don't attempt to load a session
return nil, nil
Expand Down