Skip to content

Commit

Permalink
Improve oauth2 usage examples with github and microsoft providers
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansamines committed Jun 13, 2018
1 parent 575eebb commit e40e005
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 61 deletions.
27 changes: 27 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## Examples

Before running any example, please set the following environment variables:

```bash
export CLIENT_ID="your client id"
export CLIENT_SECRET="your client secret"
```

The following authorization services are provided as examples to get a better idea of how to use this library on some of the most common use cases:


### Microsoft

Microsoft requires the credentials information during the token exchange to be sent at the request body. It also requires to send the **redirect_uri** argument. See the `./microsoft.js` module as a reference implementation or execute the example with:

```bash
npm run start:microsoft
```

### Github

See the `./github.js` module as a reference implementation or execute the example with:

```bash
npm run start:github
```
56 changes: 56 additions & 0 deletions example/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const createApplication = require('./');
const simpleOauthModule = require('./../');

createApplication(({ app, callbackUrl }) => {
const oauth2 = simpleOauthModule.create({
client: {
id: process.env.CLIENT_ID,
secret: process.env.CLIENT_SECRET,
},
auth: {
tokenHost: 'https://github.com',
tokenPath: '/login/oauth/access_token',
authorizePath: '/login/oauth/authorize',
},
});

// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: 'http://localhost:3000/callback',
scope: 'notifications',
state: '3(#0/!~',
});

// Initial page redirecting to Github
app.get('/auth', (req, res) => {
console.log(authorizationUri);
res.redirect(authorizationUri);
});

// Callback service parsing the authorization token and asking for the access token
app.get('/callback', async (req, res) => {
const code = req.query.code;
const options = {
code,
};

try {
const result = await oauth2.authorizationCode.getToken(options);

console.log('The resulting token: ', result);

const token = oauth2.accessToken.create(result);

return res.status(200).json(token)
} catch(error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
});

app.get('/', (req, res) => {
res.send('Hello<br><a href="/auth">Log in with Github</a>');
});
});
73 changes: 13 additions & 60 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,19 @@
'use strict';

const express = require('express');
const simpleOauthModule = require('./../');
const app = require('express')();
const port = 3000;

const app = express();
const oauth2 = simpleOauthModule.create({
client: {
id: '<CLIENT_ID>',
secret: '<CLIENT_SECRET>',
},
auth: {
tokenHost: 'https://github.com',
tokenPath: '/login/oauth/access_token',
authorizePath: '/login/oauth/authorize',
},
});
module.exports = (cb) => {
const callbackUrl = 'http://localhost:3000/callback';

// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: 'http://localhost:3000/callback',
scope: 'notifications',
state: '3(#0/!~',
});
app.listen(port, (err) => {
if (err) return console.error(err);

// Initial page redirecting to Github
app.get('/auth', (req, res) => {
console.log(authorizationUri);
res.redirect(authorizationUri);
});
console.log(`Express server listening at http://localhost:${port}`);

// Callback service parsing the authorization token and asking for the access token
app.get('/callback', async (req, res) => {
const code = req.query.code;
const options = {
code,
};

try {
const result = await oauth2.authorizationCode.getToken(options);

console.log('The resulting token: ', result);

const token = oauth2.accessToken.create(result);

return res.status(200).json(token)
} catch(error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
});

app.get('/success', (req, res) => {
res.send('');
});

app.get('/', (req, res) => {
res.send('Hello<br><a href="/auth">Log in with Github</a>');
});

app.listen(3000, () => {
console.log('Express server started on port 3000');
});


// Credits to [@lazybean](https://github.com/lazybean)
cb({
app,
callbackUrl,
});
});
};
59 changes: 59 additions & 0 deletions example/microsoft.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const createApplication = require('./');
const simpleOauthModule = require('./..');

createApplication(({ app, callbackUrl }) => {
const oauth2 = simpleOauthModule.create({
client: {
id: process.env.CLIENT_ID,
secret: process.env.CLIENT_SECRET,
},
auth: {
tokenHost: 'https://login.live.com',
tokenPath: '/oauth20_token.srf',
authorizePath: '/oauth20_authorize.srf',
},
options: {
authorizationMethod: 'body',
}
});

// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: callbackUrl,
scope: 'User.Read',
});

// Initial page redirecting to Github
app.get('/auth', (req, res) => {
console.log(authorizationUri);
res.redirect(authorizationUri);
});

// Callback service parsing the authorization token and asking for the access token
app.get('/callback', async (req, res) => {
const code = req.query.code;
const options = {
code,
redirect_uri: callbackUrl,
};

try {
const result = await oauth2.authorizationCode.getToken(options);

console.log('The resulting token: ', result);

const token = oauth2.accessToken.create(result);

return res.status(200).json(token)
} catch(error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
});

app.get('/', (req, res) => {
res.send('Hello<br><a href="/auth">Log in with Microsoft</a>');
});
});
3 changes: 2 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
"start:github": "node github.js",
"start:microsoft": "node microsoft.js"
},
"keywords": [],
"author": "",
Expand Down

0 comments on commit e40e005

Please sign in to comment.