New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

netlify-graph-auth

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

netlify-graph-auth - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

45

dist/auth.d.ts
import type { Storage } from './storage';
declare type Timer = ReturnType<typeof setTimeout>;
export declare type NetlifyGraphAuthService = 'adroll' | 'asana' | 'box' | 'contentful' | 'dev-to' | 'dribbble' | 'dropbox' | 'eggheadio' | 'eventil' | 'facebook' | 'firebase' | 'github' | 'gmail' | 'google' | 'google-ads' | 'google-analytics' | 'google-calendar' | 'google-compute' | 'google-docs' | 'google-search-console' | 'google-translate' | 'hubspot' | 'intercom' | 'mailchimp' | 'meetup' | 'netlify' | 'product-hunt' | 'quickbooks' | 'salesforce' | 'slack' | 'spotify' | 'stripe' | 'trello' | 'twilio' | 'twitch-tv' | 'twitter' | 'ynab' | 'youtube' | 'zeit' | 'zendesk';
export declare type NetlifyGraphAuthStaticService = 'adroll' | 'asana' | 'box' | 'contentful' | 'dev-to' | 'dribbble' | 'dropbox' | 'eggheadio' | 'eventil' | 'facebook' | 'firebase' | 'github' | 'gmail' | 'google' | 'google-ads' | 'google-analytics' | 'google-calendar' | 'google-compute' | 'google-docs' | 'google-search-console' | 'google-translate' | 'hubspot' | 'intercom' | 'mailchimp' | 'meetup' | 'netlify' | 'product-hunt' | 'quickbooks' | 'salesforce' | 'slack' | 'spotify' | 'stripe' | 'trello' | 'twilio' | 'twitch-tv' | 'twitter' | 'ynab' | 'youtube' | 'zeit' | 'zendesk';
declare type StaticService = {
type: 'service';
service: NetlifyGraphAuthStaticService;
};
declare type ServiceByGQLField = {
type: 'gqlField';
graphQLField: string;
};
declare type Service = StaticService | ServiceByGQLField;
export declare type NetlifyGraphAuthService = NetlifyGraphAuthStaticService | {
service: NetlifyGraphAuthStaticService;
} | {
graphQLField: string;
};
declare type CommunicationMode = 'post_message' | 'redirect';

