You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 2, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+27-27
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ Another hard limit is the size of a session: for Cookies it is restricted to 4kB
30
30
As long as you provide a serializer for your custom types, you can use them.
31
31
Standard types like `String`, `Integer`, `Long`, `Double` and `Map<String, String>` are supported out of the box.
32
32
33
-
[Here's an example](https://github.com/softwaremill/akka-http-session-faq/blob/master/src/main/java/session/data_types/StringTypeSession.java) with a `String` data type session:
33
+
[Here's an example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/data_types/StringTypeSession.java) with a `String` data type session:
34
34
```
35
35
$ curl -i --data "a string type" http://localhost:8080/api/do_login
36
36
@@ -43,7 +43,7 @@ Content-Length: 2
43
43
44
44
ok
45
45
```
46
-
[Here's an example](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/data_types/LongTypeSession.java) with a `Long` data type session:
46
+
[Here's an example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/data_types/LongTypeSession.java) with a `Long` data type session:
[Here's an example](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/data_types/MapTypeSession.java) with a `Map<String, String>` data type session:
59
+
[Here's an example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/data_types/MapTypeSession.java) with a `Map<String, String>` data type session:
[Here's an example](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/data_types/CustomTypeSession) with a `CustomType` data type session:
83
+
[Here's an example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/data_types/CustomTypeSession) with a `CustomType` data type session:
### How can I transport the session between server and client?
99
99
Two transport types are available: Cookies and Headers.
100
100
101
-
### Why would I use Cookies? {#cookies}
101
+
### <aname="cookies"></a>Why would I use Cookies?
102
102
Using Cookies to send session data has the advantage that is is handled automatically by client applications, like a web browser.
103
103
Also Cookies do not require you to implement a storage, since it's built-in into the browser already.
104
104
105
-
The [Cookie transport example](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/transport/CookieTransport.java) shows a typical setup for Cookies. Below is a sample use case:
105
+
The [Cookie transport example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/transport/CookieTransport.java) shows a typical setup for Cookies. Below is a sample use case:
106
106
107
107
```
108
108
$ curl -i http://localhost:8080/api/current_login
@@ -140,14 +140,14 @@ Content-Length: 8
140
140
141
141
my_login
142
142
```
143
-
### Why would I use Headers? {#headers}
143
+
### <aname="headers"></a>Why would I use Headers?
144
144
145
145
Headers are usually used in a non-Cookie world, like mobile.
146
146
You need to come up with a storage for the session data on your client application by yourself.
147
147
This is what browsers do for you when dealing with Cookies.
148
148
Additionally when using refresh tokens, they need to be persisted in that storage as well.
149
149
150
-
Take a look at [the Header transport example](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/transport/HeaderTransport.java) and how it works:
150
+
Take a look at [the Header transport example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/transport/HeaderTransport.java) and how it works:
151
151
152
152
```
153
153
$ curl -i http://localhost:8080/api/current_login
@@ -187,10 +187,10 @@ Content-Length: 8
187
187
my_login
188
188
```
189
189
190
-
### How long does a session live? {#max-age}
190
+
### <aname="max-age"></a>How long does a session live?
191
191
By default a session expires after 1 week, configurable via the `akka.http.session.max-age` config property.
192
-
[In this example](https://github.com/akka-http-session-faq/blob/master/src/main/resources/application.com) it is set to 5 minutes.
193
-
When running the [HeaderTransport example](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/transport/HeaderTransport.java) we get:
192
+
[In this example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/resources/application.conf) it is set to 5 minutes.
193
+
When running the [HeaderTransport example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/transport/HeaderTransport.java) we get:
@@ -210,7 +210,7 @@ The timestamp attached to the session is `1505470200180` and translates to `Fri
210
210
There is the `OneOff` session which when expired is of no use anymore.
211
211
An alternative is the `Refreshable` session.
212
212
It will provide the client with an additional `_refreshtoken` Cookie or `Set-Refresh-Token` Header, dependent on the transport type.
213
-
The [RefreshableSession](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/continuity/RefreshableSession.java) example shows how to use this type of sessions:
213
+
The [RefreshableSession](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/continuity/RefreshableSession.java) example shows how to use this type of sessions:
A refreshable session is typically used for "remember-me" functionality.
267
267
This is especially useful in mobile applications, where you log in once, and the session is remembered for a long time.
268
-
Make sure to adjust the `akka.http.session.refresh-token.max-age` config property in [application.conf](https://github.com/akka-http-session-faq/blob/master/src/main/resources/application.conf) which defaults to 1 month.
268
+
Make sure to adjust the `akka.http.session.refresh-token.max-age` config property in [application.conf](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/resources/application.conf) which defaults to 1 month.
269
269
Also, these sessions are persisted.
270
270
Although the default implementation stores the refresh tokens in-memory and they won't survive a server restart,
271
271
they still can be thought of a way of storing session details on the server side.
@@ -301,7 +301,7 @@ The supplied authentication is not authorized to access this resource
301
301
302
302
### Is the additional refresh token persistent?
303
303
Yes. Therefore using refreshable sessions requires you to implement a storage for these tokens.
304
-
An in-memory storage is provided as shown in the [RefreshableSession](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/continuity/RefreshableSession.java) example.
304
+
An in-memory storage is provided as shown in the [RefreshableSession](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/continuity/RefreshableSession.java) example.
305
305
However using an in-memory database will invalidate all your refresh tokens when the server restarts.
306
306
307
307
In this example a refresh token is issued and before the second request is sent, the server is restarted:
@@ -341,9 +341,9 @@ This can be either `OneOff` or `Refreshable`.
341
341
### Can a Cookie be stolen and be reused by an attacker?
342
342
Yes.
343
343
344
-
### How can I use Cookies in a secure way? {#secure-cookie}
344
+
### <aname="secure-cookie"></a>How can I use Cookies in a secure way?
345
345
1. Use the `invalidateSession` directive when a user logs out or doesn't need that session any longer.
346
-
This is demonstrated by the `do_logout` route in the [CookieTransport example](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/transport/CookieTransport.java).
346
+
This is demonstrated by the `do_logout` route in the [CookieTransport example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/transport/CookieTransport.java).
347
347
Its purpose is to send an empty `_sessiondata` Cookie to the client (typically a browser).
348
348
In consequence, the browser should erase that Cookie to prevent an attacker to read the cookie later on.
349
349
```
@@ -364,7 +364,7 @@ It's still possible, that although the browser erased the Cookie, the attacker g
364
364
2. Use a sensible `max-age` value which defaults to 7 days
365
365
For use cases where it make sense, set the `max-age` property to a low value, for example `5 minutes`.
366
366
This is especially true, when your application allows to access sensitive data, like bank accounts, emails, etc.
367
-
The `max-age` property is set in `application.conf`, like in [this example](https://github.com/akka-http-session-faq/blob/master/src/main/resources/application.conf).
367
+
The `max-age` property is set in `application.conf`, like in [this example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/resources/application.conf).
368
368
Using a cookie which expired on `Thu Sep 14 2017 07:38:19` results in a `403` response, when used in a request at `07:39:43`:
@@ -485,7 +485,7 @@ The supplied authentication is not authorized to access this resource
485
485
486
486
### What does encryption provide me with?
487
487
Enabling session data encryption allows to send data in a format that is not readable by the client.
488
-
To enable session data encryption set the `akka.http.session.encrypt-data` config property in `application.conf`, like in [this resource file](https://github.com/akka-http-session-faq/blob/master/src/main/resources/application.conf).
488
+
To enable session data encryption set the `akka.http.session.encrypt-data` config property in `application.conf`, like in [this resource file](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/resources/application.conf).
489
489
As seen below, the client received a token, that can be used to access the secured resource, but the data itself is encrypted.
490
490
491
491
```
@@ -536,7 +536,7 @@ This directive is responsible for extracting the session result to be used furth
536
536
A session result can be `Decoded` (valid), `CreatedFromToken`, `Expired`, `Corrupt` or having no token present `TokenNotFound`
537
537
The directive does not require the client to provide a session.
538
538
However if a session is present, a result is made available to the server for further processing.
539
-
[This example](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/directives/SessionDirective.java) shows two possibilities, either there is no session at all, or the session is present.
539
+
[This example](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/directives/SessionDirective.java) shows two possibilities, either there is no session at all, or the session is present.
A more detailed example is available in [VariousSessions.java](https://github.com/softwaremill/akka-http-session/blob/master/example/src/main/java/com/softwaremill/example/session/VariousSessionsJava.java).
577
+
A more detailed example is available in [VariousSessions.java](https://github.com/softwaremill/akka-http-session/tree/master/example/src/main/java/com/softwaremill/example/session/VariousSessionsJava.java).
578
578
579
579
### What is the `invalidateSession` directive good for?
580
580
This directive instructs the client to clean the Cookie or Header.
@@ -585,21 +585,21 @@ Take a look at [How can I use Cookies in a secure way?](#secure-cookie) and [Eve
585
585
This one is very similar to the `session` directive.
586
586
In this case however, we get access to the session details, which is an `Optional`.
587
587
Based on that we can decide on the server side, how to proceed.
588
-
In the [OptionalSession](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/directives/OptionalSession.java) example, we either reply with `no session` or with the session details, if present.
588
+
In the [OptionalSession](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/directives/OptionalSession.java) example, we either reply with `no session` or with the session details, if present.
589
589
590
590
### What is the `requiredSession` directive good for?
591
591
This directive is used to secure endpoints.
592
592
Submitting a HTTP request requires the client to provide a valid session.
593
593
A session can be requested through an endpoint configured with the `setSession` directive.
594
-
The [CookieTransport](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/transport/CookieTransport.java) and [HeaderTransport](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/transport/HeaderTransport.java) examples show how to use this directive.
594
+
The [CookieTransport](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/transport/CookieTransport.java) and [HeaderTransport](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/transport/HeaderTransport.java) examples show how to use this directive.
595
595
Also a sample use case of securing an endpoint is shown in [the Cookie](#cookies) and [the Header](#headers) transport example.
596
596
597
597
### What is the `touchRequiredSession` directive good for?
598
598
Sessions do expire and the max age is configurable, as mentioned in [How long does a session live?](#max-age).
599
599
If you want to expose an endpoint that will reset the expiry date, include the `touchRequiredSession` in the route chain.
600
600
Besides the expiry date, the whole token will change.
601
601
Again when using Cookies, the browser will handle this, and replace the old Cookie, but in your own client application you have to replace the Cookie or Header by yourself.
602
-
This directive is demonstrated in the [TouchRequiredSession](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/directives/TouchRequiredSessionDirective.java) example:
602
+
This directive is demonstrated in the [TouchRequiredSession](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/directives/TouchRequiredSessionDirective.java) example:
@@ -660,7 +660,7 @@ In this case the session expired on `1505741661309`, which is `Mon Sep 18 2017 1
660
660
## JWT
661
661
### How do I use Json Web Tokens
662
662
In case you want to use the JWT format for authorization tokens, replace the `BasicSessionEncoder` with the `JwtSessionEncoder` and choose one of the JWT serializers, like `JwtSessionSerializers.StringToJValueSessionSerializer`.
663
-
The [JwtEncodedSession](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/jwt/JwtEncodedSession.java) example shows this particular use case:
663
+
The [JwtEncodedSession](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/jwt/JwtEncodedSession.java) example shows this particular use case:
@@ -707,7 +707,7 @@ But the attacker is not able to read the cookie value. Hence the attacker is not
707
707
708
708
### How do I enable CSRF protection
709
709
Two directives, `randomTokenCsrfProtection` and `setNewCsrfToken`, are required.
710
-
The example [CsrfProtection](https://github.com/akka-http-session-faq/blob/master/src/main/java/session/csrf/CsrfProtection.java) uses both directives.
710
+
The example [CsrfProtection](https://github.com/softwaremill/akka-http-session-faq/tree/master/src/main/java/session/csrf/CsrfProtection.java) uses both directives.
711
711
Finally a `CheckHeader` component is required to intercept the route and lookup the header for the CSRF token.
712
712
713
713
The first one, `randomTokenCsrfProtection`, sets a new CSRF token on every GET request in form of a `XSRF-TOKEN` Cookie.
0 commit comments