File size: 3,248 Bytes
581b6d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package session

import (
	"WarpGPT/pkg/common"
	"WarpGPT/pkg/plugins"
	"WarpGPT/pkg/tools"
	"encoding/json"
	http "github.com/bogdanfinn/fhttp"
	tls_client "github.com/bogdanfinn/tls-client"
	"github.com/gin-gonic/gin"
	"io"
	shttp "net/http"
)

var context *plugins.Component
var SessionTokenInstance SessionToken

type Context struct {
	GinContext     *gin.Context
	RequestUrl     string
	RequestClient  tls_client.HttpClient
	RequestBody    io.ReadCloser
	RequestParam   string
	RequestMethod  string
	RequestHeaders http.Header
}
type SessionToken struct {
	Context Context
}

func (p *SessionToken) GetContext() Context {
	return p.Context
}
func (p *SessionToken) SetContext(conversation Context) {
	p.Context = conversation
}

func (p *SessionToken) ProcessMethod() {
	context.Logger.Debug("SessionToken")
	var requestBody map[string]interface{}
	if err := p.decodeRequestBody(&requestBody); err != nil {
		return
	}
	var auth *tools.Authenticator
	username, usernameExists := requestBody["username"]
	password, passwordExists := requestBody["password"]
	puid, puidExists := requestBody["puid"]
	refreshCookie, refreshCookieExists := requestBody["refreshCookie"]
	if !refreshCookieExists {
		if usernameExists && passwordExists {
			if puidExists {
				auth = tools.NewAuthenticator(username.(string), password.(string), puid.(string))
			} else {
				auth = tools.NewAuthenticator(username.(string), password.(string), "")
			}
			if err := auth.Begin(); err != nil {
				p.GetContext().GinContext.JSON(400, err)
				return
			}
			auth.GetModels()
			all := auth.GetAuthResult()
			var result map[string]interface{}
			accessToken := all.AccessToken
			model := all.Model
			refreshToken := all.FreshToken
			result = accessToken
			result["refreshCookie"] = refreshToken
			result["models"] = model["models"]
			p.GetContext().GinContext.JSON(200, result)
		} else {
			p.GetContext().GinContext.JSON(400, gin.H{"error": "Please provide a refreshCookie or username and password."})
			return
		}
	} else {
		auth = tools.NewAuthenticator("", "", "")
		err := auth.GetAccessTokenByRefreshToken(refreshCookie.(string))
		if err != nil {
			p.GetContext().GinContext.JSON(400, err)
			return
		}
		auth.GetModels()
		all := auth.GetAuthResult()
		var result map[string]interface{}
		accessToken := all.AccessToken
		model := all.Model
		refreshToken := all.FreshToken
		result = accessToken
		result["refreshCookie"] = refreshToken
		result["models"] = model["models"]
		p.GetContext().GinContext.JSON(200, result)
	}
}
func (p *SessionToken) decodeRequestBody(requestBody *map[string]interface{}) error {
	conversation := p.GetContext()
	if conversation.RequestBody != shttp.NoBody {
		if err := json.NewDecoder(conversation.RequestBody).Decode(requestBody); err != nil {
			conversation.GinContext.JSON(400, gin.H{"error": "JSON invalid"})
			return err
		}
	}
	return nil
}

type NotHaveUrl struct {
}

func (u NotHaveUrl) Generate(path string, rawquery string) string {
	return ""
}
func (p *SessionToken) Run(com *plugins.Component) {
	context = com
	context.Engine.POST("/getsession", func(c *gin.Context) {
		conversation := common.GetContextPack(c, NotHaveUrl{})
		common.Do[Context](new(SessionToken), Context(conversation))
	})
}