Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@apimatic/oauth-adapters

Package Overview
Dependencies
Maintainers
4
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@apimatic/oauth-adapters - npm Package Compare versions

Comparing version 0.4.0 to 0.4.1

47

es/oauthAuthenticationAdapter.js

@@ -0,4 +1,7 @@

import { __awaiter, __generator } from 'tslib';
import { passThroughInterceptor } from '@apimatic/core-interfaces';
import { setHeader, AUTHORIZATION_HEADER } from '@apimatic/http-headers';
var requestAuthenticationProvider = function (oAuthToken) {
var requestAuthenticationProvider = function (initialOAuthToken, oAuthTokenProvider, oAuthOnTokenUpdate) {
// This token is shared between all API calls for a client instance.
var lastOAuthToken = Promise.resolve(initialOAuthToken);
return function (requiresAuth) {

@@ -8,6 +11,36 @@ if (!requiresAuth) {

}
validateAuthorization(oAuthToken);
return createRequestInterceptor(oAuthToken);
return function (request, options, next) {
return __awaiter(void 0, void 0, void 0, function () {
var oAuthToken;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
return [4 /*yield*/, lastOAuthToken];
case 1:
oAuthToken = _a.sent();
if (!(oAuthTokenProvider && (!isValid(oAuthToken) || isExpired(oAuthToken)))) return [3 /*break*/, 3];
// Set the shared token for the next API calls to use.
lastOAuthToken = oAuthTokenProvider(oAuthToken);
return [4 /*yield*/, lastOAuthToken];
case 2:
oAuthToken = _a.sent();
if (oAuthOnTokenUpdate && oAuthToken) {
oAuthOnTokenUpdate(oAuthToken);
}
_a.label = 3;
case 3:
setOAuthTokenInRequest(oAuthToken, request);
return [2 /*return*/, next(request, options)];
}
});
});
};
};
};
function setOAuthTokenInRequest(oAuthToken, request) {
var _a;
validateAuthorization(oAuthToken);
request.headers = (_a = request.headers) !== null && _a !== void 0 ? _a : {};
setHeader(request.headers, AUTHORIZATION_HEADER, "Bearer " + (oAuthToken === null || oAuthToken === void 0 ? void 0 : oAuthToken.accessToken));
}
function validateAuthorization(oAuthToken) {

@@ -21,10 +54,2 @@ if (!isValid(oAuthToken)) {

}
function createRequestInterceptor(oAuthToken) {
return function (request, options, next) {
var _a;
request.headers = (_a = request.headers) !== null && _a !== void 0 ? _a : {};
setHeader(request.headers, AUTHORIZATION_HEADER, "Bearer " + (oAuthToken === null || oAuthToken === void 0 ? void 0 : oAuthToken.accessToken));
return next(request, options);
};
}
function isValid(oAuthToken) {

@@ -31,0 +56,0 @@ return typeof oAuthToken !== 'undefined';

import { OAuthToken } from './oAuthToken';
import { AuthenticatorInterface } from '@apimatic/core-interfaces';
export declare const requestAuthenticationProvider: (oAuthToken?: OAuthToken | undefined) => AuthenticatorInterface<boolean>;
export declare const requestAuthenticationProvider: (initialOAuthToken?: OAuthToken | undefined, oAuthTokenProvider?: ((token: OAuthToken | undefined) => Promise<OAuthToken>) | undefined, oAuthOnTokenUpdate?: ((token: OAuthToken) => void) | undefined) => AuthenticatorInterface<boolean>;
//# sourceMappingURL=oauthAuthenticationAdapter.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestAuthenticationProvider = void 0;
var tslib_1 = require("tslib");
var core_interfaces_1 = require("@apimatic/core-interfaces");
var http_headers_1 = require("@apimatic/http-headers");
var requestAuthenticationProvider = function (oAuthToken) {
var requestAuthenticationProvider = function (initialOAuthToken, oAuthTokenProvider, oAuthOnTokenUpdate) {
// This token is shared between all API calls for a client instance.
var lastOAuthToken = Promise.resolve(initialOAuthToken);
return function (requiresAuth) {

@@ -11,7 +14,35 @@ if (!requiresAuth) {

}
validateAuthorization(oAuthToken);
return createRequestInterceptor(oAuthToken);
return function (request, options, next) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var oAuthToken;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, lastOAuthToken];
case 1:
oAuthToken = _a.sent();
if (!(oAuthTokenProvider &&
(!isValid(oAuthToken) || isExpired(oAuthToken)))) return [3 /*break*/, 3];
// Set the shared token for the next API calls to use.
lastOAuthToken = oAuthTokenProvider(oAuthToken);
return [4 /*yield*/, lastOAuthToken];
case 2:
oAuthToken = _a.sent();
if (oAuthOnTokenUpdate && oAuthToken) {
oAuthOnTokenUpdate(oAuthToken);
}
_a.label = 3;
case 3:
setOAuthTokenInRequest(oAuthToken, request);
return [2 /*return*/, next(request, options)];
}
});
}); };
};
};
exports.requestAuthenticationProvider = requestAuthenticationProvider;
function setOAuthTokenInRequest(oAuthToken, request) {
var _a;
validateAuthorization(oAuthToken);
request.headers = (_a = request.headers) !== null && _a !== void 0 ? _a : {};
http_headers_1.setHeader(request.headers, http_headers_1.AUTHORIZATION_HEADER, "Bearer " + (oAuthToken === null || oAuthToken === void 0 ? void 0 : oAuthToken.accessToken));
}
function validateAuthorization(oAuthToken) {

@@ -25,10 +56,2 @@ if (!isValid(oAuthToken)) {

}
function createRequestInterceptor(oAuthToken) {
return function (request, options, next) {
var _a;
request.headers = (_a = request.headers) !== null && _a !== void 0 ? _a : {};
http_headers_1.setHeader(request.headers, http_headers_1.AUTHORIZATION_HEADER, "Bearer " + (oAuthToken === null || oAuthToken === void 0 ? void 0 : oAuthToken.accessToken));
return next(request, options);
};
}
function isValid(oAuthToken) {

@@ -35,0 +58,0 @@ return typeof oAuthToken !== 'undefined';

{
"name": "@apimatic/oauth-adapters",
"author": "APIMatic Ltd.",
"version": "0.4.0",
"version": "0.4.1",
"license": "MIT",

@@ -6,0 +6,0 @@ "sideEffects": false,

export * from './oauthAuthenticationAdapter';

@@ -9,4 +9,11 @@ import { OAuthToken } from './oAuthToken';

export const requestAuthenticationProvider = (
oAuthToken?: OAuthToken
initialOAuthToken?: OAuthToken,
oAuthTokenProvider?: (token: OAuthToken | undefined) => Promise<OAuthToken>,
oAuthOnTokenUpdate?: (token: OAuthToken) => void
): AuthenticatorInterface<boolean> => {
// This token is shared between all API calls for a client instance.
let lastOAuthToken: Promise<OAuthToken | undefined> = Promise.resolve(
initialOAuthToken
);
return (requiresAuth?: boolean) => {

@@ -17,7 +24,34 @@ if (!requiresAuth) {

validateAuthorization(oAuthToken);
return createRequestInterceptor(oAuthToken);
return async (request, options, next) => {
let oAuthToken = await lastOAuthToken;
if (
oAuthTokenProvider &&
(!isValid(oAuthToken) || isExpired(oAuthToken))
) {
// Set the shared token for the next API calls to use.
lastOAuthToken = oAuthTokenProvider(oAuthToken);
oAuthToken = await lastOAuthToken;
if (oAuthOnTokenUpdate && oAuthToken) {
oAuthOnTokenUpdate(oAuthToken);
}
}
setOAuthTokenInRequest(oAuthToken, request);
return next(request, options);
};
};
};
function setOAuthTokenInRequest(
oAuthToken: OAuthToken | undefined,
request: any
) {
validateAuthorization(oAuthToken);
request.headers = request.headers ?? {};
setHeader(
request.headers,
AUTHORIZATION_HEADER,
`Bearer ${oAuthToken?.accessToken}`
);
}
function validateAuthorization(oAuthToken?: OAuthToken) {

@@ -37,15 +71,2 @@ if (!isValid(oAuthToken)) {

function createRequestInterceptor(oAuthToken?: OAuthToken) {
return (request: any, options: any, next: any) => {
request.headers = request.headers ?? {};
setHeader(
request.headers,
AUTHORIZATION_HEADER,
`Bearer ${oAuthToken?.accessToken}`
);
return next(request, options);
};
}
function isValid(oAuthToken: OAuthToken | undefined): oAuthToken is OAuthToken {

@@ -52,0 +73,0 @@ return typeof oAuthToken !== 'undefined';

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc