mirror of
https://github.com/ollama/ollama.git
synced 2025-05-11 10:26:53 +02:00
use url.URL
This commit is contained in:
parent
8f827641b0
commit
2cc634689b
4 changed files with 81 additions and 56 deletions
|
@ -12,8 +12,10 @@ import (
|
|||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -43,21 +45,34 @@ func generateNonce(length int) (string, error) {
|
|||
return base64.RawURLEncoding.EncodeToString(nonce), nil
|
||||
}
|
||||
|
||||
func (r AuthRedirect) URL() (string, error) {
|
||||
func (r AuthRedirect) URL() (*url.URL, error) {
|
||||
redirectURL, err := url.Parse(r.Realm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
values := redirectURL.Query()
|
||||
|
||||
values.Add("service", r.Service)
|
||||
|
||||
for _, s := range strings.Split(r.Scope, " ") {
|
||||
values.Add("scope", s)
|
||||
}
|
||||
|
||||
values.Add("ts", strconv.FormatInt(time.Now().Unix(), 10))
|
||||
|
||||
nonce, err := generateNonce(16)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
scopes := []string{}
|
||||
for _, s := range strings.Split(r.Scope, " ") {
|
||||
scopes = append(scopes, fmt.Sprintf("scope=%s", s))
|
||||
}
|
||||
scopeStr := strings.Join(scopes, "&")
|
||||
return fmt.Sprintf("%s?service=%s&%s&ts=%d&nonce=%s", r.Realm, r.Service, scopeStr, time.Now().Unix(), nonce), nil
|
||||
values.Add("nonce", nonce)
|
||||
|
||||
redirectURL.RawQuery = values.Encode()
|
||||
return redirectURL, nil
|
||||
}
|
||||
|
||||
func getAuthToken(ctx context.Context, redirData AuthRedirect, regOpts *RegistryOptions) (string, error) {
|
||||
url, err := redirData.URL()
|
||||
redirectURL, err := redirData.URL()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -77,18 +92,10 @@ func getAuthToken(ctx context.Context, redirData AuthRedirect, regOpts *Registry
|
|||
|
||||
s := SignatureData{
|
||||
Method: "GET",
|
||||
Path: url,
|
||||
Path: redirectURL.String(),
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(s.Path, "http") {
|
||||
if regOpts.Insecure {
|
||||
s.Path = "http://" + url
|
||||
} else {
|
||||
s.Path = "https://" + url
|
||||
}
|
||||
}
|
||||
|
||||
sig, err := s.Sign(rawKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -96,7 +103,7 @@ func getAuthToken(ctx context.Context, redirData AuthRedirect, regOpts *Registry
|
|||
|
||||
headers := make(http.Header)
|
||||
headers.Set("Authorization", sig)
|
||||
resp, err := makeRequest(ctx, "GET", url, headers, nil, regOpts)
|
||||
resp, err := makeRequest(ctx, "GET", redirectURL, headers, nil, regOpts)
|
||||
if err != nil {
|
||||
log.Printf("couldn't get token: %q", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue