project files added

This commit is contained in:
mhalfmann
2021-06-15 16:00:08 +02:00
parent e156e2f053
commit db46afa351
13928 changed files with 1569902 additions and 0 deletions
+76
View File
@@ -0,0 +1,76 @@
'use strict';
const isAfter = require('date-fns/isAfter');
const parseToken = require('./parse-token');
const coreModule = require('./../core');
/**
* Wrapper for the Access Token Object
*/
module.exports = (config) => {
const core = coreModule(config);
class AccessToken {
constructor(token) {
this.token = parseToken(token);
}
/**
* Check if the access token is expired or not
*/
expired() {
return isAfter(new Date(), this.token.expires_at);
}
/**
* Refresh the access token
* @param {Object} params An optional argument for additional API request params.
*/
async refresh(params) {
const options = Object.assign({}, params, {
grant_type: 'refresh_token',
refresh_token: this.token.refresh_token,
});
const response = await core.request(config.auth.tokenPath, options);
return new AccessToken(response);
}
/**
* Revoke access or refresh token
* @param {String} tokenType A string containing the type of token to revoke.
* Should be either "access_token" or "refresh_token"
*/
async revoke(tokenType) {
const token = tokenType === 'access_token' ? this.token.access_token : this.token.refresh_token;
const options = {
token,
token_type_hint: tokenType,
};
return core.request(config.auth.revokePath, options);
}
/**
* Revoke both the existing access and refresh tokens
*/
async revokeAll() {
await this.revoke('access_token');
await this.revoke('refresh_token');
}
}
/**
* Creates an OAuth2.AccessToken instance
* @param {Object} token An object containing the token object returned from the OAuth2 server.
* @returns {AccessToken}
*/
function createAccessToken(token) {
return new AccessToken(token);
}
return {
create: createAccessToken,
};
};
@@ -0,0 +1,27 @@
'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;