-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve oauth2 usage examples with github and microsoft providers
- Loading branch information
1 parent
575eebb
commit e40e005
Showing
5 changed files
with
157 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters