Skip to content

Commit

Permalink
print auth url
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio committed Apr 28, 2023
1 parent 7f4120a commit 45d9d78
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 53 deletions.
45 changes: 1 addition & 44 deletions auth/OpenAiAuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package auth
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/url"
Expand Down Expand Up @@ -389,47 +388,5 @@ func (auth *Authenticator) partSix(oldState string, redirectURL string) Error {

}
func (auth *Authenticator) GetAccessToken() (string, Error) {
auth.URL = strings.Replace(auth.URL, "chat.openai.com/api/", "openai.openai.auth0app.com/", 1)
resp, _ := auth.Session.Get(auth.URL)
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", *NewError("get_access_token", resp.StatusCode, resp.Status, fmt.Errorf("error: Check details"))
}
// Get code=*& from URL
re := regexp.MustCompile(`code=(.*?)&`)
matches := re.FindStringSubmatch(resp.Request.URL.String())
if len(matches) != 2 {
return "", *NewError("get_access_token", 0, "Missing code", fmt.Errorf("error: Check details"))
}
auth_code := matches[1]
// Construct payload
payload := map[string]interface{}{
"client_id": auth.AuthDetails.ClientID,
"code": auth_code,
"code_verifier": auth.Verifier_code,
"grant_type": "authorization_code",
"redirect_uri": "https://labs.openai.com/auth/callback",
}
payload_json, _ := json.Marshal(payload)
println(string(payload_json))
// Construct request to https://auth0.openai.com/oauth/token
req, _ := http.NewRequest("POST", "https://auth0.openai.com/oauth/token", strings.NewReader(string(payload_json)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", auth.UserAgent)
req.Header.Set("Accept", "application/json")
req.Header.Set("auth0-client", "eyJuYW1lIjoiYXV0aDAtc3BhLWpzIiwidmVyc2lvbiI6IjEuMjAuMSJ9")

resp, _ = auth.Session.Do(req)
defer resp.Body.Close()

body, _ := io.ReadAll(resp.Body)

if resp.StatusCode != 200 {
return "", *NewError("get_access_token", resp.StatusCode, string(body), fmt.Errorf("error: Check details"))
}

println(body)

return "", Error{}

return auth.URL, Error{}
}
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ func main() {
println("Embedded error: " + err.Error.Error())
return
}
// token, err := auth.GetAccessToken()
// if err.Error != nil {
// println("Error: " + err.Details)
// println("Location: " + err.Location)
// println("Status code: " + fmt.Sprint(err.StatusCode))
// println("Embedded error: " + err.Error.Error())
// return
// }
// fmt.Println(token)
token, err := auth.GetAccessToken()
if err.Error != nil {
println("Error: " + err.Details)
println("Location: " + err.Location)
println("Status code: " + fmt.Sprint(err.StatusCode))
println("Embedded error: " + err.Error.Error())
return
}
fmt.Println(token)
}

0 comments on commit 45d9d78

Please sign in to comment.