mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-26 20:26:13 +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
|
||||
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 {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}{
|
||||
Username: username,
|
||||
Password: password,
|
||||
})
|
||||
if err != nil {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add(AuthTokenHeader, token)
|
||||
|
||||
return send(client, req)
|
||||
}
|
||||
|
||||
@ -228,7 +228,7 @@ func buildSpecificUserAuthCtxURL(url, principalID string) string {
|
||||
strings.TrimRight(url, "/"), principalID)
|
||||
}
|
||||
|
||||
// TODO update the url
|
||||
func buildLoginURL(url string) string {
|
||||
return strings.TrimRight(url, "/") + "/sso/login"
|
||||
func buildLoginURL(url, principalID string) string {
|
||||
return fmt.Sprintf("%s/auth/idm/principals/%s/security-context",
|
||||
strings.TrimRight(url, "/"), principalID)
|
||||
}
|
||||
|
@ -52,6 +52,8 @@ var (
|
||||
// AdmiralClient is initialized only under integration deploy mode
|
||||
// and can be passed to project manager as a parameter
|
||||
AdmiralClient *http.Client
|
||||
// TokenReader is used in integration mode to read token
|
||||
TokenReader pms.TokenReader
|
||||
)
|
||||
|
||||
// Init configurations
|
||||
@ -126,10 +128,11 @@ func initProjectManager() {
|
||||
path = defaultTokenFilePath
|
||||
}
|
||||
log.Infof("service token file path: %s", path)
|
||||
TokenReader = &pms.FileTokenReader{
|
||||
Path: path,
|
||||
}
|
||||
GlobalProjectMgr = pms.NewProjectManager(AdmiralClient,
|
||||
AdmiralEndpoint(), &pms.FileTokenReader{
|
||||
Path: path,
|
||||
})
|
||||
AdmiralEndpoint(), TokenReader)
|
||||
}
|
||||
|
||||
// Load configurations
|
||||
|
@ -133,8 +133,13 @@ func (b *basicAuthReqCtxModifier) Modify(ctx *beegoctx.Context) bool {
|
||||
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,
|
||||
config.AdmiralEndpoint(), username, password)
|
||||
config.AdmiralEndpoint(), username, password, token)
|
||||
if err != nil {
|
||||
log.Errorf("failed to authenticate %s: %v", username, err)
|
||||
return false
|
||||
|
Loading…
Reference in New Issue
Block a user