wikidata-token
Advanced tools
Comparing version 2.2.0 to 2.3.0
# CHANGELOG | ||
*versions follow [SemVer](http://semver.org)* | ||
## 2.3.0 - 2017-05-21 | ||
* Added [support for OAuth](https://github.com/maxlath/wikidata-token#with-username--password) | ||
## 2.2.0 - 2017-03-29 | ||
@@ -5,0 +8,0 @@ * Added a `wikibaseInstance` option to allow using it on a different Wikibase instance |
module.exports = { | ||
username: 'your-wikidata-username', | ||
password: 'your-wikidata-password' | ||
credentials: { | ||
username: 'your-wikidata-username', | ||
password: 'your-wikidata-password' | ||
}, | ||
oauth: { | ||
consumer_key: 'your-consumer-token', | ||
consumer_secret: 'your-secret-token', | ||
token: 'a-user-token', | ||
token_secret: 'a-secret-token' | ||
} | ||
} |
@@ -1,14 +0,18 @@ | ||
const got = require('got') | ||
const breq = require('bluereq') | ||
const getInstance = require('./get_instance') | ||
const getToken = (instance) => (loginCookies) => { | ||
const url = `${instance}?action=query&meta=tokens&format=json` | ||
const headers = { 'cookie': loginCookies } | ||
return got.get(url, { headers }) | ||
.then(parseTokens.bind(null, loginCookies)) | ||
const getToken = (instance, oauth) => loginCookies => { | ||
const params = { url: `${instance}?action=query&meta=tokens&format=json` } | ||
if (oauth) { | ||
params.oauth = oauth | ||
} else { | ||
params.headers = { 'cookie': loginCookies } | ||
} | ||
return breq.get(params) | ||
.then(parseTokens(loginCookies)) | ||
} | ||
const parseTokens = (loginCookies, res) => { | ||
const parseTokens = loginCookies => res => { | ||
return { | ||
token: JSON.parse(res.body).query.tokens.csrftoken, | ||
token: res.body.query.tokens.csrftoken, | ||
cookie: loginCookies | ||
@@ -18,5 +22,5 @@ } | ||
module.exports = (config, loginCookiesPromise) => { | ||
module.exports = (config, loginCookiesPromise, oauth) => { | ||
const instance = getInstance(config) | ||
return () => loginCookiesPromise.then(getToken(instance)) | ||
return () => loginCookiesPromise.then(getToken(instance, oauth)) | ||
} |
@@ -1,2 +0,2 @@ | ||
const got = require('got') | ||
const breq = require('bluereq') | ||
const _ = require('./utils') | ||
@@ -6,2 +6,3 @@ const pkg = require('../package.json') | ||
const defaultUserAgent = `wikidata-token/${pkg.version} (${pkg.repository.url})` | ||
const qs = require('querystring') | ||
@@ -11,3 +12,4 @@ module.exports = (config) => { | ||
const headers = { | ||
'user-agent': config.userAgent || defaultUserAgent | ||
'user-agent': config.userAgent || defaultUserAgent, | ||
'content-type': 'application/x-www-form-urlencoded' | ||
} | ||
@@ -23,7 +25,7 @@ const instance = getInstance(config) | ||
const getLoginToken = () => { | ||
const body = { | ||
const body = qs.stringify({ | ||
lgname: username, | ||
lgpassword: password | ||
} | ||
return got.post(loginUrl, { json: true, headers, body }) | ||
}) | ||
return breq.post({ url: loginUrl, headers, body }) | ||
.then(parseLoginToken) | ||
@@ -43,11 +45,11 @@ } | ||
const { cookies, token } = data | ||
const body = { | ||
const body = qs.stringify({ | ||
lgname: username, | ||
lgpassword: password, | ||
lgtoken: token | ||
} | ||
}) | ||
const headersWithCookies = Object.assign({}, headers, { 'Cookie': cookies }) | ||
return got.post(loginUrl, { json: true, headers: headersWithCookies, body }) | ||
return breq.post({ url: loginUrl, headers: headersWithCookies, body }) | ||
.then(res => { | ||
@@ -54,0 +56,0 @@ if (verbose) console.log('reallyLogin res', res.body) |
{ | ||
"name": "wikidata-token", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "a promises-based lib abstracting authentification for write actions on the Wikidata API", | ||
@@ -15,4 +15,4 @@ "main": "wikidata-token.js", | ||
"dependencies": { | ||
"chalk": "^1.1.3", | ||
"got": "^6.7.1" | ||
"bluereq": "^2.1.2", | ||
"chalk": "^1.1.3" | ||
}, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
@@ -11,2 +11,4 @@ a promises-based lib abstracting authentification for write actions on the [Wikidata API](https://www.wikidata.org/w/api.php) | ||
- [use](#use) | ||
- [with username / password](#with-username--password) | ||
- [with OAuth](#with-oauth) | ||
- [Example](#example) | ||
@@ -29,2 +31,5 @@ - [Development](#development) | ||
### use | ||
#### with username / password | ||
```javascript | ||
@@ -42,3 +47,2 @@ var config = { | ||
var getToken = wdToken(config) | ||
``` | ||
@@ -61,2 +65,22 @@ | ||
#### with OAuth | ||
same as with username / password but your config object will look like: | ||
```js | ||
var config = { | ||
// Required | ||
oauth: { | ||
// Obtained at registration | ||
// https://www.mediawiki.org/wiki/OAuth/For_Developers#Registration | ||
consumer_key: 'your-consumer-token', | ||
consumer_secret: 'your-secret-token', | ||
// Obtained when the user authorized your service | ||
// see https://www.mediawiki.org/wiki/OAuth/For_Developers#Authorization | ||
token: 'a-user-token', | ||
token_secret: 'a-secret-token' | ||
}, | ||
// Then the optional parameters are the same | ||
} | ||
``` | ||
## Example | ||
@@ -63,0 +87,0 @@ |
@@ -5,4 +5,4 @@ const test = require('ava') | ||
test('wikidata-token works', t => { | ||
const tokenGetter = wikidataToken(CONFIG) | ||
test('get token from username and password', t => { | ||
const tokenGetter = wikidataToken(CONFIG.credentials) | ||
t.is(typeof tokenGetter, 'function') | ||
@@ -22,1 +22,12 @@ | ||
}) | ||
test('get token from oauth', t => { | ||
const { oauth } = CONFIG | ||
const tokenGetter = wikidataToken({ oauth }) | ||
t.is(typeof tokenGetter, 'function') | ||
return tokenGetter() | ||
.then(res => { | ||
t.true(res.token.length > 40) | ||
}) | ||
}) |
@@ -5,4 +5,11 @@ const login = require('./lib/login') | ||
module.exports = (config) => { | ||
const loginCookiesPromise = login(config) | ||
return getTokenGetter(config, loginCookiesPromise) | ||
if (config.oauth) { | ||
const loginCookiesPromise = Promise.resolve('') | ||
return getTokenGetter(config, loginCookiesPromise, config.oauth) | ||
} else if (config.username && config.password) { | ||
const loginCookiesPromise = login(config) | ||
return getTokenGetter(config, loginCookiesPromise) | ||
} else { | ||
throw new Error('no authentification means provided') | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
52121
17
180
124
+ Addedbluereq@^2.1.2
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.12.0(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedbluebird@3.7.2(transitive)
+ Addedbluereq@2.1.12(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedlodash.pick@4.4.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.9.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)
- Removedgot@^6.7.1
- Removedcapture-stack-trace@1.0.2(transitive)
- Removedcreate-error-class@3.0.2(transitive)
- Removedduplexer3@0.1.5(transitive)
- Removedget-stream@3.0.0(transitive)
- Removedgot@6.7.1(transitive)
- Removedis-redirect@1.0.0(transitive)
- Removedis-retry-allowed@1.2.0(transitive)
- Removedis-stream@1.1.0(transitive)
- Removedlowercase-keys@1.0.1(transitive)
- Removedprepend-http@1.0.4(transitive)
- Removedtimed-out@4.0.1(transitive)
- Removedunzip-response@2.0.1(transitive)
- Removedurl-parse-lax@1.0.0(transitive)