mirror of
https://github.com/cadmium-im/zirconium-sharp.git
synced 2024-11-09 12:11:04 +00:00
Implement custom auth provider feature in plugin system
This commit is contained in:
parent
803d898f45
commit
11379cd6c2
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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
|
||||||
|
8
src/Zirconium/Core/Plugins/Interfaces/IAuthProvider.cs
Normal file
8
src/Zirconium/Core/Plugins/Interfaces/IAuthProvider.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace Zirconium.Core.Plugins.Interfaces
|
||||||
|
{
|
||||||
|
public interface IAuthProvider
|
||||||
|
{
|
||||||
|
bool TestToken(string token, JWTPayload payload);
|
||||||
|
string GetAuthProviderName();
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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);
|
||||||
|
@ -62,13 +62,14 @@ namespace Zirconium.Core.Plugins
|
|||||||
loader = PluginLoader.CreateFromAssemblyFile(
|
loader = PluginLoader.CreateFromAssemblyFile(
|
||||||
pluginDll,
|
pluginDll,
|
||||||
sharedTypes: new[] {
|
sharedTypes: new[] {
|
||||||
typeof(IPluginAPI),
|
typeof(IPluginAPI),
|
||||||
typeof(IPluginHostAPI),
|
typeof(IPluginHostAPI),
|
||||||
typeof(IPluginManager),
|
typeof(IPluginManager),
|
||||||
typeof(IC2SMessageHandler),
|
typeof(IAuthProvider),
|
||||||
typeof(ICoreEventHandler),
|
typeof(IC2SMessageHandler),
|
||||||
typeof(BaseMessage),
|
typeof(ICoreEventHandler),
|
||||||
typeof(CoreEvent)
|
typeof(BaseMessage),
|
||||||
|
typeof(CoreEvent)
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user