-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: change google oauth OOB flow to server
- Loading branch information
1 parent
d679685
commit b3861db
Showing
8 changed files
with
128 additions
and
83 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
.nyc_output | ||
.nyc_output | ||
.idea |
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
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 |
---|---|---|
@@ -1,43 +1,54 @@ | ||
import fetch from "node-fetch"; | ||
import { DesktopIAPOptions, Fetcher } from "../types"; | ||
import url from "url"; | ||
|
||
export function openBrowser(browser: (url: string) => void, url: string): void { | ||
browser(url); | ||
} | ||
export const isASCII = (value: string): boolean => /^[\x00-\x7F]+$/.test(value); | ||
|
||
export async function getRefreshToken( | ||
browser: (url: string) => void, | ||
inputHandler: (question: string) => Promise<string>, | ||
redirectUrl: string, | ||
fetcher: Fetcher = fetch, | ||
options: DesktopIAPOptions, | ||
redirectUri: string, | ||
): Promise<string> { | ||
const url = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${options.clientId}&response_type=code&scope=openid%20email&access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:oob`; | ||
|
||
console.log("Open this URL if your browser didn't open automatically\n", url); | ||
|
||
openBrowser(browser, url); | ||
|
||
// await input from user | ||
const loginToken = await inputHandler( | ||
"Please input the token you received in the browser", | ||
); | ||
|
||
const oauthTokenBaseUrl = "https://www.googleapis.com/oauth2/v4/token"; | ||
|
||
const body = { | ||
code: loginToken, | ||
client_id: options.clientId, | ||
client_secret: options.clientSecret, | ||
redirect_uri: "urn:ietf:wg:oauth:2.0:oob", | ||
grant_type: "authorization_code", | ||
}; | ||
|
||
const request = await fetcher(oauthTokenBaseUrl, { | ||
body: JSON.stringify(body), | ||
method: "POST", | ||
}); | ||
|
||
const response = await request.json(); | ||
|
||
return String(response["refresh_token"]); | ||
const { code, error } = url.parse(redirectUrl, true).query; | ||
if (error) { | ||
// An error response e.g. error=access_denied | ||
console.log("Error:" + error); | ||
return ""; | ||
} else { | ||
if (!code || code === "") { | ||
console.log("Authorization code is empty"); | ||
return ""; | ||
} | ||
if (Array.isArray(code)) { | ||
console.log("There is more then one authorization code in redirect url."); | ||
return ""; | ||
} | ||
if (!isASCII(code)) { | ||
console.log("Code contains not allowed characters: " + code); | ||
return ""; | ||
} | ||
|
||
const oauthTokenBaseUrl = "https://www.googleapis.com/oauth2/v4/token"; | ||
|
||
const body = { | ||
code: code, | ||
client_id: options.clientId, | ||
client_secret: options.clientSecret, | ||
redirect_uri: redirectUri, | ||
grant_type: "authorization_code", | ||
}; | ||
|
||
const request = await fetcher(oauthTokenBaseUrl, { | ||
body: JSON.stringify(body), | ||
method: "POST", | ||
}); | ||
|
||
const response = await request.json(); | ||
|
||
return String(response["refresh_token"]); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6852,6 +6852,11 @@ pump@^3.0.0: | |
end-of-stream "^1.1.0" | ||
once "^1.3.1" | ||
|
||
[email protected]: | ||
version "1.3.2" | ||
resolved "https://registry.npmjs.org/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d" | ||
integrity sha512-RofWgt/7fL5wP1Y7fxE7/EmTLzQVnB0ycyibJ0OOHIlJqTNzglYFxVwETOcIoJqJmpDXJ9xImDv+Fq34F/d4Dw== | ||
|
||
punycode@^2.1.0: | ||
version "2.1.1" | ||
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" | ||
|
@@ -6874,6 +6879,11 @@ [email protected]: | |
dependencies: | ||
side-channel "^1.0.4" | ||
|
||
[email protected]: | ||
version "0.2.0" | ||
resolved "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620" | ||
integrity sha512-X/xY82scca2tau62i9mDyU9K+I+djTMUsvwf7xnUX5GLvVzgJybOJf4Y6o9Zx3oJK/LSXg5tTZBjwzqVPaPO2g== | ||
|
||
quick-lru@^1.0.0: | ||
version "1.1.0" | ||
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-1.1.0.tgz#4360b17c61136ad38078397ff11416e186dcfbb8" | ||
|
@@ -8240,6 +8250,14 @@ url-parse-lax@^3.0.0: | |
dependencies: | ||
prepend-http "^2.0.0" | ||
|
||
url@^0.11.0: | ||
version "0.11.0" | ||
resolved "https://registry.npmjs.org/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1" | ||
integrity sha512-kbailJa29QrtXnxgq+DdCEGlbTeYM2eJUxsz6vjZavrCYPMIFHMKQmSKYAIuUK2i7hgPm28a8piX5NTUtM/LKQ== | ||
dependencies: | ||
punycode "1.3.2" | ||
querystring "0.2.0" | ||
|
||
util-deprecate@^1.0.1, util-deprecate@^1.0.2, util-deprecate@~1.0.1: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" | ||
|