mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-18 12:51:27 +01:00
support basic auth in integration mode
This commit is contained in:
parent
a9a79736b4
commit
fc2bb51582
@ -170,23 +170,23 @@ func get(client *http.Client, url, token string, username ...string) (*AuthConte
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Login with credential and returns auth context and error
|
// Login with credential and returns auth context and error
|
||||||
func Login(client *http.Client, url, username, password string) (*AuthContext, error) {
|
func Login(client *http.Client, url, username, password, token string) (*AuthContext, error) {
|
||||||
data, err := json.Marshal(&struct {
|
data, err := json.Marshal(&struct {
|
||||||
Username string `json:"username"`
|
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}{
|
}{
|
||||||
Username: username,
|
|
||||||
Password: password,
|
Password: password,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodPost, buildLoginURL(url), bytes.NewBuffer(data))
|
req, err := http.NewRequest(http.MethodPost, buildLoginURL(url, username), bytes.NewBuffer(data))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req.Header.Add(AuthTokenHeader, token)
|
||||||
|
|
||||||
return send(client, req)
|
return send(client, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,7 +228,7 @@ func buildSpecificUserAuthCtxURL(url, principalID string) string {
|
|||||||
strings.TrimRight(url, "/"), principalID)
|
strings.TrimRight(url, "/"), principalID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO update the url
|
func buildLoginURL(url, principalID string) string {
|
||||||
func buildLoginURL(url string) string {
|
return fmt.Sprintf("%s/auth/idm/principals/%s/security-context",
|
||||||
return strings.TrimRight(url, "/") + "/sso/login"
|
strings.TrimRight(url, "/"), principalID)
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,8 @@ var (
|
|||||||
// AdmiralClient is initialized only under integration deploy mode
|
// AdmiralClient is initialized only under integration deploy mode
|
||||||
// and can be passed to project manager as a parameter
|
// and can be passed to project manager as a parameter
|
||||||
AdmiralClient *http.Client
|
AdmiralClient *http.Client
|
||||||
|
// TokenReader is used in integration mode to read token
|
||||||
|
TokenReader pms.TokenReader
|
||||||
)
|
)
|
||||||
|
|
||||||
// Init configurations
|
// Init configurations
|
||||||
@ -126,10 +128,11 @@ func initProjectManager() {
|
|||||||
path = defaultTokenFilePath
|
path = defaultTokenFilePath
|
||||||
}
|
}
|
||||||
log.Infof("service token file path: %s", path)
|
log.Infof("service token file path: %s", path)
|
||||||
|
TokenReader = &pms.FileTokenReader{
|
||||||
|
Path: path,
|
||||||
|
}
|
||||||
GlobalProjectMgr = pms.NewProjectManager(AdmiralClient,
|
GlobalProjectMgr = pms.NewProjectManager(AdmiralClient,
|
||||||
AdmiralEndpoint(), &pms.FileTokenReader{
|
AdmiralEndpoint(), TokenReader)
|
||||||
Path: path,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load configurations
|
// Load configurations
|
||||||
|
@ -133,8 +133,13 @@ func (b *basicAuthReqCtxModifier) Modify(ctx *beegoctx.Context) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
token, err := config.TokenReader.ReadToken()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to read solution user token: %v", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
authCtx, err := authcontext.Login(config.AdmiralClient,
|
authCtx, err := authcontext.Login(config.AdmiralClient,
|
||||||
config.AdmiralEndpoint(), username, password)
|
config.AdmiralEndpoint(), username, password, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to authenticate %s: %v", username, err)
|
log.Errorf("failed to authenticate %s: %v", username, err)
|
||||||
return false
|
return false
|
||||||
|
Loading…
Reference in New Issue
Block a user