Skip to content

Commit

Permalink
Rewrite to fix fetch issues
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcsz committed Feb 12, 2017
1 parent ae662b9 commit 1688091
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
const fetch = require('node-fetch');
const encode = require('./lib/encode.js');
const encode = require('./encode.js');

exports.authorizationCodeUri = (appCredentials) => {
exports.formAuthorizationUri = (config) => {
return new Promise ((fulfill, reject) => {
if (appCredentials.client.id === undefined) {
reject('Not enough information to build authorization URL.');
}
if (appCredentials.responseType !== undefined) {
fulfill(`${appCredentials.uri.authorization}?client_id=${encodeURI(appCredentials.client.id)}&response_type=${encodeURI(appCredentials.responseType)}&redirect_uri=${encodeURI(appCredentials.uri.redirect)}&scope=${appCredentials.scope}`);
} else if (appCredentials.responseType === undefined) {
fulfill(`${appCredentials.uri.authorization}?client_id=${encodeURI(appCredentials.client.id)}&redirect_uri=${encodeURI(appCredentials.uri.redirect)}&scope=${appCredentials.scope}`);
if (config.client.id === undefined || config.client.secret === undefined || config.uri.authorization === undefined || config.uri.redirect === undefined || config.scope === undefined) {
reject('Not enough information to form authorization URL');
} else {
if (config.responseType !== undefined) {
fulfill(`${config.uri.authorization}?client_id=${encodeURI(config.client.id)}&response_type=${encodeURI(config.responseType)}&scope=${encodeURI(config.scope)}&redirect_uri=${encodeURIComponent(config.uri.redirect)}`);
} else if (config.responseType === undefined) {
fulfill(`${config.uri.authorization}?client_id=${encodeURI(config.client.id)}&scope=${encodeURI(config.scope)}&redirect_uri=${encodeURIComponent(config.uri.redirect)}`);
}
}
});
}

exports.getAccessToken = (app) => {
exports.getAccessToken = (config) => {
return new Promise ((fulfill, reject) => {
let encodedClientCredentials = encode.base64Encode(`${app.client.id}:${app.client.secret}`);
let encodedClientCredentials = encode.base64(`${config.client.id}:${config.client.secret}`);

fetch(app.uri.authorization, {
fetch(config.uri.token, {
method: 'POST',
header: {
headers: {
'Authorization': `Basic ${encodedClientCredentials}`,
'Content-Type': 'application/x-ww-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded'
},
body: `client_id=${app.client.id}&grant_type=authorization_code&redirect_uri-&${encodeURI(app.uri.redirect)}&code=${app.code}`
}).then((token) => {
let accessToken = token.json();

if (accessToken.error !== undefined) {
reject(accessToken);
} else {
fulfill(accessToken);
}
body: `code=${encodeURI(config.code)}&grant_type=authorization_code&redirect_uri=${encodeURI(config.uri.redirect)}`
}).catch((error) => {
reject(error);
let tokenError = error.json();
reject(tokenError);
}).then((response) => {
return response.json().catch((parseError) => {
console.log(`Error parsing token: ${parseError}`);
reject(parseError);
});
}).then((accessToken) => {
fulfill(accessToken);
});
});
}

0 comments on commit 1688091

Please sign in to comment.