enable basic authentication

This commit is contained in:
Tan Jiang 2016-03-07 22:27:47 +08:00
parent 3b71580986
commit 615e4973c1
3 changed files with 20 additions and 8 deletions

View File

@ -17,10 +17,13 @@ package api
import (
"encoding/json"
"log"
"net/http"
"github.com/vmware/harbor/auth"
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
"github.com/vmware/harbor/utils"
"github.com/astaxie/beego"
)
@ -52,6 +55,18 @@ func (b *BaseAPI) DecodeJSONReq(v interface{}) {
// ValidateUser checks if the request triggered by a valid user
func (b *BaseAPI) ValidateUser() int {
username, password := utils.ParseBasicAuth(b.Ctx.Request)
if username != "" {
log.Printf("Requst with Basic Authentication header, username: %s", username)
user, err := auth.Login(models.AuthModel{username, password})
if err != nil {
log.Printf("Error while trying to login, username: %s, error: %v", username, err)
user = nil
}
if user != nil {
return user.UserID
}
}
sessionUserID := b.GetSession("userId")
if sessionUserID == nil {
beego.Warning("No user id in session, canceling request")

View File

@ -39,13 +39,9 @@ type TokenHandler struct {
func (a *TokenHandler) Get() {
request := a.Ctx.Request
log.Println("request url: " + request.URL.String())
authorization := request.Header["Authorization"]
log.Println("authorization:", authorization)
username, password := utils.ParseBasicAuth(authorization)
username, password := utils.ParseBasicAuth(request)
authenticated := authenticate(username, password)
service := a.GetString("service")
scope := a.GetString("scope")

View File

@ -17,11 +17,11 @@ package utils
import (
"encoding/base64"
"net/http"
"strings"
"github.com/vmware/harbor/models"
"github.com/astaxie/beego"
"github.com/vmware/harbor/models"
)
// Repository holds information about repository
@ -30,7 +30,8 @@ type Repository struct {
}
// ParseBasicAuth parses the basic authorization
func ParseBasicAuth(authorization []string) (username, password string) {
func ParseBasicAuth(req *http.Request) (username, password string) {
authorization := req.Header["Authorization"]
if authorization == nil || len(authorization) == 0 {
beego.Debug("Authorization header is not set.")
return "", ""