mirror of
https://github.com/cadmium-im/zirconium-sharp.git
synced 2024-11-22 02:12:22 +00:00
Implement setting default auth provider by config
This commit is contained in:
parent
370a7e8dc2
commit
9e0088cbba
@ -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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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);
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user