boogiebaeren / contao-google-sso-bundle
Package info
github.com/boogiebaeren/contao-google-sso-bundle
Type:contao-bundle
pkg:composer/boogiebaeren/contao-google-sso-bundle
Requires
- php: ^8.1
- ext-json: *
- contao/core-bundle: ^4.13 || ^5
- google/apiclient: ^2.15
- symfony/config: ^5.4
- symfony/dependency-injection: ^5.4
- symfony/http-kernel: ^5.4
Requires (Dev)
- contao/easy-coding-standard: ^4
- contao/manager-plugin: ^2.0
- phpstan/phpstan: ^1.10
- phpstan/phpstan-symfony: ^1.2
- phpunit/phpunit: ^10
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Adds a new login url (/contao/login_sso) to log into the Contao backend using existing users
inside a Google Workspace instance.
Installation
Install the bundle via composer: composer require boogiebaeren/contao-google-sso-bundle
or install it via the Contao Manager.
Configuration
You need to define two environment variables:
- GOOGLE_SSO_CLIENTID
- GOOGLE_SSO_CLIENTSECRET
You then need to add the following configuration to your config/config.yaml file:
# config/config.yaml contao_google_sso: client_id: '%env(GOOGLE_SSO_CLIENTID)%' client_secret: '%env(GOOGLE_SSO_CLIENTSECRET)%' # Google Workspace domains that may log in. Both the "hd" claim of the Google # account and the domain of its email address must be in this list. allowed_domains: - 'example.com' - 'example.org' # Create a backend user (without admin rights and groups) on the first SSO login # if no user with that email exists. Disabled by default. auto_create_users: false
client_id, client_secret and allowed_domains are required. The login refuses to run if the
client id resolves to an empty value at runtime.
Users are matched to existing Contao backend users by email address. Only Google accounts with a verified
email that belong to one of the allowed_domains are accepted, regardless of the OAuth consent screen setting.
You should also add the following to your composer.json file to remove all unused Google Services:
{
"extra": {
"google/apiclient-services": [
"Oauth2"
]
},
"scripts": {
"pre-autoload-dump": "Google\\Task\\Composer::cleanup"
}
}
You need to be an administrator of a Google Workspace instance to create a new OAuth client.
First create a new project inside the Google Cloud Console,
then create a new OAuth client.
Then select "Web application" as the application type, enter a name and add an authorized redirection
uri https://<your-domain>/contao/login_sso/redirect.
If your Contao installation serves several hostnames, add one redirect uri per hostname
(e.g. https://www.example.com/contao/login_sso/redirect and https://www.example.org/contao/login_sso/redirect).
After you've created the OAuth client, you can copy the client id and client secret into a .env.local file in the
root folder of your Contao installation.
Set the usertype to "intern" in the OAuth consent screen. The bundle additionally enforces allowed_domains,
but the consent screen setting is an important second line of defence.

After you've configured the environment variables, you can log in using the new login
url (https://<your-domain>/contao/login_sso).
Integrating the login into the be_login page
If you don't want a different login url you can also overwrite the be_login.html5 template.
For this, you can create a be_login.html5 file under your templates folder and add the following snippet (replacing):
<?php $this->extend('be_login'); ?> <?php $this->block('head'); ?> <?php $this->parent(); ?> <script src="https://accounts.google.com/gsi/client" async></script> <?php $this->endblock(); ?> <?php $this->block('container'); ?> <?php $this->parent(); ?> <div id="g_id_onload" data-client_id="<YOUR_CLIENT_ID>" data-context="signin" data-login_uri="https://<your-domain>/contao/login_sso/redirect" data-auto_select="false" data-close_on_tap_outside="false" data-itp_support="true"> </div> <?php $this->endblock(); ?>
On a multi-domain installation use <?= \Contao\Environment::get('base') ?>contao/login_sso/redirect as
data-login_uri so the login posts back to the hostname the user is currently on, and add every hostname as an
"Authorized JavaScript origin" on the OAuth client.
References
This bundle was inspired by https://github.com/BROCKHAUS-AG/contao-microsoft-sso-bundle and uses a similar flow.
Documentation to google login: https://developers.google.com/identity/authentication