Library/Tool for automated authentication to ChatGPT. Intended for use with a tool like cURL to automate interactions with the OpenAI web interface.
The cURL tool is often used in command-line contexts to make HTTP requests. Unlike a web browser or a browser automation tool like Puppeteer or Selenium, cURL doesn't automatically handle CSRF authentication. Thus, if a web application uses CSRF tokens as part of its security, browserless automated interactions with the application must correctly handle these tokens. This involves extracting the CSRF token from a set-cookie HTTP header, storing it, and then including it in subsequent requests as required by the application.
A CSRF token, or Cross-Site Request Forgery token, is a security measure used in web development to protect against CSRF attacks. In a CSRF attack, an attacker tricks a victim into performing an action on a web application in which they're authenticated. This is possible because web applications often trust that actions coming from a user's browser are intentional and legitimate when the user is authenticated. A CSRF token is a way to prevent these attacks. It's a unique, random value associated with a user's session, and it's typically embedded within the web form that the user is submitting. When the form is submitted, the server checks that the token in the form matches the token associated with the user's session. If the tokens don't match, the request is rejected. This obviously poses an obstacle to browserless automation.
Note that the more accepted way to automate browserless interactions with OpenAI is via their API, with an API access token. For most use cases, that would be better. Possible reasons to automate via cURL, instead, include making automated requests through one's $20/month ChatGPT Plus subscription (as opposed to the pay-as-you-go API rate) and to make use of plugins or other browser-exclusive functionality that is not available through the API.
The entry point is the main
function in the main
module:
-
It creates a new instance of the
Authenticator
from theauth
package using theNewAuthenticator
function. This function requires three environment variables:OPENAI_EMAIL
,OPENAI_PASSWORD
, andPROXY
. -
The
Authenticator
then initiates the authentication process with the Begin method. If this method encounters an error, it prints out the details and exits the program. -
If the
Begin
method is successful, the program then calls theGetAccessToken
method on theAuthenticator
to retrieve an access token. If this process encounters an error, it prints out the details and exits the program. If successful, it prints the retrieved access token.
In the OpenAiAuth.go
file, we have the Authenticator
struct which carries out several operations to authenticate the user with the OpenAI service:
-
The
Begin
method sends aGET
request to the OpenAI service to retrieve a CSRF token. -
If successful, it calls the
partOne
method, passing the CSRF token. This method sends a POST request to the OpenAI service with the CSRF token as part of the payload. -
The
partOne
method then calls thepartTwo
method if the previous operation was successful. This method sends aGET
request to another OpenAI service URL and captures a state variable from the response. -
The
partTwo
method then callspartThree
which sends anotherGET
request, this time to auth0.openai.com. -
The
partThree
method callspartFour
if the previous operation was successful. This method sends aPOST
request to the OpenAI service, providing the user's email address and the previously retrieved state. -
The
partFour
method callspartFive
if the previous operation was successful. This method sends anotherPOST
request to the OpenAI service, this time providing both the user's email and password along with the state. -
Finally, the
partFive
method callspartSix
if the previous operation was successful. This method sends aGET
request to the OpenAI service with theredirectURL
received from the response ofpartFive
.
The end result of this sequence of operations is to authenticate the user with the OpenAI service and retrieve an access token, which can be used for subsequent requests to the service. Note that the choice of the user-agent string in the Authenticator
struct emulates a Chrome browser on Linux.
- @linweiyuan
- @rawandahmad698