@randajan/oauth2-client
Version:
Lightweight Node.js helper that streamlines OAuth 2.0 and service-account authentication for all Google APIs, giving downstream packages hassle-free token acquisition and refresh
97 lines (93 loc) • 2.63 kB
JavaScript
import {
OAuth2Account,
OAuth2Client,
OAuth2Grant,
RedirectError,
vault
} from "../chunk-27ATWD3Y.js";
import {
extendURL,
validateStr
} from "../chunk-RGLWNWDM.js";
// src/facebook/FacebookAccount.js
var FacebookAccount = class extends OAuth2Account {
async profile(fields = ["id", "name", "email", "picture"]) {
const grant = vault.get(this.client);
const { access_token } = this.credentials;
return grant.fetchApi(`/me`, {
access_token,
fields: fields.join(",")
}, 3);
}
async tokens() {
return this.credentials;
}
async scopes() {
const grant = vault.get(this.client);
const { access_token } = this.credentials;
const { data } = await grant.fetchApi("/me/permisssions", {
access_token
}, 3);
return super.scopes(data.filter((p) => p.status === "granted").map((p) => p.permission));
}
};
// src/facebook/FacebookGrant.js
import fetch from "node-fetch";
var FacebookGrant = class extends OAuth2Grant {
static name = "facebook";
static uidKey = "id";
static scopePrefix = "";
static scopesCommon = [];
static scopesNoPrefix = [];
static Account = FacebookAccount;
constructor(client, opt = {}) {
super(client, opt);
this.apiVersion = validateStr(true, opt.apiVersion, "options.apiVersion");
}
createApiUrl(subdomain, path, query = {}) {
const { apiVersion } = this;
return extendURL(
`https://${subdomain}.facebook.com/${apiVersion}${path}`,
query
);
}
async fetchApi(path, query, errorCode = 2) {
const url = this.createApiUrl("graph", path, query);
const res = await fetch(url);
if (!res.ok) {
throw new RedirectError(errorCode, await res.text());
}
return res.json();
}
generateAuthUrl(scope, state, extra) {
const { clientId, redirectUri } = this;
return this.createApiUrl("www", `/dialog/oauth`, {
...extra,
response_type: "code",
client_id: clientId,
redirect_uri: redirectUri,
scope: scope.join(","),
// FB odděluje čárkou
state
});
}
async swapCodeForTokens(code) {
const { clientId, clientSecret, redirectUri } = this;
return this.fetchApi(`/oauth/access_token`, {
client_id: clientId,
client_secret: clientSecret,
redirect_uri: redirectUri,
code
});
}
};
// src/facebook/index.js
var createFacebookOAuth2 = (options = {}) => new OAuth2Client(FacebookGrant, options);
var facebook_default = createFacebookOAuth2;
export {
FacebookAccount,
FacebookGrant,
createFacebookOAuth2,
facebook_default as default
};
//# sourceMappingURL=index.js.map