369 lines
15 KiB
C#
369 lines
15 KiB
C#
using CryptoNet.Models;
|
|
using CryptoNet;
|
|
using FabitArchiwum.App.Model;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Newtonsoft.Json;
|
|
using Org.BouncyCastle.Ocsp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Claims;
|
|
using System.Security.Cryptography;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
using static System.Net.WebRequestMethods;
|
|
using Org.BouncyCastle.Security;
|
|
|
|
namespace FabitArchiwum.App
|
|
{
|
|
public sealed class AppHelper
|
|
{
|
|
private AppHelper() { }
|
|
private static AppHelper _instance;
|
|
private static string URL = "https://nzozepione.myqnapcloud.com:5555";
|
|
// private static string URL = "https://localhost:44309";
|
|
private static TokenModel tokenModel;
|
|
private static RSACryptoServiceProvider publicKey;
|
|
private static RSA privateKey;
|
|
private static X509Certificate2 certificate = new X509Certificate2("nzozepione.pl.pfx", "Pjmzha15", X509KeyStorageFlags.Exportable);
|
|
|
|
public static AppHelper GetInstance()
|
|
{
|
|
if (_instance == null)
|
|
{
|
|
_instance = new AppHelper();
|
|
}
|
|
if (tokenModel != null && tokenModel.token != null)
|
|
{
|
|
var principal = GetPrincipalFromExpiredToken(tokenModel.token);
|
|
var exp = long.Parse(principal.Claims.Where(c => c.Type == "exp").FirstOrDefault().Value);
|
|
var expDate = DateTimeOffset.FromUnixTimeSeconds(exp);
|
|
if (DateTime.Now >= expDate)
|
|
{
|
|
refrehToken();
|
|
}
|
|
}
|
|
|
|
return _instance;
|
|
}
|
|
|
|
private bool _isLogged;
|
|
private string errorLogin;
|
|
|
|
private static ClaimsPrincipal GetPrincipalFromExpiredToken(string token)
|
|
{
|
|
var tokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateAudience = false,
|
|
ValidateIssuer = false,
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("This is a sample secret key - please don't use in production environment.'")),
|
|
ValidateLifetime = false
|
|
};
|
|
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
var principal = tokenHandler.ValidateToken(token, tokenValidationParameters, out SecurityToken securityToken);
|
|
|
|
//if (securityToken is not JwtSecurityToken jwtSecurityToken || !jwtSecurityToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256, StringComparison.InvariantCultureIgnoreCase))
|
|
// throw new SecurityTokenException("Invalid token");
|
|
return principal;
|
|
}
|
|
|
|
public bool login(UserModel user)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
var data = new StringContent(JsonConvert.SerializeObject(new
|
|
{
|
|
username = user.Username,
|
|
password = user.Password
|
|
}));
|
|
data.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
|
|
try
|
|
{
|
|
var response = client.PostAsync(URL + "/api/Authenticate/Login", data).Result;
|
|
var resp = response.Content.ReadAsStringAsync().Result;
|
|
tokenModel = JsonConvert.DeserializeObject<Model.TokenModel>(resp);
|
|
if (tokenModel.token == null)
|
|
{
|
|
_isLogged = false;
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
_isLogged = true;
|
|
return true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_isLogged = false;
|
|
errorLogin = ex.Message;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void refrehToken()
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
var data = new StringContent(JsonConvert.SerializeObject(new
|
|
{
|
|
token = tokenModel.token,
|
|
refreshToken = tokenModel.token
|
|
}));
|
|
data.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
|
|
try
|
|
{
|
|
var response = client.PostAsync(URL + "/api/Authenticate/RefreshToken", data).Result;
|
|
var resp = response.Content.ReadAsStringAsync().Result;
|
|
tokenModel = JsonConvert.DeserializeObject<Model.TokenModel>(resp);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
|
|
public void putTree(int parentId, string text)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenModel.token);
|
|
|
|
var data = new StringContent(JsonConvert.SerializeObject(new
|
|
{
|
|
}));
|
|
data.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
var response = client.PostAsync(URL + "/api/Archiwum/PutTree?ParentId=" + parentId + "&Text=" + text, data).Result;
|
|
var resp = response.Content.ReadAsStringAsync().Result;
|
|
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) throw new UnauthorizedAccessException();
|
|
}
|
|
}
|
|
|
|
public void renameTree(int Id, string text)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenModel.token);
|
|
|
|
var data = new StringContent(JsonConvert.SerializeObject(new
|
|
{
|
|
}));
|
|
data.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
var response = client.PostAsync(URL + "/api/Archiwum/RenameTree?Id=" + Id + "&Text=" + text, data).Result;
|
|
var resp = response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
}
|
|
|
|
public void renameDocument(int Id, string text)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenModel.token);
|
|
|
|
var data = new StringContent(JsonConvert.SerializeObject(new
|
|
{
|
|
}));
|
|
data.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
var response = client.PostAsync(URL + "/api/Archiwum/RenameDocument?Id=" + Id + "&Name=" + text, data).Result;
|
|
var resp = response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
}
|
|
|
|
public string getTree()
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client2 = new HttpClient(handler))
|
|
{
|
|
client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenModel.token);
|
|
|
|
var response2 = client2.GetAsync(URL + "/api/Archiwum/GetTree").Result;
|
|
var resp2 = response2.Content.ReadAsStringAsync().Result;
|
|
return resp2;
|
|
}
|
|
}
|
|
|
|
public string getDocumentsByTreeId(int treeId)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client2 = new HttpClient(handler))
|
|
{
|
|
client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenModel.token);
|
|
|
|
var response2 = client2.GetAsync(URL + "/api/Archiwum/GetDocumentsByTreeId?TreeId=" + treeId.ToString()).Result;
|
|
var resp2 = response2.Content.ReadAsStringAsync().Result;
|
|
return resp2;
|
|
}
|
|
}
|
|
|
|
public bool isLogged()
|
|
{
|
|
return _isLogged;
|
|
}
|
|
public string getErrorLogin()
|
|
{
|
|
return errorLogin;
|
|
}
|
|
|
|
public DocumentModel putDocument(DocumentModel doc)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
//ICryptoNet cryptoNetWithPublicKey = new CryptoNetRsa(certificate, KeyType.PublicKey);
|
|
//var r = cryptoNetWithPublicKey.EncryptFromBytes(doc.Image);
|
|
//doc.Image = r;
|
|
var data = new StringContent(JsonConvert.SerializeObject(doc));
|
|
data.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenModel.token);
|
|
|
|
var response = client.PostAsync(URL + "/api/Archiwum/PutDocument", data).Result;
|
|
var resp = response.Content.ReadAsStringAsync().Result;
|
|
return JsonConvert.DeserializeObject<DocumentModel>(resp);
|
|
}
|
|
}
|
|
|
|
public DocumentModel getDocumentById(int id)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client2 = new HttpClient(handler))
|
|
{
|
|
client2.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenModel.token);
|
|
|
|
var response2 = client2.GetAsync(URL + "/api/Archiwum/GetDocumentById?Id=" + id.ToString()).Result;
|
|
var resp2 = response2.Content.ReadAsStringAsync().Result;
|
|
//ICryptoNet cryptoNetWithPrivateKey = new CryptoNetRsa(certificate, KeyType.PrivateKey);
|
|
var cert = DotNetUtilities.FromX509Certificate(certificate);
|
|
//var res = cryptoNetWithPrivateKey.DecryptToBytes(response2.Content.ReadAsByteArrayAsync().Result);
|
|
return JsonConvert.DeserializeObject<DocumentModel>(resp2);
|
|
}
|
|
|
|
}
|
|
|
|
public void deleteDocument(int Id)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenModel.token);
|
|
|
|
var data = new StringContent(JsonConvert.SerializeObject(new
|
|
{
|
|
}));
|
|
data.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
var response = client.PostAsync(URL + "/api/Archiwum/DeleteDocument?Id=" + Id, data).Result;
|
|
var resp = response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
}
|
|
|
|
public void deleteTree(int Id)
|
|
{
|
|
var handler = new HttpClientHandler();
|
|
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
|
|
handler.ServerCertificateCustomValidationCallback =
|
|
(httpRequestMessage, cert, cetChain, policyErrors) =>
|
|
{
|
|
return true;
|
|
};
|
|
|
|
using (var client = new HttpClient(handler))
|
|
{
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenModel.token);
|
|
|
|
var data = new StringContent(JsonConvert.SerializeObject(new
|
|
{
|
|
}));
|
|
data.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
var response = client.PostAsync(URL + "/api/Archiwum/DeleteTree?Id=" + Id, data).Result;
|
|
var resp = response.Content.ReadAsStringAsync().Result;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|