Skip to content

Commit

Permalink
Merge pull request #544 from ThisIsBuddyRIch/auth-v3
Browse files Browse the repository at this point in the history
change auth to v3
  • Loading branch information
ThisIsBuddyRIch authored Sep 17, 2019
2 parents e2bc70a + ed5f3aa commit a116994
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 40 deletions.
7 changes: 7 additions & 0 deletions proto/LoginPassword.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Diadoc.Api.Proto;

message LoginPassword {
required string Login = 1;
required string Password = 2;
}

1 change: 1 addition & 0 deletions src/DiadocApi-net35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Com\Users.cs" />
<Compile Include="DiadocHttpApi.DetectCustomPrintForms.cs" />
<Compile Include="Proto\CustomPrintFormDetectionResult.proto.cs" />
<Compile Include="Proto\LoginPassword.proto.cs" />
<Compile Include="XmlSerializerExtensions.cs" />
<Compile Include="DataXml\CommonXmlNodes.cs" />
<Compile Include="DataXml\Utd820\ON_NSCHFDOPPOK_UserContract_820_05_01_01.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/DiadocApi-net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="DiadocHttpApi.DetectCustomPrintForms.Async.cs" />
<Compile Include="DiadocHttpApi.DetectCustomPrintForms.cs" />
<Compile Include="Proto\CustomPrintFormDetectionResult.proto.cs" />
<Compile Include="Proto\LoginPassword.proto.cs" />
<Compile Include="XmlSerializerExtensions.cs" />
<Compile Include="DataXml\Utd820\ON_NSCHFDOPPOK_UserContract_820_05_01_01.cs" />
<Compile Include="DataXml\Utd820\ON_NSCHFDOPPR_UserContract_820_05_01_01.cs" />
Expand Down
112 changes: 72 additions & 40 deletions src/DiadocHttpApi.Auth.cs
Original file line number Diff line number Diff line change
@@ -1,52 +1,77 @@
using System;
using System.Text;
using Diadoc.Api.Http;
using Diadoc.Api.Proto;

namespace Diadoc.Api
{
public partial class DiadocHttpApi
{
public string Authenticate(string login, string password, string key = null, string id = null)
{
var qsb = new PathAndQueryBuilder("/V2/Authenticate");
qsb.AddParameter("login", login);
qsb.AddParameter("password", password);
var qsb = new PathAndQueryBuilder("/V3/Authenticate");
qsb.AddParameter("type", "password");

var request = BuildHttpRequest(
null,
"POST",
qsb.BuildPathAndQuery(),
Serialize(new LoginPassword
{
Login = login,
Password = password
}));

if (!string.IsNullOrEmpty(key))
{
qsb.AddParameter("key", key);
qsb.AddParameter("id", id);
request.AddHeader("X-Diadoc-ServiceKey", key);
request.AddHeader("X-Diadoc-ServiceUserId", id);
}

var httpResponse = PerformHttpRequest(null, "POST", qsb.BuildPathAndQuery());
return Encoding.UTF8.GetString(httpResponse);
return PerformRequest(request);
}

public string AuthenticateByKey(string key, string id)
{
var qsb = new PathAndQueryBuilder("/V2/Authenticate");
qsb.AddParameter("key", key);
qsb.AddParameter("id", id);
var httpResponse = PerformHttpRequest(null, "POST", qsb.BuildPathAndQuery());
return Encoding.UTF8.GetString(httpResponse);
var qsb = new PathAndQueryBuilder("/V3/Authenticate");
qsb.AddParameter("type", "trust");

var request = BuildHttpRequest(null,"POST", qsb.BuildPathAndQuery(),null);

request.AddHeader("X-Diadoc-ServiceKey", key);
request.AddHeader("X-Diadoc-ServiceUserId", id);

return PerformRequest(request);
}

private string PerformRequest(HttpRequest request)
{
var httpResponse = HttpClient.PerformHttpRequest(request);
return Encoding.UTF8.GetString(httpResponse.Content);
}

public string AuthenticateBySid(string sid)
{
var qsb = new PathAndQueryBuilder("/V2/Authenticate");
qsb.AddParameter("sid", sid);
var httpResponse = PerformHttpRequest(null, "POST", qsb.BuildPathAndQuery());
return Encoding.UTF8.GetString(httpResponse);
var qsb = new PathAndQueryBuilder("/V3/Authenticate");
qsb.AddParameter("type", "sid");
var request = BuildRequest(
null,
"POST",
qsb.BuildPathAndQuery(),
new HttpRequestBody(Encoding.UTF8.GetBytes(sid), "text/plain"));

return PerformRequest(request);
}

public string Authenticate(byte[] certificateBytes, bool useLocalSystemStorage = false)
{
var token = AuthenticateByCertificate(
certificateBytes,
useLocalSystemStorage,
key: null,
id: null);
null,
null);

return ConfirmAuthenticationByCertificate(certificateBytes, token, saveBinding: false);
return ConfirmAuthenticationByCertificate(certificateBytes, token, false);
}

public string Authenticate(string thumbprint, bool useLocalSystemStorage = false)
Expand All @@ -56,31 +81,33 @@ public string Authenticate(string thumbprint, bool useLocalSystemStorage = false
var token = AuthenticateByCertificate(
userCert.RawData,
useLocalSystemStorage,
key: null,
id: null);
null,
null);

return ConfirmAuthenticationByCertificateThumbprint(userCert.Thumbprint, token, saveBinding: false);
return ConfirmAuthenticationByCertificateThumbprint(userCert.Thumbprint, token, false);
}

