mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-08 17:08:17 +01:00
enable basic authentication
This commit is contained in:
parent
3b71580986
commit
615e4973c1
15
api/base.go
15
api/base.go
@ -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")
|
||||
|
@ -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")
|
||||
|
||||
|
@ -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 "", ""
|
||||
|
Loading…
Reference in New Issue
Block a user