@@ -35,3 +49,2 @@ export declare type Opts = {

export declare type ServiceInfo = {
service: string;
serviceEnum: string;

@@ -43,7 +56,7 @@ friendlyServiceName: string;

export declare type ServicesStatus = {
[key in NetlifyGraphAuthService]?: ServiceStatus;
string?: ServiceStatus;
};
export declare type AuthResponse = {
token: Token;
service?: string;
service?: Service;
foreignUserId?: string;

@@ -54,3 +67,3 @@ };

scopes: Array<string> | undefined;
service: NetlifyGraphAuthService;
service: Service;
stateParam: string;

@@ -62,9 +75,9 @@ useTestFlow: boolean | undefined;

_authWindows: {
[key in NetlifyGraphAuthService]?: Window | null;
string?: Window | null;
};
_intervalIds: {
[key in NetlifyGraphAuthService]?: Timer;
string?: Timer;
};
_messageListeners: {
[key in NetlifyGraphAuthService]?: any;
string?: any;
};

@@ -80,8 +93,7 @@ _fetchUrl: string;

_communicationMode: CommunicationMode;
supportedServices: Array<string>;
constructor(opts: Opts);
_clearInterval: (service: NetlifyGraphAuthService) => void;
_clearMessageListener: (service: NetlifyGraphAuthService) => void;
closeAuthWindow: (service: NetlifyGraphAuthService) => void;
cleanup: (service: NetlifyGraphAuthService, keepWindowOpen?: boolean) => void;
_clearInterval: (service: string) => void;
_clearMessageListener: (service: string) => void;
closeAuthWindow: (service: string) => void;
cleanup: (service: string, keepWindowOpen?: boolean) => void;
accessToken: () => Token | null;

@@ -94,7 +106,7 @@ tokenExpireDate: () => Date | null;

};
friendlyServiceName(service: NetlifyGraphAuthService): string;
friendlyServiceName(service: Service): string;
_makeAuthUrl: (opts: _makeAuthUrlInput) => Promise<string>;
setToken: (token: Token) => void;
_waitForAuthFinishPostMessage: (service: NetlifyGraphAuthService, stateParam: StateParam, verifier: string) => Promise<AuthResponse>;
_waitForAuthFinishRedirect: (service: NetlifyGraphAuthService, stateParam: StateParam, verifier: string) => Promise<AuthResponse>;
_waitForAuthFinishPostMessage: (service: Service, stateParam: StateParam, verifier: string) => Promise<AuthResponse>;
_waitForAuthFinishRedirect: (service: Service, stateParam: StateParam, verifier: string) => Promise<AuthResponse>;
/**

@@ -108,3 +120,2 @@ * @throws {OAuthError}

}) => Promise<boolean>;
servicesStatus: () => Promise<ServicesStatus>;
allServices: () => Promise<ServicesList>;

@@ -111,0 +122,0 @@ loggedInServices: () => Promise<LoggedInServices>;

@@ -76,131 +76,151 @@ "use strict";

var helpers_1 = require("./helpers");
function getService(arg) {
if (typeof arg === 'string') {
return { type: 'service', service: arg };
}
else {
if ('graphQLField' in arg) {
return { type: 'gqlField', graphQLField: arg.graphQLField };
}
else if ('service' in arg) {
return { type: 'service', service: arg.service };
}
}
}
function serializeService(arg) {
var value = arg.type === 'service' ? arg.service : arg.graphQLField;
return arg.type + ':' + value;
}
var POLL_INTERVAL = 35;
var ALL_SERVICES = [
'adroll',
'asana',
'box',
'contentful',
'dev-to',
'dribbble',
'dropbox',
'eggheadio',
'eventil',
'facebook',
'firebase',
'github',
'gmail',
'google',
'google-ads',
'google-analytics',
'google-calendar',
'google-compute',
'google-docs',
'google-search-console',
'google-translate',
'hubspot',
'intercom',
'mailchimp',
'meetup',
'netlify',
'product-hunt',
'quickbooks',
'salesforce',
'slack',
'spotify',
'stripe',
'trello',
'twilio',
'twitch-tv',
'twitter',
'ynab',
'youtube',
'zeit',
'zendesk',
];
function friendlyServiceName(service) {
switch (service) {
case 'adroll':
return 'Adroll';
case 'asana':
return 'Asana';
case 'box':
return 'Box';
case 'dev-to':
return 'Dev.to';
case 'dribbble':
return 'Dribbble';
case 'dropbox':
return 'Dropbox';
case 'contentful':
return 'Contentful';
case 'eggheadio':
return 'Egghead.io';
case 'eventil':
return 'Eventil';
case 'facebook':
return 'Facebook';
case 'firebase':
return 'Firebase';
case 'github':
return 'GitHub';
case 'gmail':
return 'Gmail';
case 'google':
return 'Google';
case 'google-ads':
return 'Google Ads';
case 'google-analytics':
return 'Google Analytics';
case 'google-calendar':
return 'Google Calendar';
case 'google-compute':
return 'Google Compute';
case 'google-docs':
return 'Google Docs';
case 'google-search-console':
return 'Google Search Console';
case 'google-translate':
return 'Google Translate';
case 'hubspot':
return 'Hubspot';
case 'intercom':
return 'Intercom';
case 'mailchimp':
return 'Mailchimp';
case 'meetup':
return 'Meetup';
case 'netlify':
return 'Netlify';
case 'product-hunt':
return 'Product Hunt';
case 'quickbooks':
return 'QuickBooks';
case 'salesforce':
return 'Salesforce';
case 'slack':
return 'Slack';
case 'spotify':
return 'Spotify';
case 'stripe':
return 'Stripe';
case 'trello':
return 'Trello';
case 'twilio':
return 'Twilio';
case 'twitter':
return 'Twitter';
case 'twitch-tv':
return 'Twitch';
case 'ynab':
return 'You Need a Budget';
case 'youtube':
return 'YouTube';
case 'zeit':
return 'Vercel';
case 'zendesk':
return 'Zendesk';
default:
return service;
if (service.type === 'service') {
switch (service.service) {
case 'adroll':
return 'Adroll';
case 'asana':
return 'Asana';
case 'box':
return 'Box';
case 'dev-to':
return 'Dev.to';
case 'dribbble':
return 'Dribbble';
case 'dropbox':
return 'Dropbox';
case 'contentful':
return 'Contentful';
case 'eggheadio':
return 'Egghead.io';
case 'eventil':
return 'Eventil';
case 'facebook':
return 'Facebook';
case 'firebase':
return 'Firebase';
case 'github':
return 'GitHub';
case 'gmail':
return 'Gmail';
case 'google':
return 'Google';
case 'google-ads':
return 'Google Ads';
case 'google-analytics':
return 'Google Analytics';
case 'google-calendar':
return 'Google Calendar';
case 'google-compute':
return 'Google Compute';
case 'google-docs':
return 'Google Docs';
case 'google-search-console':
return 'Google Search Console';
case 'google-translate':
return 'Google Translate';
case 'hubspot':
return 'Hubspot';
case 'intercom':
return 'Intercom';
case 'mailchimp':
return 'Mailchimp';
case 'meetup':
return 'Meetup';
case 'netlify':
return 'Netlify';
case 'product-hunt':
return 'Product Hunt';
case 'quickbooks':
return 'QuickBooks';
case 'salesforce':
return 'Salesforce';
case 'slack':
return 'Slack';
case 'spotify':
return 'Spotify';
case 'stripe':
return 'Stripe';
case 'trello':
return 'Trello';
case 'twilio':
return 'Twilio';
case 'twitter':
return 'Twitter';
case 'twitch-tv':
return 'Twitch';
case 'ynab':
return 'You Need a Budget';
case 'youtube':
return 'YouTube';
case 'zeit':
return 'Vercel';
case 'zendesk':
return 'Zendesk';
default:
return service.service;
}
}
else {
return service.graphQLField;
}
}
function camelCase(s) {
return s.replace(/-./g, function (x) { return x[1].toUpperCase(); });
}
function getOAuthURLSegment(service) {
if (service.type === 'service') {
return service.service;
}
else {
switch (service.graphQLField) {
case 'gitHub':
return 'github';
case 'youTube':
return 'youtube';
case 'facebookBusiness':
return 'facebook';
case 'devTo':
return 'dev-to';
case 'googleAds':
return 'google-ads';
case 'googleAnalytics':
return 'google-analytics';
case 'googleCalendar':
return 'google-calendar';
case 'googleCompute':
return 'google-compute';
case 'googleDocs':
return 'google-docs';
case 'googleSearchConsole':
return 'google-search-console';
case 'googleTranslate':
return 'google-translate';
case 'productHunt':
return 'product-hunt';
case 'twitchTv':
return 'twitch-tv';
default:
return service.graphQLField;
}
}
}
function getWindowOpts() {

@@ -232,4 +252,4 @@ var windowWidth = Math.min(800, Math.floor(window.outerWidth * 0.8));

var w = window.open(url || '',
// A unqiue name prevents orphaned popups from stealing our window.open
(service + "_" + Math.random()).replace('.', ''), Object.keys(windowOpts)
// A unique name prevents orphaned popups from stealing our window.open
(getOAuthURLSegment(service) + "_" + Math.random()).replace('.', ''), Object.keys(windowOpts)
.map(function (k) { return k + "=" + windowOpts[k]; })

@@ -256,4 +276,4 @@ .join(','));

}
var loggedInQuery = "\nquery LoggedInQuery {\n me {\n serviceMetadata {\n loggedInServices {\n service\n foreignUserId\n usedTestFlow\n }\n }\n }\n}\n";
var allServicesQuery = "\nquery AllServicesQuery {\n oneGraph {\n services(filter: {supportsOauthLogin: true}) {\n service\n friendlyServiceName\n supportsTestFlow\n }\n }\n}\n";
var loggedInQuery = "\nquery LoggedInQuery {\n me {\n serviceMetadata {\n loggedInServices {\n id\n friendlyServiceName\n graphQLField\n foreignUserId\n usedTestFlow\n }\n }\n }\n}\n";
var allServicesQuery = "\nquery AllServicesQuery {\n oneGraph {\n services(filter: {supportsOauthLogin: true}) {\n id\n friendlyServiceName\n graphQLField\n supportsTestFlow\n }\n }\n}\n";
function getServiceEnum(service) {

@@ -267,7 +287,13 @@ return service.toUpperCase().replace(/-/g, '_');

var _a, _b, _c;
var serviceEnum = getServiceEnum(service);
var loggedInServices = ((_c = (_b = (_a = queryResult === null || queryResult === void 0 ? void 0 : queryResult.data) === null || _a === void 0 ? void 0 : _a.me) === null || _b === void 0 ? void 0 : _b.serviceMetadata) === null || _c === void 0 ? void 0 : _c.loggedInServices) || [];
return !!loggedInServices.find(function (serviceInfo) {
return serviceInfo.service === serviceEnum &&
(!foreignUserId || foreignUserId === serviceInfo.foreignUserId);
if (service.type === 'service') {
var serviceEnum = getServiceEnum(service.service);
return (serviceInfo.service === serviceEnum &&
(!foreignUserId || foreignUserId === serviceInfo.foreignUserId));
}
else {
return (serviceInfo.graphQLField === service.graphQLField &&
(!foreignUserId || foreignUserId === serviceInfo.foreignUserId));
}
});

@@ -278,4 +304,4 @@ }

}
var logoutMutation = "mutation SignOutServicesMutation(\n $services: [OneGraphServiceEnum!]!\n) {\n signoutServices(data: { services: $services }) {\n me {\n serviceMetadata {\n loggedInServices {\n service\n foreignUserId\n }\n }\n }\n }\n}";
var logoutUserMutation = "mutation SignOutServicesMutation(\n $service: OneGraphServiceEnum!\n $foreignUserId: String!\n) {\n signoutServiceUser(\n input: {\n service: $service\n foreignUserId: $foreignUserId\n }\n ) {\n me {\n serviceMetadata {\n loggedInServices {\n service\n foreignUserId\n }\n }\n }\n }\n}";
var logoutMutation = "mutation SignOutServicesMutation(\n $servicesGraphQLFields: [String!]\n $services: [OneGraphServiceEnum!]\n) {\n signoutServices(data: {\n $services: $services\n $servicesGraphQLFields: $servicesGraphQLFields\n }) {\n me {\n serviceMetadata {\n loggedInServices {\n id\n graphQLField\n friendlyServiceName\n foreignUserId\n }\n }\n }\n }\n}";
var logoutUserMutation = "mutation SignOutServicesMutation(\n $service: OneGraphServiceEnum\n $graphQLField: String\n $foreignUserId: String!\n) {\n signoutServiceUser(\n input: {\n service: $service\n graphQLField: $graphQLField\n foreignUserId: $foreignUserId\n }\n ) {\n me {\n serviceMetadata {\n loggedInServices {\n service\n foreignUserId\n }\n }\n }\n }\n}";
function fetchQuery(fetchUrl, query, variables, token) {

@@ -405,3 +431,2 @@ return __awaiter(this, void 0, void 0, function () {

this._accessToken = null;
this.supportedServices = ALL_SERVICES;
this._clearInterval = function (service) {

@@ -506,3 +531,3 @@ var intervalId = _this._intervalIds[service];

challenge = _a.sent();
query = __assign({ service: service, app_id: this.siteId, response_type: 'code', redirect_origin: this._redirectOrigin, redirect_path: this._redirectPath, communication_mode: this._communicationMode, code_challenge: challenge.challenge, code_challenge_method: challenge.method, state: stateParam }, (scopes ? { scopes: scopes.join(',') } : {}));
query = __assign({ service: getOAuthURLSegment(service), app_id: this.siteId, response_type: 'code', redirect_origin: this._redirectOrigin, redirect_path: this._redirectPath, communication_mode: this._communicationMode, code_challenge: challenge.challenge, code_challenge_method: challenge.method, state: stateParam }, (scopes ? { scopes: scopes.join(',') } : {}));
if (useTestFlow) {

@@ -526,2 +551,3 @@ query.test = 'true';

this._waitForAuthFinishPostMessage = function (service, stateParam, verifier) {
var serviceString = serializeService(service);
return new Promise(function (resolve, reject) {

@@ -569,3 +595,6 @@ function parseEvent(event) {

token: token,
service: successResponse.service,
service: {
type: 'gqlField',
graphQLField: successResponse.service_graphql_field,
},
foreignUserId: successResponse.foreign_user_id,

@@ -583,3 +612,3 @@ });

};
_this._messageListeners[service] = listener;
_this._messageListeners[serviceString] = listener;
window.addEventListener('message', listener, false);

@@ -590,5 +619,6 @@ });

return new Promise(function (resolve, reject) {
_this._intervalIds[service] = setInterval(function () {
var serviceString = serializeService(service);
_this._intervalIds[serviceString] = setInterval(function () {
try {
var authWindow = _this._authWindows[service];
var authWindow = _this._authWindows[serviceString];
var authUri = authWindow && uri_1.default.safeParse(authWindow.location.toString());

@@ -653,7 +683,8 @@ if (authUri && authUri.origin === _this._redirectOrigin) {

*/
this.login = function (service, scopes, useTestFlow) { return __awaiter(_this, void 0, void 0, function () {
var stateParam, verifier, authWindow, authFinish, windowUrl, url, result_3, e_1_1;
this.login = function (serviceInput, scopes, useTestFlow) { return __awaiter(_this, void 0, void 0, function () {
var service, serviceString, stateParam, verifier, authWindow, authFinish, windowUrl, url, result_3, e_1_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
service = getService(serviceInput);
if (!service) {

@@ -665,7 +696,8 @@ throw new oauthError_1.default({

}
this.cleanup(service);
serviceString = serializeService(service);
this.cleanup(serviceString);
stateParam = makeStateParam();
verifier = pkce_1.default.generateVerifier();
authWindow = createAuthWindow({ service: service });
this._authWindows[service] = authWindow;
this._authWindows[serviceString] = authWindow;
authFinish = this._communicationMode === 'redirect'

@@ -700,7 +732,7 @@ ? this._waitForAuthFinishRedirect

result_3 = _a.sent();
this.cleanup(service);
this.cleanup(serviceString);
return [2 /*return*/, result_3];
case 4:
e_1_1 = _a.sent();
this.cleanup(service, true);
this.cleanup(serviceString, true);
throw e_1_1;

@@ -712,3 +744,3 @@ case 5: return [2 /*return*/];

this.isLoggedIn = function (args) { return __awaiter(_this, void 0, void 0, function () {
var accessToken, service, foreignUserId, result;
var accessToken, serviceInput, service, foreignUserId, result;
return __generator(this, function (_a) {

@@ -719,7 +751,16 @@ switch (_a.label) {

if (!accessToken) return [3 /*break*/, 2];
service = typeof args === 'string' ? args : args.service;
serviceInput = typeof args === 'string'
? args
: 'service' in args
? args.service
: args;
service = getService(serviceInput);
if (!service) {
throw new Error("Missing required argument. Provide service as first argument to isLoggedIn (e.g. `auth.isLoggedIn('stripe')`).");
}
foreignUserId = typeof args === 'string' ? null : args.foreignUserId;
foreignUserId = typeof args === 'string'
? null
: 'foreignUserId' in args
? args.foreignUserId
: null;
return [4 /*yield*/, fetchQuery(this._fetchUrl, loggedInQuery, {}, accessToken)];

@@ -733,23 +774,2 @@ case 1:

}); };
this.servicesStatus = function () { return __awaiter(_this, void 0, void 0, function () {
var accessToken, result_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
accessToken = this._accessToken;
if (!accessToken) return [3 /*break*/, 2];
return [4 /*yield*/, fetchQuery(this._fetchUrl, loggedInQuery, {}, accessToken)];
case 1:
result_1 = _a.sent();
return [2 /*return*/, ALL_SERVICES.reduce(function (acc, service) {
acc[service] = { isLoggedIn: getIsLoggedIn(result_1, service) };
return acc;
}, {})];
case 2: return [2 /*return*/, Promise.resolve(ALL_SERVICES.reduce(function (acc, service) {
acc[service] = { isLoggedIn: false };
return acc;
}, {}))];
}
});
}); };
this.allServices = function () { return __awaiter(_this, void 0, void 0, function () {

@@ -798,4 +818,4 @@ var result;

}); };
this.logout = function (service, foreignUserId) { return __awaiter(_this, void 0, void 0, function () {
var accessToken, serviceEnum, signoutPromise, result, loggedIn;
this.logout = function (serviceInput, foreignUserId) { return __awaiter(_this, void 0, void 0, function () {
var service, serviceString, accessToken, signoutPromise, result, loggedIn;
var _a;

@@ -805,17 +825,21 @@ return __generator(this, function (_b) {

case 0:
service = getService(serviceInput);
if (!service) {
throw new Error("Missing required argument. Provide service as first argument to logout (e.g. `auth.logout('stripe')`).");
}
this.cleanup(service);
serviceString = serializeService(service);
this.cleanup(serviceString);
accessToken = this._accessToken;
if (!accessToken) return [3 /*break*/, 2];
serviceEnum = getServiceEnum(service);
signoutPromise = foreignUserId
? fetchQuery(this._fetchUrl, logoutUserMutation, {
service: serviceEnum,
? fetchQuery(this._fetchUrl, logoutUserMutation, Object.assign({
foreignUserId: foreignUserId,
}, accessToken)
: fetchQuery(this._fetchUrl, logoutMutation, {
services: [serviceEnum],
}, accessToken);
}, service.type === 'service'
? { service: service.service }
: { graphQLField: service.graphQLField }), accessToken)
: fetchQuery(this._fetchUrl, logoutMutation, service.type === 'service'
? {
services: [getServiceEnum(service.service)],
}
: { servicesGraphQLFields: service.graphQLField }, accessToken);
return [4 /*yield*/, signoutPromise];

@@ -825,3 +849,3 @@ case 1:

if (((_a = result.errors) === null || _a === void 0 ? void 0 : _a.length) &&
getServiceErrors(result.errors, serviceEnum).length) {
getServiceErrors(result.errors, service.type === 'service' ? service.service : service.graphQLField).length) {
return [2 /*return*/, { result: 'failure', errors: result.errors }];

@@ -840,8 +864,4 @@ }

this.destroy = function () {
Object.keys(_this._intervalIds).forEach(function (key) {
return _this.cleanup(key);
});
Object.keys(_this._authWindows).forEach(function (key) {
return _this.cleanup(key);
});
Object.keys(_this._intervalIds).forEach(function (key) { return _this.cleanup(key); });
Object.keys(_this._authWindows).forEach(function (key) { return _this.cleanup(key); });
_this._storage.removeItem(_this._storageKey);

@@ -848,0 +868,0 @@ _this._accessToken = null;

{
"name": "netlify-graph-auth",
"version": "0.0.3",
"version": "0.0.4",
"description": "Client-side authentication for Netlify GraphQL APIs",

@@ -14,3 +14,3 @@ "main": "dist/index.js",

"prepare": "npm run build",
"prettier": "prettier --write \"src/**/*.js\" \"src/**/*.ts\""
"prettier": "prettier --write \"src/**/*.ts\""
},

@@ -17,0 +17,0 @@ "keywords": [],

Sorry, the diff of this file is not supported yet

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