The authentication is implemented in internal/auth/
. In auth.go
an interface is defined that any authentication provider must fulfill. It also
acts as a dispatcher to delegate the calls to the available authentication
providers.
Two authentication types are available:
The most important routines in auth are:
Login()
Handle POST request to login user and start a new sessionAuth()
Authenticate user and put User Object in context of the requestThe http router calls auth in the following cases:
r.Handle("/login", authentication.Login( ... )).Methods(http.MethodPost)
:
The POST request on the /login
route will call the Login callback.r.Handle("/jwt-login", authentication.Login( ... ))
:
Any request on the /jwt-login
route will call the Login callback. Intended
for use for the JWT token based authenticators.secured.Use(func(next http.Handler) http.Handler {
return authentication.Auth(
// On success;
next,
// On failure:
func(rw http.ResponseWriter, r *http.Request, err error) {
// Render login form
})
})
A JWT token can be used to initiate an authenticated user session. This can either happen by calling the login route with a token provided in a header or via a special cookie containing the JWT token. For API routes the access is authenticated on every request using the JWT token and no session is initiated.
The Login function (located in auth.go
):
CanLogin
function which checks if the authentication method is
supported for this user.Login
function to authenticate the user. On success a valid user
object is returned.onSuccess
http handlerThis authenticator is applied if
return user != nil && user.AuthSource == AuthViaLocalPassword
Compares the password provided by the login form to the password hash stored in the user database table:
if e := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(r.FormValue("password"))); e != nil {
log.Errorf("AUTH/LOCAL > Authentication for user %s failed!", user.Username)
return nil, fmt.Errorf("Authentication failed")
}
This authenticator is applied if the user was found in the database and its AuthSource is LDAP:
if user != nil {
if user.AuthSource == schema.AuthViaLDAP {
return user, true
}
}
If the option SyncUserOnLogin
is set it tried to sync the user from the LDAP
directory. In case this succeeds the user is persisted to the database and can
login.
Gets the LDAP connection and tries a bind with the provided credentials:
if err := l.Bind(userDn, r.FormValue("password")); err != nil {
log.Errorf("AUTH/LDAP > Authentication for user %s failed: %v", user.Username, err)
return nil, fmt.Errorf("Authentication failed")
}
Login via JWT token will create a session without password.
For login the X-Auth-Token
header is not supported. This authenticator is
applied if the Authorization header or query parameter login-token is present:
return user, r.Header.Get("Authorization") != "" ||
r.URL.Query().Get("login-token") != ""
The Login function:
sub
: The subject, in this case this is the usernameexp
: Expiration in Unix epoch timeroles
: String array with roles of userSyncUserOnLogin
is set add user to user database table with AuthViaToken
AuthSource.Login via JWT cookie token will create a session without password. It is first checked if the required configuration options are set:
trustedIssuer
CookieName
The environment variable CROSS_LOGIN_JWT_PUBLIC_KEY
is required as well:
It is used to verify the identity of the trustedIssuer
.
The public key must match accordingly.
This authenticator is applied if the configured cookie is present:
jwtCookie, err := r.Cookie(cookieName)
if err == nil && jwtCookie.Value != "" {
return true
}
The Login function:
iss
issuer claim matched trusted issuer from configurationvalidateUser
the roles are
extracted from JWT token or taken from user object fetched from databaseSyncUserOnLogin
is set add user to user database table with AuthViaToken
AuthSource.The Auth function (located in auth.go
):
AuthViaJWT()
AuthViaSession()
Implemented in JWTAuthenticator:
X-Auth-Token
or Authorization
with Bearer
prefixvalidateUser
is set it will ensure the
user object exists in the database and takes the roles from the database userWas this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.