public virtual async Task <bool> Handle(JObject jObj, CancellationToken cancellationToken)
        {
            bool isAdded   = true;
            var  parameter = ExtractParameter(jObj);
            var  user      = await _oauthUserRepository.FindOAuthUserByClaim(SimpleIdServer.Jwt.Constants.UserClaims.ScimId, parameter.ScimId, cancellationToken);

            if (user != null)
            {
                _logger.LogError("User cannot be added because it already exists");
                throw new OAuthException(ErrorCodes.INVALID_REQUEST, string.Format(ErrorMessages.USER_ALREADY_EXISTS, parameter.ScimId));
            }

            var sub = parameter.Content.GetStr(SimpleIdServer.Jwt.Constants.UserClaims.Subject);

            if (!string.IsNullOrWhiteSpace(sub))
            {
                user = await _oauthUserRepository.FindOAuthUserByLogin(sub.ToString(), cancellationToken);

                isAdded = user == null;
            }

            if (isAdded)
            {
                user = OAuthUser.Create(sub);
            }

            UpdateUser(parameter.Content, user);
            if (parameter.GenerateOTP)
            {
                user.ResetOtp();
            }

            if (isAdded)
            {
                await _oauthUserRepository.Add(user, cancellationToken);
            }
            else
            {
                await _oauthUserRepository.Update(user, cancellationToken);
            }

            await _oauthUserRepository.SaveChanges(cancellationToken);

            _logger.LogInformation($"the user {parameter.ScimId} has been added/updated");
            return(true);
        }
Пример #2
0
        public static OAuthUser BuildOAuthUser(this ClaimsPrincipal claimsPrincipal, string scheme)
        {
            var    userClaims = claimsPrincipal.BuildClaims();
            var    claimSub   = userClaims.FirstOrDefault(c => c.Name == SimpleIdServer.Jwt.Constants.UserClaims.Subject);
            var    claimName  = userClaims.FirstOrDefault(c => c.Name == SimpleIdServer.Jwt.Constants.UserClaims.Name);
            string sub        = claimSub == null ? null : claimSub.Value;
            string name       = claimName == null ? null : claimName.Value;
            var    user       = OAuthUser.Create(name, null);

            if (claimSub != null)
            {
                userClaims.Remove(claimSub);
                userClaims.Add(new Common.Domains.UserClaim(SimpleIdServer.Jwt.Constants.UserClaims.Subject, user.Id));
            }

            user.UpdateClaims(userClaims);
            user.AddExternalAuthProvider(scheme, sub);
            return(user);
        }
        public virtual async Task <bool> Handle(JObject jObj, CancellationToken cancellationToken)
        {
            var parameter = ExtractParameter(jObj);
            var user      = await _oauthUserRepository.FindOAuthUserByClaim(SimpleIdServer.Jwt.Constants.UserClaims.ScimId, parameter.ScimId, cancellationToken);

            if (user != null)
            {
                _logger.LogError("User cannot be added because it already exists");
                throw new OAuthException(ErrorCodes.INVALID_REQUEST, string.Format(ErrorMessages.USER_ALREADY_EXISTS, parameter.ScimId));
            }

            var newUser = OAuthUser.Create(parameter.Content.GetStr(SimpleIdServer.Jwt.Constants.UserClaims.Subject));

            UpdateUser(parameter.Content, newUser);
            await _oauthUserRepository.Add(newUser, cancellationToken);

            await _oauthUserRepository.SaveChanges(cancellationToken);

            _logger.LogInformation($"the user {parameter.ScimId} has been added");
            return(true);
        }