diff --git a/README.md b/README.md index afb4fbb..bb1f844 100644 --- a/README.md +++ b/README.md @@ -138,72 +138,72 @@ The different fields that can be defined in the settings JSON in **Settings** > Users are matched by their email address in WordPress, and whichever role they have in WordPress is maintained. -| Setting | Example value -| --- | --- -| Display name | Contoso -| Client ID | 9054eff5-bfef-4cc5-82fd-8c35534e48f9 -| Client Secret | NTY5MmE5YjMwMGY2MWQ0NjU5MzYxNjdjNzE1OGNiZmY= -| Reply URL | https://www.example.com/blog/wp-login.php -| Field to match to UPN | Email Address +| Setting | Example value | +| --------------------- | -------------------------------------------- | +| Display name | Contoso | +| Client ID | 9054eff5-bfef-4cc5-82fd-8c35534e48f9 | +| Client Secret | NTY5MmE5YjMwMGY2MWQ0NjU5MzYxNjdjNzE1OGNiZmY= | +| Redirect URL | https://www.example.com/blog/wp-login.php | +| Field to match to UPN | Email Address | ### Match on username alias Users are matched by their login names in WordPress and the alias portion of their Azure AD UserPrincipalName. Whichever role they have in WordPress is maintained. -| Setting | Example value -| --- | --- -| Display name | Contoso -| Client ID | 9054eff5-bfef-4cc5-82fd-8c35534e48f9 -| Client Secret | NTY5MmE5YjMwMGY2MWQ0NjU5MzYxNjdjNzE1OGNiZmY= -| Reply URL | https://www.example.com/blog/wp-login.php -| Field to match to UPN | Login Name -| Match on alias of the UPN | Yes +| Setting | Example value | +| ------------------------- | -------------------------------------------- | +| Display name | Contoso | +| Client ID | 9054eff5-bfef-4cc5-82fd-8c35534e48f9 | +| Client Secret | NTY5MmE5YjMwMGY2MWQ0NjU5MzYxNjdjNzE1OGNiZmY= | +| Redirect URL | https://www.example.com/blog/wp-login.php | +| Field to match to UPN | Login Name | +| Match on alias of the UPN | Yes | ### Group membership-based roles, no default role Users are matched by their login names in WordPress, and WordPress roles are dictated by membership to a given Azure AD group. Access is denied if they are not members of any of these groups. -| Setting | Example value -| --- | --- -| Display name | Contoso -| Client ID | 9054eff5-bfef-4cc5-82fd-8c35534e48f9 -| Client Secret | NTY5MmE5YjMwMGY2MWQ0NjU5MzYxNjdjNzE1OGNiZmY= -| Reply URL | https://www.example.com/blog/wp-login.php -| Field to match to UPN | Login Name -| Enable Azure AD group to WP role association | Yes -| Default WordPress role if not in Azure AD group | (None, deny access) -| WordPress role to Azure AD group map |
| Administrator | 5d1915c4-2373-42ba-9796-7c092fa1dfc6 |
| Editor | 21c0f87b-4b65-48c1-9231-2f9295ef601c |
| Author | f5784693-11e5-4812-87db-8c6e51a18ffd |
| Contributor | 780e055f-7e64-4e34-9ff3-012910b7e5ad |
| Subscriber | f1be9515-0aeb-458a-8c0a-30a03c1afb67 |
| Administrator | 5d1915c4-2373-42ba-9796-7c092fa1dfc6 |
| Editor | 21c0f87b-4b65-48c1-9231-2f9295ef601c |
| Author | f5784693-11e5-4812-87db-8c6e51a18ffd |
| Contributor | 780e055f-7e64-4e34-9ff3-012910b7e5ad |
| Subscriber | f1be9515-0aeb-458a-8c0a-30a03c1afb67 |
| Administrator | 5d1915c4-2373-42ba-9796-7c092fa1dfc6 |
| Editor | 21c0f87b-4b65-48c1-9231-2f9295ef601c |
| Author | f5784693-11e5-4812-87db-8c6e51a18ffd |
| Contributor | 780e055f-7e64-4e34-9ff3-012910b7e5ad |
| Subscriber | f1be9515-0aeb-458a-8c0a-30a03c1afb67 |
| Administrator | 5d1915c4-2373-42ba-9796-7c092fa1dfc6 |
| Editor | 21c0f87b-4b65-48c1-9231-2f9295ef601c |
| Author | f5784693-11e5-4812-87db-8c6e51a18ffd |
| Contributor | 780e055f-7e64-4e34-9ff3-012910b7e5ad |
| Subscriber | f1be9515-0aeb-458a-8c0a-30a03c1afb67 |
| Administrator | 5d1915c4-2373-42ba-9796-7c092fa1dfc6 |
| Editor | 21c0f87b-4b65-48c1-9231-2f9295ef601c |
| Author | f5784693-11e5-4812-87db-8c6e51a18ffd |
| Contributor | 780e055f-7e64-4e34-9ff3-012910b7e5ad |
| Subscriber | f1be9515-0aeb-458a-8c0a-30a03c1afb67 |
| Administrator | 5d1915c4-2373-42ba-9796-7c092fa1dfc6 |
| Editor | 21c0f87b-4b65-48c1-9231-2f9295ef601c |
| Author | f5784693-11e5-4812-87db-8c6e51a18ffd |
| Contributor | 780e055f-7e64-4e34-9ff3-012910b7e5ad |
| Subscriber | f1be9515-0aeb-458a-8c0a-30a03c1afb67 |
'
- . __( 'Legacy settings have been migrated and the old configuration file has been deleted.', 'aad-sso-wordpress' )
- . __('To finish migration, unset AADSSO_SETTINGS_PATH from wp-config.php. ', 'aad-sso-wordpress')
- .'
'
- . esc_html__( 'Legacy settings have been migrated successfully. ', 'aad-sso-wordpress' )
- . sprintf( __('To finish migration, delete the file at the path %s. ', 'aad-sso-wordpress'), AADSSO_SETTINGS_PATH )
- . sprintf( __('Then, unset AADSSO_SETTINGS_PATH from wp-config.php. ', 'aad-sso-wordpress') )
- .'
'
- . sprintf( __('Legacy settings could not be migrated from %s. ', 'aad-sso-wordpress'), AADSSO_SETTINGS_PATH )
- . esc_html( 'File could not be parsed as JSON. ', 'aad-sso-wordpress' )
- . esc_html( 'Delete the file, or check its syntax.', 'aad-sso-wordpress' )
- .'
' - . __( 'Single Sign-on with Azure Active Directory settings have been reset to default.', - 'aad-sso-wordpress' ) - .'
%s
', - __( 'Map WordPress roles to Azure Active Directory groups.', 'aad-sso-wordpress' ) - ); - echo '| %s | %s |
|---|---|
| ' . htmlentities( $role['name'] ) . ' | '; - echo ''; - printf( - '', - $role_slug, - isset( $this->settings['role_map'][ $role_slug ] ) - ? esc_attr( $this->settings['role_map'][ $role_slug ] ) - : '' - ); - echo ' | '; - echo '
%s
', - __( 'Display Name will be shown on the WordPress login screen.', 'aad-sso-wordpress' ) - ); - } - - /** - * Renders the `org_domain_hint` form control. - */ - public function org_domain_hint_callback() { - $this->render_text_field( 'org_domain_hint' ); - printf( - '%s
', - __( 'Provides a hint to Azure AD about the domain or tenant they will be logging in to. If ' - . 'the domain is federated, the user will be automatically redirected to federation ' - . 'endpoint.', 'aad-sso-wordpress' ) - ); - } - - /** - * Renders the `client_id` form control - */ - public function client_id_callback() { - $this->render_text_field( 'client_id' ); - printf( - '%s
', - __( 'The client ID of the Azure AD application representing this blog.', 'aad-sso-wordpress' ) - ); - } - - /** - * Renders the `client_secret` form control - **/ - public function client_secret_callback() { - $this->render_text_field( 'client_secret' ); - printf( - '%s
', - __( 'A secret key for the Azure AD application representing this blog.', 'aad-sso-wordpress' ) - ); - } - - /** - * Renders the `redirect_uri` form control - **/ - public function redirect_uri_callback() { - $this->render_text_field( 'redirect_uri' ); - printf( - ' %s' - . '%s
', - wp_login_url(), - __( 'Set default', 'aad-sso-wordpress' ), - __( 'The URL where the user is redirected to after authenticating with Azure AD. ' - . 'This URL must be registered in Azure AD as a valid redirect URL, and it must be a ' - . 'page that invokes the "authenticate" filter. If you don\'t know what to set, leave ' - . 'the default value (which is this blog\'s login page).', 'aad-sso-wordpress' ) - ); - } - - /** - * Renders the `logout_redirect_uri` form control - **/ - public function logout_redirect_uri_callback() { - $this->render_text_field( 'logout_redirect_uri' ); - printf( - ' %s' - . '%s
', - wp_login_url(), - __( 'Set default', 'aad-sso-wordpress'), - __( 'The URL where the user is redirected to after signing out of Azure AD. ' - . 'This URL must be registered in Azure AD as a valid redirect URL. (This does not affect ' - . ' logging out of the blog, it is only used when logging out of Azure AD.)', 'aad-sso-wordpress' ) - ); - } - - /** - * Renders the `field_to_match_to_upn` form control. - */ - public function field_to_match_to_upn_callback() { - $selected = - isset( $this->settings['field_to_match_to_upn'] ) - ? $this->settings['field_to_match_to_upn'] - : ''; - ?> - - %s', - __( 'This specifies the WordPress user field which will be used to match to the Azure AD user\'s ' - . 'UserPrincipalName.', 'aad-sso-wordpress' ) - ); - } - - /** - * Renders the `match_on_upn_alias` checkbox control. - */ - public function match_on_upn_alias_callback() { - $this->render_checkbox_field( - 'match_on_upn_alias', - __( 'Match WordPress users based on the alias of their Azure AD UserPrincipalName. For example, ' - . 'Azure AD usernamebob@example.com will match WordPress user bob.',
- 'aad-sso-wordpress' )
- );
- }
-
- /**
- * Renders the `default_wp_role` control.
- */
- public function default_wp_role_callback() {
-
- // Default configuration should be most-benign
- if( ! isset( $this->settings['default_wp_role'] ) ) {
- $this->settings['default_wp_role'] = '';
- }
-
- echo '';
- printf(
- '%s
', - __('This is the default role that users will be assigned to if matching Azure AD group to ' - . 'WordPress roles is enabled, but the signed in user isn\'t a member of any of the ' - . 'configured Azure AD groups.', 'aad-sso-wordpress') - ); - } - - /** - * Renders the `enable_auto_provisioning` checkbox control. - */ - public function enable_auto_provisioning_callback() { - $this->render_checkbox_field( - 'enable_auto_provisioning', - __( 'Automatically create WordPress users, if needed, for authenticated Azure AD users.', - 'aad-sso-wordpress' ) - ); - } - - /** - * Renders the `enable_auto_forward_to_aad` checkbox control. - */ - public function enable_auto_forward_to_aad_callback() { - $this->render_checkbox_field( - 'enable_auto_forward_to_aad', - __( 'Automatically forward users to the Azure AD to sign in, skipping the WordPress login screen.', - 'aad-sso-wordpress') - ); - } - - /** - * Renders the `enable_aad_group_to_wp_role` checkbox control. - */ - public function enable_aad_group_to_wp_role_callback() { - $this->render_checkbox_field( - 'enable_aad_group_to_wp_role', - __( 'Automatically assign WordPress user roles based on Azure AD group membership.', - 'aad-sso-wordpress' ) - ); - } - - /** - * Renders the `openid_configuration_endpoint` form control - **/ - public function openid_configuration_endpoint_callback() { - $this->render_text_field( 'openid_configuration_endpoint' ); - printf( - ' %s' - . '%s
', - AADSSO_Settings::get_defaults( 'openid_configuration_endpoint' ), - __( 'Set default', 'aad-sso-wordpress'), - __( 'The OpenID Connect configuration endpoint to use. To support Microsoft Accounts and external ' - . 'users (users invited in from other Azure AD directories, known sometimes as "B2B users") you ' - . 'must use:https://login.microsoftonline.com/{tenant-id}/.well-known/openid-configuration, '
- . 'where {tenant-id} is the tenant ID or a verified domain name of your directory.',
- 'aad-sso-wordpress' )
- );
- }
-
- /**
- * Renders the `enable_full_logout` checkbox control.
- */
- public function enable_full_logout_callback() {
- $this->render_checkbox_field(
- 'enable_full_logout',
- __( 'Do a full logout of Azure AD when logging out of WordPress.',
- 'aad-sso-wordpress' )
- );
- }
-
- /**
- * Renders a simple text field and populates it with the setting value.
- *
- * @param string $name The setting name for the text input field.
- */
- public function render_text_field( $name ) {
- $value = isset( $this->settings[ $name ] ) ? esc_attr( $this->settings[ $name ] ) : '';
- printf(
- '',
- $name, $value
- );
- }
-
- /**
- * Renders a simple checkbox field and populates it with the setting value.
- *
- * @param string $name The setting name for the checkbox input field.
- * @param string $label The label to use for the checkbox.
- */
- public function render_checkbox_field( $name, $label ) {
- printf(
- ''
- . '',
- $name,
- isset( $this->settings[ $name ] ) && $this->settings[ $name ] ? 'checked' : '',
- $label
- );
- }
-
- /**
- * Indicates if user is currently on this settings page.
- */
- public function is_on_options_page() {
- $screen = get_current_screen();
- return $screen->id === $this->options_page_id;
- }
-
- /**
- * Ensures jQuery is loaded
- */
- public function maybe_include_jquery() {
- if ( $this->is_on_options_page() ) {
- wp_enqueue_script( 'jquery' );
- }
- }
-}
diff --git a/aad-sso-wordpress.php b/aad-sso-wordpress.php
index 201476a..00dde5a 100644
--- a/aad-sso-wordpress.php
+++ b/aad-sso-wordpress.php
@@ -1,746 +1,108 @@
settings = $settings;
-
- // Setup the admin settings page
- $this->setup_admin_settings();
-
- // Some debugging locations
- //add_action( 'admin_notices', array( $this, 'print_debug' ) );
- //add_action( 'login_footer', array( $this, 'print_debug' ) );
-
- // Add a link to the Settings page in the list of plugins
- add_filter(
- 'plugin_action_links_' . plugin_basename( __FILE__ ),
- array( $this, 'add_settings_link' )
- );
-
- // Register activation and deactivation hooks
- register_activation_hook( __FILE__, array( 'AADSSO', 'activate' ) );
- register_deactivation_hook( __FILE__, array( 'AADSSO', 'deactivate' ) );
-
- // If plugin is not configured, we shouldn't proceed.
- if ( ! $this->plugin_is_configured() ) {
- add_action( 'all_admin_notices', array( $this, 'print_plugin_not_configured' ) );
- return;
- }
-
- // Add the hook that starts the SESSION
- add_action( 'login_init', array( $this, 'register_session' ), 10 );
-
- // The authenticate filter
- add_filter( 'authenticate', array( $this, 'authenticate' ), 1, 3 );
-
- // Add the