surrealdb-core 3.1.2

A scalable, distributed, collaborative, document-graph database, for the realtime web
Documentation
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
use std::str::FromStr;
use std::sync::{Arc, LazyLock};

use anyhow::{Result, bail};
use chrono::{Duration, Utc};
use jsonwebtoken::Algorithm::*;
use jsonwebtoken::jwk::AlgorithmParameters::*;
use jsonwebtoken::jwk::{Jwk, JwkSet, KeyAlgorithm, KeyOperations, PublicKeyUse};
use jsonwebtoken::{DecodingKey, Validation};
use reqwest::{Client, Url};
use sha2::{Digest, Sha256};

use crate::dbs::capabilities::NetTarget;
use crate::err::Error;
use crate::kvs::Datastore;
use crate::kvs::cache::ds::{CachedJwks, DatastoreCache, Entry, Lookup};

#[cfg(test)]
static CACHE_EXPIRATION: LazyLock<chrono::Duration> = LazyLock::new(|| Duration::seconds(1));
#[cfg(not(test))]
static CACHE_EXPIRATION: LazyLock<chrono::Duration> =
	LazyLock::new(|| match std::env::var("SURREAL_JWKS_CACHE_EXPIRATION_SECONDS") {
		Ok(seconds_str) => {
			let seconds = seconds_str.parse::<u64>().expect(
				"Expected a valid number of seconds for SURREAL_JWKS_CACHE_EXPIRATION_SECONDS",
			);
			Duration::seconds(seconds as i64)
		}
		Err(_) => {
			Duration::seconds(43200) // Set default cache expiration of 12 hours
		}
	});

#[cfg(test)]
static CACHE_COOLDOWN: LazyLock<chrono::Duration> = LazyLock::new(|| Duration::seconds(300));
#[cfg(not(test))]
static CACHE_COOLDOWN: LazyLock<chrono::Duration> =
	LazyLock::new(|| match std::env::var("SURREAL_JWKS_CACHE_COOLDOWN_SECONDS") {
		Ok(seconds_str) => {
			let seconds = seconds_str.parse::<u64>().expect(
				"Expected a valid number of seconds for SURREAL_JWKS_CACHE_COOLDOWN_SECONDS",
			);
			Duration::seconds(seconds as i64)
		}
		Err(_) => {
			Duration::seconds(300) // Set default cache refresh cooldown of 5 minutes
		}
	});

#[cfg(not(target_family = "wasm"))]
static REMOTE_TIMEOUT: LazyLock<chrono::Duration> =
	LazyLock::new(|| match std::env::var("SURREAL_JWKS_REMOTE_TIMEOUT_MILLISECONDS") {
		Ok(milliseconds_str) => {
			let milliseconds = milliseconds_str.parse::<u64>().expect(
				"Expected a valid number of milliseconds for SURREAL_JWKS_REMOTE_TIMEOUT_MILLISECONDS",
			);
			Duration::milliseconds(milliseconds as i64)
		}
		Err(_) => {
			Duration::milliseconds(1000) // Set default remote timeout to 1 second
		}
	});

