@google/clasp
Version:
Develop Apps Script Projects locally
83 lines (82 loc) • 2.63 kB
JavaScript
import fs from 'fs';
function hasLegacyLocalCredentials(store) {
return store.token && store.oauth2ClientSettings;
}
function hasLegacyGlobalCredentials(store) {
return !!store.access_token;
}
export class FileCredentialStore {
constructor(filePath) {
this.filePath = filePath;
}
async save(user, credentials) {
const store = this.readFile();
if (!store.tokens) {
store.tokens = {};
}
store.tokens[user] = credentials;
this.writeFile(store);
}
async delete(user) {
let store = this.readFile();
if (!store.tokens) {
store.tokens = {};
}
store.tokens[user] = undefined;
if (user === 'default') {
// Remove legacy keys if default user
store = {
tokens: store.tokens,
};
}
this.writeFile(store);
}
async deleteAll() {
await this.writeFile({
tokens: {},
});
}
async load(user) {
var _a, _b, _c;
const store = this.readFile();
const credentials = (_a = store.tokens) === null || _a === void 0 ? void 0 : _a[user];
if (credentials) {
return credentials;
}
if (user !== 'default') {
return null;
}
if (hasLegacyLocalCredentials(store)) {
// Support previous un
return {
type: 'authorized_user',
...store.token,
client_id: (_b = store.oauth2ClientSettings) === null || _b === void 0 ? void 0 : _b.clientId,
client_secret: (_c = store.oauth2ClientSettings) === null || _c === void 0 ? void 0 : _c.clientSecret,
};
}
if (hasLegacyGlobalCredentials(store)) {
return {
type: 'authorized_user',
access_token: store.access_token,
refresh_token: store.refresh_token,
expiry_date: store.exprity_date,
token_type: store.token_type,
client_id: '1072944905499-vm2v2i5dvn0a0d2o4ca36i1vge8cvbn0.apps.googleusercontent.com',
client_secret: 'v6V3fKV_zWU7iw1DrpO1rknX',
};
}
return null;
}
readFile() {
if (fs.existsSync(this.filePath)) {
// TODO - use promises
const content = fs.readFileSync(this.filePath, { encoding: 'utf8' });
return JSON.parse(content);
}
return {};
}
writeFile(store) {
fs.writeFileSync(this.filePath, JSON.stringify(store, null, 2));
}
}