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

Added Changes for Cookie authentication as per issue #15 #27

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
2 changes: 2 additions & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const permit = new Bearer({
basic: String,
header: String,
query: String,
cookie: String,
})
```

Expand All @@ -74,6 +75,7 @@ The `Bearer` permit checks for credentials in the form of a secret bearer token
* `basic` — Either `'username'` or `'password'` denoting which field of the HTTP Basic Auth to use as a fallback.
* `header` — A custom header key to check as a fallback.
* `query` — A query parameter key to check as a fallback.
* `cookie` — A cookie parameter key to check as a fallback.

### `Permit`

Expand Down
15 changes: 13 additions & 2 deletions src/bearer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import Permit from './permit'

class Bearer extends Permit {
constructor(options = {}) {
const { basic, header, query, ...rest } = options
const { basic, header, query, cookie, ...rest } = options
const scheme = basic ? ['Bearer', 'Basic'] : 'Bearer'
super({ scheme, ...rest })
this.basic = basic
this.header = header
this.query = query
this.cookie = cookie
}

check(req) {
const { basic, header, query, proxy } = this
const { basic, header, query, proxy, cookie } = this
const auth = req.headers
? proxy ? req.headers['proxy-authorization'] : req.headers.authorization
: null
Expand Down Expand Up @@ -53,6 +54,16 @@ class Bearer extends Permit {
return parsed.query[query]
}
}

if (cookie) {
const cookies = req.headers ? req.headers.cookie : null
if (cookies) {
const cookieObj = this.parseCookies(cookies)
if (cookieObj.hasOwnProperty(cookie)) {
return cookieObj[cookie]
}
}
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/permit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class Permit {
res.setHeader(key, value)
})
}

parseCookies(cookieString) {
const rx = /([^;=\s]*)=([^;]*)/g
const obj = {}
for (let m; (m = rx.exec(cookieString)); )
obj[m[1]] = decodeURIComponent(m[2])
return obj
}
}

export default Permit
12 changes: 12 additions & 0 deletions test/fixtures/bearer/cookie-none.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Bearer } from '../../..'

export const permit = new Bearer({
cookie: 'token',
})

export const request = {
method: 'GET',
url: '/',
}

export const credentials = undefined
15 changes: 15 additions & 0 deletions test/fixtures/bearer/cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Bearer } from '../../..'

export const permit = new Bearer({
cookie: 'token',
})

export const request = {
method: 'GET',
url: '/',
headers: {
cookie: 'token=token',
},
}

export const credentials = 'token'