8000 Add support for authentication schemes · core-api/javascript-client@de84917 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit de84917

Browse files
committed
Add support for authentication schemes
1 parent 86e42c8 commit de84917

File tree

9 files changed

+119
-12
lines changed

9 files changed

+119
-12
lines changed

lib/auth/basic.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class BasicAuthentication {
2+
constructor (options = {}) {
3+
const username = options.username
4+
const password = options.password
5+
const hash = window.btoa(username + ':' + password)
6+
this.auth = 'Basic ' + hash
7+
}
8+
9+
authenticate (options) {
10+
options.headers['Authorization'] = this.auth
11+
return options
12+
}
13+
}
14+
15+
module.exports = {
16+
BasicAuthentication: BasicAuthentication
17+
}

lib/auth/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const basic = require('./basic')
2+
const session = require('./session')
3+
const token = require('./token')
4+
5+
module.exports = {
6+
BasicAuthentication: basic.BasicAuthentication,
7+
SessionAuthentication: session.SessionAuthentication,
8+
TokenAuthentication: token.TokenAuthentication
9+
}

lib/auth/session.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const utils = require('../utils')
2+
3+
function trim (str) {
4+
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '')
5+
}
6+
7+
function getCookie (cookieName, cookieString) < 8000 span class=pl-kos>{
8+
cookieString = cookieString || window.document.cookie
9+
if (cookieString && cookieString !== '') {
10+
const cookies = cookieString.split(';')
11+
for (var i = 0; i < cookies.length; i++) {
12+
const cookie = trim(cookies[i])
13+
// Does this cookie string begin with the name we want?
14+
if (cookie.substring(0, cookieName.length + 1) === (cookieName + '=')) {
15+
return decodeURIComponent(cookie.substring(cookieName.length + 1))
16+
}
17+
}
18+
}
19+
return null
20+
}
21+
22+
class SessionAuthentication {
23+
constructor (options = {}) {
24+
this.csrfToken = getCookie(options.csrfCookieName, options.cookieString)
25+
this.csrfHeaderName = options.csrfHeaderName
26+
}
27+
28+
authenticate (options) {
29+
options.credentials = 'same-origin'
30+
if (this.csrfToken && !utils.csrfSafeMethod(options.method)) {
31+
options.headers[this.csrfHeaderName] = this.csrfToken
32+
}
33+
return options
34+
}
35+
}
36+
37+
module.exports = {
38+
SessionAuthentication: SessionAuthentication
39+
}

lib/auth/token.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class TokenAuthentication {
2+
constructor (options = {}) {
3+
this.token = options.token
4+
this.prefix = options.prefix || 'Bearer'
5+
}
6+
7+
authenticate (options) {
8+
options.headers['Authorization'] = this.prefix + ' ' + this.token
9+
return options
10+
}
11+
}
12+
13+
module.exports = {
14+
TokenAuthentication: TokenAuthentication
15+
}

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function lookupLink (node, keys) {
2424
class Client {
2525
constructor (options = {}) {
2626
const transportOptions = {
27-
csrf: options.csrf,
27+
auth: options.auth || null,
2828
headers: options.headers || {},
2929
requestCallback: options.requestCallback,
3030
responseCallback: options.responseCallback

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const auth = require('./auth')
12
const client = require('./client')
23
const codecs = require('./codecs')
34
const document = require('./document')
@@ -9,6 +10,7 @@ const coreapi = {
910
Client: client.Client,
1011
Document: document.Document,
1112
Link: document.Link,
13+
auth: auth,
1214
codecs: codecs,
1315
errors: errors,
1416
transports: transports,

lib/transports/http.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const parseResponse = (response, decoders, responseCallback) => {
1919
class HTTPTransport {
2020
constructor (options = {}) {
2121
this.schemes = ['http', 'https']
22-
this.csrf = options.csrf
22+
this.auth = options.auth || null
2323
this.headers = options.headers || {}
2424
this.fetch = options.fetch || fetch
2525
this.FormData = options.FormData || window.FormData
@@ -98,11 +98,8 @@ class HTTPTransport {
9898
}
9999
}
100100

101-
if (this.csrf) {
102-
requestOptions.credentials = 'same-origin'
103-
if (!utils.csrfSafeMethod(method)) {
104-
Object.assign(requestOptions.headers, this.csrf)
105-
}
101+
if (this.auth) {
102+
requestOptions = this.auth.authenticate(requestOptions)
106103
}
107104

108105
let parsedUrl = urlTemplate.parse(link.url)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "coreapi",
3-
"version": "0.0.20",
3+
"version": "0.0.21",
44
"description": "Javascript client library for Core API",
55
"main": "lib/index.js",
66
"scripts": {

tests/transports/http.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const transports = require('../../lib/transports')
2+
const auth = require('../../lib/auth')
23
const codecs = require('../../lib/codecs')
34
const errors = require('../../lib/errors')
45
const document = require('../../lib/document')
@@ -140,9 +141,13 @@ describe('Test the HTTPTransport', function () {
140141
it('CSRF should be included with POST requests.', function () {
141142
const url = 'http://www.example.com/'
142143
const link = new document.Link(url, 'post')
143-
const csrf = {'X-CSRFToken': 'abc'}
144+
const sessionAuth = new auth.SessionAuthentication({
145+
csrfCookieName: 'csrftoken',
146+
csrfHeaderName: 'X-CSRFToken',
147+
cookieString: 'csrftoken=abc'
148+
})
144149
const transport = new transports.HTTPTransport({
145-
csrf: csrf,
150+
auth: sessionAuth,
146151
fetch: testUtils.echo
147152
})
148153

@@ -152,12 +157,35 @@ describe('Test the HTTPTransport', function () {
152157
})
153158
})
154159

160+
it('CSRF should not be included when no CSRF cookie exists.', function () {
161+
const url = 'http://www.example.com/'
162+
const link = new document.Link(url, 'post')
163+
const sessionAuth = new auth.SessionAuthentication({
164+
csrfCookieName: 'csrftoken',
165+
csrfHeaderName: 'X-CSRFToken',
166+
cookieString: ''
167+
})
168+
const transport = new transports.HTTPTransport({
169+
auth: sessionAuth,
170+
fetch: testUtils.echo
171+
})
172+
173+
return transport.action(link, decoders)
174+
.then((res) => {
175+
expect(res).toEqual({url: 'http://www.example.com/', headers: {}, method: 'POST'})
176+
})
177+
})
178+
155179
it('CSRF should not be included with GET requests.', function () {
156180
const url = 'http://www.example.com/'
157181
const link = new document.Link(url, 'get')
158-
const csrf = {'X-CSRFToken': 'abc'}
182+
const sessionAuth = new auth.SessionAuthentication({
183+
csrfCookieName: 'csrftoken',
184+
csrfHeaderName: 'X-CSRFToken',
185+
cookieString: 'csrftoken=abc'
186+
})
159187
const transport = new transports.HTTPTransport({
160-
csrf: csrf,
188+
auth: sessionAuth,
161189
fetch: testUtils.echo
162190
})
163191

0 commit comments

Comments
 (0)
0