Skip to content

Commit

Permalink
Introduce new async function for jsonwebtoken lib
Browse files Browse the repository at this point in the history
	modified:   lib/services/identity/queries/index.js
  • Loading branch information
kixxauth committed Oct 25, 2016
1 parent 5a6fb7a commit 1fa861b
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions lib/services/identity/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,42 @@ module.exports = function (service) {
const JWT_SECRET = options.jwtSecret;
const JWT_ISSUER = options.jwtIssuer;

const jwtVerify = (token, secret, options) => {
return new Promise((resolve, reject) => {
try {
jwt.verify(token, secret, options, (err, object) => {
if (err) {
return reject(err);
}
return resolve(object);
});
} catch (err) {
return reject(err);
}
});
};

const jwtSign = (payload, secret, options) => {
return new Promise((resolve, reject) => {
try {
jwt.sign(payload, secret, options, (err, token) => {
if (err) {
return reject(err);
}
return resolve(token);
});
} catch (err) {
return reject(err);
}
});
};

// args.token - String *required*
queries.verify = args => {
const token = args.token;

return new Promise((resolve, reject) => {
try {
const object = jwt.verify(token, JWT_SECRET, {issuer: JWT_ISSUER});
return jwtVerify(token, JWT_SECRET, {issuer: JWT_ISSUER})
.then(object => {
const role = 'store';
const cmd = 'get';
const channelId = object.channel;
Expand All @@ -41,10 +70,11 @@ module.exports = function (service) {
if (!channel) {
throw new Error(`Channel ${channelId} not found.`);
}
return resolve({audience, subject, channel});

return {audience, subject, channel};
});
} else if (subject) {
return resolve({audience, subject});
return {audience, subject};
} else if (viewerId && platformId) {
return Promise
.all([
Expand Down Expand Up @@ -73,7 +103,7 @@ module.exports = function (service) {
throw new Error(`User ${viewerId} not found.`);
}

return resolve({audience, channel, platform, viewer});
return {audience, channel, platform, viewer};
});
} else if (platformId) {
return Promise
Expand All @@ -95,15 +125,12 @@ module.exports = function (service) {
throw new Error(`Platform ${platformId} not found.`);
}

return resolve({audience, channel, platform});
return {audience, channel, platform};
});
}

throw new Error('JSON Web Token has no subject or platform.');
} catch (err) {
reject(err);
}
});
return Promise.reject(new Error('JSON Web Token has no subject or platform.'));
});
};

// args.audience - Array of Strings *required*
Expand Down Expand Up @@ -146,14 +173,7 @@ module.exports = function (service) {
throw new Error('args.subject or args.platform is required');
}

return new Promise((resolve, reject) => {
try {
const token = jwt.sign(payload, secret, options);
return resolve(token);
} catch (err) {
return reject(err);
}
});
return jwtSign(payload, secret, options);
};

queries.composeConfig = args => {
Expand Down

0 comments on commit 1fa861b

Please sign in to comment.