2023-07-05 09:50:44 +02:00
|
|
|
# Overview
|
|
|
|
|
2023-08-11 10:00:23 +02:00
|
|
|
The authentication is implemented in `internal/auth/`. In `auth.go`
|
2023-07-05 10:01:46 +02:00
|
|
|
an interface is defined that any authentication provider must fulfill. It also
|
2023-07-05 09:50:44 +02:00
|
|
|
acts as a dispatcher to delegate the calls to the available authentication
|
|
|
|
providers.
|
|
|
|
|
2023-08-11 10:00:23 +02:00
|
|
|
Two authentication types are available:
|
|
|
|
* JWT authentication for the REST API that does not create a session cookie
|
|
|
|
* Session based authentication using a session cookie
|
|
|
|
|
|
|
|
The most important routines in auth are:
|
2023-07-05 09:50:44 +02:00
|
|
|
* `Login()` Handle POST request to login user and start a new session
|
|
|
|
* `Auth()` Authenticate user and put User Object in context of the request
|
|
|
|
|
|
|
|
The 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.
|
|
|
|
* Any route in the secured subrouter will always call Auth(), on success it will
|
2023-07-05 10:01:46 +02:00
|
|
|
call the next handler in the chain, on failure it will render the login
|
2023-07-05 09:50:44 +02:00
|
|
|
template.
|
|
|
|
```
|
|
|
|
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
|
|
|
|
})
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2023-08-11 10:00:23 +02:00
|
|
|
A JWT token can be used to initiate an authenticated user
|
2023-07-05 10:15:12 +02:00
|
|
|
session. This can either happen by calling the login route with a token
|
2023-08-11 10:00:23 +02:00
|
|
|
provided in a header or via a special cookie containing the JWT token.
|
2023-07-05 09:50:44 +02:00
|
|
|
For API routes the access is authenticated on every request using the JWT token
|
|
|
|
and no session is initiated.
|
|
|
|
|
|
|
|
# Login
|
|
|
|
|
|
|
|
The Login function (located in `auth.go`):
|
|
|
|
* Extracts the user name and gets the user from the user database table. In case the
|
2023-07-05 10:01:46 +02:00
|
|
|
user is not found the user object is set to nil.
|
2023-07-05 09:50:44 +02:00
|
|
|
* Iterates over all authenticators and:
|
2023-08-11 10:00:23 +02:00
|
|
|
- Calls its `CanLogin` function which checks if the authentication method is
|
|
|
|
supported for this user.
|
|
|
|
- Calls its `Login` function to authenticate the user. On success a valid user
|
2023-07-05 09:50:44 +02:00
|
|
|
object is returned.
|
|
|
|
- Creates a new session object, stores the user attributes in the session and
|
|
|
|
saves the session.
|
2023-08-11 10:00:23 +02:00
|
|
|
- If the user does not yet exist in the database try to add the user
|
2023-07-05 10:01:46 +02:00
|
|
|
- Starts the `onSuccess` http handler
|
2023-07-05 09:50:44 +02:00
|
|
|
|
|
|
|
## Local authenticator
|
|
|
|
|
|
|
|
This 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("AUTH/LOCAL > Authentication failed")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## LDAP authenticator
|
|
|
|
|
|
|
|
This authenticator is applied if
|
|
|
|
```
|
|
|
|
return user != nil && user.AuthSource == AuthViaLDAP
|
|
|
|
```
|
|
|
|
|
|
|
|
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/LOCAL > Authentication for user %s failed: %v", user.Username, err)
|
|
|
|
return nil, fmt.Errorf("AUTH/LDAP > Authentication failed")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-08-11 10:00:23 +02:00
|
|
|
## JWT Session authenticator
|
2023-07-05 09:50:44 +02:00
|
|
|
|
|
|
|
Login via JWT token will create a session without password.
|
2023-08-11 10:00:23 +02:00
|
|
|
For login the `X-Auth-Token` header is not supported. This authenticator is
|
|
|
|
applied if the Authorization header is present:
|
2023-07-05 09:50:44 +02:00
|
|
|
```
|
2023-08-11 10:00:23 +02:00
|
|
|
return r.Header.Get("Authorization") != ""
|
2023-07-05 09:50:44 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
The Login function:
|
|
|
|
* Parses the token
|
|
|
|
* Check if the signing method is EdDSA or HS256 or HS512
|
|
|
|
* Check if claims are valid and extracts the claims
|
|
|
|
* The following claims have to be present:
|
|
|
|
- `sub`: The subject, in this case this is the username
|
|
|
|
- `exp`: Expiration in Unix epoch time
|
|
|
|
- `roles`: String array with roles of user
|
|
|
|
* In case user is not yet set, which is usually the case:
|
|
|
|
- Try to fetch user from database
|
2023-07-05 10:01:46 +02:00
|
|
|
- In case user is not yet present add user to user database table with `AuthViaToken` AuthSource.
|
2023-07-05 09:50:44 +02:00
|
|
|
* Return valid user object
|
|
|
|
|
2023-08-11 10:00:23 +02:00
|
|
|
## JWT Cookie Session authenticator
|
|
|
|
|
|
|
|
Login via JWT cookie token will create a session without password.
|
|
|
|
It is first checked if the required configuration keys are set:
|
|
|
|
* `publicKeyCrossLogin`
|
|
|
|
* `TrustedExternalIssuer`
|
|
|
|
* `CookieName`
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
2023-07-05 09:50:44 +02:00
|
|
|
# Auth
|
|
|
|
|
|
|
|
The Auth function (located in `auth.go`):
|
|
|
|
* Returns a new http handler function that is defined right away
|
|
|
|
* This handler iterates over all authenticators
|
|
|
|
* Calls `Auth()` on every authenticator
|
|
|
|
* If err is not nil and the user object is valid it puts the user object in the
|
2023-07-05 10:01:46 +02:00
|
|
|
request context and starts the onSuccess http handler
|
|
|
|
* Otherwise it calls the onFailure handler
|
2023-07-05 09:50:44 +02:00
|
|
|
|
|
|
|
## Local
|
|
|
|
|
|
|
|
Calls the `AuthViaSession()` function in `auth.go`. This will extract username,
|
|
|
|
projects and roles from the session and initialize a user object with those
|
|
|
|
values.
|
|
|
|
|
|
|
|
## LDAP
|
|
|
|
|
|
|
|
Calls the `AuthViaSession()` function in `auth.go`. This will extract username,
|
|
|
|
projects and roles from the session and initialize a user object with those
|
|
|
|
values.
|
|
|
|
|
|
|
|
# JWT
|
|
|
|
|
|
|
|
Check for JWT token:
|
|
|
|
* Is token passed in the `X-Auth-Token` or `Authorization` header
|
|
|
|
* If no token is found in a header it tries to read the token from a configured
|
|
|
|
cookie.
|
|
|
|
|
|
|
|
Finally it calls AuthViaSession in `auth.go` if a valid session exists. This is
|
|
|
|
true if a JWT token was previously used to initiate a session. In this case the
|
2023-07-05 10:01:46 +02:00
|
|
|
user object initialized with the session is returned right away.
|
2023-07-05 09:50:44 +02:00
|
|
|
|
|
|
|
In case a token was found extract and parse the token:
|
|
|
|
* Check if signing method is Ed25519/EdDSA
|
|
|
|
* In case publicKeyCrossLogin is configured:
|
|
|
|
- Check if `iss` issuer claim matched trusted issuer from configuration
|
|
|
|
- Return public cross login key
|
|
|
|
- Otherwise return standard public key
|
|
|
|
* Check if claims are valid
|
|
|
|
* Depending on the option `ForceJWTValidationViaDatabase ` the roles are
|
|
|
|
extracted from JWT token or taken from user object fetched from database
|
|
|
|
* In case the token was extracted from cookie create a new session and ask the
|
|
|
|
browser to delete the JWT cookie
|
|
|
|
* Return valid user object
|
|
|
|
|