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

gtoken

Package Overview
Dependencies
Maintainers
4
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gtoken - npm Package Compare versions

Comparing version 7.0.1 to 7.1.0

15

build/src/index.d.ts

@@ -38,2 +38,3 @@ /**

export declare class GoogleToken {
#private;
get accessToken(): string | undefined;

@@ -55,3 +56,2 @@ get idToken(): string | undefined;

transporter: Transporter;
private inFlightRequest?;
/**

@@ -88,5 +88,2 @@ * Create a GoogleToken.

getCredentials(keyFile: string): Promise<Credentials>;
private getTokenAsync;
private getTokenAsyncInner;
private ensureEmail;
/**

@@ -99,12 +96,2 @@ * Revoke the token if one is set.

revokeToken(callback: (err?: Error) => void): void;
private revokeTokenAsync;
/**
* Configure the GoogleToken for re-use.
* @param {object} options Configuration object.
*/
private configure;
/**
* Request the token from Google.
*/
private requestToken;
}

260

build/src/index.js

@@ -8,2 +8,14 @@ "use strict";

*/
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _GoogleToken_instances, _GoogleToken_inFlightRequest, _GoogleToken_getTokenAsync, _GoogleToken_getTokenAsyncInner, _GoogleToken_ensureEmail, _GoogleToken_revokeTokenAsync, _GoogleToken_configure, _GoogleToken_requestToken;
Object.defineProperty(exports, "__esModule", { value: true });

@@ -49,6 +61,8 @@ exports.GoogleToken = void 0;

constructor(options) {
_GoogleToken_instances.add(this);
this.transporter = {
request: opts => (0, gaxios_1.request)(opts),
};
this.configure(options);
_GoogleToken_inFlightRequest.set(this, void 0);
__classPrivateFieldGet(this, _GoogleToken_instances, "m", _GoogleToken_configure).call(this, options);
}

