|
1 | | -# simple-oauth2-stackexchange |
2 | | -A simple Node.js client library for Stack Exchange OAuth2. |
| 1 | +# Simple OAuth2 StackExchange |
| 2 | + |
| 3 | +This library is a wrapper around [Simple OAuth2 Library](https://github.com/lelylan/simple-oauth2) |
| 4 | + |
| 5 | +Specially made for [Authorization Code Flow](https://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-4.1) with StackExchange. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +Latest Node 8 LTS or newer versions. |
| 10 | + |
| 11 | +## Getting started |
| 12 | + |
| 13 | +``` |
| 14 | +npm install --save simple-oauth2 simple-oauth2-stack-exchange |
| 15 | +``` |
| 16 | + |
| 17 | +or |
| 18 | + |
| 19 | +``` |
| 20 | +yarn add simple-oauth2 simple-oauth2-stack-exchange |
| 21 | +``` |
| 22 | + |
| 23 | +### Usage |
| 24 | + |
| 25 | +```js |
| 26 | +const simpleOAuth2StackExchange = require('simple-oauth2-stack-exchange'); |
| 27 | +const stackExchange = simpleOAuth2StackExchange.create(options); |
| 28 | +``` |
| 29 | + |
| 30 | +`stackExchange` object exposes 3 keys: |
| 31 | +* authorize: Middleware to request user's authorization. |
| 32 | +* getToken: Middleware for callback processing and exchange the authorization token for an `access_token` |
| 33 | +* oauth2: The underlying [simple-oauth2](https://github.com/lelylan/simple-oauth2) instance. |
| 34 | + |
| 35 | +### Options |
| 36 | + |
| 37 | +**Required options** |
| 38 | + |
| 39 | +| Option | Description | |
| 40 | +|--------------|--------------------------------------------------------------------------------------------| |
| 41 | +| clientId | Your App Id. | |
| 42 | +| clientSecret | Your App Secret Id. | |
| 43 | +| callbackURL | Callback configured when you created the app. | |
| 44 | + |
| 45 | + |
| 46 | +**Other options** |
| 47 | + |
| 48 | +| Option | Default | Description | |
| 49 | +|------------------|------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 50 | +| scope | [] | https://api.stackexchange.com/docs/authentication | |
| 51 | +| state | '' | Your CSRF anti-forgery token. More at: https://auth0.com/docs/protocols/oauth2/oauth-state | |
| 52 | +| returnError | false | When is false (default), will call the next middleware with the error object. When is true, will set req.tokenError to the error, and call the next middleware as if there were no error. | |
| 53 | +| authorizeHost | 'https://stackoverflow.com' | | |
| 54 | +| authorizePath | '/oauth' | | |
| 55 | +| tokenHost | 'https://stackoverflow.com' | | |
| 56 | +| tokenPath | '/oauth/access_token' | | |
| 57 | +| authorizeOptions | {} | Pass extra parameters when requesting authorization. | |
| 58 | +| tokenOptions | {} | Pass extra parameters when requesting access_token. | |
| 59 | + |
| 60 | + |
| 61 | +## Example |
| 62 | + |
| 63 | +### Original boilerplate |
| 64 | + |
| 65 | +```js |
| 66 | +const oauth2 = require('simple-oauth2').create({ |
| 67 | + client: { |
| 68 | + id: process.env.STACK_EXCHANGE_APP_ID, |
| 69 | + secret: process.env.STACK_EXCHANGE_APP_SECRET |
| 70 | + }, |
| 71 | + auth: { |
| 72 | + authorizeHost: 'https://stackoverflow.com' |
| 73 | + authorizePath: '/oauth', |
| 74 | + |
| 75 | + tokenHost: 'https://stackoverflow.com', |
| 76 | + tokenPath: '/oauth/access_token' |
| 77 | + } |
| 78 | +}); |
| 79 | + |
| 80 | +router.get('/auth/stack-exchange', (req, res) => { |
| 81 | + const authorizationUri = oauth2.authorizationCode.authorizeURL({ |
| 82 | + redirect_uri: 'http://localhost:3000/auth/stack-exchange/callback', |
| 83 | + }); |
| 84 | + |
| 85 | + res.redirect(authorizationUri); |
| 86 | +}); |
| 87 | + |
| 88 | +router.get('/auth/stack-exchange/callback', async(req, res) => { |
| 89 | + const code = req.query.code; |
| 90 | + const options = { |
| 91 | + code, |
| 92 | + redirect_uri: 'http://localhost:3000/auth/stack-exchange/callback' |
| 93 | + }; |
| 94 | + |
| 95 | + try { |
| 96 | + // The resulting token. |
| 97 | + const result = await oauth2.authorizationCode.getToken(options); |
| 98 | + |
| 99 | + // Exchange for the access token. |
| 100 | + const token = oauth2.accessToken.create(result); |
| 101 | + |
| 102 | + return res.status(200).json(token); |
| 103 | + } catch (error) { |
| 104 | + console.error('Access Token Error', error.message); |
| 105 | + return res.status(500).json('Authentication failed'); |
| 106 | + } |
| 107 | +}); |
| 108 | +``` |
| 109 | + |
| 110 | +### With SimpleOAuth2StackExchange |
| 111 | + |
| 112 | +```js |
| 113 | +const simpleOAuth2StackExchange = require('simple-oauth2-stack-exchange'); |
| 114 | + |
| 115 | +const stackExchange = simpleOAuth2StackExchange.create({ |
| 116 | + clientId: process.env.STACK_EXCHANGE_APP_ID, |
| 117 | + clientSecret: process.env.STACK_EXCHANGE_APP_SECRET, |
| 118 | + callbackURL: 'http://localhost:3000/auth/stack-exchange/callback' |
| 119 | +}); |
| 120 | + |
| 121 | +// Ask the user to authorize. |
| 122 | +router.get('/auth/stack-exchange', stackExchange.authorize); |
| 123 | + |
| 124 | +// Exchange the token for the access token. |
| 125 | +router.get('/auth/stack-exchange/callback', stackExchange.accessToken, (req, res) => { |
| 126 | + return res.status(200).json(req.token); |
| 127 | +}); |
| 128 | +``` |
0 commit comments