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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use aiscript_directive::{Validator, route::RouteAnnotation};
use aiscript_vm::{ReturnValue, Vm, VmError};
use axum::{
Form, Json, RequestExt,
body::Body,
extract::{self, FromRequest, RawPathParams, Request},
http::{HeaderName, HeaderValue},
response::{IntoResponse, Response},
};
use axum_extra::{
TypedHeader,
headers::{
Authorization,
authorization::{Basic, Bearer},
},
};
use hyper::StatusCode;
use jsonwebtoken::{Algorithm, DecodingKey, TokenData, Validation, decode};
use serde_json::Value;
use sqlx::{PgPool, SqlitePool};
use std::{
collections::HashMap,
convert::Infallible,
future::Future,
mem,
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tokio::task::{self, JoinHandle};
use tower::Service;
use crate::{
Config,
ast::{self, *},
};
use crate::error::ServerError;
type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
#[derive(Clone)]
pub struct Field {
name: String,
field_type: FieldType,
required: bool,
default: Option<Value>,
validators: Arc<[Box<dyn Validator>]>,
}
#[derive(Clone)]
pub struct Endpoint {
pub annotation: RouteAnnotation,
pub path_params: Vec<Field>,
pub query_params: Vec<Field>,
pub body_type: BodyKind,
pub body_fields: Vec<Field>,
pub script: String,
pub path_specs: Vec<PathSpec>,
// pub provider_manager: Arc<ProviderManager>,
pub pg_connection: Option<PgPool>,
pub sqlite_connection: Option<SqlitePool>,
pub redis_connection: Option<redis::aio::MultiplexedConnection>,
}
enum ProcessingState {
ValidatingAuth,
ValidatingPath,
ValidatingQuery,
ValidatingBody,
Executing(JoinHandle<Result<ReturnValue, VmError>>),
}
pub struct RequestProcessor {
endpoint: Endpoint,
request: Request<Body>,
jwt_claim: Option<Value>,
path_data: HashMap<String, Value>,
query_data: HashMap<String, Value>,
body_data: HashMap<String, Value>,
state: ProcessingState,
}
impl RequestProcessor {
fn new(endpoint: Endpoint, request: Request<Body>) -> Self {
let state = if endpoint.annotation.is_auth_required() {
ProcessingState::ValidatingAuth
} else {
ProcessingState::ValidatingPath
};
Self {
endpoint,
request,
jwt_claim: None,
path_data: HashMap::new(),
query_data: HashMap::new(),
body_data: HashMap::new(),
state,
}
}
fn validate_field(field: &Field, value: &Value) -> Result<Value, ServerError> {
// Try to convert the value if it doesn't match the expected type
let converted_value = match (field.field_type, value) {
// Already correct types
(FieldType::Str, Value::String(_))
| (FieldType::Number, Value::Number(_))
| (FieldType::Bool, Value::Bool(_))
| (FieldType::Array, Value::Array(_)) => value.clone(),
// Convert string to number
(FieldType::Number, Value::String(s)) => {
// Try parsing as integer first
if let Ok(n) = s.parse::<i64>() {
Value::Number(serde_json::Number::from(n))
}
// Then try as floating point
else if let Ok(f) = s.parse::<f64>() {
match serde_json::Number::from_f64(f) {
Some(n) => Value::Number(n),
None => {
return Err(ServerError::TypeMismatch {
field: field.name.clone(),
expected: field.field_type.as_str(),
});
}
}
}
// Cannot parse as number
else {
return Err(ServerError::TypeMismatch {
field: field.name.clone(),
expected: field.field_type.as_str(),
});
}
}
// Convert string to boolean
(FieldType::Bool, Value::String(s)) => match s.to_lowercase().as_str() {
"true" /*| "yes" | "1" | "on"*/ => Value::Bool(true),
"false" /*| "no" | "0" | "off"*/ => Value::Bool(false),
_ => {
return Err(ServerError::TypeMismatch {
field: field.name.clone(),
expected: field.field_type.as_str(),
})
}
},
// Convert number to boolean (0 = false, non-zero = true)
(FieldType::Bool, Value::Number(n)) => {
if n.as_f64().unwrap_or(0.0) == 0.0 {
Value::Bool(false)
} else {
Value::Bool(true)
}
}
// Convert number to string
(FieldType::Str, Value::Number(n)) => Value::String(n.to_string()),
// Convert boolean to string
(FieldType::Str, Value::Bool(b)) => Value::String(b.to_string()),
// Other conversions not supported
_ => {
return Err(ServerError::TypeMismatch {
field: field.name.clone(),
expected: field.field_type.as_str(),
});
}
};
// Now validate with the converted value
for validator in &*field.validators {
if let Err(e) = validator.validate(&converted_value) {
return Err(ServerError::ValidationError {
field: field.name.clone(),
message: e.to_string(),
});
}
}
// Return success - the caller needs to update the original value
// with the converted value if needed
Ok(converted_value)
}
async fn process_json_body(request: Request<Body>) -> Result<Value, ServerError> {
Json::<Value>::from_request(request, &())
.await
.map(|Json(body)| body)
.map_err(ServerError::JsonParseError)
}
async fn process_form_body(request: Request<Body>) -> Result<Value, ServerError> {
Form::<Value>::from_request(request, &())
.await
.map(|Form(body)| body)
.map_err(ServerError::FormParseError)
}
fn get_request(&self) -> HashMap<&'static str, Value> {
let uri = self.request.uri();
[
("method", self.request.method().as_str().into()),
if let Some(authority) = uri.authority() {
("authority", authority.as_str().into())
} else {
("authority", serde_json::Value::Null)
},
if let Some(host) = uri.host() {
("host", host.into())
} else {
("host", serde_json::Value::Null)
},
("path", uri.path().into()),
("query", uri.query().into()),
("protocol", uri.scheme_str().into()),
("port", uri.port_u16().into()),
// ("fragment", uri.fragment().into()),
]
.into_iter()
.collect()
}
fn get_header(&self) -> HashMap<String, Value> {
self.request
.headers()
.iter()
.map(|(name, value)| {
(
name.as_str().to_owned(),
Value::String(value.to_str().unwrap().to_owned()),
)
})
.collect()
}
}
impl Future for RequestProcessor {
type Output = Result<Response, Infallible>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let config = Config::get();
loop {
match &mut self.state {
ProcessingState::ValidatingAuth => {
if self.endpoint.annotation.is_jwt_auth() {
self.jwt_claim = {
let future = self
.request
.extract_parts::<TypedHeader<Authorization<Bearer>>>();
tokio::pin!(future);
match future.poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Ok(bearer)) => {
let key =
DecodingKey::from_secret(config.auth.jwt.secret.as_bytes());
let validation = Validation::new(Algorithm::HS256);
// Decode token
let token_data: TokenData<Value> =
decode(bearer.token(), &key, &validation).unwrap();
Some(token_data.claims)
}
Poll::Ready(Err(e)) => {
return Poll::Ready(Ok(ServerError::AuthenticationError {
message: e.to_string(),
}
.into_response()));
}
}
};
} else {
// Baisc auth
// Extract the token from the authorization header
let future = self
.request
.extract_parts::<TypedHeader<Authorization<Basic>>>();
tokio::pin!(future);
match future.poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Ok(basic)) => {
if let Some(b) = config.auth.basic.as_ref() {
if *b.username != basic.username()
|| *b.password != basic.password()
{
return Poll::Ready(Ok(ServerError::AuthenticationError {
message: "Invalid username or password".to_string(),
}
.into_response()));
}
} else {
return Poll::Ready(Ok(ServerError::AuthenticationError {
message: "Basic auth is not configured".to_string(),
}
.into_response()));
}
}
Poll::Ready(Err(e)) => {
return Poll::Ready(Ok(ServerError::AuthenticationError {
message: e.to_string(),
}
.into_response()));
}
}
}
self.state = ProcessingState::ValidatingPath;
}
ProcessingState::ValidatingPath => {
let raw_path_params = {
// Extract path parameters using Axum's RawPathParams extractor
let future = self.request.extract_parts::<RawPathParams>();
tokio::pin!(future);
match future.poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Ok(params)) => params,
Poll::Ready(Err(e)) => {
return Poll::Ready(Ok(format!(
"Failed to extract path parameters: {}",
e
)
.into_response()));
}
}
};
// Process and validate each path parameter
for (param_name, param_value) in &raw_path_params {
// Find the corresponding path parameter field
if let Some(field) = self
.endpoint
.path_params
.iter()
.find(|f| f.name == param_name)
{
// Convert the value to the appropriate type based on the field definition
let value = match field.field_type {
FieldType::Str => Value::String(param_value.to_string()),
FieldType::Number => {
if let Ok(num) = param_value.parse::<i64>() {
Value::Number(num.into())
} else if let Ok(float) = param_value.parse::<f64>() {
match serde_json::Number::from_f64(float) {
Some(n) => Value::Number(n),
None => {
return Poll::Ready(Ok(
format!("Invalid path parameter type for {}: could not convert to number", param_name)
.into_response()
));
}
}
} else {
return Poll::Ready(Ok(format!(
"Invalid path parameter type for {}: expected a number",
param_name
)
.into_response()));
}
}
FieldType::Bool => match param_value.to_lowercase().as_str() {
"true" => Value::Bool(true),
"false" => Value::Bool(false),
_ => {
return Poll::Ready(Ok(
format!("Invalid path parameter type for {}: expected a boolean", param_name)
.into_response()
));
}
},
_ => {
return Poll::Ready(Ok(format!(
"Unsupported path parameter type for {}",
param_name
)
.into_response()));
}
};
// Validate the value using our existing validation infrastructure
if let Err(e) = Self::validate_field(field, &value) {
return Poll::Ready(Ok(e.into_response()));
}
// Store the validated parameter
self.path_data.insert(param_name.to_string(), value);
}
}
// Check for missing required parameters
for field in &self.endpoint.path_params {
if !self.path_data.contains_key(&field.name) && field.required {
return Poll::Ready(Ok(
ServerError::MissingField(field.name.clone()).into_response()
));
}
}
// Move to the next state
self.state = ProcessingState::ValidatingQuery;
}
ProcessingState::ValidatingQuery => {
let mut query = extract::Query::<Value>::try_from_uri(self.request.uri())
.map(|extract::Query(q)| q)
.unwrap_or_default();
let mut failed_validation = None;
for field in mem::take(&mut self.endpoint.query_params) {
if let Some(value) = query.get_mut(&field.name) {
match Self::validate_field(&field, value) {
Ok(converted_value) => *value = converted_value,
Err(e) => {
failed_validation = Some(e);
break;
}
}
self.query_data.insert(field.name.clone(), value.clone());
} else if let Some(default) = &field.default {
self.query_data.insert(field.name.clone(), default.clone());
} else if field.required {
failed_validation = Some(ServerError::MissingField(field.name.clone()));
break;
}
}
if let Some(error) = failed_validation {
return Poll::Ready(Ok(error.into_response()));
}
self.state = ProcessingState::ValidatingBody;
}
ProcessingState::ValidatingBody => {
let request_obj = self.get_request();
let header_obj = self.get_header();
if !self.endpoint.body_fields.is_empty() {
let request = mem::take(&mut self.request);
let body_fut: BoxFuture<Result<Value, ServerError>> =
match self.endpoint.body_type {
BodyKind::Json => Box::pin(Self::process_json_body(request)),
BodyKind::Form => Box::pin(Self::process_form_body(request)),
};
tokio::pin!(body_fut);
let body = match body_fut.poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Ok(value)) => value,
Poll::Ready(Err(e)) => {
return Poll::Ready(Ok(
format!("Body parsing error: {:?}", e).into_response()
));
}
};
let mut failed_validation = None;
for field in mem::take(&mut self.endpoint.body_fields) {
if let Some(value) = body.get(&field.name) {
if let Err(e) = Self::validate_field(&field, value) {
failed_validation = Some(e);
break;
}
self.body_data.insert(field.name.clone(), value.clone());
} else if let Some(default) = &field.default {
self.body_data.insert(field.name.clone(), default.clone());
} else if field.required {
failed_validation =
Some(ServerError::MissingField(field.name.clone()));
break;
}
}
if let Some(error) = failed_validation {
return Poll::Ready(Ok(error.into_response()));
}
}
let script = mem::take(&mut self.endpoint.script);
let script = Box::leak(script.into_boxed_str());
let sso_fields = if let Some(provider) = self.endpoint.annotation.sso_provider {
match crate::config::get_sso_fields(provider) {
Some(fields) => Some(fields),
None => {
return Poll::Ready(Ok(format!(
"Missing `[sso.{}]` config",
provider.as_str()
)
.into_response()));
}
}
} else {
None
};
let path_data = mem::take(&mut self.path_data);
let query_data = mem::take(&mut self.query_data);
let body_data = mem::take(&mut self.body_data);
let pg_connection = self.endpoint.pg_connection.clone();
let sqlite_connection = self.endpoint.sqlite_connection.clone();
let redis_connection = self.endpoint.redis_connection.clone();
let handle: JoinHandle<Result<ReturnValue, VmError>> =
task::spawn_blocking(move || {
let mut vm =
Vm::new(pg_connection, sqlite_connection, redis_connection);
if let Some(fields) = sso_fields {
vm.inject_sso_instance(fields);
}
vm.register_extra_native_functions();
vm.compile(script)?;
vm.eval_function(
0,
&[
Value::Object(path_data.into_iter().collect()),
Value::Object(query_data.into_iter().collect()),
Value::Object(body_data.into_iter().collect()),
Value::Object(
request_obj
.into_iter()
.map(|(k, v)| (k.to_owned(), v))
.collect(),
),
Value::Object(header_obj.into_iter().collect()),
],
)
});
self.state = ProcessingState::Executing(handle);
}
ProcessingState::Executing(handle) => {
return match Pin::new(handle).poll(cx) {
Poll::Ready(Ok(result)) => match result {
Ok(value) => {
if let ReturnValue::Response(mut fields) = value {
let mut response =
Json(fields.remove("body").unwrap_or_default())
.into_response();
*response.status_mut() = StatusCode::from_u16(
fields
.remove("status_code")
.map(|v| v.as_f64().unwrap())
.unwrap_or(200f64)
as u16,
)
.unwrap();
if let Some(headers) = fields.remove("headers") {
response.headers_mut().extend(
headers.as_object().unwrap().into_iter().map(
|(name, value)| {
(
HeaderName::try_from(name).unwrap(),
HeaderValue::from_str(
value.as_str().unwrap(),
)
.unwrap(),
)
},
),
);
}
Poll::Ready(Ok(response))
} else {
Poll::Ready(Ok(Json(value).into_response()))
}
}
Err(VmError::CompileError) => {
Poll::Ready(Ok("Compile Error".into_response()))
}
Err(VmError::RuntimeError(err)) => {
Poll::Ready(Ok(format!("Runtime Error: {err}",).into_response()))
}
},
Poll::Ready(Err(err)) => {
Poll::Ready(Ok(format!("Error:: {err}").into_response()))
}
Poll::Pending => Poll::Pending,
};
}
}
}
}
}
impl Service<Request<Body>> for Endpoint {
type Response = Response;
type Error = Infallible;
type Future = RequestProcessor;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request<Body>) -> Self::Future {
RequestProcessor::new(self.clone(), req)
}
}
pub(crate) fn convert_field(field: ast::Field) -> Field {
Field {
name: field.name,
field_type: field._type,
required: field.required,
default: field.default,
validators: Arc::from(field.validators),
}
}