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 System;
using JWT.Algorithms; using JWT.Algorithms;
using JWT.Builder; using JWT.Builder;
using Newtonsoft.Json; using Newtonsoft.Json;
using Zirconium.Utils; using Zirconium.Utils;
using Zirconium.Core.Plugins.Interfaces;
namespace Zirconium.Core namespace Zirconium.Core
{ {
@ -11,10 +13,14 @@ namespace Zirconium.Core
private App _app; private App _app;
private string _secretString; private string _secretString;
private const long DEFAULT_TOKEN_EXPIRATION_TIME_HOURS = 24 * 3600000; private const long DEFAULT_TOKEN_EXPIRATION_TIME_HOURS = 24 * 3600000;
private IList<IAuthProvider> _authProviders;
private IAuthProvider _defaultAuthProvider;
public AuthManager(App app) public AuthManager(App app)
{ {
_app = app; _app = app;
_authProviders = new List<IAuthProvider>();
_defaultAuthProvider = null;
_secretString = Guid.NewGuid().ToString(); _secretString = Guid.NewGuid().ToString();
} }
@ -42,7 +48,18 @@ namespace Zirconium.Core
.WithSecret(_secretString) .WithSecret(_secretString)
.MustVerifySignature() .MustVerifySignature()
.Decode(token); .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 // Configurations of plugins
public Dictionary<string, dynamic> Plugins { get; set; } public Dictionary<string, dynamic> Plugins { get; set; }
public string AuthenticationProvider { get; set; }
} }
public class Websocket 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); void SendMessage(ConnectionInfo connInfo, BaseMessage message);
dynamic GetSettings(IPluginAPI plugin); dynamic GetSettings(IPluginAPI plugin);
dynamic GetSettings(string pluginName); dynamic GetSettings(string pluginName);
void ProvideAuth(IAuthProvider provider);
} }
} }

View File

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

View File

@ -65,6 +65,7 @@ namespace Zirconium.Core.Plugins
typeof(IPluginAPI), typeof(IPluginAPI),
typeof(IPluginHostAPI), typeof(IPluginHostAPI),
typeof(IPluginManager), typeof(IPluginManager),
typeof(IAuthProvider),
typeof(IC2SMessageHandler), typeof(IC2SMessageHandler),
typeof(ICoreEventHandler), typeof(ICoreEventHandler),
typeof(BaseMessage), typeof(BaseMessage),