private async Task <string> GetAuthProviderByUserIdAsync(IRedisClientFacadeAsync redis, string provider, string userId, CancellationToken token = default) { var idx = IndexProviderToUserIdHash(provider); var oAuthProviderId = await redis.GetValueFromHashAsync(idx, userId, token).ConfigAwait(); return(oAuthProviderId); }
private Task <IUserAuth> GetUserAuthAsync(IRedisClientFacadeAsync redis, string userAuthId, CancellationToken token = default) { if (userAuthId == null || !long.TryParse(userAuthId, out var longId)) { return(null); } return(redis.AsAsync <IUserAuth>().GetByIdAsync(longId, token)); }
private async Task <IUserAuth> GetUserAuthByUserNameAsync(IRedisClientFacadeAsync redis, string userNameOrEmail, CancellationToken token = default) { if (userNameOrEmail == null) { return(null); } var isEmail = userNameOrEmail.Contains("@"); var userId = isEmail ? await redis.GetValueFromHashAsync(IndexEmailToUserId, userNameOrEmail, token).ConfigAwait() : await redis.GetValueFromHashAsync(IndexUserNameToUserId, userNameOrEmail, token).ConfigAwait(); return(userId == null ? null : await redis.AsAsync <IUserAuth>().GetByIdAsync(userId, token).ConfigAwait()); }
private async Task <IUserAuth> GetUserAuthAsync(IRedisClientFacadeAsync redis, IAuthSession authSession, IAuthTokens tokens, CancellationToken token = default) { if (!authSession.UserAuthId.IsNullOrEmpty()) { var userAuth = await GetUserAuthAsync(redis, authSession.UserAuthId, token).ConfigAwait(); if (userAuth != null) { return(userAuth); } } if (!authSession.UserAuthName.IsNullOrEmpty()) { var userAuth = await GetUserAuthByUserNameAsync(authSession.UserAuthName, token).ConfigAwait(); if (userAuth != null) { return(userAuth); } } if (tokens == null || tokens.Provider.IsNullOrEmpty() || tokens.UserId.IsNullOrEmpty()) { return(null); } var oAuthProviderId = await GetAuthProviderByUserIdAsync(redis, tokens.Provider, tokens.UserId, token).ConfigAwait(); if (!oAuthProviderId.IsNullOrEmpty()) { var oauthProvider = await redis.AsAsync <TUserAuthDetails>().GetByIdAsync(oAuthProviderId, token).ConfigAwait(); if (oauthProvider != null) { return(await redis.AsAsync <IUserAuth>().GetByIdAsync(oauthProvider.UserAuthId, token)); } } return(null); }
private async Task AssertNoExistingUserAsync(IRedisClientFacadeAsync redis, IUserAuth newUser, IUserAuth exceptForExistingUser = null, CancellationToken token = default) { if (newUser.UserName != null) { var existingUser = await GetUserAuthByUserNameAsync(redis, newUser.UserName, token).ConfigAwait(); if (existingUser != null && (exceptForExistingUser == null || existingUser.Id != exceptForExistingUser.Id)) { throw new ArgumentException(ErrorMessages.UserAlreadyExistsFmt.LocalizeFmt(newUser.UserName.SafeInput())); } } if (newUser.Email != null) { var existingUser = await GetUserAuthByUserNameAsync(redis, newUser.Email, token).ConfigAwait(); if (existingUser != null && (exceptForExistingUser == null || existingUser.Id != exceptForExistingUser.Id)) { throw new ArgumentException(ErrorMessages.EmailAlreadyExistsFmt.LocalizeFmt(newUser.Email.SafeInput())); } } }