Authenticating to the Che server using OpenID
OpenID authentication on the Che server implies the presence of an external OpenID Connect provider and has the following main steps:
-
Authenticate the user through a JWT token that is retrieved from an HTTP request or, in case of a missing or invalid token, redirect the user to the Keycloak login page.
-
Send authentication tokens in an Authorization header. In limited cases, when it is not possible to use the Authorization header, the token can be sent in the token query parameter. Example: OAuth authentication initialization.
-
Compose an internal
subjectobject that represents the current user inside the Che server code.
| The only supported and tested OpenID provider is Keycloak. |
To authenticate to the Che server using OpenID authentication:
-
Request the OpenID settings service where clients can find all the necessary URLs and properties of the OpenId provider, such as
jwks.endpoint,token.endpoint,logout.endpoint,realm.name, orclient_idreturned in the JSON format. -
The service URL is
<che.host>:<che.port>/api/keycloak/settings, and it is only available in the Che multi-user mode. The presence of the service in the URL confirms that the authentication is enabled in the current deployment.Example output:
{ "che.keycloak.token.endpoint": "http://172.19.20.9:5050/auth/realms/che/protocol/openid-connect/token", "che.keycloak.profile.endpoint": "http://172.19.20.9:5050/auth/realms/che/account", "che.keycloak.client_id": "che-public", "che.keycloak.auth_server_url": "http://172.19.20.9:5050/auth", "che.keycloak.password.endpoint": "http://172.19.20.9:5050/auth/realms/che/account/password", "che.keycloak.logout.endpoint": "http://172.19.20.9:5050/auth/realms/che/protocol/openid-connect/logout", "che.keycloak.realm": "che" }The service allows downloading the JavaScript client library to interact with the provider using the
<che.host>:<che.port>/api/keycloak/OIDCKeycloak.jsURL. -
Redirect the user to the appropriate provider’s login page with all the necessary parameters, including
client_idand the return redirection path. This can be done with any client library (JS or Java). -
When the user is logged in to the provider, the client side-code is obtained, and the JWT token has validated the token, the creation of the
subjectbegins.
The verification of the token signature occurs in two main steps:
-
Authentication: The token is extracted from the Authorization header or from the
tokenquery parameter and is parsed using the public key retrieved from the provider. In case of expired, invalid, or malformed tokens, a403error is sent to the user. The use of the query parameter should be minimised because its support may be limited or removed in future versions.If the validation is successful, the parsed form of the token is passed to the environment initialization step:
-
Environment initialization: The filter extracts data from the JWT token claims, creates the user in the local database if it is not yet present, and constructs the
subjectobject and sets it into the per-request EnvironmentContext object, which is statically accessible everywhere.If the request was made using only a machine token, the following single authentication filter is used:
org.eclipse.che.multiuser.machine.authentication.server.MachineLoginFilter: The filter finds the user that the
userIdtoken belongs to, retrieves the user instance, and sets the principal to the session. The Che server-to-server requests are performed using a dedicated request factory that signs every request with the current subject token obtained from theEnvironmentContextobject.
|
Providing user-specific data
Since Keycloak may store user-specific information (first and last name, phone number, job title), there is a special implementation of the ProfileDao that can provide this data to consumers. The implementation is read-only, so users cannot perform create and update operations. |
Obtaining the token from credentials through Keycloak
Clients that cannot run JavaScript or other clients (such as command-line clients or Selenium tests) must request the authorization token directly from Keycloak.
To obtain the token, send a request to the token endpoint with the username and password credentials. This request can be schematically described as the following cURL request:
$ curl --data "grant_type=password&client_id=<client_name>&username=<username>&password=<password>" \ http://<keyckloak_host>:5050/auth/realms/<realm_name>/protocol/openid-connect/token
The Che dashboard uses a customized Keycloak login page and an authentication mechanism based on grant_type=authorization_code. It is a two-step authentication process:
-
Logging in and obtaining the authorization code.
-
Obtaining the token using this authorization code.
Obtaining the token from the OpenShift token through Keycloak
When Che has been installed on OpenShift using the Operator, and the OpenShift OAuth integration has been enabled (it is by default), then the Che authentication token of a user can also be retrieved from the user OpenShift token.
To do this, send a request to the token endpoint that can be schematically described as the following cURL request:
$ curl -X POST -d "client_id=<client_name>" \ --data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \ -d "subject_token=<user_openshift_token>" \ -d "subject_issuer=<openshift_identity_provider_name>" \ --data-urlencode "subject_token_type=urn:ietf:params:oauth:token-type:access_token" \ http://<keyckloak_host>:5050/auth/realms/<realm_name>/protocol/openid-connect/token
The default values for <openshift_identity_provider_name> are:
-
On OpenShift 3.11:
openshift-v3 -
On OpenShift 4.x:
openshift-v4
<user_openshift_token> is the token retrieved by the end-user with the command:
$ oc whoami --show-token
| Before using this token exchange feature for an end user, the end user should have logged in at least once interactively to the Che Dashboard through the OpenShift login page. This is required to properly link OpenShift and Keycloak user accounts, and set the required user profile information (email, first and last names). |
Authenticating to the Che server using other authentication implementations
This procedure describes how to use an OIDC authentication implementation other than Keycloak.
-
Update the authentication configuration parameters that are stored in the
multiuser.propertiesfile (such as client ID, authentication URL, realm name). -
Write a single filter or a chain of filters to validate tokens, create the user in the Che dashboard, and compose the
subjectobject. -
If the new authorization provider supports the OpenID protocol, use the OIDC JS client library available at the settings endpoint because it is decoupled from specific implementations.
-
If the selected provider stores additional data about the user (first and last name, job title), it is recommended to write a provider-specific ProfileDao implementation that provides this information.
Authenticating to the Che server using OAuth
For easy user interaction with third-party services, the Che server supports OAuth authentication. OAuth tokens are also used for GitHub-related plug-ins.
OAuth authentication has two main flows: internal and external, which is based on the Keycloak brokering mechanism. The following are the two main OAuth API implementations:
-
org.eclipse.che.security.oauth.EmbeddedOAuthAPI, for the internal flow.
-
org.eclipse.che.multiuser.keycloak.server.oauth2.DelegatedOAuthAPI, for the external flow.
Use the che.oauth.service_mode=<embedded|delegated> configuration property to switch between the two implementations. The main REST endpoint in the OAuth API is org.eclipse.che.security.oauth.OAuthAuthenticationService, which contains:
-
An authenticate method to start the OAuth authentication flow
-
A callback method to process callbacks from the provider
-
A token to retrieve the current user’s OAuth token
These methods refer to the currently activated embedded or delegated OAuthAPI that does the underlying operations, including finding the appropriate authenticator, initializing the login process, and user forwarding.
Using Swagger or REST clients to execute queries
The user’s Keycloak token is used to execute queries to the secured API on the user’s behalf through REST clients. A valid token must be attached as the Request header or the ?token=$token query parameter.
Access the Che Swagger interface at http://che_host:8080/swagger. The user must be signed in through Keycloak, so that the access token is included in the Request header.