Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensuring that an user authenticated in AAD has a corresponding group … #167

Merged

Conversation

ryanwelcher
Copy link
Contributor

@ryanwelcher ryanwelcher commented Feb 7, 2018

This PR checks the Groups for the user attempting to access before creating the user. This is an attempt to address #126

Copy link
Owner

@psignoret psignoret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request! It looks like you're assuming that Azure AD group membership to WordPress role mapping will always be enabled, which is not necessarily the case. Could you also take into account the scenario where the plugin is not configured to check for group membership?


// Of the AAD groups defined in the settings, get only those where the user is a member
$group_ids = array_keys( $this->settings->aad_group_to_wp_role_map );
$group_memberships = AADSSO_GraphHelper::user_check_member_groups( $jwt->oid, $group_ids );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only be checking for group membership if group->roles is enabled (the enable_aad_group_to_wp_role setting), for a couple reasons:

  • Checking for group membership adds a Graph API request to the sign-in, so we definitely don't want to be checking if there's no reason to.
  • Checking for group membership requires permission to read all groups. This is a permission which can only be granted by an administrator, which could limit use of the plugin for those who just want it for simple sign-in (and are not admins). Also, if the plugin hasn't been configured for group->role enforcement, it is unlikely it has been given the right permissions for the Azure AD Graph API.

At minimum, this part should only be run if enable_aad_group_to_wp_role is enabled.

@@ -364,7 +375,8 @@ function get_wp_user_from_aad_user( $jwt ) {

// Since the user was authenticated with AAD, but not found in WordPress,
// need to decide whether to create a new user in WP on-the-fly, or to stop here.
if( true === $this->settings->enable_auto_provisioning ) {
// 3. If there are no groups for this user, we should not be creating it.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is only true if enable_aad_group_to_wp_role is true.

@ryanwelcher
Copy link
Contributor Author

@psignoret thanks for the feedback! I've made some changes to respect the appropriate settings in regards to groups. Please let me know if I have missed anything.

@ryanwelcher
Copy link
Contributor Author

I just realized my logic was incorrect in the new if() statement. Updated now.

Copy link
Owner

@psignoret psignoret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, with just a few tiny cosmetic requests.

if ( true === $this->settings->enable_aad_group_to_wp_role && empty( $group_memberships->value ) ) {
// The user was authenticated, but not found in WP and auto-provisioning is disabled
return new WP_Error(
'user_not_registered',
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to something unique? (E.g. user_not_assigned_to_group.)


// 3. If we are configured to check, and there are no groups for this user, we should not be creating it.
if ( true === $this->settings->enable_aad_group_to_wp_role && empty( $group_memberships->value ) ) {
// The user was authenticated, but not found in WP and auto-provisioning is disabled
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update the comment to reflect the error condition? (The user was authenticated, but is not a member of a role-granting group.)

// Of the AAD groups defined in the settings, get only those where the user is a member
$group_ids = array_keys( $this->settings->aad_group_to_wp_role_map );
$group_memberships = AADSSO_GraphHelper::user_check_member_groups( $aad_user_id, $group_ids );
function update_wp_user_roles( $user, $group_memberships ) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also update the function's DocBlock accordingly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been updated.

@psignoret psignoret merged commit 5c9f647 into psignoret:master Feb 14, 2018
@psignoret
Copy link
Owner

Thanks, @ryanwelcher!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants