Skip to content

Commit c2a876c

Browse files
committed
fix(proxy): get scope from headers for authentication
1 parent f2f8c66 commit c2a876c

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

internal/proxy/bearer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ func (b *Bearer) GetToken() string {
2121
return b.AccessToken
2222
}
2323

24-
func NewBearer(endpoint string, scope string) (*Bearer, error) {
25-
response, err := http.Get(endpoint + "/v2")
24+
func NewBearer(endpoint string, path string) (*Bearer, error) {
25+
response, err := http.Get(endpoint + path)
2626
if err != nil {
2727
return nil, err
2828
}
2929

3030
bearer := Bearer{}
3131
if response.StatusCode == 401 {
3232
wwwAuthenticate := parseWwwAuthenticate(response.Header.Get("www-authenticate"))
33-
url := fmt.Sprintf("%s?service=%s&scope=%s", wwwAuthenticate["realm"], wwwAuthenticate["service"], scope)
33+
url := fmt.Sprintf("%s?service=%s&scope=%s", wwwAuthenticate["realm"], wwwAuthenticate["service"], wwwAuthenticate["scope"])
3434

3535
response, err := http.Get(url)
3636
if err != nil {

internal/proxy/proxy.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package proxy
22

33
import (
44
"errors"
5-
"fmt"
65
"net/http"
76
"net/http/httputil"
87
"net/url"
@@ -44,12 +43,6 @@ func proxyRegistry(c *gin.Context, endpoint string, image string, httpToError bo
4443
image = strings.Join(parts, "/")
4544
}
4645

47-
scope := fmt.Sprintf("repository:%s:pull", image)
48-
bearer, err := NewBearer(endpoint, scope)
49-
if err != nil {
50-
panic(err)
51-
}
52-
5346
proxy := httputil.NewSingleHostReverseProxy(remote)
5447

5548
proxy.Director = func(req *http.Request) {
@@ -59,6 +52,10 @@ func proxyRegistry(c *gin.Context, endpoint string, image string, httpToError bo
5952
req.URL.Host = remote.Host
6053
req.URL.Path = "/v2/" + originRegistry + strings.Join(strings.Split(req.URL.Path, "/")[2:], "/")
6154

55+
bearer, err := NewBearer(endpoint, req.URL.Path)
56+
if err != nil {
57+
panic(err)
58+
}
6259
token := bearer.GetToken()
6360
if token != "" {
6461
req.Header.Set("Authorization", "Bearer "+token)

0 commit comments

Comments
 (0)