Skip to content

Commit 5d827ac

Browse files
committed
Spec the middleware intervention for exposing the token, fix MrSwitch#11
1 parent 658d2a5 commit 5d827ac

File tree

3 files changed

+76
-57
lines changed

3 files changed

+76
-57
lines changed

README.md

Lines changed: 74 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,95 @@
11
# OAuth-shim
2-
This node module provides a "shim" service for clientside web apps adopting serverside OAuth2 or OAuth1 authentication but fighting to keep it all the browser, and shims the tedious dog legging through servers that has become OAuth1's curse.
2+
Middleware offering OAuth1/OAuth2 authorization handshake for web applications using the [HelloJS](http://adodson.com/hello.js) clientside authentication library.
33

44

5-
## Use case
5+
## tl;dr;
66

7-
Popular API's like Twitter, Dropbox and Yahoo require this server-to-server authentication paradigm. What oauthshim does is set up a RESTful service which shims up these web API's. This is used by clientside libraries like [HelloJS](http://adodson.com/hello.js) as a fallback to keep everything running in the client.
7+
[https://auth-server.herokuapp.com](https://auth-server.herokuapp.com) is a service which utilizes this package. If you dont want to implement your own you can simply and freely register thirdparty application Key's and Secret's there.
88

9-
## Demo
109

11-
[https://auth-server.herokuapp.com](https://auth-server.herokuapp.com) is a service which utilizes this package. You can register your own Application Key and Secret there if you dont want to set your own up. But for production you shouldn't rely on that service.
10+
## Implement
1211

1312

14-
## Installing on the server
13+
```bash
14+
npm install oauth-shim
15+
```
1516

16-
Install the package
17+
Middleware for Express/Connect
1718

18-
npm install oauth-shim
1919

20+
```javascript
21+
var oauthshim = require('oauth-shim'),
22+
express = require('express');
2023

21-
## Using with ExpressJS
22-
23-
var oauthshim = require('oauth-shim'),
24-
express = require('express');
24+
var app = express();
25+
app.all('/oauthproxy', oauthshim);
26+
27+
// Initiate the shim with Client ID's and secret, e.g.
28+
oauthshim.init({
29+
// id : secret
30+
'12345' : 'secret678910',
31+
'abcde' : 'secretfghijk'
32+
});
33+
```
2534

26-
var app = express();
27-
app.all('/oauthproxy', oauthshim.request);
2835

29-
// Initiate the shim with Client ID's e.g.
30-
oauthshim.init({
31-
// key : Secret
32-
'12345' : 'secret678910',
33-
'abcde' : 'secretfghijk'
34-
});
3536

36-
// Print request->response to console.
37-
oauthshim.debug = true;
3837

3938
The code above says apply the shim to all requests to the pathname `/oauthproxy`.
4039

41-
## Using with ConnectJS
40+
## Customised Middleware
41+
42+
### Capture Access Tokens
43+
44+
Use the middleware to capture the access_token registered with your app at any point in the series of operations that this module steps through. In the example below they are disseminated with a `customHandler` in the middleware chain to capture the access_token...
45+
46+
47+
```javascript
4248

43-
Change `oauthshim.request` to `oauthshim.listen`
49+
app.all('/oauthproxy',
50+
oauthshim.interpret,
51+
customHandler,
52+
oauthshim.proxy,
53+
oauthshim.redirect,
54+
oauthshim.unhandled);
4455

4556

46-
### Asynchronsly access secret
57+
function customHandler(req, res, next){
4758

48-
If you want to return clientID's asynchronosly (perhaps you want to look up from a database) then override the getCredentials method. Here's the basics e.g...
59+
// Check that this is a login redirect with an access_token (not a RESTful API call via proxy)
60+
if( req.oauthshim &&
61+
req.oauthshim.redirect &&
62+
req.oauthshim.data &&
63+
req.oauthshim.data.access_token &&
64+
req.oauthshim.options &&
65+
!req.oauthshim.options.path ){
4966

50-
oauthshim.getCredentials = function(id,callback){
51-
// Return
52-
if(id === '12345'){
53-
callback('secret678910');
54-
}
55-
if(id === 'abcde'){
56-
callback('secretfghijk');
57-
}
67+
// do something with the token (req.oauthshim.data.access_token)
5868
}
5969

70+
// Call next to complete the operation
71+
next()
72+
}
73+
74+
```
75+
76+
77+
### Asynchronsly retrieve the secret
78+
79+
Rewrite the function `getCredentials` to change the way the client secret is stored/retrieved. This method is asyncronous, to access the secret from a database etc..
80+
e.g...
81+
82+
```javascript
83+
oauthshim.getCredentials = function(id,callback){
84+
// Return
85+
if(id === '12345'){
86+
callback('secret678910');
87+
}
88+
if(id === 'abcde'){
89+
callback('secretfghijk');
90+
}
91+
}
92+
```
6093

6194
## Authentication API
6295

@@ -171,20 +204,12 @@ Add a JSONP callback function and override the method. E.g.
171204
&callback=myJSONP
172205

173206

174-
## Contributing
175-
176-
Don't forget to run the tests.
177-
178-
# Install the test dependencies.
179-
180-
npm install -l
181-
182-
# Run the tests, continuously
183-
184-
npm test
185-
186-
# Single
187-
188-
mocha test
207+
## Specs
189208

209+
```bash
210+
# Install the test dependencies.
211+
npm install -l
190212

213+
# Run tests
214+
npm test
215+
```

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "oauth-shim",
3-
"version": "0.1.7",
3+
"version": "0.2.0",
44
"description": "OAuth2 shim for OAuth1 services, works with the clientside library HelloJS",
55
"main": "index.js",
66
"scripts": {
@@ -21,15 +21,11 @@
2121
"bugs": {
2222
"url": "https://github.com/MrSwitch/node-oauth-shim/issues"
2323
},
24-
"dependencies": {
25-
"crypto": "0.0.3"
26-
},
2724
"devDependencies": {
28-
"should": ">= 0.0.0",
2925
"expect.js" : "*",
3026
"express": "*",
3127
"connect": "*",
3228
"supertest": "*",
33-
"mocha": "^2.0.1"
29+
"mocha": "*"
3430
}
3531
}

test/oauth-shim.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ var app = express();
2929
// SETUP SHIM LISTENING
3030
////////////////////////////////
3131

32-
oauthshim.debug = false;
33-
3432
oauthshim.init({
3533
// OAuth 1
3634
'oauth_consumer_key' : 'oauth_consumer_secret',

0 commit comments

Comments
 (0)