Every asfquart application has a secret, a random string that only the application knows. It is used to sign the session cookie, so that a session cannot be read or made up by anyone else. asfquart creates the secret, keeps it in a file, and reads it back when the application starts. Most applications never have to think about it.
It is available as app.secret_key once the application has been constructed.
By default the secret is in apptoken.txt, in the application's directory (the app_dir argument of asfquart.construct(), or the directory the application was started in).
secrets.token_hex(), 256 random bits) and written to the file. The file is created with mode 0600, so that only the user the application runs as can read it.0600 before the secret goes in. (A file can be left empty if the application is stopped just as it creates it.)Session permanence cannot be guaranteed!. See “Without a token file” below for what that means.0600, a warning is written to standard error, and the file is used all the same. Fix the mode: anybody who can read the file can forge a session.The token_file argument puts the file somewhere else. A relative path is taken from the application's directory, and an absolute path is used as it is:
app = asfquart.construct("myapp", token_file="secrets/apptoken.txt")
The file's content is used exactly as it is, including a newline at the end if there is one. Leave the file as asfquart wrote it. If you must copy a secret from one host to another, copy the file, and do not paste its content into an editor.
Keep the file out of version control and out of backups that are less well protected than the application itself.
With a token file, restarting the application changes nothing: the same secret is read back, and the cookies that users already have are still accepted.
An asfquart application runs as one process: one instance, with one worker. Sharing a token file does not make it safe to run several. Logins in progress are kept in the memory of the process that started them (see Multi-instance limitation), and the token file is not created or replaced in a way that is safe for two processes that start at the same moment: each can end up with a secret of its own, and refuse the other's session cookies. See issue 160.
token_file=None tells asfquart not to keep the secret in a file:
app = asfquart.construct("myapp", token_file=None)
A new secret is then generated every time the application starts. Every session cookie from before a restart is refused, and everybody has to log in again. That suits tests, and applications that supply the secret themselves, as described below. It does not suit an application that should keep its users logged in across a restart.
To replace the secret:
Every session cookie that was signed with the old secret is refused from then on, and users log in again. Nothing else is lost. An application with a session store (app.sessions) still has its stored sessions, but no browser holds a cookie that leads to them any more, so they are of no use and can be cleared out.
Replace the secret at once if it may have been disclosed, for instance if the token file was readable by others or has ended up in a repository or a backup. Whether to replace it on a schedule as well is for the application's own policy to say.
An application can keep the secret somewhere of its own, a secrets manager for instance, or alongside other data that has to be reset together with the sessions. Construct the application without a token file, and set app.secret_key before the first request:
app = asfquart.construct("myapp", token_file=None) app.secret_key = load_my_secret() # the same value every time, from wherever it is kept
The secret is then the application's to look after:
secrets.token_hex() gives, and never a word or a phrase.The other way round also works: let asfquart generate the secret, and read app.secret_key after asfquart.construct() to store a copy of it.
app.secret_key is the same thing as the SECRET_KEY entry of app.config. Anything that loads a whole configuration into app.config can therefore replace the secret, and app.config.from_object() does so even when the object's SECRET_KEY is None. The application is then left with no secret, and fails with The session is unavailable because no secret key was set as soon as a session is written.
Keep the secret across such a call:
app = asfquart.construct("myapp") secret_key = app.secret_key app.config.from_object(MySettings) app.secret_key = secret_key
asfquart's own settings, in config.yaml, are kept in app.cfg and do not touch the secret.