-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from foundersandcoders/add/refresh-token-request
Add a refresh token request
- Loading branch information
Showing
11 changed files
with
207 additions
and
82 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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,15 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
module.exports = reqCookiesObject => { | ||
const { access } = reqCookiesObject && reqCookiesObject; | ||
|
||
return new Promise((resolve, reject) => { | ||
jwt.verify(access, process.env.JWT_SECRET, (error, decoded) => { | ||
if (error) { | ||
return reject(error); | ||
} else { | ||
return resolve(decoded); | ||
} | ||
}); | ||
}); | ||
}; |
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
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,14 @@ | ||
const axios = require('axios'); | ||
|
||
module.exports.modify = (options, tools) => { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
await axios(options); | ||
resolve(); | ||
} catch (error) { | ||
error.response.data.error === 'Unauthorized' | ||
? reject({ Unauthorized: true }) | ||
: reject(); | ||
} | ||
}); | ||
}; |
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,41 @@ | ||
const verifyToken = require('../helpers/verify_token.js'); | ||
const axios = require('axios'); | ||
const qs = require('querystring'); | ||
const jwt = require('jsonwebtoken'); | ||
const { oauthTokenBaseURL } = require('../constants/urls.json'); | ||
|
||
module.exports = reqCookiesObject => { | ||
const requestToken = async token => { | ||
const tokenQueries = { | ||
grant_type: 'refresh_token', | ||
refresh_token: token.refresh_token, | ||
client_id: process.env.CLIENT_ID, | ||
client_secret: process.env.CLIENT_SECRET, | ||
redirect_uri: process.env.REDIRECT_URI, | ||
}; | ||
|
||
const options = { | ||
method: 'post', | ||
baseURL: oauthTokenBaseURL, | ||
data: qs.stringify(tokenQueries), | ||
}; | ||
try { | ||
const apiTokenResponse = await axios(options); | ||
const { access_token, refresh_token } = apiTokenResponse.data; | ||
const token = await jwt.sign( | ||
{ access_token, refresh_token }, | ||
process.env.JWT_SECRET, | ||
); | ||
return { access_token, token }; | ||
} catch (e) { | ||
return e; | ||
} | ||
}; | ||
|
||
return new Promise((resolve, reject) => { | ||
verifyToken(reqCookiesObject) | ||
.then(requestToken) | ||
.then(tokens => resolve(tokens)) | ||
.catch(reject); | ||
}); | ||
}; |
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,5 @@ | ||
module.exports.events = require('./events.js'); | ||
|
||
module.exports.auth = { | ||
getRefreshToken: require('./get_refresh_token.js'), | ||
}; |