public async Task RequestJwtAsync(UserAuthenticationModel userData, bool forceRefresh)
        {
            var fileExists = await _keyStorageContainer.PublicKeyExists(_deviceId);

            if (forceRefresh || !fileExists || fileExists && string.IsNullOrEmpty(await _storageContainer.ReadFileAsStringAsync(_jwtFilePath)))
            {
                var key         = _cryptoService.RetrieveMergedKey("server");
                var cryptedData = await _cryptoService.EncryptTripleDESAsync(JsonConvert.SerializeObject(userData), key);

                var jwtRequest = new SecureAuthenticationModel()
                {
                    Id      = _deviceId,
                    Content = Convert.ToBase64String(cryptedData)
                };

                var content = new StringContent(JsonConvert.SerializeObject(jwtRequest), Encoding.UTF8, "application/json");

                var response = await _httpClient.PostAsync("api/jwt/requestjwt", content);

                if (response.IsSuccessStatusCode)
                {
                    var responseString = await response.Content.ReadAsStringAsync();

                    var responseModel = JsonConvert.DeserializeObject <SecureJwtModel>(responseString);
                    await _storageContainer.WriteFileAsync(_jwtFilePath, JsonConvert.SerializeObject(responseModel.TokenModel));
                }
                else if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    throw new UnauthorizedAccessException();
                }
                else
                {
                    await _storageContainer.WriteFileAsync(_jwtFilePath, "");

                    throw new Exception(response.ReasonPhrase);
                }
            }
        }
Esempio n. 2
0
 private async Task <bool> RSAKeysExists(string id)
 {
     return(await _keyStorageContainer.PublicKeyExists(id) &&
            await _keyStorageContainer.PrivateKeyExists(id));
 }