10
10
import PerfectLib
11
11
import PerfectHTTP
12
12
import PerfectSession
13
- import TurnstileCrypto
14
13
15
14
public class WebHandlers {
16
15
/* =================================================================================================================
17
16
Index
18
17
================================================================================================================= */
19
18
open static func indexHandlerGet( request: HTTPRequest , _ response: HTTPResponse ) {
20
19
21
- let rand = URandom ( )
22
-
23
- request. session. data [ rand. secureToken] = rand. secureToken
24
-
25
20
var dump = " "
26
21
do {
27
- dump = try request. session. data. jsonEncodedString ( )
22
+ dump = ( try request. session? . data. jsonEncodedString ( ) ) ?? " "
28
23
} catch {
29
24
dump = " \( error) "
30
25
}
31
- var body = " <p>Your Session ID is: <code> \( request. session. token) </code></p><p>Session data: <code> \( dump) </code></p> "
26
+ var body = " <p>Your Session ID is: <code> \( request. session? . token ?? " " ) </code></p><p>Session data: <code> \( dump) </code></p> "
32
27
body += " <p><a href= \" /withcsrf \" >CSRF Test Form</a></p> "
33
28
body += " <p><a href= \" /nocsrf \" >No CSRF Test Form</a></p> "
34
29
@@ -44,7 +39,7 @@ public class WebHandlers {
44
39
open static func CORSHandlerGet( request: HTTPRequest , _ response: HTTPResponse ) {
45
40
46
41
response. addHeader ( . contentType, value: " application/json " )
47
- try ? response. setBody ( json: [ " Success " : " CORS Request " ] )
42
+ let _ = try ? response. setBody ( json: [ " Success " : " CORS Request " ] )
48
43
response. completed ( )
49
44
50
45
}
@@ -57,7 +52,7 @@ public class WebHandlers {
57
52
================================================================================================================= */
58
53
open static func formNoCSRF( request: HTTPRequest , _ response: HTTPResponse ) {
59
54
60
- var body = " <p>Your Session ID is: <code> \( request. session. token) </code></p><form method= \" POST \" action= \" ? \" enctype= \" multipart/form-data \" > "
55
+ var body = " <p>Your Session ID is: <code> \( request. session? . token ?? " " ) </code></p><form method= \" POST \" action= \" ? \" enctype= \" multipart/form-data \" > "
61
56
body += " <p>No CSRF Form</p> "
62
57
body += " <p>NOTE: You should get a failed request because there is no CSRF</p> "
63
58
body += " <p><input type= \" text \" name= \" testing \" value= \" testing123 \" ></p> "
@@ -72,8 +67,8 @@ public class WebHandlers {
72
67
formWithCSRF
73
68
================================================================================================================= */
74
69
open static func formWithCSRF( request: HTTPRequest , _ response: HTTPResponse ) {
75
- let t = request. session. data [ " csrf " ] as? String ?? " "
76
- var body = " <p>Your Session ID is: <code> \( request. session. token) </code></p><form method= \" POST \" action= \" ? \" enctype= \" multipart/form-data \" > "
70
+ let t = request. session? . data [ " csrf " ] as? String ?? " "
71
+ var body = " <p>Your Session ID is: <code> \( request. session? . token ?? " " ) </code></p><form method= \" POST \" action= \" ? \" enctype= \" multipart/form-data \" > "
77
72
body += " <p>CSRF Form</p> "
78
73
body += " <p><input type= \" text \" name= \" testing \" value= \" testing123 \" ></p> "
79
74
body += " <p><input type= \" text \" name= \" _csrf \" value= \" \( t) \" ></p> "
@@ -88,8 +83,7 @@ public class WebHandlers {
88
83
formReceive
89
84
================================================================================================================= */
90
85
open static func formReceive( request: HTTPRequest , _ response: HTTPResponse ) {
91
- // print("in formReceive")
92
- var body = " <p>Your Session ID is: <code> \( request. session. token) </code></p> "
86
+ var body = " <p>Your Session ID is: <code> \( request. session? . token ?? " " ) </code></p> "
93
87
body += " <p>CSRF Test response</p> "
94
88
body += " <p>Params: \( request. postParams) </p> "
95
89
response. setBody ( string: header+ body+ footer)
0 commit comments