Skip to content

Commit

Permalink
Fix local auth support for run command (#844)
Browse files Browse the repository at this point in the history
fix: Fix `login --creds` and local auth support for the run command.
  • Loading branch information
sqrrrl committed May 21, 2021
1 parent 003d422 commit 44e1e7d
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 29 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
src
test
.*
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"build-fresh": "npm cache clean --force && npm i && npm run build",
"watch": "tsc --project tsconfig.json --watch",
"prepare": "npm run compile",
"publish": "npm publish --access public",
"lint": "npm run check",
"test": "nyc mocha --cache false --timeout 100000 --recursive build/test",
"coverage": "nyc --cache false report --reporter=text-lcov | coveralls",
Expand Down
2 changes: 1 addition & 1 deletion src/apiutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const enableOrDisableAPI = async (serviceName: string, enable: boolean):
* Enable 'script.googleapis.com' of Google API.
*/
export const enableAppsScriptAPI = async (): Promise<void> => {
await loadAPICredentials();
await loadAPICredentials(true);
const name = `projects/${await getProjectIdOrDie()}/services/script.googleapis.com`;
await serviceUsage.services.enable({name});
};
14 changes: 5 additions & 9 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ export const authorize = async (options: {

// Save the token and own creds together.
let claspToken: ClaspToken;
// TODO: deprecate `--creds` option
if (options.creds) {
const {client_id: clientId, client_secret: clientSecret, redirect_uris: redirectUri} = options.creds.installed;
// Save local ClaspCredentials.
Expand All @@ -171,8 +170,7 @@ export const authorize = async (options: {
isLocalCreds: false,
};
}

await DOTFILE.AUTH().write(claspToken);
await DOTFILE.AUTH(claspToken.isLocalCreds).write(claspToken);
console.log(LOG.SAVED_CREDS(Boolean(options.creds)));
} catch (error) {
if (error instanceof ClaspError) {
Expand Down Expand Up @@ -304,14 +302,12 @@ const setOauthClientCredentials = async (rc: ClaspToken) => {
localOAuth2Client = new OAuth2Client({clientId, clientSecret, redirectUri});
localOAuth2Client.setCredentials(rc.token);
await refreshCredentials(localOAuth2Client);
} else {
globalOAuth2Client.setCredentials(rc.token);
await refreshCredentials(globalOAuth2Client);
}

// Always use the global credentials too for non-run functions.
globalOAuth2Client.setCredentials(rc.token);
await refreshCredentials(globalOAuth2Client);

// Save the credentials.
await DOTFILE.AUTH().write(rc);
await DOTFILE.AUTH(rc.isLocalCreds).write(rc);
} catch (error) {
if (error instanceof ClaspError) {
throw error;
Expand Down
6 changes: 5 additions & 1 deletion src/conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class Conf {
* @see {ClaspToken}
*/
readonly auth: AuthFile;
// Local auth for backwards compatibility
readonly authLocal: AuthFile;
// readonly manifest: PathProxy;

/**
Expand All @@ -63,13 +65,15 @@ export class Conf {
this.ignore = new IgnoreFile({dir: os.homedir(), base: `.${PROJECT_NAME}ignore`});

// default `auth` path is `~/.clasprc.json`
// Default Auth is global. Any other implies local auth
// default local auth path is './.clasprc.json'
this.auth = new AuthFile({dir: os.homedir(), base: `.${PROJECT_NAME}rc.json`});
this.authLocal = new AuthFile({dir: '.', base: `.${PROJECT_NAME}rc.json`});

// resolve environment variables
setPathWithEnvVar(ENV.DOT_CLASP_PROJECT, this.project);
setPathWithEnvVar(ENV.DOT_CLASP_IGNORE, this.ignore);
setPathWithEnvVar(ENV.DOT_CLASP_AUTH, this.auth);
setPathWithEnvVar(ENV.DOT_CLASP_AUTH, this.authLocal);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/dotfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {Credentials, OAuth2ClientOptions} from 'google-auth-library';

export type {Dotfile} from 'dotf';

const {auth, ignore, project} = Conf.get();
const {auth, authLocal, ignore, project} = Conf.get();

// Project settings file (Saved in .clasp.json)
export interface ProjectSettings {
Expand Down Expand Up @@ -77,9 +77,10 @@ export const DOTFILE = {
throw new Error('Project file must start with a dot (i.e. .clasp.json)');
},
// Stores {ClaspCredentials}
AUTH: () => {
AUTH: (local?: boolean) => {
const configPath = local ? authLocal : auth;
// ! TODO: currently limited if filename doesn't start with a dot '.'
const {dir, base} = path.parse(auth.resolve());
const {dir, base} = path.parse(configPath.resolve());
if (base[0] === '.') {
return dotf(dir || '.', base.slice(1));
}
Expand Down
15 changes: 1 addition & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,7 @@ export const hasOauthClientSettings = (local = false): boolean => {
*/
export const getOAuthSettings = async (local: boolean): Promise<ClaspToken> => {
try {
let previousPath: string | undefined;

if (local && auth.isDefault()) {
// if no local auth defined, try current directory
previousPath = auth.path;
auth.path = '.';
}

const result = DOTFILE.AUTH().read<ClaspToken>();

if (previousPath) {
auth.path = previousPath;
}

const result = DOTFILE.AUTH(local).read<ClaspToken>();
return result;
} catch (error) {
throw new ClaspError(getErrorMessage(error) ?? ERROR.NO_CREDENTIALS(local));
Expand Down

0 comments on commit 44e1e7d

Please sign in to comment.