Skip to content

Commit 7aea049

Browse files
authored
Clarify Token secrecy in docs (#5768)
1 parent 8f2770e commit 7aea049

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

lib/phoenix/token.ex

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
defmodule Phoenix.Token do
22
@moduledoc """
3-
Tokens provide a way to generate and verify bearer
4-
tokens for use in Channels or API authentication.
5-
6-
The data stored in the token is signed to prevent tampering
7-
but not encrypted. This means it is safe to store identification
8-
information (such as user IDs) but should not be used to store
9-
confidential information (such as credit card numbers).
3+
Conveniences to sign/encrypt data inside tokens
4+
for use in Channels, API authentication, and more.
5+
6+
The data stored in the token is signed to prevent tampering, and is
7+
optionally encrypted. This means that, so long as the
8+
key (see below) remains secret, you can be assured that the data
9+
stored in the token has not been tampered with by a third party.
10+
However, unless the token is encrypted, it is not safe to use this
11+
token to store private information, such as a user's sensitive
12+
identification data, as it can be trivially decoded. If the
13+
token is encrypted, its contents will kept secret from the client,
14+
but it is still a best practice to encode as little secret
15+
information as possible, to minimize the impact of key leakage.
1016
1117
## Example
1218
@@ -24,7 +30,8 @@ defmodule Phoenix.Token do
2430
`endpoint`. We guarantee the token will only be valid for one day
2531
by setting a max age (recommended).
2632
27-
The first argument to both `sign/4` and `verify/4` can be one of:
33+
The first argument to `sign/4`, `verify/4`, `encrypt/4`, and
34+
`decrypt/4` can be one of:
2835
2936
* the module name of a Phoenix endpoint (shown above) - where
3037
the secret key base is extracted from the endpoint
@@ -37,10 +44,10 @@ defmodule Phoenix.Token do
3744
to provide adequate entropy
3845
3946
The second argument is a [cryptographic salt](https://en.wikipedia.org/wiki/Salt_(cryptography))
40-
which must be the same in both calls to `sign/4` and `verify/4`.
41-
For instance, it may be called "user auth" and treated as namespace
42-
when generating a token that will be used to authenticate users on
43-
channels or on your APIs.
47+
which must be the same in both calls to `sign/4` and `verify/4`, or
48+
both calls to `encrypt/4` and decrypt/4`. For instance, it may be
49+
called "user auth" and treated as namespace when generating a token
50+
that will be used to authenticate users on channels or on your APIs.
4451
4552
The third argument can be any term (string, int, list, etc.)
4653
that you wish to codify into the token. Upon valid verification,
@@ -131,7 +138,9 @@ defmodule Phoenix.Token do
131138
end
132139

133140
@doc """
134-
Encodes, encrypts, and signs data into a token you can send to clients.
141+
Encodes, encrypts, and signs data into a token you can send to
142+
clients. Its usage is identical to that of `sign/4`, but the data
143+
is extracted using `decrypt/4`, rather than `verify/4`.
135144
136145
## Options
137146
@@ -218,6 +227,8 @@ defmodule Phoenix.Token do
218227
@doc """
219228
Decrypts the original data from the token and verifies its integrity.
220229
230+
Its usage is identical to `verify/4` but for encrypted tokens.
231+
221232
## Options
222233
223234
* `:key_iterations` - option passed to `Plug.Crypto.KeyGenerator`

0 commit comments

Comments
 (0)