Exemplo n.º 1
0
        public override List <IUser> Add(IEnumerable <IUser> users)
        {
            var duplicateUsernames = new List <string>();

            foreach (User user in users)
            {
                if (Get(user.Username) != null)
                {
                    duplicateUsernames.Add(user.Username);
                    continue;
                }
                if (!user.IsHashed)
                {
                    continue;
                }
                if (string.IsNullOrWhiteSpace(user.Salt))
                {
                    user.Salt = Hash.Get(user.Username);
                }
                if (string.IsNullOrWhiteSpace(user.Password))
                {
                    // Todo: Password notification email here. Maybe plugins for handling password (Creation, Resetting, etc.)
                    user.Password = Hash.Get(CryptoRandomString.GetCryptoRandomAlphaNumericString(10), user.Salt);
                }
                user.Password = Hash.Get(user.Password, user.Salt);
            }
            if (duplicateUsernames.Count > 0)
            {
                throw new Exception("Duplicate username(s) detected: " + string.Join(", ", duplicateUsernames));
            }
            return(Repo.Create(users));
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public virtual async Task <IToken> BuildAsync(ICredentials creds, IUser user, List <RelatedEntityCollection> relatedEntityCollections)
        {
            if (user == null)
            {
                var userClient = ClientsCache.Generic.GetValueOrNew <EntityClientAdminAsync <User, long> >(typeof(User).Name);
                var odataUser  = await userClient.GetAsync(creds.User) ?? throw new Exception("User not found.");

                user = odataUser.Object;
                relatedEntityCollections = relatedEntityCollections ?? new List <RelatedEntityCollection>();
                relatedEntityCollections.AddRange(odataUser.RelatedEntityCollection);
            }
            var tokenClient = ClientsCache.Generic.GetValueOrNew <EntityClientAdminAsync <Token, long>, bool>(typeof(Token).Name, true);
            var token       = new Token {
                Text = CryptoRandomString.GetCryptoRandomAlphaNumericString(TokenSize), UserId = user.Id
            };
            var odataToken = await tokenClient.PostAsync(new List <Token> {
                token
            });

            var claimConfigClient = ClientsCache.Generic.GetValueOrNew <EntityClientAdminAsync <ClaimConfiguration, int> >(typeof(ClaimConfiguration).Name);
            var claimConfigs      = await claimConfigClient.GetAllAsync();

            var claims = await ClaimsBuilder.BuildAsync(user, claimConfigs?.Select(c => c.Object));

            if (claims != null && claims.Count > 0)
            {
                token.ClaimDomains.AddRange(claims);
            }
            Task.WaitAll();
            return(token);
        }