340 lines
12 KiB
C#
340 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Web;
|
|
using System.Windows;
|
|
using ChatController.LoginKlassen;
|
|
using ChatController.Utilities;
|
|
|
|
using Newtonsoft.Json;
|
|
using RestSharp;
|
|
|
|
namespace ChatController.HauptKlassen
|
|
{
|
|
public class Login
|
|
{
|
|
private string _Url = string.Empty;
|
|
|
|
private const string _NewUrl = "https://dict.ownchat.de/api/server/resolve/1/";
|
|
|
|
public string ServerUrl { get; set; } = string.Empty;
|
|
private string _AuthToken = string.Empty;
|
|
|
|
private readonly string _UserName;
|
|
private readonly string _Password;
|
|
private readonly string _Tenant;
|
|
private readonly string _ChatCode;
|
|
private readonly string _ApiKey;
|
|
|
|
private long? _UserId;
|
|
|
|
private bool _ownchatVerbindungsaufbauOK;
|
|
private bool _gruppeholenverbindungsaufbauOK;
|
|
|
|
private AuthenticationData _serverconect;
|
|
private UserDaten _LoggedInUser;
|
|
private ChatGruppenDaten _AllGroups;
|
|
|
|
private readonly bool _ShouldShowMessageBox = true;
|
|
|
|
public Login(string pTenant, string pChatCode, string pUserName, string pPassword)
|
|
{
|
|
_Tenant = pTenant;
|
|
_ChatCode = pChatCode;
|
|
_UserName = pUserName;
|
|
_Password = pPassword;
|
|
_ApiKey = "0000";//default, damit nichts kaputt geht
|
|
}
|
|
|
|
public Login(string pTenant, string pChatCode, string pUserName, string pPassword, string pApiKey)
|
|
{
|
|
_Tenant = pTenant;
|
|
_ChatCode = pChatCode;
|
|
_UserName = pUserName;
|
|
_Password = pPassword;
|
|
_ApiKey = pApiKey;
|
|
}
|
|
|
|
//
|
|
public Login(string pTenant)
|
|
{
|
|
_Tenant = pTenant;
|
|
_ShouldShowMessageBox = false;
|
|
ServerErmittlung();
|
|
}
|
|
|
|
public ChatDatenUebergabe AnmeldevorgangDurchFuehren()
|
|
{
|
|
if(ServerErmittlung()){
|
|
var dic = new Dictionary<string, string>
|
|
{
|
|
{"username", _UserName},
|
|
{ "password", _Password},
|
|
{ "chatcode", _ChatCode}
|
|
};
|
|
|
|
VerbindeMitChatServer(dic);
|
|
|
|
if (_ownchatVerbindungsaufbauOK)
|
|
{
|
|
GetGroups();
|
|
}
|
|
|
|
if (_gruppeholenverbindungsaufbauOK)
|
|
{
|
|
var maxUploadSize = GetMaiximumAllowedFileUploadSize(_AuthToken,_Tenant);
|
|
|
|
return new ChatDatenUebergabe(_serverconect, _LoggedInUser, _AllGroups, ServerUrl, _AuthToken, _UserId, _Tenant, _ApiKey, maxUploadSize);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public bool ServerErmittlung()
|
|
{
|
|
if (string.IsNullOrEmpty(ServerUrl))
|
|
{
|
|
try
|
|
{
|
|
var serverURl = _NewUrl + _Tenant;
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
var request = WebRequest.Create(serverURl);
|
|
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
|
|
WebResponse response;
|
|
|
|
try
|
|
{
|
|
response = request.GetResponse();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: Es konnte keine Verbindung aufgebaut werden.", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
var responseFromServer = Utils.ReadStream(response);
|
|
|
|
try
|
|
{
|
|
var jsonDatentyp = JsonConvert.DeserializeObject<AuthenticationDataType1>(responseFromServer);
|
|
|
|
switch(jsonDatentyp.Status)
|
|
{
|
|
case 1:
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: Kundennummer unbekannt.\nBitte überprüfen Sie die Anmeldeinformationen.", "ownChat Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
}
|
|
|
|
return false;
|
|
case 2:
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Der Diensttyp ist für die angegebene Kundennummer nicht definiert.", "ownChat Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
_Url = jsonDatentyp.Url;
|
|
}
|
|
catch (Exception eds)
|
|
{
|
|
try
|
|
{
|
|
var jsonDatentyp = JsonConvert.DeserializeObject<AuthenticationDataType2>(responseFromServer);
|
|
|
|
switch(jsonDatentyp.Status)
|
|
{
|
|
case 1:
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: Kundennummer unbekannt.\nBitte überprüfen Sie die Anmeldeinformationen.", "ownChat Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
}
|
|
|
|
return false;
|
|
case 2:
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Der Diensttyp ist für die angegebene Kundennummer nicht definiert.", "ownChat Info", MessageBoxButton.OK, MessageBoxImage.Asterisk);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
_Url = jsonDatentyp.Url;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + e.Message, "ownChat Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
response.Close();
|
|
|
|
ServerUrl = _Url;
|
|
return true;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + e.Message, "ownChat Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private void VerbindeMitChatServer(Dictionary<string, string> postparameter)
|
|
{
|
|
try
|
|
{
|
|
var endurl = _Url + "/api/user/loginwithchatcode";
|
|
|
|
var requestBody = postparameter.Keys.Aggregate(string.Empty, (current, key) => current + HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(postparameter[key]) + "&");
|
|
|
|
var client = new RestClient(endurl);
|
|
var request = new RestRequest(Method.POST);
|
|
|
|
request.AddHeader("content-type", "application/x-www-form-urlencoded");
|
|
request.AddParameter(Constants.CustomerId, _Tenant, ParameterType.HttpHeader);
|
|
request.AddParameter("application/x-www-form-urlencoded", requestBody, ParameterType.RequestBody);
|
|
|
|
var response = client.Post(request);
|
|
|
|
if (response.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
var userDaten = JsonConvert.DeserializeObject<UserDaten>(response.Content);
|
|
|
|
if(userDaten.Success)
|
|
{
|
|
_AuthToken = userDaten.Response.User.Token;
|
|
_UserId = userDaten.Response.User.Oid;
|
|
|
|
_LoggedInUser = userDaten;
|
|
|
|
_ownchatVerbindungsaufbauOK = true;
|
|
}
|
|
else
|
|
{
|
|
if(userDaten.Errors.chatcode_failed != null)
|
|
{
|
|
if(_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + userDaten.Errors.chatcode_failed[0] + "\nBitte überprüfen Sie die Anmeldeinformationen.", "Info", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
else if(userDaten.Errors.login_failed != null)
|
|
{
|
|
if(_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + userDaten.Errors.login_failed[0] + "\nBitte überprüfen Sie die Anmeldeinformationen.", "Info", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
|
|
_ownchatVerbindungsaufbauOK = false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(response.Content, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler:" + e.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void GetGroups()
|
|
{
|
|
try
|
|
{
|
|
var endurl = _Url + Constants.GetChatGroupsUrl;
|
|
|
|
var request = WebRequest.Create(endurl);
|
|
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
request.Headers[Constants.Token] = _AuthToken;
|
|
request.Headers[Constants.CustomerId] = _Tenant;
|
|
|
|
using (var response = request.GetResponse())
|
|
{
|
|
var serverResponse = Utils.ReadStream(response);
|
|
|
|
if (!string.IsNullOrEmpty(serverResponse))
|
|
{
|
|
var jsonDaten = JsonConvert.DeserializeObject<ChatGruppenDaten>(serverResponse);
|
|
|
|
_AllGroups = jsonDaten;
|
|
|
|
_gruppeholenverbindungsaufbauOK = true;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (_ShouldShowMessageBox)
|
|
{
|
|
MessageBox.Show("Fehler: " + e.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
#region Maximal erlaubte File Größe
|
|
|
|
public long GetMaiximumAllowedFileUploadSize(string pToken, string pCustomerId)
|
|
{
|
|
long maximumFileSize;
|
|
|
|
try
|
|
{
|
|
var endurl = ServerUrl + Constants.MaxUploadFileSizeUrl;
|
|
|
|
var request = WebRequest.Create(endurl);
|
|
request.Headers[Constants.Token] = pToken;
|
|
request.Headers[Constants.CustomerId] = pCustomerId;
|
|
|
|
request.Credentials = CredentialCache.DefaultCredentials;
|
|
request.Proxy = null;
|
|
|
|
using (var response = request.GetResponse())
|
|
{
|
|
var responseFromServer = Utils.ReadStream(response);
|
|
|
|
var definiti = new {file_upload_max_size = ""};
|
|
|
|
var jsonDatentyp = JsonConvert.DeserializeAnonymousType(responseFromServer, definiti);
|
|
maximumFileSize = Convert.ToInt64(jsonDatentyp.file_upload_max_size);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
return maximumFileSize;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|