private IEnumerable <Auth0.Connection> GetConnections(string connectionType)
        {
            IEnumerable <Auth0.Connection> connections = null;

            try
            {
                if (string.IsNullOrEmpty(connectionType))
                {
                    // All connections
                    connections = this.auth0Client.GetConnections();
                }
                else if (connectionType.Equals(EnterpriseHierarchyNode, StringComparison.OrdinalIgnoreCase))
                {
                    connections = this.auth0Client.GetEnterpriseConnections();
                }
                else if (connectionType.Equals(SocialHierarchyNode, StringComparison.OrdinalIgnoreCase))
                {
                    connections = this.auth0Client.GetSocialConnections();
                }

                Auth0LoggingService.Write("GetConnections: Total connections {0}", connections != null ? connections.Count() : 0);
            }
            catch (Exception ex)
            {
                Auth0LoggingService.WriteError(ex.ToString());
            }

            return(connections != null ? connections : new List <Auth0.Connection>());
        }
        protected void InitializeAuth0Client()
        {
            this.auth0Config = this.configurationRepository.GetConfiguration();

            if (!this.auth0Config.IsValid)
            {
                return;
            }

            try
            {
                var clientsIds     = this.auth0Config.ClientId.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                var clientsSecrets = this.auth0Config.ClientSecret.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                var clientIdIndex  = Array.IndexOf(clientsIds, Utils.GetClaimsValue(ClientIdClaimsType));

                // if clientID was not found, use the first one configured on central admin
                if (clientIdIndex == -1)
                {
                    clientIdIndex = 0;
                }

                this.auth0Client = new Auth0.Client(
                    clientsIds[clientIdIndex],
                    clientsSecrets[clientIdIndex],
                    this.auth0Config.Domain);
            }
            catch (Exception ex)
            {
                Auth0LoggingService.WriteError(ex.ToString());
            }

            this.alwaysResolveValue    = true; //this.auth0Config.AlwaysResolveUserInput;
            this.pickerEntityGroupName = this.auth0Config.PickerEntityGroupName;
        }
        protected virtual ICollection <ConsolidatedResult> ResolveInputBulk(string input, string selectedNode)
        {
            Auth0LoggingService.Write("ResolveInputBulk: input={0}, selectedNode={1}", input, selectedNode);

            if (string.IsNullOrEmpty(input))
            {
                return(null);
            }

            if (this.auth0Client == null)
            {
                Auth0LoggingService.WriteError("Auth0 client was not initialized.");
                return(null);
            }

            IEnumerable <Auth0.User> users = null;
            var consolidatedResults        = new Collection <ConsolidatedResult>();

            try
            {
                Auth0LoggingService.Write("ResolveInputBulk: Searching for social and enterprise users.");

                var socialUsers     = this.auth0Client.GetSocialUsers(input);
                var enterpriseUsers = this.auth0Client.GetEnterpriseUsers(input);

                // Distinct by user.Email
                users = socialUsers.Union(enterpriseUsers).DistinctBy(u => u.Email);

                // Log results.
                Auth0LoggingService.Write("ResolveInputBulk: Found {0}.", users != null ? users.Count() : 0);
            }
            catch (Exception ex)
            {
                Auth0LoggingService.WriteError(ex.ToString());
            }

            if (users != null)
            {
                foreach (var user in users)
                {
                    var claimAttribute = new ClaimAttribute
                    {
                        ClaimEntityType = SPClaimEntityTypes.User,
                        PeoplePickerAttributeDisplayName     = UsersNode,
                        PeoplePickerAttributeHierarchyNodeId = UsersNode
                    };

                    consolidatedResults.Add(new ConsolidatedResult
                    {
                        Attribute    = claimAttribute,
                        Auth0User    = user,
                        PickerEntity = this.GetPickerEntity(user, SPClaimEntityTypes.User)
                    });
                }
            }

            return(consolidatedResults);
        }
Пример #4
0
        /// <summary>
        /// Get the first TrustedLoginProvider associated with current claim provider
        /// </summary>
        /// <param name="ProviderInternalName"></param>
        /// <returns></returns>
        public static SPTrustedLoginProvider GetSPTrustAssociatedWithCP(string providerInternalName)
        {
            var providers = SPSecurityTokenServiceManager.Local.TrustedLoginProviders.Where(p => p.ClaimProviderName == providerInternalName);

            if (providers != null && providers.Count() > 0)
            {
                if (providers.Count() == 1)
                {
                    return(providers.First());
                }
                else
                {
                    Auth0LoggingService.WriteError("Claim provider '{0}' is associated to several TrustedLoginProvider, which is not supported because there is no way to determine what TrustedLoginProvider is currently calling the claim provider during search and resolution.", providerInternalName);
                }
            }

            Auth0LoggingService.WriteError("Claim provider '{0}' is not associated with any SPTrustedLoginProvider, and it cannot create permissions for a trust if it is not associated to it.\r\nUse PowerShell cmdlet Get-SPTrustedIdentityTokenIssuer to create association", providerInternalName);

            return(null);
        }
        protected override void FillResolve(Uri context, string[] entityTypes, SPClaim resolveInput, List <PickerEntity> resolved)
        {
            Auth0LoggingService.Write("FillResolve input: {0}", resolveInput != null ? resolveInput.ClaimType + "/" + resolveInput.Value : "empty");

            if (!this.SetSPTrustInCurrentContext(context))
            {
                Auth0LoggingService.Write("FillResolve: SetSPTrustInCurrentContext=false.");
                return;
            }

            if (!String.Equals(resolveInput.OriginalIssuer,
                               SPOriginalIssuers.Format(SPOriginalIssuerType.TrustedProvider, this.associatedSPTrustedLoginProvider.Name),
                               StringComparison.OrdinalIgnoreCase))
            {
                Auth0LoggingService.Write("FillResolve: Original issuers don't match, {0} != {1}.", resolveInput.OriginalIssuer, SPOriginalIssuers.Format(SPOriginalIssuerType.TrustedProvider, this.associatedSPTrustedLoginProvider.Name));
                return;
            }

            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                var input = resolveInput.Value.Contains(IdentifierValuesSeparator) ?
                            resolveInput.Value.Split(IdentifierValuesSeparator)[1] : resolveInput.Value;
                var connectionName = resolveInput.Value.Contains(IdentifierValuesSeparator) ?
                                     resolveInput.Value.Split(IdentifierValuesSeparator)[0] : string.Empty;

                var consolidatedResults = this.ResolveInputBulk(input, connectionName);

                if (consolidatedResults != null && consolidatedResults.Count > 0)
                {
                    resolved.Add(consolidatedResults.ElementAt(0).PickerEntity);
                    return;
                }

                if (resolveInput.ClaimType == ConnectionClaimType)
                {
                    var user = new Auth0.User
                    {
                        Identities = new List <Identity>
                        {
                            new Identity {
                                Connection = resolveInput.Value
                            }
                        }
                    };

                    resolved.Add(this.GetPickerEntity(user, SPClaimEntityTypes.FormsRole));
                }
                else if (resolveInput.ClaimType == RoleClaimType)
                {
                    resolved.Add(this.GetRolePickerEntity(resolveInput.Value));
                }
                else if (this.alwaysResolveValue)
                {
                    var user = new Auth0.User
                    {
                        Email      = input,
                        Name       = string.Empty,
                        Picture    = string.Empty,
                        Identities = new List <Identity>
                        {
                            new Identity {
                                Connection = connectionName
                            }
                        }
                    };

                    resolved.Add(this.GetPickerEntity(user, SPClaimEntityTypes.User));
                }
            });
        }