@apimatic/oauth-adapters
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -1,1 +0,1 @@ | ||
export { requestAuthenticationProvider } from './oauthAuthenticationAdapter.js'; | ||
export { isExpired, isValid, requestAuthenticationProvider } from './oauthAuthenticationAdapter.js'; |
import { __awaiter, __generator } from 'tslib'; | ||
import { passThroughInterceptor } from '@apimatic/core-interfaces'; | ||
import { setHeader, AUTHORIZATION_HEADER } from '@apimatic/http-headers'; | ||
var requestAuthenticationProvider = function (initialOAuthToken, oAuthTokenProvider, oAuthOnTokenUpdate) { | ||
var requestAuthenticationProvider = function (initialOAuthToken, oAuthTokenProvider, oAuthOnTokenUpdate, oAuthConfiguration) { | ||
// This token is shared between all API calls for a client instance. | ||
@@ -20,3 +20,3 @@ var lastOAuthToken = Promise.resolve(initialOAuthToken); | ||
oAuthToken = _a.sent(); | ||
if (!(oAuthTokenProvider && (!isValid(oAuthToken) || isExpired(oAuthToken)))) return [3 /*break*/, 3]; | ||
if (!(oAuthTokenProvider && (!isValid(oAuthToken) || isExpired(oAuthToken, oAuthConfiguration === null || oAuthConfiguration === void 0 ? void 0 : oAuthConfiguration.clockSkew)))) return [3 /*break*/, 3]; | ||
// Set the shared token for the next API calls to use. | ||
@@ -32,3 +32,3 @@ lastOAuthToken = oAuthTokenProvider(oAuthToken); | ||
case 3: | ||
setOAuthTokenInRequest(oAuthToken, request); | ||
setOAuthTokenInRequest(oAuthToken, request, oAuthConfiguration === null || oAuthConfiguration === void 0 ? void 0 : oAuthConfiguration.clockSkew); | ||
return [2 /*return*/, next(request, options)]; | ||
@@ -41,13 +41,13 @@ } | ||
}; | ||
function setOAuthTokenInRequest(oAuthToken, request) { | ||
function setOAuthTokenInRequest(oAuthToken, request, clockSkew) { | ||
var _a; | ||
validateAuthorization(oAuthToken); | ||
validateAuthorization(oAuthToken, clockSkew); | ||
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) { | ||
function validateAuthorization(oAuthToken, clockSkew) { | ||
if (!isValid(oAuthToken)) { | ||
throw new Error('Client is not authorized. An OAuth token is needed to make API calls.'); | ||
} | ||
if (isExpired(oAuthToken)) { | ||
if (isExpired(oAuthToken, clockSkew)) { | ||
throw new Error('OAuth token is expired. A valid token is needed to make API calls.'); | ||
@@ -59,5 +59,15 @@ } | ||
} | ||
function isExpired(oAuthToken) { | ||
return typeof oAuthToken.expiry !== 'undefined' && oAuthToken.expiry < Date.now() / 1000; | ||
function isExpired(oAuthToken, clockSkew) { | ||
if (typeof oAuthToken.expiry === 'undefined') { | ||
return false; // Expiry is undefined, token cannot be expired | ||
} | ||
var tokenExpiry = oAuthToken.expiry; | ||
// Adjust expiration time if clockSkew is provided and is not undefined | ||
if (clockSkew && typeof clockSkew !== 'undefined') { | ||
tokenExpiry -= BigInt(clockSkew); // Subtract clockSkew from expiry | ||
} | ||
return tokenExpiry < Date.now() / 1000; | ||
} | ||
export { requestAuthenticationProvider }; | ||
export { isExpired, isValid, requestAuthenticationProvider }; |
export * from './oauthAuthenticationAdapter'; | ||
export * from './oAuthConfiguration'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -5,1 +5,2 @@ "use strict"; | ||
tslib_1.__exportStar(require("./oauthAuthenticationAdapter"), exports); | ||
tslib_1.__exportStar(require("./oAuthConfiguration"), exports); |
import { OAuthToken } from './oAuthToken'; | ||
import { AuthenticatorInterface } from '@apimatic/core-interfaces'; | ||
export declare const requestAuthenticationProvider: (initialOAuthToken?: OAuthToken | undefined, oAuthTokenProvider?: ((token: OAuthToken | undefined) => Promise<OAuthToken>) | undefined, oAuthOnTokenUpdate?: ((token: OAuthToken) => void) | undefined) => AuthenticatorInterface<boolean>; | ||
import { OAuthConfiguration } from './oAuthConfiguration'; | ||
export declare const requestAuthenticationProvider: (initialOAuthToken?: OAuthToken | undefined, oAuthTokenProvider?: ((token: OAuthToken | undefined) => Promise<OAuthToken>) | undefined, oAuthOnTokenUpdate?: ((token: OAuthToken) => void) | undefined, oAuthConfiguration?: OAuthConfiguration | undefined) => AuthenticatorInterface<boolean>; | ||
export declare function isValid(oAuthToken: OAuthToken | undefined): oAuthToken is OAuthToken; | ||
export declare function isExpired(oAuthToken: OAuthToken, clockSkew?: number): boolean; | ||
//# sourceMappingURL=oauthAuthenticationAdapter.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.requestAuthenticationProvider = void 0; | ||
exports.isExpired = exports.isValid = 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 (initialOAuthToken, oAuthTokenProvider, oAuthOnTokenUpdate) { | ||
var requestAuthenticationProvider = function (initialOAuthToken, oAuthTokenProvider, oAuthOnTokenUpdate, oAuthConfiguration) { | ||
// This token is shared between all API calls for a client instance. | ||
@@ -22,3 +22,4 @@ var lastOAuthToken = Promise.resolve(initialOAuthToken); | ||
if (!(oAuthTokenProvider && | ||
(!isValid(oAuthToken) || isExpired(oAuthToken)))) return [3 /*break*/, 3]; | ||
(!isValid(oAuthToken) || | ||
isExpired(oAuthToken, oAuthConfiguration === null || oAuthConfiguration === void 0 ? void 0 : oAuthConfiguration.clockSkew)))) return [3 /*break*/, 3]; | ||
// Set the shared token for the next API calls to use. | ||
@@ -34,3 +35,3 @@ lastOAuthToken = oAuthTokenProvider(oAuthToken); | ||
case 3: | ||
setOAuthTokenInRequest(oAuthToken, request); | ||
setOAuthTokenInRequest(oAuthToken, request, oAuthConfiguration === null || oAuthConfiguration === void 0 ? void 0 : oAuthConfiguration.clockSkew); | ||
return [2 /*return*/, next(request, options)]; | ||
@@ -43,13 +44,13 @@ } | ||
exports.requestAuthenticationProvider = requestAuthenticationProvider; | ||
function setOAuthTokenInRequest(oAuthToken, request) { | ||
function setOAuthTokenInRequest(oAuthToken, request, clockSkew) { | ||
var _a; | ||
validateAuthorization(oAuthToken); | ||
validateAuthorization(oAuthToken, clockSkew); | ||
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) { | ||
function validateAuthorization(oAuthToken, clockSkew) { | ||
if (!isValid(oAuthToken)) { | ||
throw new Error('Client is not authorized. An OAuth token is needed to make API calls.'); | ||
} | ||
if (isExpired(oAuthToken)) { | ||
if (isExpired(oAuthToken, clockSkew)) { | ||
throw new Error('OAuth token is expired. A valid token is needed to make API calls.'); | ||
@@ -61,5 +62,14 @@ } | ||
} | ||
function isExpired(oAuthToken) { | ||
return (typeof oAuthToken.expiry !== 'undefined' && | ||
oAuthToken.expiry < Date.now() / 1000); | ||
exports.isValid = isValid; | ||
function isExpired(oAuthToken, clockSkew) { | ||
if (typeof oAuthToken.expiry === 'undefined') { | ||
return false; // Expiry is undefined, token cannot be expired | ||
} | ||
var tokenExpiry = oAuthToken.expiry; | ||
// Adjust expiration time if clockSkew is provided and is not undefined | ||
if (clockSkew && typeof clockSkew !== 'undefined') { | ||
tokenExpiry -= BigInt(clockSkew); // Subtract clockSkew from expiry | ||
} | ||
return tokenExpiry < Date.now() / 1000; | ||
} | ||
exports.isExpired = isExpired; |
{ | ||
"name": "@apimatic/oauth-adapters", | ||
"author": "APIMatic Ltd.", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"license": "MIT", | ||
@@ -72,3 +72,3 @@ "sideEffects": false, | ||
}, | ||
"gitHead": "43e7ca2560f262b85c1c6d15f5f01d00c2b43b37" | ||
"gitHead": "8425ef7e44f774fe08e89bf636ed048766d047c5" | ||
} |
export * from './oauthAuthenticationAdapter'; | ||
export * from './oAuthConfiguration'; |
@@ -7,2 +7,3 @@ import { OAuthToken } from './oAuthToken'; | ||
import { AUTHORIZATION_HEADER, setHeader } from '@apimatic/http-headers'; | ||
import { OAuthConfiguration } from './oAuthConfiguration'; | ||
@@ -12,3 +13,4 @@ export const requestAuthenticationProvider = ( | ||
oAuthTokenProvider?: (token: OAuthToken | undefined) => Promise<OAuthToken>, | ||
oAuthOnTokenUpdate?: (token: OAuthToken) => void | ||
oAuthOnTokenUpdate?: (token: OAuthToken) => void, | ||
oAuthConfiguration?: OAuthConfiguration | ||
): AuthenticatorInterface<boolean> => { | ||
@@ -29,3 +31,4 @@ // This token is shared between all API calls for a client instance. | ||
oAuthTokenProvider && | ||
(!isValid(oAuthToken) || isExpired(oAuthToken)) | ||
(!isValid(oAuthToken) || | ||
isExpired(oAuthToken, oAuthConfiguration?.clockSkew)) | ||
) { | ||
@@ -39,3 +42,7 @@ // Set the shared token for the next API calls to use. | ||
} | ||
setOAuthTokenInRequest(oAuthToken, request); | ||
setOAuthTokenInRequest( | ||
oAuthToken, | ||
request, | ||
oAuthConfiguration?.clockSkew | ||
); | ||
return next(request, options); | ||
@@ -48,5 +55,6 @@ }; | ||
oAuthToken: OAuthToken | undefined, | ||
request: any | ||
request: any, | ||
clockSkew?: number | ||
) { | ||
validateAuthorization(oAuthToken); | ||
validateAuthorization(oAuthToken, clockSkew); | ||
request.headers = request.headers ?? {}; | ||
@@ -60,3 +68,3 @@ setHeader( | ||
function validateAuthorization(oAuthToken?: OAuthToken) { | ||
function validateAuthorization(oAuthToken?: OAuthToken, clockSkew?: number) { | ||
if (!isValid(oAuthToken)) { | ||
@@ -68,3 +76,3 @@ throw new Error( | ||
if (isExpired(oAuthToken)) { | ||
if (isExpired(oAuthToken, clockSkew)) { | ||
throw new Error( | ||
@@ -76,11 +84,21 @@ 'OAuth token is expired. A valid token is needed to make API calls.' | ||
function isValid(oAuthToken: OAuthToken | undefined): oAuthToken is OAuthToken { | ||
export function isValid( | ||
oAuthToken: OAuthToken | undefined | ||
): oAuthToken is OAuthToken { | ||
return typeof oAuthToken !== 'undefined'; | ||
} | ||
function isExpired(oAuthToken: OAuthToken) { | ||
return ( | ||
typeof oAuthToken.expiry !== 'undefined' && | ||
oAuthToken.expiry < Date.now() / 1000 | ||
); | ||
export function isExpired(oAuthToken: OAuthToken, clockSkew?: number) { | ||
if (typeof oAuthToken.expiry === 'undefined') { | ||
return false; // Expiry is undefined, token cannot be expired | ||
} | ||
let tokenExpiry = oAuthToken.expiry; | ||
// Adjust expiration time if clockSkew is provided and is not undefined | ||
if (clockSkew && typeof clockSkew !== 'undefined') { | ||
tokenExpiry -= BigInt(clockSkew); // Subtract clockSkew from expiry | ||
} | ||
return tokenExpiry < Date.now() / 1000; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
16426
16
313
0