Implement setting default auth provider by config

This commit is contained in:
ChronosX88 2020-10-08 21:07:39 +04:00
parent 370a7e8dc2
commit 9e0088cbba
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
4 changed files with 27 additions and 6 deletions

View File

@ -29,6 +29,7 @@ namespace Zirconium.Core
Database = new DatabaseConnector(this); Database = new DatabaseConnector(this);
PluginManager = new PluginManager(PluginHostAPI); PluginManager = new PluginManager(PluginHostAPI);
PluginManager.LoadPlugins(config.PluginsDirPath, config.EnabledPlugins); PluginManager.LoadPlugins(config.PluginsDirPath, config.EnabledPlugins);
AuthManager.SetDefaultAuthProvider();
Log.Info("Zirconium is initialized successfully"); Log.Info("Zirconium is initialized successfully");
} }

View File

@ -5,6 +5,7 @@ using JWT.Builder;
using Newtonsoft.Json; using Newtonsoft.Json;
using Zirconium.Utils; using Zirconium.Utils;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
using System.Linq;
namespace Zirconium.Core namespace Zirconium.Core
{ {
@ -14,13 +15,13 @@ namespace Zirconium.Core
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 IList<IAuthProvider> _authProviders;
private IAuthProvider _defaultAuthProvider; public IAuthProvider DefaultAuthProvider { get; private set; }
public AuthManager(App app) public AuthManager(App app)
{ {
_app = app; _app = app;
_authProviders = new List<IAuthProvider>(); _authProviders = new List<IAuthProvider>();
_defaultAuthProvider = null; DefaultAuthProvider = null;
_secretString = Guid.NewGuid().ToString(); _secretString = Guid.NewGuid().ToString();
} }
@ -37,7 +38,8 @@ namespace Zirconium.Core
.Encode(); .Encode();
} }
public string CreateToken(string entityID, string deviceID) { public string CreateToken(string entityID, string deviceID)
{
return CreateToken(entityID, deviceID, DEFAULT_TOKEN_EXPIRATION_TIME_HOURS); return CreateToken(entityID, deviceID, DEFAULT_TOKEN_EXPIRATION_TIME_HOURS);
} }
@ -49,17 +51,23 @@ namespace Zirconium.Core
.MustVerifySignature() .MustVerifySignature()
.Decode(token); .Decode(token);
var payload = JsonConvert.DeserializeObject<JWTPayload>(jsonPayload); var payload = JsonConvert.DeserializeObject<JWTPayload>(jsonPayload);
if (_defaultAuthProvider == null) { if (DefaultAuthProvider == null)
{
throw new Exception("Default auth provider isn't specified"); throw new Exception("Default auth provider isn't specified");
} }
var validToken = _defaultAuthProvider.TestToken(token, payload); var validToken = DefaultAuthProvider.TestToken(token, payload);
if (!validToken) if (!validToken)
return null; return null;
return payload; return payload;
} }
public void AddAuthProvider(IAuthProvider provider) { public void AddAuthProvider(IAuthProvider provider)
{
_authProviders.Add(provider); _authProviders.Add(provider);
} }
public void SetDefaultAuthProvider() {
DefaultAuthProvider = _authProviders.Where(x => x.GetAuthProviderName() == _app.Config.AuthenticationProvider).FirstOrDefault();
}
} }
} }

View File

@ -12,12 +12,14 @@ namespace Zirconium.Core.Plugins.Interfaces
void UnhookCoreEvent(ICoreEventHandler handler); void UnhookCoreEvent(ICoreEventHandler handler);
void FireEvent(CoreEvent coreEvent); void FireEvent(CoreEvent coreEvent);
string GenerateAuthToken(string entityID, string deviceID, int tokenExpirationMillis); string GenerateAuthToken(string entityID, string deviceID, int tokenExpirationMillis);
string GenerateAuthToken(string entityID, string deviceID);
string[] GetServerDomains(); string[] GetServerDomains();
string GetServerID(); string GetServerID();
void SendMessage(Session session, BaseMessage message); void SendMessage(Session session, BaseMessage message);
dynamic GetSettings(IPluginAPI plugin); dynamic GetSettings(IPluginAPI plugin);
dynamic GetSettings(string pluginName); dynamic GetSettings(string pluginName);
void ProvideAuth(IAuthProvider provider); void ProvideAuth(IAuthProvider provider);
IAuthProvider GetAuthProvider();
IExposedSessionManager GetSessionManager(); IExposedSessionManager GetSessionManager();
void RegisterIPCService(IPluginAPI plugin, dynamic service); void RegisterIPCService(IPluginAPI plugin, dynamic service);
Task<dynamic> MakeIPCRequest(string pluginName, string methodName, dynamic paramsObject); Task<dynamic> MakeIPCRequest(string pluginName, string methodName, dynamic paramsObject);

View File

@ -102,5 +102,15 @@ namespace Zirconium.Core.Plugins
{ {
return _app.Database.MongoDatabase; return _app.Database.MongoDatabase;
} }
public IAuthProvider GetAuthProvider()
{
return _app.AuthManager.DefaultAuthProvider;
}
public string GenerateAuthToken(string entityID, string deviceID)
{
return _app.AuthManager.CreateToken(entityID, deviceID);
}
} }
} }