Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add PKCE

Surajcf83f35e869a
diff --git a/packages/cli/.env.example b/packages/cli/.env.example
deleted file mode 100644
index 23e952fe9c..0000000000
--- a/packages/cli/.env.example
+++ /dev/null
@@ -1,3 +0,0 @@
-DUB_CLIENT_ID=
-DUB_CLIENT_SECRET=
-DUB_CALLBACK_URL=
\ No newline at end of file
diff --git a/packages/cli/package.json b/packages/cli/package.json
index dd2720a6a1..76ab39ba79 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -44,7 +44,6 @@
"chalk": "^5.3.0",
"commander": "^11.1.0",
"configstore": "^6.0.0",
- "dotenv": "^16.4.5",
"dub": "^0.43.0",
"fs-extra": "^11.2.0",
"json-colorizer": "^2.2.2",
diff --git a/packages/cli/src/api/callback.ts b/packages/cli/src/api/callback.ts
index a0e22279ec..f2cbb79107 100644
--- a/packages/cli/src/api/callback.ts
+++ b/packages/cli/src/api/callback.ts
@@ -9,13 +9,13 @@ import * as url from "url";
interface OAuthCallbackServerProps {
oauth2Client: OAuth2Client;
- callbackUrl: string;
+ redirectUri: string;
spinner: Ora;
}
export function oauthCallbackServer({
oauth2Client,
- callbackUrl,
+ redirectUri,
spinner,
}: OAuthCallbackServerProps) {
const server = http.createServer(async (req, res) => {
@@ -41,9 +41,13 @@ export function oauthCallbackServer({
try {
spinner.text = "Verifying";
+ const config = new Configstore("dub-cli");
+ const codeVerifier = config.get("code_verifier");
+
const { accessToken } = await oauth2Client.authorizationCode.getToken({
code,
- redirectUri: `${callbackUrl}/callback`,
+ redirectUri,
+ codeVerifier,
});
spinner.text = "Configuring";
@@ -53,7 +57,6 @@ export function oauthCallbackServer({
domain: "dub.sh",
};
- const config = new Configstore("dub-cli");
config.set(configInfo);
if (!config.path) {
diff --git a/packages/cli/src/commands/login.ts b/packages/cli/src/commands/login.ts
index 74b0f5cd9e..306ab59349 100644
--- a/packages/cli/src/commands/login.ts
+++ b/packages/cli/src/commands/login.ts
@@ -1,16 +1,15 @@
import { oauthCallbackServer } from "@/api/callback";
+import { getNanoid } from "@/utils/get-nanoid";
import { handleError } from "@/utils/handle-error";
import { OAuth2Client } from "@badgateway/oauth2-client";
import { Command } from "commander";
-import * as dotenv from "dotenv";
+import Configstore from "configstore";
import open from "open";
import ora from "ora";
-dotenv.config();
-
const oauth2Client = new OAuth2Client({
- clientId: `${process.env.DUB_CLIENT_ID}`,
- clientSecret: `${process.env.DUB_CLIENT_SECRET}`,
+ // TODO: add client id here
+ clientId: "dub_app_adbe7acb08aea1f6e45a5d1fbe3a1a185822fc3cf50b527e",
authorizationEndpoint: "https://app.dub.co/oauth/authorize",
tokenEndpoint: "https://api.dub.co/oauth/token",
});
@@ -20,8 +19,15 @@ export const login = new Command()
.description("Log into the Dub platform")
.action(async () => {
try {
+ const codeVerifier = getNanoid(64);
+ const redirectUri = "http://localhost:4040/callback";
+
+ const config = new Configstore("dub-cli");
+ config.set("code_verifier", codeVerifier);
+
const authUrl = await oauth2Client.authorizationCode.getAuthorizeUri({
- redirectUri: `${process.env.DUB_CALLBACK_URL}/callback`,
+ redirectUri,
+ codeVerifier,
scope: ["links.read", "links.write", "domains.read"],
});
@@ -33,7 +39,7 @@ export const login = new Command()
oauthCallbackServer({
oauth2Client,
- callbackUrl: process.env.DUB_CALLBACK_URL as string,
+ redirectUri,
spinner,
});
} catch (error) {
diff --git a/packages/cli/src/commands/shorten.ts b/packages/cli/src/commands/shorten.ts
index dc7f8f6b48..cdfbed7ddd 100644
--- a/packages/cli/src/commands/shorten.ts
+++ b/packages/cli/src/commands/shorten.ts
@@ -31,7 +31,7 @@ export const shorten = new Command()
type: "text",
name: "key",
message: "Enter your Short link:",
- initial: getNanoid(),
+ initial: getNanoid(7),
},
],
{
diff --git a/packages/cli/src/types/index.ts b/packages/cli/src/types/index.ts
index f8ab3e5eb7..33eda8c65b 100644
--- a/packages/cli/src/types/index.ts
+++ b/packages/cli/src/types/index.ts
@@ -1,6 +1,7 @@
export interface DubConfig {
token: string;
domain?: string;
+ code_verifier?: string;
}
export type Domain = {
diff --git a/packages/cli/src/utils/get-nanoid.ts b/packages/cli/src/utils/get-nanoid.ts
index 910f5aa1e4..958424117b 100644
--- a/packages/cli/src/utils/get-nanoid.ts
+++ b/packages/cli/src/utils/get-nanoid.ts
@@ -1,7 +1,9 @@
import { customAlphabet } from "nanoid";
-export function getNanoid() {
- const alphabet = "0123456789abcdefghijklmnopqrstuvwxyz";
- const nanoid = customAlphabet(alphabet, 10);
- return nanoid(7);
+export function getNanoid(length: number = 10) {
+ const alphabet =
+ "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ const nanoid = customAlphabet(alphabet, length);
+
+ return nanoid();
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 7259fc1269..3cd8a6f863 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -493,9 +493,6 @@ importers:
configstore:
specifier: ^6.0.0
version: 6.0.0
- dotenv:
- specifier: ^16.4.5
- version: 16.4.5
dub:
specifier: ^0.43.0
version: 0.43.0([email protected])
@@ -6549,10 +6546,6 @@ packages:
resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==}
engines: {node: '>=12'}
- resolution: {integrity: sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==}
- engines: {node: '>=12'}
-
resolution: {integrity: sha512-PxS0165fQqeMNDYdSbAAxc69WphtonAyVWh3IVbM0isdGfOeTNIX9gqqZ8ZZORr5/L4CVnipnip3+PRIlVu5Rg==}
peerDependencies:
@@ -18630,8 +18623,6 @@ snapshots:
-
dependencies:
zod: 3.23.8