-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move auth functions to auth.ts #154
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { OAuth2Client } from 'google-auth-library'; | ||
import { ClaspSettings, DOTFILE } from './utils.js'; | ||
import * as http from 'http'; | ||
import * as url from 'url'; | ||
const open = require('open'); | ||
const readline = require('readline'); | ||
//Just for now, until it moves into /src/commands.ts | ||
import { LOG } from '../index.js'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See note, this is just temporary until we make |
||
|
||
// API settings | ||
// @see https://developers.google.com/oauthplayground/ | ||
export const REDIRECT_URI_OOB = 'urn:ietf:wg:oauth:2.0:oob'; | ||
export const oauth2Client = new OAuth2Client({ | ||
clientId: '1072944905499-vm2v2i5dvn0a0d2o4ca36i1vge8cvbn0.apps.googleusercontent.com', | ||
clientSecret: 'v6V3fKV_zWU7iw1DrpO1rknX', | ||
redirectUri: 'http://localhost', | ||
}); | ||
|
||
/** | ||
* Loads the Apps Script API credentials for the CLI. | ||
* Required before every API call. | ||
* @param {Function} cb The callback | ||
* @param {boolean} isLocal If we should load local API credentials for this clasp project. | ||
*/ | ||
export function getAPICredentials(cb: (rc: ClaspSettings | void) => void, isLocal?: boolean) { | ||
const dotfile = isLocal ? DOTFILE.RC_LOCAL : DOTFILE.RC; | ||
dotfile.read().then((rc: ClaspSettings) => { | ||
oauth2Client.setCredentials(rc); | ||
cb(rc); | ||
}).catch((err: object) => { | ||
console.error('Could not read API credentials. Error:'); | ||
console.error(err); | ||
process.exit(-1); | ||
}); | ||
} | ||
|
||
/** | ||
* Requests authorization to manage Apps Scrpit projects. Spins up | ||
* a temporary HTTP server to handle the auth redirect. | ||
* | ||
* @param {Object} opts OAuth2 options TODO formalize options | ||
* @return {Promise} Promise resolving with the authorization code | ||
*/ | ||
export function authorizeWithLocalhost(opts: any): Promise<string> { | ||
return new Promise((res: Function, rej: Function) => { | ||
const server = http.createServer((req: http.ServerRequest, resp: http.ServerResponse) => { | ||
const urlParts = url.parse(req.url || '', true); | ||
if (urlParts.query.code) { | ||
res(urlParts.query.code); | ||
} else { | ||
rej(urlParts.query.error); | ||
} | ||
resp.end(LOG.AUTH_PAGE_SUCCESSFUL); | ||
setTimeout(() => { // TODO Remove hack to shutdown server. | ||
process.exit(); | ||
}, 1000); | ||
}); | ||
|
||
server.listen(0, () => { | ||
oauth2Client.redirectUri = `http://localhost:${server.address().port}`; | ||
const authUrl = oauth2Client.generateAuthUrl(opts); | ||
console.log(LOG.AUTHORIZE(authUrl)); | ||
open(authUrl); | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Requests authorization to manage Apps Script projects. Requires the | ||
* user to manually copy/paste the authorization code. No HTTP server is | ||
* used. | ||
* | ||
* @param {Object} opts OAuth2 options | ||
* @return {Promise} Promise resolving with the authorization code | ||
*/ | ||
export function authorizeWithoutLocalhost(opts: any): Promise<string> { | ||
oauth2Client.redirectUri = REDIRECT_URI_OOB; | ||
const authUrl = oauth2Client.generateAuthUrl(opts); | ||
console.log(LOG.AUTHORIZE(authUrl)); | ||
|
||
return new Promise((res, rej) => { | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
rl.question(LOG.AUTH_CODE, (code: string) => { | ||
if (code && code.length) { | ||
res(code); | ||
} else { | ||
rej("No authorization code entered."); | ||
} | ||
rl.close(); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See note above: this is just temporary