Skip to content

Commit

Permalink
Add log for auth
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jul 16, 2024
1 parent a85916a commit e595b2c
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"hash"
"io"
"net/http"
"net/url"
"strings"
"time"
"net/url"

"github.com/distribution/distribution/v3/registry/api/errcode"
)
Expand All @@ -32,10 +32,6 @@ func (c *CRProxy) AuthToken(rw http.ResponseWriter, r *http.Request) {

if c.simpleAuthUserpassFunc != nil {
authorization := r.Header.Get("Authorization")
if authorization == "" {
errcode.ServeJSON(rw, errcode.ErrorCodeUnauthorized)
return
}
auth := strings.SplitN(authorization, " ", 2)
if len(auth) != 2 {
errcode.ServeJSON(rw, errcode.ErrorCodeUnauthorized)
Expand All @@ -49,18 +45,27 @@ func (c *CRProxy) AuthToken(rw http.ResponseWriter, r *http.Request) {
return
}

p := false
var u *url.Userinfo
if ok {
p = c.simpleAuthUserpassFunc(r, url.UserPassword(user, pass))
u = url.UserPassword(user, pass)
} else {
p = c.simpleAuthUserpassFunc(r, url.User(user))
u = url.User(user)
}
if !p {
if !c.simpleAuthUserpassFunc(r, u) {
if c.logger != nil {
c.logger.Println("Login failed", u)
}
errcode.ServeJSON(rw, errcode.ErrorCodeUnauthorized)
return
}

if c.logger != nil {
c.logger.Println("Login succeed", u.Username())
}
default:
// TODO: Support others authorization
if c.logger != nil {
c.logger.Println("Unsupported authorization", authorization)
}
errcode.ServeJSON(rw, errcode.ErrorCodeUnauthorized)
return
}
Expand Down

0 comments on commit e595b2c

Please sign in to comment.