// Generates a verification configuration from a JWKS object hosted in a remote
// location Performs local caching of all JWKS objects to prevent unnecessary
// network requests Implements checks to prevent denial of service and
// unauthorized network requests Validates the JWK objects found in the JWKS
// object according to RFC 7517 Source: https://datatracker.ietf.org/doc/html/rfc7517
pub(super) async fn config(
	kvs: &Datastore,
	kid: &str,
	url: &str,
	token_alg: jsonwebtoken::Algorithm,
) -> Result<(DecodingKey, Validation)> {
	let cache = kvs.cache();
	// Attempt to fetch relevant JWK object either from local cache or remote
	// location
	let jwk = match fetch_jwks_from_cache(cache.as_ref(), url) {
		Some(cached) => {
			trace!("Successfully fetched JWKS object from local cache");
			// Check that the cached JWKS object has not expired yet
			if Utc::now().signed_duration_since(cached.time) < *CACHE_EXPIRATION {
				// Attempt to find JWK in JWKS object from local cache
				match cached.jwks.find(kid) {
					Some(jwk) => jwk.to_owned(),
					_ => {
						trace!(
							"Could not find valid JWK object with key identifier '{kid}' in cached JWKS object"
						);
						// Check that the cached JWKS object has not been recently updated
						if Utc::now().signed_duration_since(cached.time) < *CACHE_COOLDOWN {
							debug!("Refused to refresh cache before cooldown period is over");
							bail!(Error::InvalidAuth); // Return opaque error
						}
						find_jwk_from_url(kvs, url, kid).await?
					}
				}
			} else {
				trace!("Fetched JWKS object from local cache has expired");
				find_jwk_from_url(kvs, url, kid).await?
			}
		}
		None => {
			trace!("Could not fetch JWKS object from local cache");
			find_jwk_from_url(kvs, url, kid).await?
		}
	};

	// Use algorithm provided, if specified
	// This parameter is not required to be present, although is usually expected
	// When missing, tokens must be validated using only the required key type
	// parameter This is discouraged, as it requires relying on the algorithm
	// specified in the token Source: https://datatracker.ietf.org/doc/html/rfc7517#section-4.4
	let alg = match jwk.common.key_algorithm {
		Some(alg) => match alg {
			KeyAlgorithm::HS256 => HS256,
			KeyAlgorithm::HS384 => HS384,
			KeyAlgorithm::HS512 => HS512,
			KeyAlgorithm::EdDSA => EdDSA,
			KeyAlgorithm::ES256 => ES256,
			KeyAlgorithm::ES384 => ES384,
			KeyAlgorithm::PS256 => PS256,
			KeyAlgorithm::PS384 => PS384,
			KeyAlgorithm::PS512 => PS512,
			KeyAlgorithm::RS256 => RS256,
			KeyAlgorithm::RS384 => RS384,
			KeyAlgorithm::RS512 => RS512,
			_ => {
				warn!("Unspported value for parameter 'alg' in JWK object: '{:?}'", alg);
				bail!(Error::InvalidAuth); // Return opaque error
			}
		},
		// If not specified, use the algorithm provided in the token header
		// It is critical that the JWT library prevents the "none" algorithm from being used
		// Reference: https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/#Meet-the--None--Algorithm
		// In the case of "jsonwebtoken", the "none" algorithm is not part of the enumeration
		// Source: https://docs.rs/jsonwebtoken/latest/jsonwebtoken/enum.Algorithm.html
		// Confirmation: https://github.com/Keats/jsonwebtoken/issues/381
		_ => {
			// Ensure that the algorithm specified in the token matches the key type defined
			// in the JWK
			match (&jwk.algorithm, token_alg) {
				(RSA(_), RS256 | RS384 | RS512 | PS256 | PS384 | PS512) => token_alg,
				(RSA(key), _) => {
					warn!(
						"Algorithm from token '{:?}' does not match JWK key type '{:?}'",
						token_alg, key.key_type
					);
					bail!(Error::InvalidAuth); // Return opaque error
				}
				(EllipticCurve(_), ES256 | ES384) => token_alg,
				(EllipticCurve(key), _) => {
					warn!(
						"Algorithm from token '{:?}' does not match JWK key type '{:?}'",
						token_alg, key.key_type
					);
					bail!(Error::InvalidAuth); // Return opaque error
				}
				(OctetKey(_), HS256 | HS384 | HS512) => token_alg,
				(OctetKey(key), _) => {
					warn!(
						"Algorithm from token '{:?}' does not match JWK key type '{:?}'",
						token_alg, key.key_type
					);
					bail!(Error::InvalidAuth); // Return opaque error
				}
				(OctetKeyPair(_), EdDSA) => token_alg,
				(OctetKeyPair(key), _) => {
					warn!(
						"Algorithm from token '{:?}' does not match JWK key type '{:?}'",
						token_alg, key.key_type
					);
					bail!(Error::InvalidAuth); // Return opaque error
				}
			}
		}
	};
	// Check if the key use (if specified) is intended to be used for signing
	// Source: https://datatracker.ietf.org/doc/html/rfc7517#section-4.2
	match &jwk.common.public_key_use {
		Some(PublicKeyUse::Signature) => (),
		Some(key_use) => {
			warn!("Invalid value for parameter 'use' in JWK object: '{:?}'", key_use);
			bail!(Error::InvalidAuth); // Return opaque error
		}
		None => (),
	}
	// Check if the key operations (if specified) include verification
	// Source: https://datatracker.ietf.org/doc/html/rfc7517#section-4.3
	if let Some(ops) = &jwk.common.key_operations
		&& !ops.contains(&KeyOperations::Verify)
	{
		warn!(
			"Invalid values for parameter 'key_ops' in JWK object: '{:?}'",
			jwk.common.key_operations
		);
		bail!(Error::InvalidAuth); // Return opaque error
	}

	// Return verification configuration if a decoding key can be retrieved from the
	// JWK object
	match DecodingKey::from_jwk(&jwk) {
		Ok(dec) => {
			let mut val = Validation::new(alg);

			// TODO(gguillemas): This keeps the existing behavior as of SurrealDB
			// 2.0.0-alpha.9. Up to that point, a fork of the "jsonwebtoken" crate in
			// version 8.3.0 was being used. Now that the audience claim is validated by
			// default, we could allow users to leverage this. This will most likely
			// involve defining an audience string via "DEFINE ACCESS ... TYPE JWT".
			val.validate_aud = false;

			Ok((dec, val))
		}
		Err(err) => {
			warn!("Failed to retrieve decoding key from JWK object: '{}'", err);
			Err(anyhow::Error::new(Error::InvalidAuth)) // Return opaque error
		}
	}
}

// Checks if network access to a remote location is allowed by the datastore
// capabilities Attempts to find a relevant JWK object inside a JWKS object
// fetched from the remote location
async fn find_jwk_from_url(kvs: &Datastore, url: &str, kid: &str) -> Result<Jwk> {
	// Check that the datastore capabilities allow connections to the URL host
	if let Err(err) = check_capabilities_url(kvs, url) {
		warn!("Network access to JWKS location is not allowed: '{}'", err);
		bail!(Error::InvalidAuth); // Return opaque error
	}

	let cache = kvs.cache();
	// Attempt to fetch JWKS object from remote location
	match fetch_jwks_from_url(cache.as_ref(), url, &kvs.config().surrealdb_user_agent).await {
		Ok(jwks) => {
			trace!("Successfully fetched JWKS object from remote location");
			// Attempt to find JWK in JWKS by the key identifier
			match jwks.find(kid) {
				Some(jwk) => Ok(jwk.to_owned()),
				_ => {
					debug!(
						"Failed to find JWK object with key identifier '{kid}' in remote JWKS object"
					);
					Err(anyhow::Error::new(Error::InvalidAuth)) // Return opaque error
				}
			}
		}
		Err(err) => {
			warn!("Failed to fetch JWKS object from remote location: '{}'", err);
			Err(anyhow::Error::new(Error::InvalidAuth)) // Return opaque error
		}
	}
}

// Returns an error if network access to the address from a given URL string is
// not allowed
fn check_capabilities_url(kvs: &Datastore, url: &str) -> Result<()> {
	let url_parsed = match Url::parse(url) {
		Ok(url) => url,
		Err(_) => {
			bail!(Error::InvalidUrl(url.to_string()));
		}
	};
	let addr = match url_parsed.host_str() {
		Some(host) => {
			if let Some(port) = url_parsed.port() {
				format!("{host}:{port}")
			} else {
				host.to_string()
			}
		}
		None => {
			bail!(Error::InvalidUrl(url.to_string()));
		}
	};
	let target = match NetTarget::from_str(&addr) {
		Ok(host) => host,
		Err(_) => {
			bail!(Error::InvalidUrl(url.to_string()));
		}
	};
	if !kvs.allows_network_target(&target) {
		warn!("Capabilities denied outgoing network connection attempt, target: '{target}'");
		bail!(Error::InvalidUrl(url.to_string()));
	}
	trace!("Capabilities allowed outgoing network connection, target: '{target}'");

	Ok(())
}

