|
1 | 1 | # 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. |
3 | 3 |
|
4 | 4 |
|
5 |
| -## Use case |
| 5 | +## tl;dr; |
6 | 6 |
|
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. |
8 | 8 |
|
9 |
| -## Demo |
10 | 9 |
|
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 |
12 | 11 |
|
13 | 12 |
|
14 |
| -## Installing on the server |
| 13 | +```bash |
| 14 | +npm install oauth-shim |
| 15 | +``` |
15 | 16 |
|
16 |
| -Install the package |
| 17 | +Middleware for Express/Connect |
17 | 18 |
|
18 |
| - npm install oauth-shim |
19 | 19 |
|
| 20 | +```javascript |
| 21 | +var oauthshim = require('oauth-shim'), |
| 22 | + express = require('express'); |
20 | 23 |
|
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 | +``` |
25 | 34 |
|
26 |
| - var app = express(); |
27 |
| - app.all('/oauthproxy', oauthshim.request); |
28 | 35 |
|
29 |
| - // Initiate the shim with Client ID's e.g. |
30 |
| - oauthshim.init({ |
31 |
| - // key : Secret |
32 |
| - '12345' : 'secret678910', |
33 |
| - 'abcde' : 'secretfghijk' |
34 |
| - }); |
35 | 36 |
|
36 |
| - // Print request->response to console. |
37 |
| - oauthshim.debug = true; |
38 | 37 |
|
39 | 38 | The code above says apply the shim to all requests to the pathname `/oauthproxy`.
|
40 | 39 |
|
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 |
42 | 48 |
|
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); |
44 | 55 |
|
45 | 56 |
|
46 |
| -### Asynchronsly access secret |
| 57 | +function customHandler(req, res, next){ |
47 | 58 |
|
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 ){ |
49 | 66 |
|
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) |
58 | 68 | }
|
59 | 69 |
|
| 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 | +``` |
60 | 93 |
|
61 | 94 | ## Authentication API
|
62 | 95 |
|
@@ -171,20 +204,12 @@ Add a JSONP callback function and override the method. E.g.
|
171 | 204 | &callback=myJSONP
|
172 | 205 |
|
173 | 206 |
|
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 |
189 | 208 |
|
| 209 | +```bash |
| 210 | +# Install the test dependencies. |
| 211 | +npm install -l |
190 | 212 |
|
| 213 | +# Run tests |
| 214 | +npm test |
| 215 | +``` |
0 commit comments