mirror of
https://github.com/cadmium-im/zirconium-sharp.git
synced 2024-11-25 11:52:23 +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 JWT.Algorithms;
|
||||
using JWT.Builder;
|
||||
using Newtonsoft.Json;
|
||||
using Zirconium.Utils;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
|
||||
namespace Zirconium.Core
|
||||
{
|
||||
@ -11,10 +13,14 @@ namespace Zirconium.Core
|
||||
private App _app;
|
||||
private string _secretString;
|
||||
private const long DEFAULT_TOKEN_EXPIRATION_TIME_HOURS = 24 * 3600000;
|
||||
private IList<IAuthProvider> _authProviders;
|
||||
private IAuthProvider _defaultAuthProvider;
|
||||
|
||||
public AuthManager(App app)
|
||||
{
|
||||
_app = app;
|
||||
_authProviders = new List<IAuthProvider>();
|
||||
_defaultAuthProvider = null;
|
||||
_secretString = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
@ -42,7 +48,18 @@ namespace Zirconium.Core
|
||||
.WithSecret(_secretString)
|
||||
.MustVerifySignature()
|
||||
.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
|
||||
public Dictionary<string, dynamic> Plugins { get; set; }
|
||||
|
||||
public string AuthenticationProvider { get; set; }
|
||||
}
|
||||
|
||||
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);
|
||||
dynamic GetSettings(IPluginAPI plugin);
|
||||
dynamic GetSettings(string pluginName);
|
||||
void ProvideAuth(IAuthProvider provider);
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Zirconium.Core.Models;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
@ -17,6 +16,10 @@ namespace Zirconium.Core.Plugins
|
||||
_app = app;
|
||||
}
|
||||
|
||||
public void ProvideAuth(IAuthProvider provider) {
|
||||
_app.AuthManager.AddAuthProvider(provider);
|
||||
}
|
||||
|
||||
public void FireEvent(CoreEvent coreEvent)
|
||||
{
|
||||
_router.RouteCoreEvent(coreEvent);
|
||||
|
@ -62,13 +62,14 @@ namespace Zirconium.Core.Plugins
|
||||
loader = PluginLoader.CreateFromAssemblyFile(
|
||||
pluginDll,
|
||||
sharedTypes: new[] {
|
||||
typeof(IPluginAPI),
|
||||
typeof(IPluginHostAPI),
|
||||
typeof(IPluginManager),
|
||||
typeof(IC2SMessageHandler),
|
||||
typeof(ICoreEventHandler),
|
||||
typeof(BaseMessage),
|
||||
typeof(CoreEvent)
|
||||
typeof(IPluginAPI),
|
||||
typeof(IPluginHostAPI),
|
||||
typeof(IPluginManager),
|
||||
typeof(IAuthProvider),
|
||||
typeof(IC2SMessageHandler),
|
||||
typeof(ICoreEventHandler),
|
||||
typeof(BaseMessage),
|
||||
typeof(CoreEvent)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user