File size: 1,480 Bytes
6bc074c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package handler

import (
	"net/http"

	"aurora/internal/accounts"
	"aurora/internal/chatgpt"
	officialtypes "aurora/typings/official"
	bogdanfinn "aurora/httpclient/bogdanfinn"

	"github.com/gin-gonic/gin"
)

type AuthHandler struct {
	accountPool *accounts.Pool
}

func NewAuthHandler(pool *accounts.Pool) *AuthHandler {
	return &AuthHandler{accountPool: pool}
}

func (h *AuthHandler) Refresh(c *gin.Context) {
	var req officialtypes.OpenAIRefreshToken
	if err := c.BindJSON(&req); err != nil {
		respondError(c, http.StatusBadRequest, err)
		return
	}
	client := bogdanfinn.NewStdClient()
	result, status, err := chatgpt.GETTokenForRefreshToken(client, req.RefreshToken, "")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"message": "Request must be proper JSON",
			"type":    "invalid_request_error",
			"param":   nil,
			"code":    err.Error(),
		})
		return
	}
	c.JSON(status, result)
}

func (h *AuthHandler) Session(c *gin.Context) {
	var req officialtypes.OpenAISessionToken
	if err := c.BindJSON(&req); err != nil {
		respondError(c, http.StatusBadRequest, err)
		return
	}
	client := bogdanfinn.NewStdClient()
	result, status, err := chatgpt.GETTokenForSessionToken(client, req.SessionToken, "")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": gin.H{
				"message": "Request must be proper JSON",
				"type":    "invalid_request_error",
				"param":   nil,
				"code":    err.Error(),
			},
		})
		return
	}
	c.JSON(status, result)
}