# Authentication Galaxy supports the following authentication mechanisms: - [Galaxy Database](#galaxy-database) - Galaxy-specific login using e-mail address and password (the default); - [OIDC and OAuth2.0](#oidc-and-oauth2-0) - Login to Galaxy through an external OpenID Connect identity provider (e.g. Google, Keycloak, LS Login), without having to create a Galaxy password; - [Authentication Framework](#authentication-framework) - A plugin-driven framework supporting LDAP/Active Directory and PAM; - [Proxy Authentication](#remote-user-authentication) - HTTP [remote user](http://httpd.apache.org/docs/current/mod/mod_cgi.html#env) provided by any front-end Web server. ## Galaxy Database Without any additional configuration Galaxy will simply use its database to maintain usernames and passwords. The Galaxy user interface and API provide functions allowing users to register accounts, change passwords, etc.... If deploying Galaxy using the default authentication option, user activation can be enabled also. This is documented [below](#user-activation). ## OIDC and OAuth2.0 Leveraging OpenID Connect (OIDC) protocol, we enable login to Galaxy without explicitly creating a Galaxy user. This feature is disabled by default. In short, to enable this feature, a Galaxy server admin has to take the following two steps: 1. Register the Galaxy instance as a client with an OIDC identity provider (IdP). The IdP issues a _client ID_ and a _client secret_, and needs to be told the callback URL `/authnz//callback`. 2. Configure Galaxy. In the `galaxy.yml` file enable the OIDC service using the `enable_oidc` key and set the two configuration files (i.e., `oidc_config_file` and `oidc_backends_config_file`), based on the IdP information. Samples for both files are in [oidc_config.xml.sample](https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy/config/sample/oidc_config.xml.sample) and [oidc_backends_config.xml.sample](https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy/config/sample/oidc_backends_config.xml.sample). Each `` entry in `oidc_backends_config_file` is one of the backends Galaxy ships: `google`, `globus`, `okta`, `azure`, `keycloak`, `cilogon`, `auth0`, `egi_checkin`, `lifescience` (LS Login), `einfracz`, `nfdi`, `tapis`, the deprecated `elixir`, and the generic `oidc` backend for any standards-compliant provider. Per-provider options such as `label`, `require_create_confirmation`, `require_user_activation` and `require_session_refresh` are documented in [oidc_backends_config.xml.sample](https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy/config/sample/oidc_backends_config.xml.sample). The configuration is explained with provider-specific details at [User Authentication Configuration](https://galaxyproject.org/authnz/config/oidc/). How to authenticate from the user perspective we describe [here](https://galaxyproject.org/authnz/use/oidc/). ### PKCE [PKCE](https://datatracker.ietf.org/doc/html/rfc7636) (Proof Key for Code Exchange) binds the authorization code the IdP returns to the browser that started the login, so an intercepted code is useless on its own. It is available for the `oidc`, `keycloak`, `cilogon` and `auth0` backends and is enabled per provider in `oidc_backends_config_file`: ```xml ... true ``` With PKCE enabled, each login works as follows: 1. `/authnz//login` generates a random code verifier and sends its base64url-encoded SHA-256 hash (`code_challenge`, `code_challenge_method=S256`) to the IdP with the authorization request. 2. The verifier is stored on the server with the rest of the login state, bound to the browser's Galaxy session and the provider. The record expires after ten minutes, so the sign-in at the IdP has to finish within that time. 3. `/authnz//callback` consumes that record, so each login attempt can complete only once, and sends the verifier with the token request. The IdP issues tokens only when the verifier hashes to the challenge from step 1. For this to work, the browser must send its Galaxy session cookie back on the callback request. The `redirect_uri` therefore has to use the same host name users browse Galaxy under (for example, `localhost` and `127.0.0.1` are different hosts to the browser). Set `cookie_domain` in `galaxy.yml` if login and callback are served from different subdomains. To check that PKCE is active, open the browser's developer tools before logging in. The redirect to the IdP carries `code_challenge` and `code_challenge_method=S256` in its query string. To make the IdP reject logins without PKCE, require it on the client there. In Keycloak this is _Clients > your client > Advanced > Proof Key for Code Exchange Code Challenge Method_ set to `S256`. Galaxy refuses the login itself when the callback arrives without the matching login state, so it never depends on the IdP enforcing PKCE. Users then see "Authentication has expired or is invalid. Please start logging in again." If the IdP rejects the verifier, users see "Authentication with `` was canceled or the authorization code has expired". ### Authorization request parameters You can customize the login request Galaxy sends to an identity provider with standard [OIDC authorization request parameters](https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest). For example, you can request a particular authentication level or ask the IdP to re-authenticate users whose last authentication at the IdP is too old. The following parameters are supported by Galaxy's OIDC backends except `azure` and `tapis`, and require social-auth-core 4.7.0 or later: | Parameter | Setter property | Purpose | | ------------ | --------------- | -------------------------------------------------------------- | | `acr_values` | `ACR_VALUES` | Request authentication contexts understood by the IdP. | | `login_hint` | `LOGIN_HINT` | Suggest the user identifier to use at login. | | `max_age` | `MAX_AGE` | Request re-authentication after the specified age, in seconds. | | `ui_locales` | `UI_LOCALES` | Request preferred languages for the IdP's login interface. | | `display` | `DISPLAY` | Request how the IdP displays its login interface. | Set these properties with `` elements inside `` in `oidc_config_file` (usually `config/oidc_config.xml`). Configure `prompt` separately with the existing `` element for the provider in `oidc_backends_config_file`. #### Configure a provider To apply a setting to one provider, prefix the Setter property with that provider's name in upper case followed by `_`: for example, `KEYCLOAK_ACR_VALUES`. Use the prefixes in this table for providers whose backend names differ from their configuration names: | Provider | Prefix | | ------------- | ----------------------- | | `google` | `GOOGLE_OPENIDCONNECT_` | | `okta` | `OKTA_OPENIDCONNECT_` | | `lifescience` | `LIFE_SCIENCE_` | | `einfracz` | `E_INFRA_CZ_` | | `nfdi` | `INFRAPROXY_` | Use `Type="int"` for `MAX_AGE` and `Type="str"` for the other properties. For example, these Setters request the REFEDS MFA authentication context for `keycloak` and a maximum authentication age of one hour for the generic `oidc` provider: ```xml ``` To provide a default for all supported providers, omit the prefix: for example, ``. A provider-specific setting takes precedence over that default. #### Verify the request Open the browser's developer tools before logging in, then inspect the redirect to the IdP. Its query string should contain the configured parameter names and values, such as `max_age=3600`. Galaxy silently ignores a Setter with an incorrect provider prefix, so check the prefix if a parameter is missing. The IdP determines how it handles these parameters. In particular, `acr_values` requests an authentication level, but Galaxy does not check the `acr` claim returned by the IdP. To require multi-factor authentication, enforce it for Galaxy's client at the IdP as well. ## Sending users back to another site after login After login, Galaxy sends users back to the page they came from, but only if that page is on Galaxy itself. If another site, such as a portal or a tool, sends its users to Galaxy to log in and needs them back afterwards, list that site's host in `galaxy.yml`: ```yaml galaxy: allowed_login_redirect_hosts: tools.example.org,portal.example.org:8443 ``` The site then links to Galaxy's login page with its own URL, percent-encoded, in `redirect`: ``` https://galaxy.example.org/login/start?redirect=https%3A%2F%2Ftools.example.org%2Fback%3Fitem%3D1 ``` After a password or OIDC login, users land on `https://tools.example.org/back?item=1`. Users who are already logged in are sent there straight away. The option is empty by default. ### Writing entries * An entry is a host name. Add the port if it is not 443: `portal.example.org:8443`. * Hosts must match exactly. There are no wildcards, so list every subdomain you trust. * Only `https` targets are followed. * Galaxy ignores entries that are not host names, such as full URLs or wildcards, and logs a warning at startup. ### Before adding a host * Every page on that host is trusted. If any page there forwards visitors to arbitrary URLs, anyone can use a Galaxy login link to send your users to any site. * The site learns nothing from the redirect. Galaxy does not add a token or any proof that the user logged in. * The list is public. Anyone can read it from `/api/configuration`. ### Troubleshooting * If users land on the Galaxy home page instead of the site, the target was refused. Galaxy logs `Ignoring post-login redirect target that is not allowed: `. Check that the target uses `https`, that its host and port match an entry, and that it was percent-encoded. * Users who link an OIDC identity to an existing Galaxy account land on the page listing their linked identities instead of the target. ## Authentication Framework Galaxy is distributed with a plugin-driven authentication framework for which the default database authentication is just one (and the default plugin). This framework can be used to allow Galaxy to delegate authentication to an LDAP server, an Active Directory server, or to PAM. Currently, we provide two variants of the LDAP authenticator namely, `ldap` and `ldap3`. Both are identical implementations that use different Python modules for binding and making queries to a LDAP server. The authenticator `ldap` is based on the [python-ldap](https://www.python-ldap.org/) module which is a wrapper around the OpenLDAP's client library `libldap`. Currently, `python-ldap` does not provide pre-built Python wheel packages and hence, the OpenLDAP client libraries are needed to install `python-ldap` on the Galaxy server. On the other hand, `ldap3` is a pure-Python implementation of the OpenLDAP client library and has no external dependencies. This package can be installed out-of-the-box, so we recommend to use it when the OpenLDAP client libraries are not available on the Galaxy server. These same mechanisms can also be configured by proxies serving Galaxy (e.g. nginx or Apache), but configuring them within Galaxy allows users to use the Galaxy UI for logging in instead of relying on a proxy. To configure one or more authentication plugins, simply copy [auth_conf.xml.sample](https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy/config/sample/auth_conf.xml.sample) to `config/auth_conf.xml`. The provided sample configuration file has numerous commented out examples and serves as the most up-to-date source of documentation on configuring these plugins. It is relatively straight-forward to add a custom authenticator to the framework. In this example, we will demonstrate a new authenticator based on the LDAP authenticator that performs custom checks besides password verification for authenticating a user on the Galaxy instance. First, we will create an authenticator source file `lib/galaxy/auth/providers/ldap_custom.py` with the following content: ``` import logging import ipaddress from ..providers.ldap_ad import LDAP log = logging.getLogger(__name__) class LDAPCustom(LDAP): """ Attempts to authenticate users against an LDAP server. """ plugin_type = "ldap_custom" def __init__(self): super().__init__() def authenticate(self, email, username, password, options, request): """ See abstract method documentation. """ if options.get("continue-on-failure", "False") == "False": failure_mode = None # reject and do not continue else: failure_mode = False # reject but continue # Check if user's remote IP is declared in whitelisted IPs if "white-listed-ips" in options: user_remote_ip = request.remote_addr # Get all white listed IPs from the config file white_listed_ips = options.get("white-listed-ips") # Convert them into a list white_listed_ips = white_listed_ips.split(' ') # Convert user remote IP into IPv4Address object user_remote_ip_obj = ipaddress.IPv4Address(user_remote_ip) white_listed_ip_objs = [ipaddress.IPv4Network(ip) for ip in white_listed_ip_objs] # Make sure white_listed_ip_objs is not empty if white_listed_ip_objs: # Effectively we are checking if remote ip network subnet # is subset of allowed subnets. If at least one subnet matches, # we allow user to authenticate allowed = any(user_remote_ip_obj in sn for sn in white_listed_ip_objs) if not allowed: log.info("LDAP authentication: User remote IP is not listed in whitelisted IPs") return failure_mode, "", "" return super().authenticate(email, username, password, options, request) __all__ = ("LDAPCustom",) ``` In this simple custom LDAP authenticator, we are verifying the remote IP address of the client is declared in the whitelisted IP addresses. The so-called `white-listed-ips` can be provided within the `auth_conf.xml` file. However this can be done in many ways depending on the deployments like getting white listed IPs from LDAP query or simply reading it from a file on the file system. This list of IP addresses can be a global whitelist or IP addresses per user. In order to use this authenticator, we need to declare it in the `auth_conf.xml` file as `ldap_custom`. ## Remote User Authentication If Galaxy is deployed with either nginx or Apache serving as a front-end proxy for Galaxy requests, they can be configured to authenticate users and pass this authentication information along to Galaxy using the HTTP remote user mechanism. See, for example, the [authentication and authorization guide for the Apache Web server](http://httpd.apache.org/docs/2.4/howto/auth.html) for an impression of the possibilities. Thus, by the time Galaxy is aware of a request, the user identity will have been determined and there will be no need for Galaxy to do any additional authentication work, such as showing a login screen or checking user credentials. However, accepting an identity asserted by the Web server does not relieve Galaxy from having to create a user account for such an identity. Thus, Galaxy automatically creates a user for each identity of this kind, recording that the user is "external" and also creating a random password in order to effectively disable traditionally performed logins for the user, although the remote user mechanism should normally prohibit any login mechanism other than that imposed by the Web server, and the "external" flag should itself prohibit the traditional mechanism being used with the user concerned. When a remote user returns to Galaxy and is not already logged in, the details of the user are retrieved according to the identity information supplied by the Web server. Enabling remote user authentication requires you to edit Galaxy's configuration file and set `use_remote_user` to `true`. This file is likely located in `config/galaxy.yml` and can be created by copying Galaxy's sample `config/galaxy.yml.sample`. Additional Galaxy configuration options related to remote user authentication are documented in Galaxy's sample configuration file. The options `remote_user_maildomain`, `remote_user_header`, and `normalize_remote_user_email` can adapt Galaxy to different responses from the proxy, while `remote_user_secret` can be used to provide added security, and `remote_user_logout_href` can be used to fix Galaxy's logout for the deployed setup. ## User Activation Galaxy admins using the default authentication mechanism have an option to turn on the email verification feature to require users to provide working email during the registration. You can also turn on the disposable email domains filter to disable registration for users using known disposable email provider. How to set up this config is presented here. _Note: SQLite database is not supported with this feature. Please use PostgreSQL._ ### Account activation feature In the Galaxy config file **config/galaxy.yml** there is the user activation setting that you have to turn on. By default it is off. ```yaml user_activation_on: true ``` After you turn this on every user that will try to register after this configuration file takes effect will have the activation email sent to the email address provided. Unless the `activation_grace_period` (see below) is set, the user won't be able to login before the activation happens. Furthermore in order for this to work correctly smtp server and email need be set: ```yaml smtp_server: some.server.edu:587 smtp_username: example_username smtp_password: example_passsword email_from: galaxy-instance@example.com ``` Smtp server takes care of the email sending and the `email_from` is used as the _From_ address in the verification email. ### Email template You can modify the activation email body by modifying the provided templates. You can use them in [html](https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy/config/templates/mail/activation-email.html) or [txt](https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy/config/templates/mail/activation-email.txt) depending on your emailing configuration. The mapping of mentioned templates' variables to config options is as follows: ``` "terms_url": config.terms_url, "contact_email": config.error_email_to, "instance_resource_url": config.instance_resource_url, "custom_message": config.custom_activation_email_message, ``` ### Changing email address If an activated user changes email address in user settings, their account will be deactivated. A new activation link will be sent and the user will have to visit it to activate the account again. ### Grace period In case you want the account activation feature but don't want to disable login completely you can set the `activation_grace_period` parameter. It specifies, in hours, the period in between registration time and the login time that the user will be allowed to log in even with an inactive account. ```yaml # Activation grace period (in hours). Activation is not forced (login is not # disabled) until grace period has passed. Users under grace period can't run # jobs. Enter 0 to disable grace period. Default is 3. activation_grace_period: 3 ``` Accounts created through an OIDC login are activated immediately, because the e-mail address verified by the identity provider is trusted. A provider can opt out of this with `require_user_activation` in [oidc_backends_config.xml](https://github.com/galaxyproject/galaxy/blob/dev/lib/galaxy/config/sample/oidc_backends_config.xml.sample), in which case its users go through the same activation flow as password accounts. However with inactive account the user won't be able to run jobs and warning message will be shown to them at the top of the page. It is customizable via the `inactivity_box_content` parameter. ```yaml # Used for warning box for inactive accounts (unable to run jobs). # In use only if activation_grace_period is set. inactivity_box_content: Your account has not been activated yet. Please activate your account by verifying your email address. For now you can access everything at Galaxy but your jobs won't run. ``` ### Disposable email address filtering To prevent users from using unwanted email addresses for the email activation admins can select from two methods of filtering: domain blocking and allowing. #### Blocklist The domain blocklist can be turned on through the `email_domain_blocklist_file` path parameter. Users that use disposable email domains defined at the file in this provided path will be refused registration. ```yaml # E-mail domains blocklist is used for filtering out users that are using disposable email address # during the registration. If their address domain matches any domain in the BL they are refused the registration. email_domain_blocklist_file: config/disposable_email_blocklist.conf ``` In the file each domain is on its own line and without the _@_ sign. Example of the blocklist file format: ``` drdrb.com mailinator.com sogetthis.com spamgourmet.com trashmail.net ``` A disposable domains blocklist file for download and modification is [at GitHub](https://github.com/martenson/disposable-email-domains/blob/master/disposable_email_blocklist.conf) #### Allowlist The domain allowlist can be turned on through the `email_domain_allowlist_file` path parameter. The file has the same format as the `email_domain_blocklist_file` above. The difference is that domains _outside_ of the allowlist will not be allowed for account registration. ## Authentication Related Code Password login, registration, account activation and logout for database-backed accounts live in the `User` controller in `lib/galaxy/webapps/galaxy/controllers/user.py` (`login`, `create`, `activate`, `logout`) and in `lib/galaxy/managers/users.py`. Password checks are delegated to the plugins in `lib/galaxy/auth/`, which is where the [Authentication Framework](#authentication-framework) providers (`localdb`, `ldap`, `ldap3`, `activedirectory`, `PAM`, `alwaysreject`) are implemented. OIDC login is handled by the `authnz` controller in `lib/galaxy/webapps/galaxy/controllers/authnz.py` together with `lib/galaxy/authnz/managers.py` (provider configuration, token refresh) and `lib/galaxy/authnz/psa_authnz.py` (the [Python Social Auth](https://python-social-auth.readthedocs.io/) pipeline that creates users and associates identities). Remote user headers are consumed in `lib/galaxy/webapps/base/webapp.py` when the session for a request is established. ### Database Tables The database employs a `galaxy_user` table which records the details of all registered users, and this table is exposed to the code through the `User` class in `lib/galaxy/model/__init__.py`. Each logged-in user is assigned a session which references the user in the `galaxy_session` table (exposed via `GalaxySession`). Users created through remote user authentication are flagged with `galaxy_user.external`. External OIDC identities are stored in `oidc_user_authnz_tokens` (exposed via `UserAuthnzToken`). Each row links one external identity to one Galaxy user and carries the provider's backend name, the subject identifier (`uid`) issued by the provider, and the access, refresh and ID tokens in `extra_data`. #### Removing or replacing an OIDC provider Rows in `oidc_user_authnz_tokens` are keyed by the backend name of the provider, which for some providers differs from the name used in `oidc_backends_config_file` (for example `google` is stored as `google-openidconnect`, `lifescience` as `life_science`; `elixir`, `keycloak`, `oidc` and most others are stored unchanged). `SELECT DISTINCT provider FROM oidc_user_authnz_tokens;` shows what your database holds. If you remove a provider from `oidc_backends_config_file`, or replace it with a different backend (for example the deprecated `elixir` with `lifescience`), the rows for the old provider stay in the database. They are harmless: Galaxy skips them when refreshing tokens, and nobody can log in through a provider that is not configured. Affected users still see the old entry under _User > Preferences > Manage Third-Party Identities_, labelled with the raw provider name, and cannot disconnect it themselves because there is no configured backend to disconnect from. The old rows are not migrated to a replacement provider. When a user logs in through the new provider, Galaxy creates a fresh row for that identity. If the user is already logged in, the identity is attached to their account. Otherwise, if an account with the same e-mail address exists, the user is either associated automatically (when the new provider is the only login method configured) or asked to log in to that account first and confirm the connection. To remove stale rows for a provider that is gone for good, run for example: ```sql DELETE FROM oidc_user_authnz_tokens WHERE provider = 'elixir'; ``` Do this only if the provider will not come back under the same name. The row is what ties the external identity to the Galaxy account, so after deleting it a user who logs in through a re-added provider goes through the account association flow again. ### Authenticating a User The following steps are followed in any code that seeks to recognise a user within Galaxy and allow access to the application: 1. The identity credential, currently the e-mail address of the user, is used to find any previously-registered user in the database. 1. Where no user exists and the login mechanism requires explicit registration, authentication fails at this point. Otherwise, a user is automatically created for previously unknown identities. 1. For conventional accounts requiring a password, authentication fails at this point if a valid password is not specified. Otherwise, an alternative mechanism for completing authentication may be invoked. 1. Upon completion of the authentication of a user's identity, any association of that identity with the Galaxy user instance may be performed. For example, an OIDC identity may be associated with a user created for that identity. 1. Finally, the login is handled using the `handle_user_login` method on the `GalaxyWebTransaction` object, associating the user with a new session.