// Attempts to fetch a JWKS object from a remote location and stores it in the
// cache if successful
async fn fetch_jwks_from_url(
	cache: &DatastoreCache,
	url: &str,
	user_agent: &str,
) -> Result<JwkSet> {
	#[cfg(target_family = "wasm")]
	let _ = user_agent;
	let client = Client::new();
	let req = client.get(url);
	// Add a User-Agent header so that WAF rules don't reject the request
	#[cfg(not(target_family = "wasm"))]
	let req = req.header(reqwest::header::USER_AGENT, user_agent);
	#[cfg(not(target_family = "wasm"))]
	let res = req.timeout((*REMOTE_TIMEOUT).to_std().expect("valid duration")).send().await?;
	#[cfg(target_family = "wasm")]
	let res = req.send().await?;
	if !res.status().is_success() {
		warn!(
			"Unsuccessful HTTP status code received when fetching JWKS object from remote location: '{:?}'",
			res.status()
		);
		bail!(Error::InvalidAuth); // Return opaque error
	}
	let jwks = res.bytes().await?;

	match serde_json::from_slice::<JwkSet>(&jwks) {
		Ok(jwks) => {
			// If successful, cache the JWKS object by its URL
			store_jwks_in_cache(cache, jwks.clone(), url);
			Ok(jwks)
		}
		Err(err) => {
			warn!("Failed to parse malformed JWKS object: '{}'", err);
			Err(anyhow::Error::new(Error::InvalidAuth)) // Return opaque error
		}
	}
}

// Attempts to fetch a JWKS object from the local cache
fn fetch_jwks_from_cache(cache: &DatastoreCache, url: &str) -> Option<Arc<CachedJwks>> {
	let path = cache_key_from_url(url);
	let entry = cache.get(&Lookup::Jwk(path.as_str()))?;
	entry.try_into_jwk().ok()
}

// Attempts to store a JWKS object in the local cache
fn store_jwks_in_cache(cache: &DatastoreCache, jwks: JwkSet, url: &str) {
	let path = cache_key_from_url(url);
	let entry = Entry::Jwk(Arc::new(CachedJwks {
		jwks,
		time: Utc::now(),
	}));
	cache.insert(Lookup::Jwk(path.as_str()), entry);
	trace!("Stored JWKS object in local cache");
}

// Generates a unique cache key for a given URL string
fn cache_key_from_url(url: &str) -> String {
	let mut hasher = Sha256::new();
	hasher.update(url);
	let result = hasher.finalize();

	format!("{:x}", result)
}

#[cfg(test)]
mod tests {
	use rand::distr::{Alphanumeric, SampleString};
	use wiremock::matchers::{header, method, path};
	use wiremock::{Mock, MockServer, ResponseTemplate};

	use super::*;
	use crate::dbs::capabilities::{Capabilities, NetTarget, Targets};

	// Use unique path to prevent accidental cache reuse
	fn random_path() -> String {
		Alphanumeric.sample_string(&mut rand::rng(), 8)
	}

	static DEFAULT_JWKS: LazyLock<JwkSet> = LazyLock::new(|| {
		JwkSet{
		keys: vec![Jwk{
			common: jsonwebtoken::jwk::CommonParameters {
				public_key_use: Some(jsonwebtoken::jwk::PublicKeyUse::Signature),
				key_operations: None,
				key_algorithm: Some(KeyAlgorithm::RS256),
				key_id: Some("test_1".to_string()),
				x509_url: None,
				x509_chain: Some(vec![
					"MIIDBTCCAe2gAwIBAgIJdeyDfUXHLwX+MA0GCSqGSIb3DQEBCwUAMCAxHjAcBgNVBAMTFWdlcmF1c2VyLmV1LmF1dGgwLmNvbTAeFw0yMzEwMzExNzI5MDBaFw0zNzA3MDkxNzI5MDBaMCAxHjAcBgNVBAMTFWdlcmF1c2VyLmV1LmF1dGgwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANp7Er60Z7sOjiyQqpbZIkQBldO/t+YrHT8Mk661kNz8MRGXPpQ26rkO2fRZMeWPpXVcwW0xxLG5oQ2Iwbm1JUuHYbLb6WA/d/et0sOkOoEI6MP0+MVqrGnro+D6XGoz4yP8m2w8C2u2yFxAc+wAt1AIMWNJIYhEX6tqrliGnitDCye2wXKchhe4WctUlHoUNfO/sgazPQ7ItqisUF/fNSRbHLRJyS2mm76FlDELDLnEyVwUaeV/2xie9F44AfOQzVk1asO18BH3v6YjOQ3L41XEfOm2DMPkLOOmtyM7yA7OeF/fvn6zN+SByza6cFW37IOKoJsmvxkzxeDUlWm9MWkCAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUOeOmT9I3/MJ/zI/lS74gPQmAQfEwDgYDVR0PAQH/BAQDAgKEMA0GCSqGSIb3DQEBCwUAA4IBAQBDue8iM90XJcLORvr6e+h15f5tlvVjZ/cAzv09487QSHJtUd6qwTlunEABS5818TgMEMFDRafQ7CDX3KaAAXFpo2bnyWY9c+Ozp0PWtp8ChunOs94ayaG+viO0AiTrIY28cc26ehNBZ/4gC4/1k0IlXEk8rd1e03hQuhNiy7SQaxS2f1xkJfR4vCeF8HTN5omjKvIMcWRqkkwFZm4gdgkMfi2lNsV8V6+HXyTc3XUIdcwOUcC+Ms/m+vKxnyxw0UGh0RliB1qBc0ADg83hOsXEqZjneHh1ZhqqVF4IkKSJTfK5ofcc14GqvpLjjTR3s2eX6zxdujzwf4gnHdxjVvdJ".to_string(),
				]),
				x509_sha1_fingerprint: None,
				x509_sha256_fingerprint: None,
			},
			algorithm: jsonwebtoken::jwk::AlgorithmParameters::RSA(
				jsonwebtoken::jwk::RSAKeyParameters{
				key_type: jsonwebtoken::jwk::RSAKeyType::RSA,
				n: "2nsSvrRnuw6OLJCqltkiRAGV07-35isdPwyTrrWQ3PwxEZc-lDbquQ7Z9Fkx5Y-ldVzBbTHEsbmhDYjBubUlS4dhstvpYD93963Sw6Q6gQjow_T4xWqsaeuj4PpcajPjI_ybbDwLa7bIXEBz7AC3UAgxY0khiERfq2quWIaeK0MLJ7bBcpyGF7hZy1SUehQ187-yBrM9Dsi2qKxQX981JFsctEnJLaabvoWUMQsMucTJXBRp5X_bGJ70XjgB85DNWTVqw7XwEfe_piM5DcvjVcR86bYMw-Qs46a3IzvIDs54X9--frM35IHLNrpwVbfsg4qgmya_GTPF4NSVab0xaQ".to_string(),
				e: "AQAB".to_string(),
				}
			),
		},
		Jwk{
			common: jsonwebtoken::jwk::CommonParameters {
				public_key_use: Some(jsonwebtoken::jwk::PublicKeyUse::Signature),
				key_operations: None,
				key_algorithm: Some(KeyAlgorithm::RS256),
				key_id: Some("test_2".to_string()),
				x509_url: None,
				x509_chain: Some(vec![
					"MIIDBTCCAe2gAwIBAgIJUzJ062XCgOVQMA0GCSqGSIb3DQEBCwUAMCAxHjAcBgNVBAMTFWdlcmF1c2VyLmV1LmF1dGgwLmNvbTAeFw0yMzEwMzExNzI5MDBaFw0zNzA3MDkxNzI5MDBaMCAxHjAcBgNVBAMTFWdlcmF1c2VyLmV1LmF1dGgwLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL7TgjrojPySPO5kPBOiAgiS0guSpYkfb0AtLNVyVeANe5vgjJoBQDe7vAGm68SHft841GQBPp5KWpxDTO+liECVAnbdR9YHwuZWOGuPRVVwNqtVmS8A75YG/mTWGV4tr2h+dLjjV3jvV0hvXRJwVFShlUS9+BqgevFBoF6zxi5AHIx/k1tCg1y2fhSlzYUHxEiFRgx0RhtJfizyv9QHoLSY3RFI4QOAkPtYwN5C1X69nEHPK0Q+W+POkeV7wuMQZWTRRT+xZuYn+JIYQCQviZ52FoJsrTzOEO5jlmrUa9PMEJpn0Aw68OdyLHjQPsip8B2JSegoVP1LTc0tDoqVGqUCAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUYp67WM42b2pqF7ES0LsFvAI/Qy8wDgYDVR0PAQH/BAQDAgKEMA0GCSqGSIb3DQEBCwUAA4IBAQANOhmYz0jxNJG6pZ0klNtH00E6SoEsM/MNYH+atraTVZNeqPLAZH9514gMcqdu7+rBfQ/pRjpQG1YbkdZGQBaq5cZNlE6hNCT4BSgKddBYsN0WbfTGxstDVdoXLySgGCYpyKO6Hek4ULxwAf1LmMyOYpn4JrECy4mYShsCcfe504qfzUTd7pz1VaZ4minclOhz0dZbgYa+slUepe0C2+w+T3US138x0lPB9C266SLDakb6n/JTum+Czn2xlFBf4K4w6eWuSknvTlRrqTGE8RX3vzOiKTM3hpDdjU7Tu7eNsZpLkDR1e+w33m5NMi9iYgJcyTGsIeeHr0xjrRPD9Dwh".to_string(),
				]),
				x509_sha1_fingerprint: None,
				x509_sha256_fingerprint: None,
			},
			algorithm: jsonwebtoken::jwk::AlgorithmParameters::RSA(
				jsonwebtoken::jwk::RSAKeyParameters{
				key_type: jsonwebtoken::jwk::RSAKeyType::RSA,
				n: "vtOCOuiM_JI87mQ8E6ICCJLSC5KliR9vQC0s1XJV4A17m-CMmgFAN7u8AabrxId-3zjUZAE-nkpanENM76WIQJUCdt1H1gfC5lY4a49FVXA2q1WZLwDvlgb-ZNYZXi2vaH50uONXeO9XSG9dEnBUVKGVRL34GqB68UGgXrPGLkAcjH-TW0KDXLZ-FKXNhQfESIVGDHRGG0l-LPK_1AegtJjdEUjhA4CQ-1jA3kLVfr2cQc8rRD5b486R5XvC4xBlZNFFP7Fm5if4khhAJC-JnnYWgmytPM4Q7mOWatRr08wQmmfQDDrw53IseNA-yKnwHYlJ6ChU_UtNzS0OipUapQ".to_string(),
				e: "AQAB".to_string(),
				}
			),
		}
		],
	}
	});

