Beispiel #1
0
        public KeysRequest GetData()
        {
            KeysRequest request = new KeysRequest();

            request.Acts.AddRange(acts);

            return(request);
        }
        // public override Task<TableSamples> GetAllTableSamples(FiltersRequest requestData, ServerCallContext context)
        // {
        //   var responseData = new TableSamples();
        // }

        public override Task <TableSample> GetTableSample(KeysRequest request, ServerCallContext context)
        {
            var tableSample = new Models.GetTableSample(request);

            if (tableSample.Status <= 0)
            {
                return(null);
            }
            return(Task.FromResult(tableSample.Present));
        }
        private (int, Entities.TableSample) accept(KeysRequest request)
        {
            // Console.WriteLine($"request.id = {request.Id}");
            var sql = $@"
      select
        id,
        code,
        name,
        created_at
      from
        table_samples
      where
        id = @requestId
      ;
      ";

            // Console.WriteLine($"query = {DbConnection.TrimSql(sql, 6)}");

            try
            {
                Entities.TableSample row;
                // using (SqlConnection conn = new SqlConnection())
                using (NpgsqlConnection conn = new NpgsqlConnection(DbConnection.connectionString))
                {
                    // conn.ConnectionString = DbConnection.connectionString;
                    conn.Open();
                    Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
                    // Console.WriteLine($"db.State = {conn.State}");
                    // var tests = conn.QuerySingleOrDefault<Entities.Tests>("select * from tests where id = @requestId", new { requestId = 1 });
                    // Console.WriteLine($"tests.name = {tests.Name}");
                    row = conn.QuerySingleOrDefault <Entities.TableSample>(DbConnection.TrimSql(sql, 6), new { requestId = request.Id });
                    Console.WriteLine(row);
                    if (row == null)
                    {
                        Console.WriteLine("result 0.");
                        return(0, null);
                    }
                    else
                    {
                        Console.WriteLine($"row.id = {row.Id}");
                        Console.WriteLine($"row.code = {row.Code}");
                        Console.WriteLine($"row.name = {row.Name}");
                        return(1, row);
                    }
                }
            }
            catch
            {
                return(-1, null);
            }
        }
        public GetTableSample(KeysRequest request)
        {
            Status  = -1;
            Present = null;

            var(acceptStatus, accptResult) = this.accept(request);
            if (acceptStatus <= 0)
            {
                Status = acceptStatus;
                return;
            }

            var(presentStatus, presentResult) = this.present(accptResult);
            if (presentStatus < 0)
            {
                Status = presentStatus;
                return;
            }

            Status  = presentStatus;
            Present = presentResult;
        }
 public Task PostAccountKeysAsync(KeysRequest request)
 {
     return(SendAsync <KeysRequest, object>(HttpMethod.Post, "/accounts/keys", request, true, false));
 }
Beispiel #6
0
        private async Task <AuthResult> LogInHelperAsync(string email, string hashedPassword, string localHashedPassword,
                                                         string code, string codeVerifier, string redirectUrl, SymmetricCryptoKey key,
                                                         TwoFactorProviderType?twoFactorProvider = null, string twoFactorToken = null, bool?remember = null,
                                                         string captchaToken = null, string orgId = null)
        {
            var storedTwoFactorToken = await _tokenService.GetTwoFactorTokenAsync(email);

            var appId = await _appIdService.GetAppIdAsync();

            var deviceRequest = new DeviceRequest(appId, _platformUtilsService);

            string[] emailPassword;
            string[] codeCodeVerifier;
            if (email != null && hashedPassword != null)
            {
                emailPassword = new[] { email, hashedPassword };
            }
            else
            {
                emailPassword = null;
            }
            if (code != null && codeVerifier != null && redirectUrl != null)
            {
                codeCodeVerifier = new[] { code, codeVerifier, redirectUrl };
            }
            else
            {
                codeCodeVerifier = null;
            }

            TokenRequest request;

            if (twoFactorToken != null && twoFactorProvider != null)
            {
                request = new TokenRequest(emailPassword, codeCodeVerifier, twoFactorProvider, twoFactorToken, remember,
                                           captchaToken, deviceRequest);
            }
            else if (storedTwoFactorToken != null)
            {
                request = new TokenRequest(emailPassword, codeCodeVerifier, TwoFactorProviderType.Remember,
                                           storedTwoFactorToken, false, captchaToken, deviceRequest);
            }
            else
            {
                request = new TokenRequest(emailPassword, codeCodeVerifier, null, null, false, captchaToken, deviceRequest);
            }

            var response = await _apiService.PostIdentityTokenAsync(request);

            ClearState();
            var result = new AuthResult {
                TwoFactor = response.TwoFactorNeeded, CaptchaSiteKey = response.CaptchaResponse?.SiteKey
            };

            if (result.CaptchaNeeded)
            {
                return(result);
            }

            if (result.TwoFactor)
            {
                // Two factor required.
                Email = email;
                MasterPasswordHash      = hashedPassword;
                LocalMasterPasswordHash = localHashedPassword;
                Code                      = code;
                CodeVerifier              = codeVerifier;
                SsoRedirectUrl            = redirectUrl;
                _key                      = _setCryptoKeys ? key : null;
                TwoFactorProvidersData    = response.TwoFactorResponse.TwoFactorProviders2;
                result.TwoFactorProviders = response.TwoFactorResponse.TwoFactorProviders2;
                CaptchaToken              = response.TwoFactorResponse.CaptchaToken;
                return(result);
            }

            var tokenResponse = response.TokenResponse;

            result.ResetMasterPassword = tokenResponse.ResetMasterPassword;
            result.ForcePasswordReset  = tokenResponse.ForcePasswordReset;
            if (tokenResponse.TwoFactorToken != null)
            {
                await _tokenService.SetTwoFactorTokenAsync(tokenResponse.TwoFactorToken, email);
            }
            await _tokenService.SetAccessTokenAsync(tokenResponse.AccessToken, true);

            await _stateService.AddAccountAsync(
                new Account(
                    new Account.AccountProfile()
            {
                UserId = _tokenService.GetUserId(),
                Email = _tokenService.GetEmail(),
                Name = _tokenService.GetName(),
                KdfType = tokenResponse.Kdf,
                KdfIterations = tokenResponse.KdfIterations,
                HasPremiumPersonally = _tokenService.GetPremium(),
            },
                    new Account.AccountTokens()
            {
                AccessToken = tokenResponse.AccessToken,
                RefreshToken = tokenResponse.RefreshToken,
            }
                    )
                );

            _messagingService.Send("accountAdded");
            if (_setCryptoKeys)
            {
                if (key != null)
                {
                    await _cryptoService.SetKeyAsync(key);
                }

                if (localHashedPassword != null)
                {
                    await _cryptoService.SetKeyHashAsync(localHashedPassword);
                }

                if (code == null || tokenResponse.Key != null)
                {
                    if (tokenResponse.KeyConnectorUrl != null)
                    {
                        await _keyConnectorService.GetAndSetKey(tokenResponse.KeyConnectorUrl);
                    }

                    await _cryptoService.SetEncKeyAsync(tokenResponse.Key);

                    // User doesn't have a key pair yet (old account), let's generate one for them.
                    if (tokenResponse.PrivateKey == null)
                    {
                        try
                        {
                            var keyPair = await _cryptoService.MakeKeyPairAsync();

                            await _apiService.PostAccountKeysAsync(new KeysRequest
                            {
                                PublicKey           = keyPair.Item1,
                                EncryptedPrivateKey = keyPair.Item2.EncryptedString
                            });

                            tokenResponse.PrivateKey = keyPair.Item2.EncryptedString;
                        }
                        catch { }
                    }

                    await _cryptoService.SetEncPrivateKeyAsync(tokenResponse.PrivateKey);
                }
                else if (tokenResponse.KeyConnectorUrl != null)
                {
                    // SSO Key Connector Onboarding
                    var password = await _cryptoFunctionService.RandomBytesAsync(64);

                    var k = await _cryptoService.MakeKeyAsync(Convert.ToBase64String(password), _tokenService.GetEmail(), tokenResponse.Kdf, tokenResponse.KdfIterations);

                    var keyConnectorRequest = new KeyConnectorUserKeyRequest(k.EncKeyB64);
                    await _cryptoService.SetKeyAsync(k);

                    var encKey = await _cryptoService.MakeEncKeyAsync(k);

                    await _cryptoService.SetEncKeyAsync(encKey.Item2.EncryptedString);

                    var keyPair = await _cryptoService.MakeKeyPairAsync();

                    try
                    {
                        await _apiService.PostUserKeyToKeyConnector(tokenResponse.KeyConnectorUrl, keyConnectorRequest);
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Unable to reach Key Connector", e);
                    }

                    var keys = new KeysRequest
                    {
                        PublicKey           = keyPair.Item1,
                        EncryptedPrivateKey = keyPair.Item2.EncryptedString
                    };
                    var setPasswordRequest = new SetKeyConnectorKeyRequest(
                        encKey.Item2.EncryptedString, keys, tokenResponse.Kdf, tokenResponse.KdfIterations, orgId
                        );
                    await _apiService.PostSetKeyConnectorKey(setPasswordRequest);
                }
            }

            await _stateService.SetBiometricLockedAsync(false);

            _messagingService.Send("loggedIn");
            return(result);
        }