Skip to content

Commit

Permalink
Fix token scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jun 20, 2024
1 parent 114583d commit 9665b77
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
12 changes: 8 additions & 4 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,15 @@ func (c *CRProxy) AuthToken(rw http.ResponseWriter, r *http.Request) {
}

func (c *CRProxy) authenticate(rw http.ResponseWriter, r *http.Request) {
var scheme = "http"
if r.TLS != nil {
scheme = "https"
tokenURL := c.tokenURL
if tokenURL == "" {
var scheme = "http"
if r.TLS != nil {
scheme = "https"
}
tokenURL = scheme + "://" + r.Host + "/auth/token"
}
header := fmt.Sprintf("Bearer realm=%q,service=%q", scheme+"://"+r.Host+"/auth/token", r.Host)
header := fmt.Sprintf("Bearer realm=%q,service=%q", tokenURL, r.Host)
rw.Header().Set("WWW-Authenticate", header)
c.errorResponse(rw, r, errcode.ErrorCodeUnauthorized)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/crproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ var (
enablePprof bool
defaultRegistry string
simpleAuth bool
tokenURL string
)

func init() {
Expand All @@ -79,6 +80,7 @@ func init() {
pflag.BoolVar(&enablePprof, "enable-pprof", false, "Enable pprof")
pflag.StringVar(&defaultRegistry, "default-registry", "", "default registry used for non full-path docker pull, like:docker.io")
pflag.BoolVar(&simpleAuth, "simple-auth", false, "enable simple auth")
pflag.StringVar(&tokenURL, "token-url", "", "token url")
pflag.Parse()
}

Expand Down Expand Up @@ -283,7 +285,7 @@ func main() {
}

if simpleAuth {
opts = append(opts, crproxy.WithSimpleAuth(true))
opts = append(opts, crproxy.WithSimpleAuth(true, tokenURL))
}

crp, err := crproxy.NewCRProxy(opts...)
Expand Down
4 changes: 3 additions & 1 deletion crproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ type CRProxy struct {
privilegedIPSet map[string]struct{}
disableTagsList bool
simpleAuth bool
tokenURL string
matcher hostmatcher.Matcher

defaultRegistry string
}

type Option func(c *CRProxy)

func WithSimpleAuth(b bool) Option {
func WithSimpleAuth(b bool, tokenURL string) Option {
return func(c *CRProxy) {
c.simpleAuth = b
c.tokenURL = tokenURL
}
}

Expand Down

0 comments on commit 9665b77

Please sign in to comment.