Comparing version 0.1.4 to 0.2.0
54
index.js
'use strict'; | ||
var hashes = require('jshashes'), | ||
xtend = require('xtend'), | ||
sha1 = new hashes.SHA1(); | ||
@@ -83,2 +84,55 @@ | ||
/** | ||
* Takes an options object for configuration (consumer_key, | ||
* consumer_secret, version, signature_method, token) and returns a | ||
* function that generates the Authorization header for given data. | ||
* | ||
* The returned function takes these parameters: | ||
* - method: GET/POST/... | ||
* - uri: full URI with protocol, port, path and query string | ||
* - extra_params: any extra parameters (that are passed in the POST data), | ||
* can be an object or a from-urlencoded string. | ||
* | ||
* Returned function returns full OAuth header with "OAuth" string in it. | ||
*/ | ||
ohauth.headerGenerator = function(options) { | ||
options = options || {}; | ||
var consumer_key = options.consumer_key || '', | ||
consumer_secret = options.consumer_secret || '', | ||
signature_method = options.signature_method || 'HMAC-SHA1', | ||
version = options.version || '1.0', | ||
token = options.token || ''; | ||
return function(method, uri, extra_params) { | ||
method = method.toUpperCase(); | ||
if (typeof extra_params === 'string' && extra_params.length > 0) { | ||
extra_params = ohauth.stringQs(extra_params); | ||
} | ||
var uri_parts = uri.split('?', 2), | ||
base_uri = uri_parts[0]; | ||
var query_params = uri_parts.length === 2 ? | ||
ohauth.stringQs(uri_parts[1]) : {}; | ||
var oauth_params = { | ||
oauth_consumer_key: consumer_key, | ||
oauth_signature_method: signature_method, | ||
oauth_version: version, | ||
oauth_timestamp: ohauth.timestamp(), | ||
oauth_nonce: ohauth.nonce() | ||
}; | ||
if (token) oauth_params.oauth_token = token; | ||
var all_params = xtend({}, oauth_params, query_params, extra_params), | ||
base_str = ohauth.baseString(method, base_uri, all_params); | ||
oauth_params.oauth_signature = ohauth.signature(consumer_secret, token, base_str); | ||
return 'OAuth ' + ohauth.authHeader(oauth_params); | ||
}; | ||
}; | ||
module.exports = ohauth; |
{ | ||
"name": "ohauth", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "browser oauth", | ||
@@ -51,4 +51,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"jshashes": "~1.0.3" | ||
"jshashes": "~1.0.3", | ||
"xtend": "~2.0.3" | ||
} | ||
} |
@@ -9,4 +9,2 @@ ## ohauth | ||
This includes [Paul Johnston's venerable implementation of SHA1](http://pajhome.org.uk/crypt/md5/). | ||
If you use this on a server [different from the one authenticated against](http://en.wikipedia.org/wiki/Same_origin_policy), | ||
@@ -67,1 +65,17 @@ you'll need to [enable](http://enable-cors.org/) and use [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing) | ||
``` | ||
#### Just generating the headers | ||
```js | ||
// create a function holding configuration | ||
var auth = ohauth.headerGenerator({ | ||
consumer_key: '...', | ||
consumer_secret: '...' | ||
}); | ||
// pass just the data to produce the OAuth header with optional | ||
// POST data (as long as it'll be form-urlencoded in the request) | ||
var header = auth('GET', 'http://.../?a=1&b=2', { c: 3, d: 4 }); | ||
// or pass the POST data as an form-urlencoded | ||
var header = auth('GET', 'http://.../?a=1&b=2', 'c=3&d=4'); | ||
``` |
@@ -45,2 +45,9 @@ if (typeof require !== 'undefined') { | ||
}); | ||
describe('#headerGenerator', function() { | ||
it('generates a header function', function() { | ||
expect(ohauth.headerGenerator({})).to.be.a(Function); | ||
expect(ohauth.headerGenerator({})('GET', 'http://foo.com/')).to.be.a.string; | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
1806
80
1
0
83708
2
11
+ Addedxtend@~2.0.3
+ Addedforeach@2.0.6(transitive)
+ Addedindexof@0.0.1(transitive)
+ Addedis@0.2.7(transitive)
+ Addedis-object@0.1.2(transitive)
+ Addedobject-keys@0.2.0(transitive)
+ Addedxtend@2.0.6(transitive)