Tumortisch-Dist/resources/app/node_modules/simple-oauth2/lib/access-token/parse-token.js
2021-06-15 16:00:08 +02:00

28 lines
766 B
JavaScript

'use strict';
const debug = require('debug')('access-token');
const isDate = require('date-fns/isDate');
const parseISO = require('date-fns/parseISO');
const addSeconds = require('date-fns/addSeconds');
const parseTokenDateProperties = (token) => {
const parsedTokenProps = {};
if ('expires_at' in token) {
if (!isDate(token.expires_at)) {
parsedTokenProps.expires_at = parseISO(token.expires_at);
}
} else if ('expires_in' in token) {
parsedTokenProps.expires_at = addSeconds(
new Date(),
Number.parseInt(token.expires_in, 10)
);
} else {
debug('No token expiration property was found. Ignoring date parsing');
}
return Object.assign({}, token, parsedTokenProps);
};
module.exports = parseTokenDateProperties;