@@ -95,6 +109,6 @@ /**

const cb = callback;
this.getTokenAsync(opts).then(t => cb(null, t), callback);
__classPrivateFieldGet(this, _GoogleToken_instances, "m", _GoogleToken_getTokenAsync).call(this, opts).then(t => cb(null, t), callback);
return;
}
return this.getTokenAsync(opts);
return __classPrivateFieldGet(this, _GoogleToken_instances, "m", _GoogleToken_getTokenAsync).call(this, opts);
}

@@ -135,134 +149,132 @@ /**

}
async getTokenAsync(opts) {
if (this.inFlightRequest && !opts.forceRefresh) {
return this.inFlightRequest;
revokeToken(callback) {
if (callback) {
__classPrivateFieldGet(this, _GoogleToken_instances, "m", _GoogleToken_revokeTokenAsync).call(this).then(() => callback(), callback);
return;
}
try {
return await (this.inFlightRequest = this.getTokenAsyncInner(opts));
}
finally {
this.inFlightRequest = undefined;
}
return __classPrivateFieldGet(this, _GoogleToken_instances, "m", _GoogleToken_revokeTokenAsync).call(this);
}
async getTokenAsyncInner(opts) {
if (this.isTokenExpiring() === false && opts.forceRefresh === false) {
return Promise.resolve(this.rawToken);
}
if (!this.key && !this.keyFile) {
throw new Error('No key or keyFile set.');
}
if (!this.key && this.keyFile) {
const creds = await this.getCredentials(this.keyFile);
this.key = creds.privateKey;
this.iss = creds.clientEmail || this.iss;
if (!creds.clientEmail) {
this.ensureEmail();
}
}
return this.requestToken();
}
exports.GoogleToken = GoogleToken;
_GoogleToken_inFlightRequest = new WeakMap(), _GoogleToken_instances = new WeakSet(), _GoogleToken_getTokenAsync = async function _GoogleToken_getTokenAsync(opts) {
if (__classPrivateFieldGet(this, _GoogleToken_inFlightRequest, "f") && !opts.forceRefresh) {
return __classPrivateFieldGet(this, _GoogleToken_inFlightRequest, "f");
}
ensureEmail() {
if (!this.iss) {
throw new ErrorWithCode('email is required.', 'MISSING_CREDENTIALS');
}
try {
return await (__classPrivateFieldSet(this, _GoogleToken_inFlightRequest, __classPrivateFieldGet(this, _GoogleToken_instances, "m", _GoogleToken_getTokenAsyncInner).call(this, opts), "f"));
}
revokeToken(callback) {
if (callback) {
this.revokeTokenAsync().then(() => callback(), callback);
return;
finally {
__classPrivateFieldSet(this, _GoogleToken_inFlightRequest, undefined, "f");
}
}, _GoogleToken_getTokenAsyncInner = async function _GoogleToken_getTokenAsyncInner(opts) {
if (this.isTokenExpiring() === false && opts.forceRefresh === false) {
return Promise.resolve(this.rawToken);
}
if (!this.key && !this.keyFile) {
throw new Error('No key or keyFile set.');
}
if (!this.key && this.keyFile) {
const creds = await this.getCredentials(this.keyFile);
this.key = creds.privateKey;
this.iss = creds.clientEmail || this.iss;
if (!creds.clientEmail) {
__classPrivateFieldGet(this, _GoogleToken_instances, "m", _GoogleToken_ensureEmail).call(this);
}
return this.revokeTokenAsync();
}
async revokeTokenAsync() {
if (!this.accessToken) {
throw new Error('No token to revoke.');
}
const url = GOOGLE_REVOKE_TOKEN_URL + this.accessToken;
await this.transporter.request({ url });
this.configure({
email: this.iss,
sub: this.sub,
key: this.key,
keyFile: this.keyFile,
scope: this.scope,
additionalClaims: this.additionalClaims,
return __classPrivateFieldGet(this, _GoogleToken_instances, "m", _GoogleToken_requestToken).call(this);
}, _GoogleToken_ensureEmail = function _GoogleToken_ensureEmail() {
if (!this.iss) {
throw new ErrorWithCode('email is required.', 'MISSING_CREDENTIALS');
}
}, _GoogleToken_revokeTokenAsync = async function _GoogleToken_revokeTokenAsync() {
if (!this.accessToken) {
throw new Error('No token to revoke.');
}
const url = GOOGLE_REVOKE_TOKEN_URL + this.accessToken;
await this.transporter.request({
url,
retry: true,
});
__classPrivateFieldGet(this, _GoogleToken_instances, "m", _GoogleToken_configure).call(this, {
email: this.iss,
sub: this.sub,
key: this.key,
keyFile: this.keyFile,
scope: this.scope,
additionalClaims: this.additionalClaims,
});
}, _GoogleToken_configure = function _GoogleToken_configure(options = {}) {
this.keyFile = options.keyFile;
this.key = options.key;
this.rawToken = undefined;
this.iss = options.email || options.iss;
this.sub = options.sub;
this.additionalClaims = options.additionalClaims;
if (typeof options.scope === 'object') {
this.scope = options.scope.join(' ');
}
else {
this.scope = options.scope;
}
this.eagerRefreshThresholdMillis = options.eagerRefreshThresholdMillis;
if (options.transporter) {
this.transporter = options.transporter;
}
}, _GoogleToken_requestToken =
/**
* Request the token from Google.
*/
async function _GoogleToken_requestToken() {
var _a, _b;
const iat = Math.floor(new Date().getTime() / 1000);
const additionalClaims = this.additionalClaims || {};
const payload = Object.assign({
iss: this.iss,
scope: this.scope,
aud: GOOGLE_TOKEN_URL,
exp: iat + 3600,
iat,
sub: this.sub,
}, additionalClaims);
const signedJWT = jws.sign({
header: { alg: 'RS256' },
payload,
secret: this.key,
});
try {
const r = await this.transporter.request({
method: 'POST',
url: GOOGLE_TOKEN_URL,
data: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: signedJWT,
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
responseType: 'json',
retryConfig: {
httpMethodsToRetry: ['POST'],
},
});
this.rawToken = r.data;
this.expiresAt =
r.data.expires_in === null || r.data.expires_in === undefined
? undefined
: (iat + r.data.expires_in) * 1000;
return this.rawToken;
}
/**
* Configure the GoogleToken for re-use.
* @param {object} options Configuration object.
*/
configure(options = {}) {
this.keyFile = options.keyFile;
this.key = options.key;
catch (e) {
this.rawToken = undefined;
this.iss = options.email || options.iss;
this.sub = options.sub;
this.additionalClaims = options.additionalClaims;
if (typeof options.scope === 'object') {
this.scope = options.scope.join(' ');
this.tokenExpires = undefined;
const body = e.response && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data)
? (_b = e.response) === null || _b === void 0 ? void 0 : _b.data
: {};
if (body.error) {
const desc = body.error_description
? `: ${body.error_description}`
: '';
e.message = `${body.error}${desc}`;
}
else {
this.scope = options.scope;
}
this.eagerRefreshThresholdMillis = options.eagerRefreshThresholdMillis;
if (options.transporter) {
this.transporter = options.transporter;
}
throw e;
}
/**
* Request the token from Google.
*/
async requestToken() {
var _a, _b;
const iat = Math.floor(new Date().getTime() / 1000);
const additionalClaims = this.additionalClaims || {};
const payload = Object.assign({
iss: this.iss,
scope: this.scope,
aud: GOOGLE_TOKEN_URL,
exp: iat + 3600,
iat,
sub: this.sub,
}, additionalClaims);
const signedJWT = jws.sign({
header: { alg: 'RS256' },
payload,
secret: this.key,
});
try {
const r = await this.transporter.request({
method: 'POST',
url: GOOGLE_TOKEN_URL,
data: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: signedJWT,
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
responseType: 'json',
});
this.rawToken = r.data;
this.expiresAt =
r.data.expires_in === null || r.data.expires_in === undefined
? undefined
: (iat + r.data.expires_in) * 1000;
return this.rawToken;
}
catch (e) {
this.rawToken = undefined;
this.tokenExpires = undefined;
const body = e.response && ((_a = e.response) === null || _a === void 0 ? void 0 : _a.data)
? (_b = e.response) === null || _b === void 0 ? void 0 : _b.data
: {};
if (body.error) {
const desc = body.error_description
? `: ${body.error_description}`
: '';
e.message = `${body.error}${desc}`;
}
throw e;
}
}
}
exports.GoogleToken = GoogleToken;
};
//# sourceMappingURL=index.js.map

@@ -7,2 +7,9 @@ # Changelog

## [7.1.0](https://github.com/googleapis/node-gtoken/compare/v7.0.1...v7.1.0) (2024-02-01)
### Features
* Enable Token Retries ([#481](https://github.com/googleapis/node-gtoken/issues/481)) ([ed9f91e](https://github.com/googleapis/node-gtoken/commit/ed9f91e4764744426de95fd2510b68ee53677514))
## [7.0.1](https://github.com/googleapis/node-gtoken/compare/v7.0.0...v7.0.1) (2023-07-12)

@@ -9,0 +16,0 @@

{
"name": "gtoken",
"version": "7.0.1",
"version": "7.1.0",
"description": "Node.js Google Authentication Service Account Tokens",

@@ -50,5 +50,5 @@ "main": "./build/src/index.js",

"@types/mocha": "^9.0.0",
"@types/node": "^18.0.0",
"c8": "^8.0.0",
"gts": "^3.1.1",
"@types/node": "^20.0.0",
"c8": "^9.0.0",
"gts": "^5.0.0",
"linkinator": "^4.0.0",

@@ -55,0 +55,0 @@ "mocha": "^9.2.2",

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