2017-04-13 12:54:58 +02:00
|
|
|
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-02-26 11:54:14 +01:00
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
package auth
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-10-18 08:39:02 +02:00
|
|
|
"time"
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2018-01-10 08:58:01 +01:00
|
|
|
"github.com/vmware/harbor/src/common"
|
|
|
|
"github.com/vmware/harbor/src/common/dao"
|
2016-10-19 08:32:00 +02:00
|
|
|
"github.com/vmware/harbor/src/common/models"
|
2016-11-16 06:25:37 +01:00
|
|
|
"github.com/vmware/harbor/src/common/utils/log"
|
|
|
|
"github.com/vmware/harbor/src/ui/config"
|
2016-02-01 12:59:10 +01:00
|
|
|
)
|
|
|
|
|
2016-10-18 08:39:02 +02:00
|
|
|
// 1.5 seconds
|
|
|
|
const frozenTime time.Duration = 1500 * time.Millisecond
|
|
|
|
|
|
|
|
var lock = NewUserLock(frozenTime)
|
|
|
|
|
2018-03-01 08:40:39 +01:00
|
|
|
//ErrAuth is the type of error to indicate a failed authentication due to user's error.
|
|
|
|
type ErrAuth struct {
|
|
|
|
details string
|
|
|
|
}
|
|
|
|
|
|
|
|
//Error ...
|
|
|
|
func (ea ErrAuth) Error() string {
|
|
|
|
return fmt.Sprintf("Failed to authenticate user, due to error '%s'", ea.details)
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewErrAuth ...
|
|
|
|
func NewErrAuth(msg string) ErrAuth {
|
|
|
|
return ErrAuth{details: msg}
|
|
|
|
}
|
|
|
|
|
2017-12-13 13:58:27 +01:00
|
|
|
// AuthenticateHelper provides interface for user management in different auth modes.
|
2017-11-22 05:19:13 +01:00
|
|
|
type AuthenticateHelper interface {
|
2016-02-26 11:35:55 +01:00
|
|
|
|
2018-03-01 08:40:39 +01:00
|
|
|
// Authenticate authenticate the user based on data in m. Only when the error returned is an instance
|
|
|
|
// of ErrAuth, it will be considered a bad credentials, other errors will be treated as server side error.
|
2016-02-25 06:40:08 +01:00
|
|
|
Authenticate(m models.AuthModel) (*models.User, error)
|
2017-11-22 05:19:13 +01:00
|
|
|
// OnBoardUser will check if a user exists in user table, if not insert the user and
|
2017-12-13 13:58:27 +01:00
|
|
|
// put the id in the pointer of user model, if it does exist, fill in the user model based
|
|
|
|
// on the data record of the user
|
2017-11-22 05:19:13 +01:00
|
|
|
OnBoardUser(u *models.User) error
|
|
|
|
// Get user information from account repository
|
|
|
|
SearchUser(username string) (*models.User, error)
|
2017-12-08 08:07:36 +01:00
|
|
|
// Update user information after authenticate, such as Onboard or sync info etc
|
|
|
|
PostAuthenticate(u *models.User) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultAuthenticateHelper - default AuthenticateHelper implementation
|
|
|
|
type DefaultAuthenticateHelper struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authenticate ...
|
|
|
|
func (d *DefaultAuthenticateHelper) Authenticate(m models.AuthModel) (*models.User, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnBoardUser will check if a user exists in user table, if not insert the user and
|
|
|
|
// put the id in the pointer of user model, if it does exist, fill in the user model based
|
|
|
|
// on the data record of the user
|
|
|
|
func (d *DefaultAuthenticateHelper) OnBoardUser(u *models.User) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//SearchUser - Get user information from account repository
|
|
|
|
func (d *DefaultAuthenticateHelper) SearchUser(username string) (*models.User, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//PostAuthenticate - Update user information after authenticate, such as Onboard or sync info etc
|
|
|
|
func (d *DefaultAuthenticateHelper) PostAuthenticate(u *models.User) error {
|
|
|
|
return nil
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 05:19:13 +01:00
|
|
|
var registry = make(map[string]AuthenticateHelper)
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Register add different authenticators to registry map.
|
2017-12-13 13:58:27 +01:00
|
|
|
func Register(name string, h AuthenticateHelper) {
|
2016-02-01 12:59:10 +01:00
|
|
|
if _, dup := registry[name]; dup {
|
2017-12-21 13:07:23 +01:00
|
|
|
log.Infof("authenticator: %s has been registered,skip", name)
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
2017-12-13 13:58:27 +01:00
|
|
|
registry[name] = h
|
2017-12-21 13:07:23 +01:00
|
|
|
log.Debugf("Registered authencation helper for auth mode: %s", name)
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Login authenticates user credentials based on setting.
|
2016-02-25 06:40:08 +01:00
|
|
|
func Login(m models.AuthModel) (*models.User, error) {
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-12-30 11:04:01 +01:00
|
|
|
authMode, err := config.AuthMode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-10 08:58:01 +01:00
|
|
|
if authMode == "" || dao.IsSuperUser(m.Principal) {
|
|
|
|
authMode = common.DBAuth
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-03-25 08:08:32 +01:00
|
|
|
log.Debug("Current AUTH_MODE is ", authMode)
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
authenticator, ok := registry[authMode]
|
|
|
|
if !ok {
|
2016-02-01 12:59:10 +01:00
|
|
|
return nil, fmt.Errorf("Unrecognized auth_mode: %s", authMode)
|
|
|
|
}
|
2016-10-18 08:39:02 +02:00
|
|
|
if lock.IsLocked(m.Principal) {
|
|
|
|
log.Debugf("%s is locked due to login failure, login failed", m.Principal)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
user, err := authenticator.Authenticate(m)
|
2018-03-01 08:40:39 +01:00
|
|
|
if err != nil {
|
|
|
|
if _, ok = err.(ErrAuth); ok {
|
2018-02-05 14:00:19 +01:00
|
|
|
log.Debugf("Login failed, locking %s, and sleep for %v", m.Principal, frozenTime)
|
|
|
|
lock.Lock(m.Principal)
|
|
|
|
time.Sleep(frozenTime)
|
|
|
|
}
|
2018-03-01 08:40:39 +01:00
|
|
|
return nil, err
|
2016-10-18 08:39:02 +02:00
|
|
|
}
|
2017-12-08 08:07:36 +01:00
|
|
|
|
2018-01-16 15:18:56 +01:00
|
|
|
err = authenticator.PostAuthenticate(user)
|
2017-12-08 08:07:36 +01:00
|
|
|
|
2016-10-18 08:39:02 +02:00
|
|
|
return user, err
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2017-11-22 05:19:13 +01:00
|
|
|
|
2017-12-13 13:58:27 +01:00
|
|
|
func getHelper() (AuthenticateHelper, error) {
|
|
|
|
authMode, err := config.AuthMode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-11-22 05:19:13 +01:00
|
|
|
}
|
|
|
|
AuthenticateHelper, ok := registry[authMode]
|
|
|
|
if !ok {
|
2017-12-13 13:58:27 +01:00
|
|
|
return nil, fmt.Errorf("Can not get authenticator, authmode: %s", authMode)
|
2017-11-22 05:19:13 +01:00
|
|
|
}
|
|
|
|
return AuthenticateHelper, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnBoardUser will check if a user exists in user table, if not insert the user and
|
|
|
|
// put the id in the pointer of user model, if it does exist, return the user's profile.
|
|
|
|
func OnBoardUser(user *models.User) error {
|
2018-01-02 11:28:48 +01:00
|
|
|
log.Debugf("OnBoardUser, user %+v", user)
|
2017-12-13 13:58:27 +01:00
|
|
|
helper, err := getHelper()
|
2017-11-22 05:19:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-13 13:58:27 +01:00
|
|
|
return helper.OnBoardUser(user)
|
2017-11-22 05:19:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SearchUser --
|
|
|
|
func SearchUser(username string) (*models.User, error) {
|
2017-12-13 13:58:27 +01:00
|
|
|
helper, err := getHelper()
|
2017-11-22 05:19:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-13 13:58:27 +01:00
|
|
|
return helper.SearchUser(username)
|
2017-11-22 05:19:13 +01:00
|
|
|
}
|
2017-12-08 08:07:36 +01:00
|
|
|
|
|
|
|
// PostAuthenticate -
|
|
|
|
func PostAuthenticate(u *models.User) error {
|
|
|
|
helper, err := getHelper()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return helper.PostAuthenticate(u)
|
|
|
|
}
|