Support link-to-existing-gerrit-accounts for Keycloak

The SAP IAS and Discovery providers already honor
link-to-existing-gerrit-accounts: when set, a first browser login
links to an existing Gerrit account by username instead of always
creating a new one. Add the same option to Keycloak, reusing that
mechanism rather than inventing a new one.

KeycloakUserInfoMapper gains the same map()/mapForBrowser() split as
DiscoveryUserInfoMapper. map() (the Git-over-HTTP path) keeps a null
claimed identity. mapForBrowser(), when the flag is set, emits a
gerrit:<preferred_username> claimed identity, and fails closed if the
flag is set but no preferred_username is present, so linking is never
silently skipped. Claimed identity is only read by the browser
OAuthSession, so the Git path is unaffected by the flag.

KeycloakOAuthService reads LINK_TO_EXISTING_GERRIT_ACCOUNT and maps
browser claims via mapForBrowser(). KeycloakTokenValidator keeps mapping
via map(), so Git-over-HTTP continues to authenticate by external id
only. KeycloakInitStep prompts for the option like the Discovery init
step.

Feature: https://github.com/davido/gerrit-oauth-provider/issues/181
Change-Id: I19d01a6964be8c5432bd20bea0f061b07114d401
8 files changed
tree: 45c3065663c427f4cb1edf0d96e97555028b2504
  1. .settings/
  2. contrib/
  3. src/
  4. .bazelversion
  5. .gitignore
  6. BUILD.bazel
  7. external_plugin_deps.MODULE.bazel
  8. LICENSE
  9. MODULE.bazel
  10. MODULE.bazel.lock
  11. oauth_plugin_deps.lock.json
  12. oauth_third_party_runtime_jars.allowlist.txt
  13. provider.bzl
  14. README.md
README.md

Gerrit OAuth2 authentication provider

With this plugin Gerrit can use OAuth2 protocol for authentication. Supported OAuth providers:

Auth0, Authentik, Amazon Cognito, LemonLDAP::NG and Tuleap were previously shipped as dedicated providers. They are standard OpenID Connect providers, so they have been removed in favour of the generic Discovery provider; see Documentation/config-discovery.md for how to migrate their configuration.

See Documentation/providers.md for a per-provider capability matrix, and the Wiki for what it can do for you.

Prebuilt artifacts

Prebuilt binary artifacts are available on release page. Make sure to pick the right JAR for your Gerrit version.

Build

This plugin is built with Bazel inside the Gerrit source tree. Clone or link it into Gerrit's plugins directory, link its dependency fragment over the placeholder, and build it from the Gerrit tree:

  git clone https://gerrit.googlesource.com/gerrit
  git clone https://gerrit.googlesource.com/plugins/oauth
  cd gerrit/plugins
  ln -s ../../oauth .
  rm external_plugin_deps.MODULE.bazel
  ln -s oauth/external_plugin_deps.MODULE.bazel .
  cd ..
  bazel build plugins/oauth

Besides the default oauth.jar, the plugin can also be built as slim, single-provider artifacts that bundle just one provider plus the shared core. Each registers as the same gerrit-oauth-provider plugin and ships its own init step; see Documentation/build.md for the target list. SAP IAS is shipped only as the oauth-sapias artifact.

Install

Copy the bazel-bin/plugins/oauth/oauth.jar to $gerrit_site/plugins and re-run init to configure it:

  java -jar gerrit.war init -d <site>
  [...]
  *** OAuth Authentication Provider
  ***
  Use Bitbucket OAuth provider for Gerrit login ? [Y/n]? n
  Use Google OAuth provider for Gerrit login ? [Y/n]?
  Application client id          : <client-id>
  Application client secret      : 
                confirm password : 
  Link to OpenID accounts? [true]: 
  Use GitHub OAuth provider for Gerrit login ? [Y/n]? n

Reporting bugs

Make sure to read the FAQ before reporting issues.

License

Apache License 2.0