UNPKG

@panoptic-it-solutions/n8n-nodes-datto-rmm

Version:

n8n node for Datto RMM integration

51 lines 1.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAccessToken = getAccessToken; async function getAccessToken(credentials) { var _a; if (((_a = credentials.tokenData) === null || _a === void 0 ? void 0 : _a.access_token) && credentials.tokenData.expires_at) { const now = Date.now(); const expiresAt = credentials.tokenData.expires_at; if (now < expiresAt - 300000) { return credentials.tokenData.access_token; } } const tokenUrl = `${credentials.apiUrl}/auth/oauth/token`; const clientCredentials = Buffer.from('public-client:public').toString('base64'); const tokenRequestOptions = { method: 'POST', url: tokenUrl, headers: { 'Content-Type': 'application/x-www-form-urlencoded', Authorization: `Basic ${clientCredentials}`, }, form: { grant_type: 'password', username: credentials.apiKey, password: credentials.apiSecret, }, }; try { const tokenResponse = await this.helpers.request(tokenRequestOptions); let parsedResponse = tokenResponse; if (typeof tokenResponse === 'string') { parsedResponse = JSON.parse(tokenResponse); } const accessToken = parsedResponse.access_token; const expiresIn = parsedResponse.expires_in || 360000; if (!accessToken) { throw new Error('No access token received from OAuth2 response'); } credentials.tokenData = { access_token: accessToken, token_type: parsedResponse.token_type || 'Bearer', expires_in: expiresIn, expires_at: Date.now() + expiresIn * 1000, }; return accessToken; } catch (error) { throw new Error(`OAuth2 authentication failed: ${error.message}`); } } //# sourceMappingURL=oauth2.helper.js.map