Skip to content

Commit 2fb765c

Browse files
committed
Test use case for cookie authentication
1 parent 6f86823 commit 2fb765c

13 files changed

Lines changed: 547 additions & 8171 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ _local
44
*~
55
*.merlin
66
*.install
7+
*.exe
8+
.ocamlformat
9+
test/session/*.js

.ocamlformat

Lines changed: 0 additions & 6 deletions
This file was deleted.

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ clean:
1616
doc:
1717
@dune build @doc
1818
@rsync -ru _build/default/_doc/_html/* docs/
19+
20+
build-tests:
21+
@opam install ocurl websocket-lwt-unix js_of_ocaml
22+
@dune build test
23+

src/session/ezSession.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ module TYPES = struct
3333
(** Associated to user information *)
3434
type user_info
3535

36-
(** Web host, that should be used in access control headers. *)
37-
val web_host : string
36+
(** Web host, that should be used in access control headers, if specified. If web_host isn't specified,
37+
then acces-control header in response will be set to '*' and authentication wwith cookies wouldn't
38+
work.
39+
Note : Cookies would be set by browser only if request's flag 'with_credentials' is set to true.
40+
Last one in turn, requires that "Access-control_allow_origin" header by reponse returns something
41+
different from "*". *)
42+
val web_host : string option
3843

3944
(** Json encoding for user's id *)
4045
val user_id_encoding : user_id Json_encoding.encoding
@@ -374,7 +379,8 @@ module Make(S : SessionArg) = struct
374379

375380
let access_control =
376381
[ "access-control-allow-credentials", "true";
377-
"access-control-allow-origin", S.web_host ]
382+
"access-control-allow-origin",
383+
match S.web_host with None -> "*" | Some origin -> origin ]
378384

379385

380386
(** Connection service that requires authentication token. For more details, see corresponding

test/session/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
CLIENT_BIN_PATH=../../_build/default/test/session/test_cookie_client.bc.js
3+
SERVER_BIN_PATH=../../_build/default/test/session/test_cookie_server.exe
4+
5+
all: build website api-server
6+
7+
build:
8+
dune build --profile release
9+
10+
website: $(CLIENT_BIN_PATH)
11+
cp -f $(CLIENT_BIN_PATH) client.js
12+
13+
api-server: $(SERVER_BIN_PATH)
14+
cp -f $(SERVER_BIN_PATH) server.exe
15+
16+
run-web: website
17+
php -S localhost:8885
18+
19+
run-api: api-server
20+
./server.exe

test/session/client.js

Lines changed: 0 additions & 8161 deletions
This file was deleted.

test/session/dune

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(library
2+
(name test_session_lib)
3+
(modules test_session_lib)
4+
(libraries ez_api ez_api.sha2 ez_api.session))
5+
6+
(executable
7+
(name test_cookie_client)
8+
(modules test_cookie_client)
9+
(preprocess (pps js_of_ocaml-ppx))
10+
(libraries js_of_ocaml test_session_lib ez_api.session_client ez_api.icoxhr)
11+
(modes js)
12+
(js_of_ocaml
13+
(flags
14+
(:standard --no-sourcemap))))
15+
16+
(executable
17+
(name test_cookie_server)
18+
(modules test_cookie_server)
19+
(libraries test_session_lib ez_api.server_session ez_api.server))

test/session/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<html>
2+
3+
<head>
4+
<title>Test cookie session</title>
5+
<script src="client.js" defer></script>
6+
</head>
7+
8+
<body>
9+
<div>
10+
<div>
11+
<h2>Connection status</h2>
12+
<input id="submit-connection" value="Login" type="button">
13+
<p id="connection-status"></p>
14+
</div>
15+
<div>
16+
<h2>Api services to check (check console for results)</h2>
17+
<h3>Without authentication</h3>
18+
<div>
19+
<input id="test1" value="Test 1" type="button" />
20+
<input id="test1'" value="Test 1'" type="button" />
21+
<input id="test2" value="Test 2" type="button" />
22+
<input id="test2'" value="Test 2'" type="button" />
23+
<input id="test3" value="Test 3" type="button" />
24+
</div>
25+
<h3>With authentication</h3>
26+
<div><input id="test4" value="Test 4" type="button" /></div>
27+
28+
</div>
29+
</div>
30+
</body>
31+
32+
</html>

test/session/server.exe

-10.5 MB
Binary file not shown.

0 commit comments

Comments
 (0)