-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathSparqlServiceModule.cs
More file actions
126 lines (106 loc) · 4.12 KB
/
SparqlServiceModule.cs
File metadata and controls
126 lines (106 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.IoTGateway;
using Waher.IoTGateway.Setup;
using Waher.Networking;
using Waher.Networking.HTTP;
using Waher.Networking.HTTP.Authentication;
using Waher.Networking.XMPP.Concentrator;
using Waher.Runtime.Inventory;
using Waher.Security.JWT;
using Waher.Security.Users;
namespace Waher.WebService.Sparql
{
/// <summary>
/// Pluggable module registering the SPARQL endpoint to the web server.
/// </summary>
[Singleton]
public class SparqlServiceModule : IModule
{
internal const string QueryPrivileges = "Admin.Graphs.Query";
internal const string GetPrivileges = "Admin.Graphs.Get";
internal const string AddPrivileges = "Admin.Graphs.Add";
internal const string UpdatePrivileges = "Admin.Graphs.Update";
internal const string DeletePrivileges = "Admin.Graphs.Delete";
private SparqlEndpoint sparqlEndpoint;
private GraphStore graphStore;
/// <summary>
/// Pluggable module registering the SPARQL endpoint to the web server.
/// </summary>
public SparqlServiceModule()
{
}
/// <summary>
/// Starts the module.
/// </summary>
public Task Start()
{
List<HttpAuthenticationScheme> Schemes = new List<HttpAuthenticationScheme>();
bool RequireEncryption;
int MinSecurityStrength;
if (DomainConfiguration.Instance.UseEncryption && !string.IsNullOrEmpty(DomainConfiguration.Instance.Domain))
{
RequireEncryption = true;
MinSecurityStrength = 128;
}
else
{
RequireEncryption = false;
MinSecurityStrength = 0;
}
if (Types.TryGetModuleParameter("JWT", out JwtFactory JwtFactory) &&
!JwtFactory.Disposed)
{
Schemes.Add(new JwtAuthentication(RequireEncryption, MinSecurityStrength, Gateway.Domain, null, JwtFactory)); // Any JWT token generated by the server will suffice. Does not have to point to a registered user.
}
if (!(Gateway.HttpServer is null) && Gateway.HttpServer.ClientCertificates != ClientCertificates.NotUsed)
Schemes.Add(new MutualTlsAuthentication(Users.Source));
Schemes.Add(new BasicAuthentication(RequireEncryption, MinSecurityStrength, Gateway.Domain, Users.Source));
Schemes.Add(new DigestAuthentication(RequireEncryption, MinSecurityStrength, DigestAlgorithm.MD5, Gateway.Domain, Users.Source));
Schemes.Add(new DigestAuthentication(RequireEncryption, MinSecurityStrength, DigestAlgorithm.SHA256, Gateway.Domain, Users.Source));
Schemes.Add(new DigestAuthentication(RequireEncryption, MinSecurityStrength, DigestAlgorithm.SHA3_256, Gateway.Domain, Users.Source));
if (!(Gateway.HttpServer is null))
Schemes.Add(new SessionAuthentication(Gateway.HttpServer));
RequiredPrivileges Auth = new RequiredPrivileges(Schemes.ToArray(), QueryPrivileges);
this.sparqlEndpoint = new SparqlEndpoint("/sparql", Auth);
Gateway.HttpServer?.Register(this.sparqlEndpoint);
this.graphStore = new GraphStore("/rdf-graph-store", Auth);
Gateway.HttpServer?.Register(this.graphStore);
if (!(Gateway.ConcentratorServer is null))
{
Gateway.ConcentratorServer.SourceRegistered += this.ConcentratorServer_SourceRegistered;
Gateway.ConcentratorServer.SourceUnregistered += this.ConcentratorServer_SourceUnregistered;
}
return Task.CompletedTask;
}
/// <summary>
/// Stops the module.
/// </summary>
public Task Stop()
{
if (!(Gateway.HttpServer is null))
{
Gateway.HttpServer.Unregister(this.sparqlEndpoint);
this.sparqlEndpoint = null;
Gateway.HttpServer.Unregister(this.graphStore);
this.graphStore = null;
}
if (!(Gateway.ConcentratorServer is null))
{
Gateway.ConcentratorServer.SourceRegistered -= this.ConcentratorServer_SourceRegistered;
Gateway.ConcentratorServer.SourceUnregistered -= this.ConcentratorServer_SourceUnregistered;
}
return Task.CompletedTask;
}
private Task ConcentratorServer_SourceRegistered(object Sender, DataSourceEventArgs e)
{
GraphStore.InvalidateDefaultGraph();
return Task.CompletedTask;
}
private Task ConcentratorServer_SourceUnregistered(object Sender, DataSourceEventArgs e)
{
GraphStore.InvalidateDefaultGraph();
return Task.CompletedTask;
}
}
}