92 lines
2.8 KiB
C#
Raw Normal View History

2021-09-11 13:53:40 +02:00
using FabAccessAPI.Schema;
using S22.Sasl;
using System.Collections.Generic;
using System.Threading.Tasks;
2022-03-12 22:52:28 +01:00
using FabAccessAPI.Exceptions;
using System.Linq;
2021-09-11 13:53:40 +02:00
namespace FabAccessAPI
{
/// <summary>
2022-03-12 22:52:28 +01:00
/// Authenticate with SASL
2021-09-11 13:53:40 +02:00
/// </summary>
public class Auth
{
2022-03-12 22:52:28 +01:00
#region Private Fields
private readonly IAuthentication _AuthCap;
2021-09-11 13:53:40 +02:00
#endregion
2022-03-12 22:52:28 +01:00
#region Constructors
public Auth(IAuthentication authCap)
2021-09-11 13:53:40 +02:00
{
2022-01-03 21:13:03 +00:00
_AuthCap = authCap;
2021-09-11 13:53:40 +02:00
}
2022-03-12 22:52:28 +01:00
#endregion
2021-09-11 13:53:40 +02:00
2022-03-12 22:52:28 +01:00
#region Methods
2022-05-11 15:02:17 +02:00
/// <summary>
/// Authenticate Connection to get Session
/// </summary>
/// <exception cref="BadMechanismException"></exception>
/// <exception cref="InvalidCredentialsException"></exception>
/// <exception cref="AuthenticationFailedException"></exception>
2022-03-12 22:52:28 +01:00
public async Task<Session> Authenticate(string mech, Dictionary<string, object> properties)
2021-09-11 13:53:40 +02:00
{
2022-03-12 22:52:28 +01:00
SaslMechanism? saslMechanism = SaslFactory.Create(mech);
2021-09-11 13:53:40 +02:00
foreach (KeyValuePair<string, object> entry in properties)
{
2022-03-12 22:52:28 +01:00
saslMechanism.Properties.Add(entry.Key, entry.Value);
2021-09-11 13:53:40 +02:00
}
2022-03-12 22:52:28 +01:00
byte[] data = new byte[0];
2021-09-11 13:53:40 +02:00
2022-03-12 22:52:28 +01:00
if (saslMechanism.HasInitial)
2021-09-11 13:53:40 +02:00
{
2022-03-12 22:52:28 +01:00
data = saslMechanism.GetResponse(new byte[0]);
}
2021-09-11 13:53:40 +02:00
2022-03-12 22:52:28 +01:00
Response? response = await _AuthCap.Step(data);
while (!saslMechanism.IsCompleted)
2021-09-11 13:53:40 +02:00
{
2022-03-12 22:52:28 +01:00
if(response.Failed != null)
2021-09-11 13:53:40 +02:00
{
2022-05-11 15:02:17 +02:00
break;
2021-09-11 13:53:40 +02:00
}
2022-03-12 22:52:28 +01:00
if(response.Challenge != null)
2021-09-11 13:53:40 +02:00
{
2022-03-12 22:52:28 +01:00
byte[]? additional = saslMechanism.GetResponse(response.Challenge.ToArray());
response = await _AuthCap.Step(additional);
2021-09-11 13:53:40 +02:00
}
else
{
2022-03-12 22:52:28 +01:00
throw new AuthenticationFailedException();
2021-09-11 13:53:40 +02:00
}
}
2022-03-16 00:37:08 +01:00
if (response.Successful != null)
{
return response.Successful.Session;
}
2022-05-11 15:02:17 +02:00
else if (response.Failed != null)
{
switch (response.Failed.Code)
{
case Response.Error.badMechanism:
throw new BadMechanismException();
case Response.Error.invalidCredentials:
throw new InvalidCredentialsException();
case Response.Error.aborted:
case Response.Error.failed:
default:
throw new AuthenticationFailedException(response.Failed.AdditionalData.ToArray());
}
}
2022-03-16 00:37:08 +01:00
else
{
throw new AuthenticationFailedException();
}
2021-09-11 13:53:40 +02:00
}
2022-03-12 22:52:28 +01:00
#endregion
2021-09-11 13:53:40 +02:00
}
}