From 3a724a7c803db255943f4bf646d2b94909564c3e Mon Sep 17 00:00:00 2001 From: Josh Yan Date: Fri, 5 Jul 2024 14:18:25 -0700 Subject: [PATCH] isLocal firstdraft --- api/client.go | 25 +++++++++++++++++++++++++ server/routes.go | 1 + 2 files changed, 26 insertions(+) diff --git a/api/client.go b/api/client.go index 7bf6012a2..e1e7cde21 100644 --- a/api/client.go +++ b/api/client.go @@ -413,3 +413,28 @@ func (c *Client) ServerConfig(ctx context.Context) (*ServerConfig, error) { } return &config, nil } + +func Authorization(ctx context.Context, request *http.Request) (string, error) { + + data := []byte(fmt.Sprintf("%s,%s,%d", request.Method, request.URL.RequestURI(), time.Now().Unix())) + + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + + knownHostsFile, err := os.OpenFile(filepath.Join(home, ".ollama", "known_hosts"), os.O_CREATE|os.O_RDWR|os.O_APPEND, 0600) + if err != nil { + return "", err + } + defer knownHostsFile.Close() + + token, err := auth.Sign(ctx, data) + if err != nil { + return "", err + } + + // interleave request data into the token + key, sig, _ := strings.Cut(token, ":") + return fmt.Sprintf("%s:%s:%s", key, base64.StdEncoding.EncodeToString(data), sig), nil +} diff --git a/server/routes.go b/server/routes.go index 2ad851edc..6c318eeb3 100644 --- a/server/routes.go +++ b/server/routes.go @@ -785,6 +785,7 @@ func (s *Server) CreateBlobHandler(c *gin.Context) { c.Status(http.StatusOK) return } + if c.GetHeader("X-Redirect-Create") == "1" && s.IsLocal(c) { c.Header("LocalLocation", path) c.Status(http.StatusTemporaryRedirect)