@design-sdk/figma-oauth
Version:
OAuth management for figma
201 lines (192 loc) • 4.53 kB
JavaScript
;
var Axios = require('axios');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var Axios__default = /*#__PURE__*/_interopDefault(Axios);
// lib/request.ts
// lib/urls.ts
function oauthBrowserUrl({
client_id,
redirect_uri,
state,
scope = "file_read",
response_type = "code"
}) {
const params = {
client_id,
redirect_uri,
scope,
state,
response_type
};
const _q_str = new URLSearchParams(params).toString();
return `https://www.figma.com/oauth?${_q_str}`;
}
function oauthTokenRequestUrl({
client_id,
client_secret,
grant_type = "authorization_code",
redirect_uri,
code
}) {
const params = {
client_id,
client_secret,
grant_type,
redirect_uri,
code
};
const _q_str = new URLSearchParams(params).toString();
return `https://www.figma.com/api/oauth/token?${_q_str}`;
}
function oauthTokenRefreshUrl({
client_id,
client_secret,
refresh_token
}) {
const params = {
client_id,
client_secret,
refresh_token
};
const _q_str = new URLSearchParams(params).toString();
return `https://www.figma.com/api/oauth/refresh?${_q_str}`;
}
var urls = {
oauth_token_request_url: oauthTokenRequestUrl,
oauth_token_refresh_url: oauthTokenRefreshUrl,
oauth_browser_url: oauthBrowserUrl
};
// lib/request.ts
async function requestOauthToken({
client_id,
client_secret,
grant_type = "authorization_code",
redirect_uri,
code
}) {
const res = await Axios__default.default.post(
oauthTokenRequestUrl({
client_id,
client_secret,
redirect_uri,
grant_type,
code
})
);
return res.data;
}
async function refreshOauthToken({
client_id,
client_secret,
refresh_token
}) {
const res = await Axios__default.default.post(
oauthTokenRefreshUrl({
client_id,
client_secret,
refresh_token
})
);
return res.data;
}
var request = {
authentication: requestOauthToken,
tokenrefresh: refreshOauthToken
};
// lib/configure.ts
function configure({
client_id,
client_secret,
redirect_uri
}) {
__client_id = client_id;
__client_secret = client_secret;
__redirect_uri = redirect_uri;
}
var __client_id = process.env.FIGMA_APP_CLIENT_ID;
var __client_secret = process.env.FIGMA_APP_CLIENT_SECRET;
var __redirect_uri = process.env.FIGMA_OAUTH_CALLBACL_URI;
function __cfg_get_client_id() {
return __client_id;
}
function __cfg_get_client_secret() {
return __client_secret;
}
function __cfg_get_redirect_uri() {
return __redirect_uri;
}
// lib/oauth.ts
var OAuth = class {
static new({
state,
redirect_uri
}) {
const oauth = new OAuthState({ state, redirect_uri });
this._authprocs.set(oauth.state, oauth);
return oauth;
}
static get(state) {
return this._authprocs.get(state);
}
static resolve(state) {
this._authprocs.delete(state);
}
};
OAuth._authprocs = /* @__PURE__ */ new Map();
var OAuthState = class {
get stage() {
return this._stage;
}
constructor({
state = _stateid(),
redirect_uri = __cfg_get_redirect_uri()
}) {
this.state = state;
this._app_id = __cfg_get_client_id();
this._app_secret = __cfg_get_client_secret();
if (!redirect_uri) {
throw "redirect_uri must be provided by configuration. use `.env` or `configure()`.";
}
this.redirect_uri = redirect_uri;
}
get authUrl() {
return urls.oauth_browser_url({
client_id: this._app_id,
redirect_uri: this.redirect_uri,
state: this.state
});
}
async authenticate({ code }) {
return await request.authentication({
client_id: this._app_id,
client_secret: this._app_secret,
grant_type: "authorization_code",
redirect_uri: this.redirect_uri,
code
});
}
resolve() {
try {
OAuth.resolve(this.state);
} catch (_) {
throw new Error(
"Unable to resolve OAuth state: This OAuthState was not initialized by `OAuth.new()`"
);
}
}
};
function _stateid() {
return Math.random().toString(36).substring(7);
}
exports.OAuth = OAuth;
exports.OAuthState = OAuthState;
exports.configure = configure;
exports.oauthBrowserUrl = oauthBrowserUrl;
exports.oauthTokenRefreshUrl = oauthTokenRefreshUrl;
exports.oauthTokenRequestUrl = oauthTokenRequestUrl;
exports.refreshOauthToken = refreshOauthToken;
exports.request = request;
exports.requestOauthToken = requestOauthToken;
exports.urls = urls;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map