Merge pull request #230 from ywk253100/auth_of_api

add authtication to repository API
This commit is contained in:
Daniel Jiang 2016-05-16 16:34:39 +08:00
commit f2db860021

View File

@ -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)
}