func (f *ChatGPTFake) userCode(w http.ResponseWriter, r *http.Request) {
var body map[string]string
func (f *ChatGPTFake) userCode(w http.ResponseWriter, r *http.Request) {
var body map[string]string
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil || body["client_id"] != chatgptClientID {
http.Error(w, "bad client", http.StatusBadRequest)
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil || body["client_id"] != chatgptClientID {
http.Error(w, "bad client", http.StatusBadRequest)
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil || body["device_auth_id"] != "device-1" || body["user_code"] != "CODE-1234" {
http.Error(w, "bad device", http.StatusBadRequest)
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil || body["device_auth_id"] != "device-1" || body["user_code"] != "CODE-1234" {
http.Error(w, "bad device", http.StatusBadRequest)
return
}
if f.polls <= f.pending {
http.Error(w, "pending", http.StatusForbidden)
return
}
if f.polls <= f.pending {
http.Error(w, "pending", http.StatusForbidden)
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil || body["grant_type"] != "refresh_token" || body["client_id"] != chatgptClientID {
http.Error(w, "bad refresh", http.StatusBadRequest)
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil || body["grant_type"] != "refresh_token" || body["client_id"] != chatgptClientID {
http.Error(w, "bad refresh", http.StatusBadRequest)
f.refreshed = body["refresh_token"]
if f.failRefresh {
http.Error(w, `{"error": "invalid_grant", "token": "`+body["refresh_token"]+`"}`, http.StatusUnauthorized)
f.refreshed = body["refresh_token"]
if f.failRefresh {
http.Error(w, `{"error": "invalid_grant", "token": "`+body["refresh_token"]+`"}`, http.StatusUnauthorized)
mux.HandleFunc("POST /api/accounts/deviceauth/usercode", f.userCode)
mux.HandleFunc("POST /api/accounts/deviceauth/token", f.deviceToken)
mux.HandleFunc("POST /oauth/token", f.oauthToken)
mux.HandleFunc("POST /api/accounts/deviceauth/usercode", f.userCode)
mux.HandleFunc("POST /api/accounts/deviceauth/token", f.deviceToken)
mux.HandleFunc("POST /oauth/token", f.oauthToken)
func chatgptWriteJSON(w http.ResponseWriter, value any) {
w.Header().Set("Content-Type", "application/json")
func chatgptWriteJSON(w http.ResponseWriter, value any) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(value)
if err != nil {
panic(fmt.Sprint("encode answer: ", err))
err := json.NewEncoder(w).Encode(value)
if err != nil {
panic(fmt.Sprint("encode answer: ", err))