Implement custom auth provider feature in plugin system

This commit is contained in:
ChronosX88 2020-09-13 19:26:24 +04:00
parent 803d898f45
commit 11379cd6c2
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
6 changed files with 41 additions and 9 deletions

View File

@ -1,8 +1,10 @@
using System.Collections.Generic;
using System;
using JWT.Algorithms;
using JWT.Builder;
using Newtonsoft.Json;
using Zirconium.Utils;
using Zirconium.Core.Plugins.Interfaces;
namespace Zirconium.Core
{
@ -11,10 +13,14 @@ namespace Zirconium.Core
private App _app;
private string _secretString;
private const long DEFAULT_TOKEN_EXPIRATION_TIME_HOURS = 24 * 3600000;
private IList<IAuthProvider> _authProviders;
private IAuthProvider _defaultAuthProvider;
public AuthManager(App app)
{
_app = app;
_authProviders = new List<IAuthProvider>();
_defaultAuthProvider = null;
_secretString = Guid.NewGuid().ToString();
}
@ -42,7 +48,18 @@ namespace Zirconium.Core
.WithSecret(_secretString)
.MustVerifySignature()
.Decode(token);
return JsonConvert.DeserializeObject<JWTPayload>(jsonPayload);
var payload = JsonConvert.DeserializeObject<JWTPayload>(jsonPayload);
if (_defaultAuthProvider == null) {
throw new Exception("Default auth provider isn't specified");
}
var validToken = _defaultAuthProvider.TestToken(token, payload);
if (!validToken)
return null;
return payload;
}
public void AddAuthProvider(IAuthProvider provider) {
_authProviders.Add(provider);
}
}
}

View File

@ -21,6 +21,8 @@ namespace Zirconium.Core
// Configurations of plugins
public Dictionary<string, dynamic> Plugins { get; set; }
public string AuthenticationProvider { get; set; }
}
public class Websocket

View File

@ -0,0 +1,8 @@
namespace Zirconium.Core.Plugins.Interfaces
{
public interface IAuthProvider
{
bool TestToken(string token, JWTPayload payload);
string GetAuthProviderName();
}
}

View File

@ -15,5 +15,6 @@ namespace Zirconium.Core.Plugins.Interfaces
void SendMessage(ConnectionInfo connInfo, BaseMessage message);
dynamic GetSettings(IPluginAPI plugin);
dynamic GetSettings(string pluginName);
void ProvideAuth(IAuthProvider provider);
}
}

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System;
using Newtonsoft.Json;
using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces;
@ -17,6 +16,10 @@ namespace Zirconium.Core.Plugins
_app = app;
}
public void ProvideAuth(IAuthProvider provider) {
_app.AuthManager.AddAuthProvider(provider);
}
public void FireEvent(CoreEvent coreEvent)
{
_router.RouteCoreEvent(coreEvent);

View File

@ -62,13 +62,14 @@ namespace Zirconium.Core.Plugins
loader = PluginLoader.CreateFromAssemblyFile(
pluginDll,
sharedTypes: new[] {
typeof(IPluginAPI),
typeof(IPluginHostAPI),
typeof(IPluginManager),
typeof(IC2SMessageHandler),
typeof(ICoreEventHandler),
typeof(BaseMessage),
typeof(CoreEvent)
typeof(IPluginAPI),
typeof(IPluginHostAPI),
typeof(IPluginManager),
typeof(IAuthProvider),
typeof(IC2SMessageHandler),
typeof(ICoreEventHandler),
typeof(BaseMessage),
typeof(CoreEvent)
}
);
}