forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptService.cs
More file actions
170 lines (148 loc) · 4.34 KB
/
ScriptService.cs
File metadata and controls
170 lines (148 loc) · 4.34 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Waher.Networking.HTTP;
using Waher.Script;
using Waher.Script.Graphs;
using Waher.Script.Model;
using Waher.Script.Objects;
using Waher.Security;
namespace Waher.WebService.Script
{
/// <summary>
/// Web service that can be used to execute script on the server.
/// </summary>
public class ScriptService : HttpAsynchronousResource, IHttpPostMethod
{
private readonly HttpAuthenticationScheme[] authenticationSchemes;
/// <summary>
/// Web service that can be used to execute script on the server.
/// </summary>
/// <param name="ResourceName">Name of resource.</param>
/// <param name="AuthenticationSchemes">Authentication schemes.</param>
public ScriptService(string ResourceName, params HttpAuthenticationScheme[] AuthenticationSchemes)
: base(ResourceName)
{
this.authenticationSchemes = AuthenticationSchemes;
}
/// <summary>
/// If the resource handles sub-paths.
/// </summary>
public override bool HandlesSubPaths
{
get
{
return false;
}
}
/// <summary>
/// If the resource uses user sessions.
/// </summary>
public override bool UserSessions
{
get
{
return true;
}
}
/// <summary>
/// If the POST method is allowed.
/// </summary>
public bool AllowsPOST
{
get { return true; }
}
/// <summary>
/// Executes the POST method on the resource.
/// </summary>
/// <param name="Request">HTTP Request</param>
/// <param name="Response">HTTP Response</param>
/// <exception cref="HttpException">If an error occurred when processing the method.</exception>
public async Task POST(HttpRequest Request, HttpResponse Response)
{
if (Request.Session is null ||
!Request.Session.TryGetVariable("User", out Variable v) ||
!(v.ValueObject is IUser User) ||
!User.HasPrivilege("Admin.Data.Prompt"))
{
throw new ForbiddenException("Access denied.");
}
object Obj = Request.HasData ? Request.DecodeData() : null;
string s = Obj as string;
string Tag = Request.Header["X-TAG"];
if (string.IsNullOrEmpty(Tag))
throw new BadRequestException();
State State;
if (string.IsNullOrEmpty(s))
{
if (!string.IsNullOrEmpty(s = Request.Header["X-X"]) && int.TryParse(s, out int x) &&
!string.IsNullOrEmpty(s = Request.Header["X-Y"]) && int.TryParse(s, out int y))
{
if (!Request.Session.TryGetVariable("Graphs", out v) ||
!(v.ValueObject is Dictionary<string, KeyValuePair<Graph, object[]>> Graphs))
{
throw new NotFoundException("Graphs not found.");
}
KeyValuePair<Graph, object[]> Rec;
lock (Graphs)
{
if (!Graphs.TryGetValue(Tag, out Rec))
throw new NotFoundException("Graph not found.");
}
s = Rec.Key.GetBitmapClickScript(x, y, Rec.Value);
Response.ContentType = "text/plain";
await Response.Write(s);
await Response.SendResponse();
}
else
{
if (!State.TryGetState(Tag, out State))
throw new NotFoundException("Expression not found.");
State.SetRequestResponse(Request, Response, User);
}
}
else
{
if (!Request.Session.TryGetVariable("Timeout", out v) ||
!(v.ValueObject is double Timeout) || Timeout <= 0)
{
Timeout = 5 * 60 * 1000; // 5 minutes
}
State = new State(Request, Response, Tag, (int)(Timeout + 0.5), User);
try
{
Expression Exp = new Expression(s);
if (!Exp.ForAll(this.IsAuthorized, User, false))
{
await State.SendResponse(new ObjectValue(new ForbiddenException("Unauthorized to execute expression.")), false);
return;
}
State.SetExpression(Exp);
}
catch (Exception ex)
{
await State.SendResponse(new ObjectValue(ex), false);
return;
}
State.Start();
}
}
private bool IsAuthorized(ref ScriptNode Node, object State)
{
if (Node is null)
return true;
else if (State is IUser User)
return User.HasPrivilege("Script." + Node.GetType().FullName);
else
return false;
}
/// <summary>
/// Any authentication schemes used to authenticate users before access is granted to the corresponding resource.
/// </summary>
/// <param name="Request">Current request</param>
public override HttpAuthenticationScheme[] GetAuthenticationSchemes(HttpRequest Request)
{
return this.authenticationSchemes;
}
}
}