You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
14 KiB
Markdown

# OpenID Connect Generic Client
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
A simple client that provides SSO or opt-in authentication against a generic OAuth2 Server implementation.
## Description
This plugin allows to authenticate users against OpenID Connect OAuth2 API with Authorization Code Flow.
Once installed, it can be configured to automatically authenticate users (SSO), or provide a "Login with OpenID Connect"
button on the login form. After consent has been obtained, an existing user is automatically logged into WordPress, while
new users are created in WordPress database.
Much of the documentation can be found on the Settings > OpenID Connect Generic dashboard page.
## Table of Contents
- [Installation](#installation)
- [Composer](#composer)
- [Frequently Asked Questions](#frequently-asked-questions)
- [What is the client's Redirect URI?](#what-is-the-clients-redirect-uri)
- [Can I change the client's Redirect URI?](#can-i-change-the-clients-redirect-uri)
3.8.0 Release (#230) * Initial Coding Standards & Static Analysis Chanages. * Adds WordPress coding standards configuration. * Adds WordPress/PHP static analysis configuration. * Adds Git hooks to enforce checks and ensure quality on commits. * Adds initial local Docker development environment setup. * Current state of coding standards and analysis fixes. * Near Completion Update of PHP Code Sniffer Compliance Changes. * Fixes all PHP Code Sniffer WordPress Coding Standards Issues. * Updates Code Base to Pass Level 5 Baseline * Ensures PHP Code Sniffs continue to pass. * Fixes all code base issues to pass a level 5 PHP static analysis. * Updates PHPStan configurations to use a level 5 baseline. * Fixes Travis CI Configuration for Static Analysis * Fixes Plugin Pass i18n Checks * Adds i18n check to Travis CI builds. * Adds additional i18n run scripts to package.json. * Internationalization Checking & Fixes * Fixes missing i18n translation in main plugin file. * Adds update POT file. * Enforces i18n checks on commit with GrumPHP. * Adds i18n check step to Travis CI builds. * Gitattributes for export exclusions * Fixes missing loaded settings property assignment. * Adds Support for IDP Settings as Defined Constants - Reads from defined constants on plugin bootstrap. - Disabled plugin settings fields when defined constants are used. - Prevents savings plugin settings that are using defined constants. * Adds Node/NPM Environment Requirements * Fixes GrumPHP Bin Directory Configuration * Updates GrumPHP for Required Features - Bumps Composer package PHP version to 7.3. - Updates GrumPHP configuration to new format. * Plugin Settings Page Updates Using Constants - Ensures that any available defined constants are loaded in place of any database stored settings as an override. * Composer Dependency Updates & Travis CI Caching Fix * Travis CI Build Composer Update Change * NPM Updates & NVM Version Lock * Fixes NPM Package Lock File for Node v12 * Updates NPM Package Dependencies * Updates Changelog & README Files With Relevant Changes * Fixes Localizaion on Error Output * Changes GrumPHP Configuration to Provide a Full PHPCS Report * Fixes Local Dev Setup to Activate Plugin by Default * Adds Contribution Guide and Issue & PR Templates (#222) * Fixes Support GitHub Issue Template (#223) * Fixes space/typo with Wiki link (#224) * Fixes invalid wp-env plugin configuration (#225) * Improve Local Dev Setup by Reducing Setup Commands (#226) * Improve Local Dev Setup by Reducing Setup Commands * Adds Code Owners Configuration for Pull Requests * Fixes Development Dependencies and Setup Scripts (#227) * Dev release/3.8 (#229) * Adds dev Branch to Travis CI Builds * Release Preparation Enhancements & Release Changes Co-authored-by: Jonathan Daggerhart <jonathan@daggerhart.com>
4 years ago
- [Configuration Environment Variables/Constants](#configuration-environment-variables-constants)
- [Hooks](#hooks)
- [Filters](#filters)
- [openid-connect-generic-alter-request](#openid-connect-generic-alter-request)
- [openid-connect-generic-login-button-text](#openid-connect-generic-login-button-text)
- [openid-connect-generic-auth-url](#openid-connect-generic-auth-url)
- [openid-connect-generic-user-login-test](#openid-connect-generic-user-login-test)
- [openid-connect-generic-user-creation-test](#openid-connect-generic-user-creation-test)
- <del>[openid-connect-generic-alter-user-claim](#openid-connect-generic-alter-user-claim)</del>
- [openid-connect-generic-alter-user-data](#openid-connect-generic-alter-user-data)
- [openid-connect-generic-settings-fields](#openid-connect-generic-settings-fields)
- [Actions](#actions)
- [openid-connect-generic-user-create](#openid-connect-generic-user-create)
- [openid-connect-generic-user-update](#openid-connect-generic-user-update)
- [openid-connect-generic-update-user-using-current-claim](#openid-connect-generic-update-user-using-current-claim)
- [openid-connect-generic-redirect-user-back](#openid-connect-generic-redirect-user-back)
## Installation
1. Upload to the `/wp-content/plugins/` directory
1. Activate the plugin
1. Visit Settings > OpenID Connect and configure to meet your needs
### Composer
[OpenID Connect Generic on packagist](https://packagist.org/packages/daggerhart/openid-connect-generic)
Installation:
`composer require daggerhart/openid-connect-generic`
## Frequently Asked Questions
### What is the client's Redirect URI?
Most OAuth2 servers should require a whitelist of redirect URIs for security purposes. The Redirect URI provided
by this client is like so: `https://example.com/wp-admin/admin-ajax.php?action=openid-connect-authorize`
Replace `example.com` with your domain name and path to WordPress.
### Can I change the client's Redirect URI?
Some OAuth2 servers do not allow for a client redirect URI to contain a query string. The default URI provided by
this module leverages WordPress's `admin-ajax.php` endpoint as an easy way to provide a route that does not include
HTML, but this will naturally involve a query string. Fortunately, this plugin provides a setting that will make use of
an alternate redirect URI that does not include a query string.
On the settings page for this plugin (Dashboard > Settings > OpenID Connect Generic) there is a checkbox for
**Alternate Redirect URI**. When checked, the plugin will use the Redirect URI
`https://example.com/openid-connect-authorize`.
3.8.0 Release (#230) * Initial Coding Standards & Static Analysis Chanages. * Adds WordPress coding standards configuration. * Adds WordPress/PHP static analysis configuration. * Adds Git hooks to enforce checks and ensure quality on commits. * Adds initial local Docker development environment setup. * Current state of coding standards and analysis fixes. * Near Completion Update of PHP Code Sniffer Compliance Changes. * Fixes all PHP Code Sniffer WordPress Coding Standards Issues. * Updates Code Base to Pass Level 5 Baseline * Ensures PHP Code Sniffs continue to pass. * Fixes all code base issues to pass a level 5 PHP static analysis. * Updates PHPStan configurations to use a level 5 baseline. * Fixes Travis CI Configuration for Static Analysis * Fixes Plugin Pass i18n Checks * Adds i18n check to Travis CI builds. * Adds additional i18n run scripts to package.json. * Internationalization Checking & Fixes * Fixes missing i18n translation in main plugin file. * Adds update POT file. * Enforces i18n checks on commit with GrumPHP. * Adds i18n check step to Travis CI builds. * Gitattributes for export exclusions * Fixes missing loaded settings property assignment. * Adds Support for IDP Settings as Defined Constants - Reads from defined constants on plugin bootstrap. - Disabled plugin settings fields when defined constants are used. - Prevents savings plugin settings that are using defined constants. * Adds Node/NPM Environment Requirements * Fixes GrumPHP Bin Directory Configuration * Updates GrumPHP for Required Features - Bumps Composer package PHP version to 7.3. - Updates GrumPHP configuration to new format. * Plugin Settings Page Updates Using Constants - Ensures that any available defined constants are loaded in place of any database stored settings as an override. * Composer Dependency Updates & Travis CI Caching Fix * Travis CI Build Composer Update Change * NPM Updates & NVM Version Lock * Fixes NPM Package Lock File for Node v12 * Updates NPM Package Dependencies * Updates Changelog & README Files With Relevant Changes * Fixes Localizaion on Error Output * Changes GrumPHP Configuration to Provide a Full PHPCS Report * Fixes Local Dev Setup to Activate Plugin by Default * Adds Contribution Guide and Issue & PR Templates (#222) * Fixes Support GitHub Issue Template (#223) * Fixes space/typo with Wiki link (#224) * Fixes invalid wp-env plugin configuration (#225) * Improve Local Dev Setup by Reducing Setup Commands (#226) * Improve Local Dev Setup by Reducing Setup Commands * Adds Code Owners Configuration for Pull Requests * Fixes Development Dependencies and Setup Scripts (#227) * Dev release/3.8 (#229) * Adds dev Branch to Travis CI Builds * Release Preparation Enhancements & Release Changes Co-authored-by: Jonathan Daggerhart <jonathan@daggerhart.com>
4 years ago
## Configuration Environment Variables/Constants
- Client ID: `OIDC_CLIENT_ID`
- Client Secret Key: `OIDC_CLIENT_SECRET`
- Login Endpoint URL: `OIDC_ENDPOINT_LOGIN_URL`
- Userinfo Endpoint URL: `OIDC_ENDPOINT_USERINFO_URL`
- Token Validation Endpoint URL: `OIDC_ENDPOINT_TOKEN_URL`
- End Session Endpoint URL: `OIDC_ENDPOINT_LOGOUT_URL`
3.9.0 Feature & Bugfix Release (#394) * Update openid-connect-generic-client-wrapper.php Add openid-connect-generic-session-expired action before to allow consumers to act on the refresh token expiration * Implement singleton pattern for OpenID_Connect_Generic class This will allow developers who want to be able to call methods belonging to this class (or methods belonging to any of this class's properties) to do so, without having to create a new instance, and therefore repeat all the bootstrapping. Instead, they will just be able to call OpenID_Connect_Generic::instance() to retrieve the singleton. * Add functions.php file for global functions These will act as wrappers for methods you would otherwise call by getting the Open_ID_Connect_Generic singleton and then calling the appropriate method. * Make client_wrapper a public property This allows us to access client_wrapper methods via the singleton of Open_ID_Connect_Generic. * Support aggregated claims (#254) * use tabs instead of spaces * added define config with OIDC_LOGIN_TYPE and OIDC_CLIENT_SCOPE scopes are needed when logging in so this is a no-brainer and this can be deployed using deployments login type can be defined here when you want to enforce SSO from the deployment. * More define-driven settings * fix linting * fix code analysis errors * fix more linting, add more exception handling * Added the OIDC_CREATE_IF_DOES_NOT_EXIST define * Added documentation and phpstan defines * Added data in environment_settings * Fixes QS not being added to the redirect URL * fix #178 Update last-token-response user meta after refresh * add a basic method to refresh user claim outside of this plugin * fix return doc * add a basic method to refresh user claim outside of this plugin * fix return doc * Revert "fix return doc" This reverts commit e19d6f3ffd3f7684dd843408ec911c4b6d614328. * Revert "add a basic method to refresh user claim outside of this plugin" This reverts commit 1a5fa9b045c7b50a17dc3cfd5482fc33be9aa6ee. * Initial Codepsaces Development Environment * Adds Gitpod Support Alongside Codespaces Support * Only load admin CSS when showing settings page * Updates Development Environment for PHP & WordPress - Updates local development environment to use a Composer Docker container. - Updates VS Code/Codespaces devcontainer to bullseye/PHP 7.4. - Updates VS Code/Codespaces devcontainer & GitPod starting commands. - Updates build environment to use PHP 7.4 & Composer 2. - Updates build environment Composer packages. - Updates Composer WordPress install to use the johnpbloch packages. * Fixes Running PHPStan in Docker & Static Analysis Issues - Updates the PHP/Composer Docker image memory limit to allow PHPStand to run properly. - Fixes static analysis issues reported by PHPStan. - Updates PHPStan & extensions to latest versions. - Updates PHPStan configuration for latest version. * Update GitHub Actions for PHP/Composer/WordPress Changes - Updates GitHub Actions to use Composer v2. - Updates GitHub Actions to use PHP 7.4. - Updates GitHub Actions to use WordPress 5.7 as stable. * Updates WordPress Version Composer Dependencies - Updates the installed WordPress version to 5.7. - Updates the installed WordPress stubs version to 5.7. * Fixes GitHub Actions for Composer 2 Changes * Fixes GitHub Actions for PHP Unit testing - Changes PHPUnit switch to dev-master for PHP 8.0. * Updates GrumPHP & GrumPHP Unit Testing PHP 8 Support - Updates GrumPHP to a newer version. - Updates Unit Testing PHP 8.0 to use GrumPHP dev-master. * Updates Unit Testing for PHPUnit Testing w/ PHP8 * Updates Unit Testing Composer Updates to Run w/ PHP 8 * Updates Composer PHPUnit for PHP 8 Override * Multisite/network configurations use your current blog as the default when using the WP_User_Query which means if a user already exists, from a different blog, you will not find them, but also can't create a new account because they already exist. This overrides your current blog and will search 'all' * CodeSpaces Dev Container Updates & Composer Updates * Updating GrumPHP & Composer Dependencies * Updates NPM Modules & Changes Plugin Instance Visibility * Updates @wordpress/scripts & ~wordpress/env to the latest versions. * Changes the visibility of the plugin class instance attribute. * Bump tar from 4.4.13 to 4.4.19 Bumps [tar](https://github.com/npm/node-tar) from 4.4.13 to 4.4.19. - [Release notes](https://github.com/npm/node-tar/releases) - [Changelog](https://github.com/npm/node-tar/blob/main/CHANGELOG.md) - [Commits](https://github.com/npm/node-tar/compare/v4.4.13...v4.4.19) --- updated-dependencies: - dependency-name: tar dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * Bump hosted-git-info from 2.8.8 to 2.8.9 Bumps [hosted-git-info](https://github.com/npm/hosted-git-info) from 2.8.8 to 2.8.9. - [Release notes](https://github.com/npm/hosted-git-info/releases) - [Changelog](https://github.com/npm/hosted-git-info/blob/v2.8.9/CHANGELOG.md) - [Commits](https://github.com/npm/hosted-git-info/compare/v2.8.8...v2.8.9) --- updated-dependencies: - dependency-name: hosted-git-info dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * Bump lodash from 4.17.19 to 4.17.21 Bumps [lodash](https://github.com/lodash/lodash) from 4.17.19 to 4.17.21. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](https://github.com/lodash/lodash/compare/4.17.19...4.17.21) --- updated-dependencies: - dependency-name: lodash dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * Bump ssri from 6.0.1 to 6.0.2 Bumps [ssri](https://github.com/npm/ssri) from 6.0.1 to 6.0.2. - [Release notes](https://github.com/npm/ssri/releases) - [Changelog](https://github.com/npm/ssri/blob/v6.0.2/CHANGELOG.md) - [Commits](https://github.com/npm/ssri/compare/v6.0.1...v6.0.2) --- updated-dependencies: - dependency-name: ssri dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * Bump path-parse from 1.0.6 to 1.0.7 Bumps [path-parse](https://github.com/jbgutierrez/path-parse) from 1.0.6 to 1.0.7. - [Release notes](https://github.com/jbgutierrez/path-parse/releases) - [Commits](https://github.com/jbgutierrez/path-parse/commits/v1.0.7) --- updated-dependencies: - dependency-name: path-parse dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * Bump ws from 6.2.1 to 6.2.2 Bumps [ws](https://github.com/websockets/ws) from 6.2.1 to 6.2.2. - [Release notes](https://github.com/websockets/ws/releases) - [Commits](https://github.com/websockets/ws/compare/6.2.1...6.2.2) --- updated-dependencies: - dependency-name: ws dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> * coding standards issues * Fix wordpress coding standards issues * Redone acr-values-support acr-values support * Fix code-standard errors Fixed reported code-standard issues found * Update openid-connect-generic-client.php fixing code-indentment after report * Update openid-connect-generic-client-wrapper.php fix for reported code-standard errors * Update openid-connect-generic-settings-page.php Fixing code-standard reported errors * Update openid-connect-generic-client.php re-aligned line 225 ( indentment error) * Refactors new ACR Handling Code for Better Code Quality * Fixes some incorrect ACR handling code that incorrectly added the ACR when it wasn't set. * Adds missing settings disable handling when ACR constant is set. * Refactors code for simplicity and code quality. * Fixes #243 Nickname from Claim Regression * Fixes the regression caused by the code changes in 3.8.0 which resulted in not performing proper fallbacks for missing cliams such as the nickname. * Fixes invalid Node version in NVM configuration. * Fixes Indentity Mapping & Creation Issues * Fixes issues with improperly handling combinations of the "Link Existing Users", "Create user if does not exist", and "Identify with User Name" configuration options. * Addresses issues related to case sensitivity of usernames when attempting to "Link Existing Users". * Fixes typos and formatting on the plugin Settings screen. * Dependabot Security Related Fixes & Refactoring * Update to require Node v14 & NPM 6.14.15. * Updates to Grunt 1.4.1. * Forces resolution of dependencies for security fixes. * Update @wordpress packages to their latest versions. * Refactors the NPM & Composer scripts. * Updates WP to Latest Dev Standard for Local Development * Refactors Callable Refresh User Claims Function & Action * Moves `openid-connect-generic-update-user-using-current-claim` action to within update user metadata during login. * Adds a new publicly callable method that uses the plugin singleton. * Prep for 3.9.0 Release Co-authored-by: jkouris <35877237+jkouris@users.noreply.github.com> Co-authored-by: Rob Skilling <rob@dxw.com> Co-authored-by: Martin Schanzenbach <schanzen@gnunet.org> Co-authored-by: Chloé "Matcha" Desoutter <chloe.desoutter@bluenove.com> Co-authored-by: Martin <schanzen@users.noreply.github.com> Co-authored-by: Sylwester Kardziejonek <sylwester.kardziejonek@gmail.com> Co-authored-by: Nicolas Dhers <nicolas@rkcreation.fr> Co-authored-by: Tom McCaffery <tom@weareadjacent.com> Co-authored-by: Dan <dan@testinnovators.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: John Kouris <jkouris@everyincome.com> Co-authored-by: Glowsome <administrator@comsolve.nl>
3 years ago
- OpenID scope: `OIDC_CLIENT_SCOPE` (space separated)
- OpenID login type: `OIDC_LOGIN_TYPE` ('button' or 'auto')
- Enforce privacy: `OIDC_ENFORCE_PRIVACY` (boolean)
- Create user if they do not exist: `OIDC_CREATE_IF_DOES_NOT_EXIST` (boolean)
- Link existing user: `OIDC_LINK_EXISTING_USERS` (boolean)
- Redirect user back to origin page: `OIDC_REDIRECT_USER_BACK` (boolean)
- Redirect on logout: `OIDC_REDIRECT_ON_LOGOUT` (boolean)
3.8.0 Release (#230) * Initial Coding Standards & Static Analysis Chanages. * Adds WordPress coding standards configuration. * Adds WordPress/PHP static analysis configuration. * Adds Git hooks to enforce checks and ensure quality on commits. * Adds initial local Docker development environment setup. * Current state of coding standards and analysis fixes. * Near Completion Update of PHP Code Sniffer Compliance Changes. * Fixes all PHP Code Sniffer WordPress Coding Standards Issues. * Updates Code Base to Pass Level 5 Baseline * Ensures PHP Code Sniffs continue to pass. * Fixes all code base issues to pass a level 5 PHP static analysis. * Updates PHPStan configurations to use a level 5 baseline. * Fixes Travis CI Configuration for Static Analysis * Fixes Plugin Pass i18n Checks * Adds i18n check to Travis CI builds. * Adds additional i18n run scripts to package.json. * Internationalization Checking & Fixes * Fixes missing i18n translation in main plugin file. * Adds update POT file. * Enforces i18n checks on commit with GrumPHP. * Adds i18n check step to Travis CI builds. * Gitattributes for export exclusions * Fixes missing loaded settings property assignment. * Adds Support for IDP Settings as Defined Constants - Reads from defined constants on plugin bootstrap. - Disabled plugin settings fields when defined constants are used. - Prevents savings plugin settings that are using defined constants. * Adds Node/NPM Environment Requirements * Fixes GrumPHP Bin Directory Configuration * Updates GrumPHP for Required Features - Bumps Composer package PHP version to 7.3. - Updates GrumPHP configuration to new format. * Plugin Settings Page Updates Using Constants - Ensures that any available defined constants are loaded in place of any database stored settings as an override. * Composer Dependency Updates & Travis CI Caching Fix * Travis CI Build Composer Update Change * NPM Updates & NVM Version Lock * Fixes NPM Package Lock File for Node v12 * Updates NPM Package Dependencies * Updates Changelog & README Files With Relevant Changes * Fixes Localizaion on Error Output * Changes GrumPHP Configuration to Provide a Full PHPCS Report * Fixes Local Dev Setup to Activate Plugin by Default * Adds Contribution Guide and Issue & PR Templates (#222) * Fixes Support GitHub Issue Template (#223) * Fixes space/typo with Wiki link (#224) * Fixes invalid wp-env plugin configuration (#225) * Improve Local Dev Setup by Reducing Setup Commands (#226) * Improve Local Dev Setup by Reducing Setup Commands * Adds Code Owners Configuration for Pull Requests * Fixes Development Dependencies and Setup Scripts (#227) * Dev release/3.8 (#229) * Adds dev Branch to Travis CI Builds * Release Preparation Enhancements & Release Changes Co-authored-by: Jonathan Daggerhart <jonathan@daggerhart.com>
4 years ago
## Hooks
This plugin provides a number of hooks to allow for a significant amount of customization of the plugin operations from
elsewhere in the WordPress system.
### Filters
Filters are WordPress hooks that are used to modify data. The first argument in a filter hook is always expected to be
returned at the end of the hook.
WordPress filters API - [`add_filter()`](https://developer.wordpress.org/reference/functions/add_filter/) and
[`apply_filters()`](https://developer.wordpress.org/reference/functions/apply_filters/).
Most often you'll only need to use `add_filter()` to hook into this plugin's code.
#### `openid-connect-generic-alter-request`
Hooks directly into client before requests are sent to the OpenID Server.
Provides 2 arguments: the request array being sent to the server, and the operation currently being executed by this
plugin.
Possible operations:
- get-authentication-token
- refresh-token
- get-userinfo
```
add_filter('openid-connect-generic-alter-request', function( $request, $operation ) {
if ( $operation == 'get-authentication-token' ) {
$request['some_key'] = 'modified value';
}
return $request;
}, 10, 2);
```
#### `openid-connect-generic-login-button-text`
Modify the login button text. Default value is `__( 'Login with OpenID Connect' )`.
Provides 1 argument: the current login button text.
```
add_filter('openid-connect-generic-login-button-text', function( $text ) {
$text = __('Login to my super cool IDP server');
return $text;
});
```
#### `openid-connect-generic-auth-url`
Modify the authentication URL before presented to the user. This is the URL that will send the user to the IDP server
for login.
Provides 1 argument: the plugin generated URL.
```
add_filter('openid-connect-generic-auth-url', function( $url ) {
// Add some custom data to the url.
$url.= '&my_custom_data=123abc';
return $url;
});
```
#### `openid-connect-generic-user-login-test`
Determine whether or not the user should be logged into WordPress.
Provides 2 arguments: the boolean result of the test (default `TRUE`), and the `$user_claim` array from the server.
```
add_filter('openid-connect-generic-user-login-test', function( $result, $user_claim ) {
// Don't let Terry login.
if ( $user_claim['email'] == 'terry@example.com' ) {
$result = FALSE;
}
return $result;
}, 10, 2);
```
#### `openid-connect-generic-user-creation-test`
Determine whether or not the user should be created. This filter is called when a new user is trying to login and they
do not currently exist within WordPress.
Provides 2 arguments: the boolean result of the test (default `TRUE`), and the `$user_claim` array from the server.
```
add_filter('', function( $result, $user_claim ) {
// Don't let anyone from example.com create an account.
$email_array = explode( '@', $user_claim['email'] );
if ( $email_array[1] == 'example.com' ) {
$result = FALSE;
}
return $result;
}, 10, 2)
```
#### <del>`openid-connect-generic-alter-user-claim`</del>
Modify the `$user_claim` before the plugin builds the `$user_data` array for new user created.
**Deprecated** - This filter is not very useful due to some changes that were added later. Recommend not using this
filter, and using the `openid-connect-generic-alter-user-data` filter instead. Practically, you can only change the
user's `first_name` and `last_name` values with this filter, but you could easily do that in
`openid-connect-generic-alter-user-data` as well.
Provides 1 argument: the `$user_claim` from the server.
```
// Not a great example because the hook isn't very useful.
add_filter('openid-connect-generic-alter-user-claim', function( $user_claim ) {
// Use the beginning of the user's email address as the user's first name.
if ( empty( $user_claim['given_name'] ) ) {
$email_array = explode( '@', $user_claim['email'] );
$user_claim['given_name'] = $email_array[0];
}
return $user_claim;
});
```
#### `openid-connect-generic-alter-user-data`
Modify a new user's data immediately before the user is created.
Provides 2 arguments: the `$user_data` array that will be sent to `wp_insert_user()`, and the `$user_claim` from the
server.
```
add_filter('openid-connect-generic-alter-user-claim', function( $user_data, $user_claim ) {
// Don't register any user with their real email address. Create a fake internal address.
if ( !empty( $user_data['user_email'] ) ) {
$email_array = explode( '@', $user_data['user_email'] );
$email_array[1] = 'my-fake-domain.co';
$user_data['user_email'] = implode( '@', $email_array );
}
return $user_data;
}, 10, 2);
```
#### `openid-connect-generic-settings-fields`
For extending the plugin with a new setting field (found on Dashboard > Settings > OpenID Connect Generic) that the site
administrator can modify. Also useful to alter the existing settings fields.
See `/includes/openid-connect-generic-settings-page.php` for how fields are constructed.
New settings fields will be automatically saved into the wp_option for this plugin's settings, and will be available in
the `\OpenID_Connect_Generic_Option_Settings` object this plugin uses.
**Note:** It can be difficult to get a copy of the settings from within other hooks. The easiest way to make use of
settings in your custom hooks is to call
`$settings = get_option('openid_connect_generic_settings', array());`.
Provides 1 argument: the existing fields array.
```
add_filter('openid-connect-generic-settings-fields', function( $fields ) {
// Modify an existing field's title.
$fields['endpoint_userinfo']['title'] = __('User information endpoint url');
// Add a new field that is a simple checkbox.
$fields['block_terry'] = array(
'title' => __('Block Terry'),
'description' => __('Prevent Terry from logging in'),
'type' => 'checkbox',
'section' => 'authorization_settings',
);
// A select field that provides options.
$fields['deal_with_terry'] = array(
'title' => __('Manage Terry'),
'description' => __('How to deal with Terry when he tries to log in.'),
'type' => 'select',
'options' => array(
'allow' => __('Allow login'),
'block' => __('Block'),
'redirect' => __('Redirect'),
),
'section' => 'authorization_settings',
);
return $fields;
});
```
"Sections" are where your setting appears on the admin settings page. Keys for settings sections:
- client_settings
- user_settings
- authorization_settings
- log_settings
Field types:
- text
- checkbox
- select (requires an array of "options")
### Actions
WordPress actions are generic events that other plugins can react to.
Actions API: [`add_action`](https://developer.wordpress.org/reference/functions/add_action/) and [`do_actions`](https://developer.wordpress.org/reference/functions/do_action/)
You'll probably only ever want to use `add_action` when hooking into this plugin.
#### `openid-connect-generic-user-create`
React to a new user being created by this plugin.
Provides 2 arguments: the `\WP_User` object that was created, and the `$user_claim` from the IDP server.
```
add_action('openid-connect-generic-user-create', function( $user, $user_claim ) {
// Send the user an email when their account is first created.
wp_mail(
$user->user_email,
__('Welcome to my web zone'),
"Hi {$user->first_name},\n\nYour account has been created at my cool website.\n\n Enjoy!"
);
}, 10, 2);
```
#### `openid-connect-generic-user-update`
React to the user being updated after login. This is the event that happens when a user logins and they already exist as
a user in WordPress, as opposed to a new WordPress user being created.
Provides 1 argument: the user's WordPress user ID.
```
add_action('openid-connect-generic-user-update', function( $uid ) {
// Keep track of the number of times the user has logged into the site.
$login_count = get_user_meta( $uid, 'my-user-login-count', TRUE);
$login_count += 1;
add_user_meta( $uid, 'my-user-login-count', $login_count, TRUE);
});
```
#### `openid-connect-generic-update-user-using-current-claim`
React to an existing user logging in (after authentication and authorization).
Provides 2 arguments: the `WP_User` object, and the `$user_claim` provided by the IDP server.
```
add_action('openid-connect-generic-update-user-using-current-claim', function( $user, $user_claim) {
// Based on some data in the user_claim, modify the user.
if ( !empty( $user_claim['wp_user_role'] ) ) {
if ( $user_claim['wp_user_role'] == 'should-be-editor' ) {
$user->set_role( 'editor' );
}
}
}, 10, 2);
```
#### `openid-connect-generic-redirect-user-back`
React to a user being redirected after a successful login. This hook is the last hook that will fire when a user logs
in. It will only fire if the plugin setting "Redirect Back to Origin Page" is enabled at Dashboard > Settings >
OpenID Connect Generic. It will fire for both new and existing users.
Provides 2 arguments: the url where the user will be redirected, and the `WP_User` object.
```
add_action('openid-connect-generic-redirect-user-back', function( $redirect_url, $user ) {
// Take over the redirection complete. Send users somewhere special based on their capabilities.
if ( $user->has_cap( 'edit_users' ) ) {
wp_redirect( admin_url( 'users.php' ) );
exit();
}
}, 10, 2);
```
### User Meta Data
This plugin stores meta data about the user for both practical and debugging purposes.
* `openid-connect-generic-subject-identity` - The identity of the user provided by the IDP server.
* `openid-connect-generic-last-id-token-claim` - The user's most recent `id_token` claim, decoded and stored as an array.
* `openid-connect-generic-last-user-claim` - The user's most recent `user_claim`, stored as an array.
* `openid-connect-generic-last-token-response` - The user's most recent `token_response`, stored as an array.