mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-22 16:48:30 +01:00
Merge pull request #230 from ywk253100/auth_of_api
add authtication to repository API
This commit is contained in:
commit
f2db860021
@ -29,7 +29,6 @@ import (
|
||||
svc_utils "github.com/vmware/harbor/service/utils"
|
||||
"github.com/vmware/harbor/utils/log"
|
||||
"github.com/vmware/harbor/utils/registry"
|
||||
"github.com/vmware/harbor/utils/registry/auth"
|
||||
"github.com/vmware/harbor/utils/registry/errors"
|
||||
)
|
||||
|
||||
@ -40,21 +39,11 @@ import (
|
||||
type RepositoryAPI struct {
|
||||
BaseAPI
|
||||
userID int
|
||||
username string
|
||||
}
|
||||
|
||||
// Prepare will set a non existent user ID in case the request tries to view repositories under a project he doesn't has permission.
|
||||
func (ra *RepositoryAPI) Prepare() {
|
||||
userID, ok := ra.GetSession("userId").(int)
|
||||
if !ok {
|
||||
userID = dao.NonExistUserID
|
||||
}
|
||||
ra.userID = userID
|
||||
|
||||
username, ok := ra.GetSession("username").(string)
|
||||
if ok {
|
||||
ra.username = username
|
||||
}
|
||||
ra.userID = ra.ValidateUser()
|
||||
}
|
||||
|
||||
// Get ...
|
||||
@ -250,19 +239,6 @@ func (ra *RepositoryAPI) GetManifests() {
|
||||
}
|
||||
|
||||
func (ra *RepositoryAPI) initializeRepositoryClient(repoName string) (r *registry.Repository, err error) {
|
||||
endpoint := os.Getenv("REGISTRY_URL")
|
||||
|
||||
//no session, use basic auth
|
||||
if ra.userID == dao.NonExistUserID {
|
||||
username, password, _ := ra.Ctx.Request.BasicAuth()
|
||||
credential := auth.NewBasicAuthCredential(username, password)
|
||||
|
||||
return registry.NewRepositoryWithCredential(repoName, endpoint, credential)
|
||||
|
||||
}
|
||||
|
||||
//session exists, use username
|
||||
if len(ra.username) == 0 {
|
||||
u := models.User{
|
||||
UserID: ra.userID,
|
||||
}
|
||||
@ -271,8 +247,7 @@ func (ra *RepositoryAPI) initializeRepositoryClient(repoName string) (r *registr
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ra.username = user.Username
|
||||
}
|
||||
endpoint := os.Getenv("REGISTRY_URL")
|
||||
|
||||
return registry.NewRepositoryWithUsername(repoName, endpoint, ra.username)
|
||||
return registry.NewRepositoryWithUsername(repoName, endpoint, user.Username)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user