Skip to content

Commit

Permalink
Add clearance token to fix issue introduced by cloudfront
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Barnes committed Dec 12, 2022
1 parent ac47ae7 commit 606c174
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The `README.md` file for this extension was generated by ChatGPT.
- `ChatGPT: Why is my code broken?` (analyses your code to highlight any logic/syntax errors)
- `ChatGPT: Explain code`
- `ChatGPT: Refactor`
- `ChatGPT: Reset token` (clears access token if expired or having issues)
- `ChatGPT: Reset token` (clears access & clearance tokens if expired or having issues)

*Everything except Reset Token and Query are available from the context menu when right-clicking in the editor.*

Expand All @@ -23,17 +23,17 @@ The `README.md` file for this extension was generated by ChatGPT.

To use the extension:
**This package requires a valid session token from ChatGPT to access it's unofficial REST API.**
Open the VS Code Command Palette and Type `ChatGPT: Reset token`, this will prompt you for your session token.
Open the VS Code Command Palette and Type `ChatGPT: Reset token`, this will prompt you for your session token and as of 12/12/2022 you also need a clearance token.

To get a session token:
To get the tokens:

1. Go to https://chat.openai.com/chat and log in or sign up.
2. Open dev tools.
3. Open `Application` > `Cookies` (`Storage` > `Cookies` on FireFox)

![image](https://user-images.githubusercontent.com/38425102/205900045-185c2c41-b4ff-408c-9da6-bbb606ac39c6.png)
![image](https://user-images.githubusercontent.com/38425102/207054121-dc87c625-a2f8-4ad6-92b7-9a52684d525c.png)

4. Copy the value for `__Secure-next-auth.session-token` and enter it into the prompt from `ChatGPT: Reset token`
4. Copy the value for `__Secure-next-auth.session-token` & `cl_clearance` and enter it into the prompt from `ChatGPT: Reset token`

*Once you're logged in, you can ask ChatGPT any question and supply source code from your current file/selection.*

Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"publisher": "JayBarnes",
"displayName": "ChatGPT VSCode Plugin",
"description": "A ChatGPT integration build using ChatGPT & 9 beers",
"version": "1.1.6",
"version": "1.1.7",
"engines": {
"vscode": "^1.73.0"
},
Expand Down Expand Up @@ -119,6 +119,6 @@
"webpack-cli": "^5.0.0"
},
"dependencies": {
"chatgpt": "^2.0.6"
"chatgpt": "^2.2.0"
}
}
27 changes: 20 additions & 7 deletions src/chatgpt-view-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class ChatGptViewProvider implements vscode.WebviewViewProvider {
private chatGptApi?: ChatGPTAPI;
private chatGptConversaion?: ChatGPTConversation;
private sessionToken?: string;
private clearanceToken?: string;
private message?: any;

constructor(private context: vscode.ExtensionContext) { }
Expand Down Expand Up @@ -38,25 +39,37 @@ export default class ChatGptViewProvider implements vscode.WebviewViewProvider {
}
}

public async setUpSessionToken() {
const sessionTokenName = 'chatgpt-session-token';
this.sessionToken = await this.context.globalState.get(sessionTokenName) as string;
public async setUpTokens() {
this.sessionToken = await this.context.globalState.get('chatgpt-session-token') as string;

if (!this.sessionToken) {
const userSessionToken = await vscode.window.showInputBox({
prompt: "Please enter your token (__Secure-next-auth.session-token), this can be retrieved using the guide on the README "
prompt: "Please enter your session token (__Secure-next-auth.session-token), this can be retrieved using the guide on the README "
});
this.sessionToken = userSessionToken!;
this.context.globalState.update(sessionTokenName, this.sessionToken);
this.context.globalState.update('chatgpt-session-token', this.sessionToken);
}

this.clearanceToken = await this.context.globalState.get('chatgpt-clearance-token') as string;

if (!this.clearanceToken) {
const userSessionToken = await vscode.window.showInputBox({
prompt: "Please enter your clearance token (cf_clearance), this can be retrieved using the guide on the README "
});
this.sessionToken = userSessionToken!;
this.context.globalState.update('chatgpt-clearance-token', this.sessionToken);
}
}

public async sendApiRequest(prompt: string, code?: string) {
await this.setUpSessionToken();
await this.setUpTokens();

if (!this.chatGptApi || !this.chatGptConversaion) {
try {
this.chatGptApi = new ChatGPTAPI({ sessionToken: this.sessionToken as string });
this.chatGptApi = new ChatGPTAPI({
sessionToken: this.sessionToken as string,
clearanceToken: this.clearanceToken as string
});
this.chatGptConversaion = this.chatGptApi?.getConversation();
} catch (error: any) {
vscode.window.showErrorMessage("Failed to connect to ChatGPT", error?.message);
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export async function activate(context: vscode.ExtensionContext) {

async function resetToken() {
await context.globalState.update('chatgpt-session-token', null);
await chatViewProvider.setUpSessionToken();
await context.globalState.update('chatgpt-clearance-token', null);
await chatViewProvider.setUpTokens();
// await vscode.window.showInformationMessage("Token reset, you'll be prompted for it next to you next ask ChatGPT a question.");
}

Expand Down

0 comments on commit 606c174

Please sign in to comment.