public string AuthenticateWithKey(byte[] certificateBytes, bool useLocalSystemStorage = false, string key = null, string id = null, bool autoConfirm = true)
public string AuthenticateWithKey(byte[] certificateBytes, bool useLocalSystemStorage = false,
string key = null, string id = null, bool autoConfirm = true)
{
var authenticationWithKey = !string.IsNullOrEmpty(key);
var token = AuthenticateByCertificate(certificateBytes, useLocalSystemStorage, key, id);

return autoConfirm
? ConfirmAuthenticationByCertificate(certificateBytes, token, saveBinding: authenticationWithKey)
? ConfirmAuthenticationByCertificate(certificateBytes, token, authenticationWithKey)
: token;
}

public string AuthenticateWithKey(string thumbprint, bool useLocalSystemStorage = false, string key = null, string id = null, bool autoConfirm = true)
public string AuthenticateWithKey(string thumbprint, bool useLocalSystemStorage = false, string key = null,
string id = null, bool autoConfirm = true)
{
var authenticationWithKey = !string.IsNullOrEmpty(key);
var userCert = crypt.GetCertificateWithPrivateKey(thumbprint, useLocalSystemStorage);

var token = AuthenticateByCertificate(userCert.RawData, useLocalSystemStorage, key, id);

return autoConfirm
? ConfirmAuthenticationByCertificateThumbprint(userCert.Thumbprint, token, saveBinding: authenticationWithKey)
? ConfirmAuthenticationByCertificateThumbprint(userCert.Thumbprint, token, authenticationWithKey)
: token;
}

Expand All @@ -94,28 +121,33 @@ public string AuthenticateWithKeyConfirm(string thumbprint, string token, bool s
return ConfirmAuthenticationByCertificateThumbprint(thumbprint, token, saveBinding);
}

private string AuthenticateByCertificate(byte[] certificateBytes, bool useLocalSystemStorage, string key, string id)
private string AuthenticateByCertificate(byte[] certificateBytes, bool useLocalSystemStorage, string key,
string id)
{
var qsb = new PathAndQueryBuilder("/V2/Authenticate");
var authenticationWithKey = !string.IsNullOrEmpty(key);
if (authenticationWithKey)
var qsb = new PathAndQueryBuilder("/V3/Authenticate");
qsb.AddParameter("type", "certificate");

var request = BuildRequest(
null,
"POST",
qsb.BuildPathAndQuery(),
new HttpRequestBody(certificateBytes, "application/octet-stream"));

if (!string.IsNullOrEmpty(key))
{
qsb.AddParameter("key", key);
qsb.AddParameter("id", id);
request.AddHeader("X-Diadoc-ServiceKey", key);
request.AddHeader("X-Diadoc-ServiceUserId", id);
}

return PerformHttpRequest(null,
"POST",
qsb.BuildPathAndQuery(),
certificateBytes,
responseContent => Convert.ToBase64String(crypt.Decrypt(responseContent, useLocalSystemStorage)));
var httpResponse = HttpClient.PerformHttpRequest(request);
return Convert.ToBase64String(crypt.Decrypt(httpResponse.Content, useLocalSystemStorage));
}

private string ConfirmAuthenticationByCertificate(byte[] certificateBytes, string token, bool saveBinding)
{
var qsb = new PathAndQueryBuilder("/V2/AuthenticateConfirm");
var qsb = new PathAndQueryBuilder("/V3/AuthenticateConfirm");
qsb.AddParameter("token", token);
qsb.AddParameter("saveBinding", saveBinding.ToString());

return PerformHttpRequest(
null,
"POST",
Expand All @@ -126,7 +158,7 @@ private string ConfirmAuthenticationByCertificate(byte[] certificateBytes, strin

private string ConfirmAuthenticationByCertificateThumbprint(string thumbprint, string token, bool saveBinding)
{
var qsb = new PathAndQueryBuilder("/V2/AuthenticateConfirm");
var qsb = new PathAndQueryBuilder("/V3/AuthenticateConfirm");
qsb.AddParameter("thumbprint", thumbprint);
qsb.AddParameter("token", token);
qsb.AddParameter("saveBinding", saveBinding.ToString());
Expand Down
37 changes: 37 additions & 0 deletions src/Proto/LoginPassword.proto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// Generated from: LoginPassword.proto
namespace Diadoc.Api.Proto
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"LoginPassword")]
public partial class LoginPassword : global::ProtoBuf.IExtensible
{
public LoginPassword() {}

private string _Login;
[global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"Login", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string Login
{
get { return _Login; }
set { _Login = value; }
}
private string _Password;
[global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"Password", DataFormat = global::ProtoBuf.DataFormat.Default)]
public string Password
{
get { return _Password; }
set { _Password = value; }
}
private global::ProtoBuf.IExtension extensionObject;
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
}

}

0 comments on commit a116994

Please sign in to comment.