	#[tokio::test]
	async fn test_golden_path() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let jwks = DEFAULT_JWKS.clone();

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.and(header("user-agent", "SurrealDB"))
			.respond_with(response)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		// Get first token configuration from remote location
		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(res.is_ok(), "Failed to validate token the first time: {:?}", res.err());

		// Drop server to force usage of the local cache
		drop(mock_server);

		// Get second token configuration from local cache
		let res = config(
			&ds,
			"test_2",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(res.is_ok(), "Failed to validate token the second time: {:?}", res.err());
	}

	#[tokio::test]
	async fn test_capabilities_default() {
		let ds = Datastore::new("memory").await.unwrap();
		let jwks = DEFAULT_JWKS.clone();

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.expect(0)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		// Get token configuration from unallowed remote location
		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(res.is_err(), "Unexpected success validating token from unallowed remote location");
	}

	#[tokio::test]
	async fn test_capabilities_specific_port() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1:443").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let jwks = DEFAULT_JWKS.clone();

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.expect(0)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		// Get token configuration from unallowed remote location
		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(res.is_err(), "Unexpected success validating token from unallowed remote location");
	}

	#[tokio::test]
	async fn test_cache_expiration() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let jwks = DEFAULT_JWKS.clone();

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.expect(2)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		// Get token configuration from remote location
		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(res.is_ok(), "Failed to validate token the first time: {:?}", res.err());

		// Wait for cache to expire
		std::thread::sleep((*CACHE_EXPIRATION + Duration::seconds(1)).to_std().unwrap());

		// Get same token configuration again after cache has expired
		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(res.is_ok(), "Failed to validate token the second time: {:?}", res.err());

		// The server will panic if it does not receive exactly two expected
		// requests
	}

	#[tokio::test]
	async fn test_cache_cooldown() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let jwks = DEFAULT_JWKS.clone();

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.expect(1)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		// Use token with invalid key identifier claim to force cache refresh
		let res = config(
			&ds,
			"invalid",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(res.is_err(), "Unexpected success validating token with invalid key identifier");

		// Use token with invalid key identifier claim to force cache refresh again
		// before cooldown
		let res = config(
			&ds,
			"invalid",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(res.is_err(), "Unexpected success validating token with invalid key identifier");

		// The server will panic if it receives more than the single expected
		// request
	}

	#[tokio::test]
	async fn test_cache_expiration_remote_down() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let jwks = DEFAULT_JWKS.clone();

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.up_to_n_times(1) // Only respond the first time
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		// Get token configuration from remote location
		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(res.is_ok(), "Failed to validate token the first time: {:?}", res.err());

		// Wait for cache to expire
		std::thread::sleep((*CACHE_EXPIRATION + Duration::seconds(1)).to_std().unwrap());

		// Get same token configuration again after cache has expired
		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(
			res.is_err(),
			"Unexpected success validating token with an expired cache and remote down"
		);
	}

	#[tokio::test]
	async fn test_no_algorithm() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let mut jwks = DEFAULT_JWKS.clone();
		jwks.keys[0].common.key_algorithm = None;

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(
			res.is_ok(),
			"Failed to validate token with key that does not specify algorithm: {:?}",
			res.err()
		);
	}

	#[tokio::test]
	// An attacker can issue token indicating that it has been signed with an HMAC
	// algorithm If the original issuer was trusted using RSA, this may allow the
	// attacker to sign the token with the public key This test verifies that
	// SurrealDB will not trust a token specifying an algorithm that does not match
	// the key type Reference: https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/#RSA-or-HMAC
	async fn test_no_algorithm_invalid() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();

		let mut jwks = DEFAULT_JWKS.clone();
		jwks.keys[0].common.key_algorithm = None;

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			// The token is signed using HMAC
			jsonwebtoken::Algorithm::HS256,
		)
		.await;
		assert!(
			res.is_err(),
			"Unexpected success validating token signed with algorithm that does not match the defined key type"
		);
	}

	#[tokio::test]
	async fn test_unsupported_algorithm() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let mut jwks = DEFAULT_JWKS.clone();
		jwks.keys[0].common.key_algorithm = Some(KeyAlgorithm::RSA_OAEP_256);

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(
			res.is_err(),
			"Unexpected success validating token with key specifies an unsupported algorithm"
		);
	}

	#[tokio::test]
	async fn test_no_key_use() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let mut jwks = DEFAULT_JWKS.clone();
		jwks.keys[0].common.public_key_use = None;

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(
			res.is_ok(),
			"Failed to validate token with key that does not specify use: {:?}",
			res.err()
		);
	}

	#[tokio::test]
	async fn test_key_use_enc() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let mut jwks = DEFAULT_JWKS.clone();
		jwks.keys[0].common.public_key_use = Some(jsonwebtoken::jwk::PublicKeyUse::Encryption);

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(
			res.is_err(),
			"Unexpected success validating token with key that only supports encryption"
		);
	}

	#[tokio::test]
	async fn test_key_ops_encrypt_only() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let mut jwks = DEFAULT_JWKS.clone();
		jwks.keys[0].common.key_operations = Some(vec![jsonwebtoken::jwk::KeyOperations::Encrypt]);

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200).set_body_json(jwks);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(
			res.is_err(),
			"Unexpected success validating token with key that only supports encryption"
		);
	}

	#[tokio::test]
	async fn test_remote_down() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(500);
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.mount(&mock_server)
			.await;

		let url = mock_server.uri();

		// Get token configuration from remote location responding with Internal Server
		// Error
		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(
			res.is_err(),
			"Unexpected success validating token configuration with unavailable remote location"
		);
	}

	#[tokio::test]
	#[cfg(not(target_family = "wasm"))]
	async fn test_remote_timeout() {
		let ds = Datastore::builder()
			.with_capabilities(Capabilities::default().with_network_targets(
				Targets::<NetTarget>::Some([NetTarget::from_str("127.0.0.1").unwrap()].into()),
			))
			.build_with_path("memory")
			.await
			.unwrap();
		let jwks = DEFAULT_JWKS.clone();

		let jwks_path = format!("{}/jwks.json", random_path());
		let mock_server = MockServer::start().await;
		let response = ResponseTemplate::new(200)
			.set_body_json(jwks)
			.set_delay((*REMOTE_TIMEOUT + Duration::seconds(10)).to_std().unwrap());
		Mock::given(method("GET"))
			.and(path(&jwks_path))
			.respond_with(response)
			.expect(1)
			.mount(&mock_server)
			.await;
		let url = mock_server.uri();

		let start_time = Utc::now();
		// Get token configuration from remote location responding very slowly
		let res = config(
			&ds,
			"test_1",
			&format!("{}/{}", &url, &jwks_path),
			jsonwebtoken::Algorithm::RS256,
		)
		.await;
		assert!(
			res.is_err(),
			"Unexpected success validating token configuration with unavailable remote location"
		);
		assert!(
			Utc::now() - start_time < *REMOTE_TIMEOUT + Duration::seconds(1),
			"Remote request was not aborted immediately after timeout"
		);
	}
}