Skip to content

Commit

Permalink
add ability to redefine jwt query name
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed May 13, 2019
1 parent c45d538 commit 7c577fe
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Opts struct {
JWTHeaderKey string // default "X-JWT"
XSRFCookieName string // default "XSRF-TOKEN"
XSRFHeaderKey string // default "X-XSRF-TOKEN"
JWTQuery string // default "token"

Issuer string // optional value for iss claim, usually the application name, default "go-pkgz/auth"

Expand Down Expand Up @@ -94,6 +95,7 @@ func NewService(opts Opts) (res *Service) {
JWTHeaderKey: opts.JWTHeaderKey,
XSRFCookieName: opts.XSRFCookieName,
XSRFHeaderKey: opts.XSRFHeaderKey,
JWTQuery: opts.JWTQuery,
Issuer: res.issuer,
AudienceReader: opts.AudienceReader,
})
Expand Down
7 changes: 4 additions & 3 deletions token/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (
defaultTokenDuration = time.Minute * 15
defaultCookieDuration = time.Hour * 24 * 31

tokenQuery = "token"
defaultTokenQuery = "token"
)

// Opts holds constructor params
Expand All @@ -61,7 +61,7 @@ type Opts struct {
JWTHeaderKey string
XSRFCookieName string
XSRFHeaderKey string

JWTQuery string
AudienceReader Audience // allowed aud values
Issuer string // optional value for iss claim, usually application name
}
Expand All @@ -80,6 +80,7 @@ func NewService(opts Opts) *Service {
setDefault(&res.JWTHeaderKey, defaultJWTHeaderKey)
setDefault(&res.XSRFCookieName, defaultXSRFCookieName)
setDefault(&res.XSRFHeaderKey, defaultXSRFHeaderKey)
setDefault(&res.JWTQuery, defaultTokenQuery)
setDefault(&res.Issuer, defaultIssuer)

if opts.TokenDuration == 0 {
Expand Down Expand Up @@ -220,7 +221,7 @@ func (j *Service) Get(r *http.Request) (Claims, string, error) {
tokenString := ""

// try to get from "token" query param
if tkQuery := r.URL.Query().Get(tokenQuery); tkQuery != "" {
if tkQuery := r.URL.Query().Get(j.JWTQuery); tkQuery != "" {
tokenString = tkQuery
}

Expand Down

0 comments on commit 7c577fe

Please sign in to comment.