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);
PluginManager = new PluginManager(PluginHostAPI);
PluginManager.LoadPlugins(config.PluginsDirPath, config.EnabledPlugins);
AuthManager.SetDefaultAuthProvider();
Log.Info("Zirconium is initialized successfully");
}

View File

@ -5,6 +5,7 @@ using JWT.Builder;
using Newtonsoft.Json;
using Zirconium.Utils;
using Zirconium.Core.Plugins.Interfaces;
using System.Linq;
namespace Zirconium.Core
{
@ -14,13 +15,13 @@ namespace Zirconium.Core
private string _secretString;
private const long DEFAULT_TOKEN_EXPIRATION_TIME_HOURS = 24 * 3600000;
private IList<IAuthProvider> _authProviders;
private IAuthProvider _defaultAuthProvider;
public IAuthProvider DefaultAuthProvider { get; private set; }
public AuthManager(App app)
{
_app = app;
_authProviders = new List<IAuthProvider>();
_defaultAuthProvider = null;
DefaultAuthProvider = null;
_secretString = Guid.NewGuid().ToString();
}
@ -37,7 +38,8 @@ namespace Zirconium.Core
.Encode();
}
public string CreateToken(string entityID, string deviceID) {
public string CreateToken(string entityID, string deviceID)
{
return CreateToken(entityID, deviceID, DEFAULT_TOKEN_EXPIRATION_TIME_HOURS);
}
@ -49,17 +51,23 @@ namespace Zirconium.Core
.MustVerifySignature()
.Decode(token);
var payload = JsonConvert.DeserializeObject<JWTPayload>(jsonPayload);
if (_defaultAuthProvider == null) {
if (DefaultAuthProvider == null)
{
throw new Exception("Default auth provider isn't specified");
}
var validToken = _defaultAuthProvider.TestToken(token, payload);
var validToken = DefaultAuthProvider.TestToken(token, payload);
if (!validToken)
return null;
return payload;
}
public void AddAuthProvider(IAuthProvider provider) {
public void AddAuthProvider(IAuthProvider 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 FireEvent(CoreEvent coreEvent);
string GenerateAuthToken(string entityID, string deviceID, int tokenExpirationMillis);
string GenerateAuthToken(string entityID, string deviceID);
string[] GetServerDomains();
string GetServerID();
void SendMessage(Session session, BaseMessage message);
dynamic GetSettings(IPluginAPI plugin);
dynamic GetSettings(string pluginName);
void ProvideAuth(IAuthProvider provider);
IAuthProvider GetAuthProvider();
IExposedSessionManager GetSessionManager();
void RegisterIPCService(IPluginAPI plugin, dynamic service);
Task<dynamic> MakeIPCRequest(string pluginName, string methodName, dynamic paramsObject);

View File

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