110 lines
3.9 KiB
C#
110 lines
3.9 KiB
C#
#nullable enable
|
|
namespace bts.api.core {
|
|
using bts.api.user;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Reflection;
|
|
|
|
public sealed class ServiceContext : Spark.Api.IServiceProvider, IDisposable {
|
|
IntPtr _qbus;
|
|
AuthToken? _token;
|
|
|
|
public ServiceContext(string? config_source = null) {
|
|
BTS.init(config_source);
|
|
_qbus = ApiNativeMethods.api_create_quickbus(null, 0);
|
|
}
|
|
|
|
internal ServiceContext(IntPtr qbus) =>
|
|
_qbus = qbus;
|
|
|
|
public AuthTokenImmutable? token => _token?.immutable();
|
|
public UserTuple? user { get; private set; }
|
|
public bool is_authenticated => _token is not null;
|
|
|
|
internal IntPtr native_context => _qbus;
|
|
|
|
public static ServiceContext create(string? config_server = null) {
|
|
BTS.init(config_server);
|
|
var qbus = ApiNativeMethods.api_create_quickbus(null, 0);
|
|
return new ServiceContext(qbus);
|
|
}
|
|
|
|
public T locate<T>(string qualifier = "") where T : class, IApiClient {
|
|
var ctr = typeof(T).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
|
|
.FirstOrDefault(ci => ci.GetParameters()
|
|
.SingleOrDefault()?.ParameterType == typeof(ClientChannel));
|
|
if (ctr is null) {
|
|
throw new InvalidOperationException(
|
|
$"Unable to find appropriate constructor for type '{typeof(T)}'");
|
|
}
|
|
ClientChannel channel = new QuickbusClientChannel(this, typeof(T).Name, qualifier);
|
|
return (T)ctr.Invoke(new object[] { channel });
|
|
}
|
|
|
|
public T GetService<T>(string? qualifier) where T : class, IApiClient =>
|
|
locate<T>(qualifier ?? string.Empty);
|
|
|
|
public async Task<bool> try_login(string username, string password) {
|
|
var res = await login(username, password);
|
|
return res.success;
|
|
}
|
|
|
|
public async Task<LoginResult> login(string username, string password) {
|
|
var res = await impl();
|
|
if (!res.success) {
|
|
Log.Api.error($"Login failed: for username: {username} password: {password} reason: {res.reason}");
|
|
}
|
|
return res;
|
|
|
|
async Task<LoginResult> impl() {
|
|
var auth_service = locate<AuthService>();
|
|
var result = await auth_service.authenticate_login_async(
|
|
new AuthenticateLoginRequest() {
|
|
Login = username ?? string.Empty,
|
|
Password = password ?? string.Empty
|
|
});
|
|
if (result.success) {
|
|
if (result.value.IsValid) {
|
|
_token = result.value.Token;
|
|
user = new(result.value.UserId, username);
|
|
return new LoginResult(result.value.Reason, true);
|
|
} else {
|
|
return new LoginResult(result.value.Reason);
|
|
}
|
|
} else {
|
|
return new LoginResult(result.error_message);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void login(AuthTokenImmutable token) {
|
|
user = UserTuple.empty;
|
|
_token = token.CloneToMutable();
|
|
}
|
|
|
|
public void Dispose() {
|
|
if (_qbus != IntPtr.Zero) {
|
|
ApiNativeMethods.api_destroy_quickbus(_qbus);
|
|
_qbus = IntPtr.Zero;
|
|
}
|
|
}
|
|
|
|
internal ServerChannel publish<T>(T service, string service_type, string qualifier) where T : IApiServer {
|
|
var ch = new ServerChannel(this, service_type, qualifier ?? "");
|
|
ch.on_request += service.dispatch;
|
|
return ch;
|
|
}
|
|
}
|
|
|
|
public class LoginResult {
|
|
public LoginResult() { success = true; }
|
|
public LoginResult(string reason) { success = false; this.reason = reason; }
|
|
public LoginResult(string reason, bool success) { this.success = success; this.reason = reason; }
|
|
public bool success { get; }
|
|
public string? reason { get; }
|
|
}
|
|
}
|