Skip to content

Commit

Permalink
fix: Carry verification Token in Header issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Jan 27, 2024
1 parent 66d5080 commit 3ab0166
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 11 additions & 3 deletions models/gemini/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type RequestConverter interface {
Name() string
Convert(req *http.Request, config *define.ModelConfig, payload []byte, openaiPayload define.OpenAI_Payload) (*http.Request, error)
Convert(req *http.Request, config *define.ModelConfig, payload []byte, openaiPayload define.OpenAI_Payload, apikey string) (*http.Request, error)
}

type StripPrefixConverter struct {
Expand All @@ -22,7 +22,7 @@ func (c *StripPrefixConverter) Name() string {
return "StripPrefix"
}

func (c *StripPrefixConverter) Convert(req *http.Request, config *define.ModelConfig, payload []byte, openaiPayload define.OpenAI_Payload) (*http.Request, error) {
func (c *StripPrefixConverter) Convert(req *http.Request, config *define.ModelConfig, payload []byte, openaiPayload define.OpenAI_Payload, apikey string) (*http.Request, error) {
req.Host = config.URL.Host
req.URL.Scheme = config.URL.Scheme
req.URL.Host = config.URL.Host
Expand All @@ -36,7 +36,15 @@ func (c *StripPrefixConverter) Convert(req *http.Request, config *define.ModelCo
req.URL.RawPath = req.URL.EscapedPath()

query := req.URL.Query()
query.Add("key", config.Key)
if config.Key == "" {
if apikey == "" {
return nil, fmt.Errorf("missing api key")
} else {
query.Add("key", apikey)
}
} else {
query.Add("key", config.Key)
}
req.URL.RawQuery = query.Encode()
req.Body = io.NopCloser(bytes.NewBuffer(payload))
req.ContentLength = int64(len(payload))
Expand Down
4 changes: 2 additions & 2 deletions models/gemini/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func getDirector(req *http.Request, body []byte, c *gin.Context, requestConverte
network.SendError(c, errors.New("token is empty"))
return
}
req.Header.Set("Authorization", token)
req.Header.Del("Authorization")

repack, err := json.Marshal(payload)
if err != nil {
Expand All @@ -135,7 +135,7 @@ func getDirector(req *http.Request, body []byte, c *gin.Context, requestConverte
}

originURL := req.URL.String()
req, err = requestConverter.Convert(req, deployment, repack, openaiPayload)
req, err = requestConverter.Convert(req, deployment, repack, openaiPayload, token)
if err != nil {
network.SendError(c, errors.Wrap(err, "convert request error"))
return
Expand Down

0 comments on commit 3ab0166

Please sign in to comment.