asfquart will, by default, set up an OAuth endpoint at /auth. Unauthenticated access to any restricted end-point will automatically trigger a redirect to the OAuth workflow and redirect back to the restricted end-point once successful.
You can tailor these automatic behavior to suit your need, as shown in this example:
import asfquart # Construct an app, with auto OAuth at /my_oauth app = asfquart.construct("myapp", oauth="/my_oauth") # Make another app, but do not enable oauth nor force login redirect (implied by no oauth) otherapp = asfquart.construct("otherapp", oauth=False) # Make a third app, enable oauth at /auth, but do not force logins thirdapp = asfquart.construct("thirdapp", oauth="/auth", force_login=False)
A login is a round trip: the browser visits /auth?login (optionally /auth?login=/foo), is redirected to the ASF OAuth provider, and comes back to /auth?state=...&code=.... asfquart only accepts the callback if it is consistent with the request that started the login. The rules are implemented in setup_oauth() in src/asfquart/generics.py.
On /auth?login or /auth?login=/foo, asfquart:
Rejects the redirect target with 400 Invalid redirect URI. unless it is a local path:
/ and does not start with //,/\example.org as //example.org,Refresh header, and because browsers drop tabs and newlines from a URL. This covers every character in a Unicode “C” category (control, format, private use, unassigned), and the line and paragraph separators U+2028 and U+2029,asfquart.generics.MAX_REDIRECT_URI_LENGTH), because the target of a login is kept in memory until the login completes or expires.The same rule applies to the target of /auth?logout=/foo.
Generates a random state value (secrets.token_hex(16)).
Records, in pending_states[state]:
Stores the browser identifier in the asfquart-oauth-state cookie. If the browser already has this cookie, the existing value is reused. The cookie is HttpOnly, Secure, SameSite=Lax, scoped to the OAuth endpoint path, and expires after the workflow timeout.
Redirects the browser to the OAuth provider with the state and the HTTPS callback URL.
A callback must carry both code and state. It is accepted only if all of the following hold:
pending_states as it is looked up, so each state can be used at most once. Unknown, forged, or replayed states are rejected.asfquart-oauth-state cookie sent with the callback must match the digest stored with the state. The comparison uses hmac.compare_digest. This prevents a login started in one browser from being completed in another (login CSRF).workflow_timeout seconds of the login starting (900 seconds, i.e. 15 minutes, by default).Because the state is removed before the other checks run, a callback that fails any rule also invalidates the state; the user has to start a new login.
If rule 1, 2 or 3 fails, the response is the same in every case, so the client cannot tell which check failed:
403 Invalid or expired OAuth state provided. OAuth workflows must be completed within 900 seconds.
Once the rules pass, the code is exchanged with the OAuth provider, in the body of a POST (see the external services page). The exchange is time limited (asfquart.generics.OAUTH_TIMEOUT): 10 seconds to connect, 15 seconds between reads, and 30 seconds in total. The connection to the provider must use TLS 1.2 or later, and the provider's certificate must be valid and match its host name.
The session is only created if the provider answers 200 with a JSON object whose uid is a string in the form of an ASF user id (asfquart.generics.OAUTH_UID_RE: lower case letters, digits, - and _, starting with a letter, at least two characters). In every other case (the provider cannot be reached, is too slow, refuses the code, or sends something else) nothing is stored and the response is 403 OAuth authentication failed.
The response to the user is the same in each of those cases. What went wrong is logged as a warning by the asfquart.generics logger (OAuth code exchange failed: ...): the kind of error if the provider could not be reached, the status if it did not answer 200, or which check its response failed. The log does not have the code, the state, or the response.
After a successful exchange the browser is redirected to the recorded target (using a Refresh header rather than a 30x response, so that SameSite session cookies are kept).
Any response to a URL on the OAuth endpoint with a code in it is sent with Referrer-Policy: no-referrer, so the code isn't passed on in a Referer header.
workflow_timeout is a parameter of asfquart.generics.setup_oauth(). asfquart.construct() uses the default of 900 seconds; to change it, construct the app with oauth=False and call setup_oauth() yourself (plus enforce_login() if you want the automatic login redirect that construct() would otherwise set up):
import asfquart import asfquart.generics app = asfquart.construct("myapp", oauth=False) asfquart.generics.setup_oauth(app, uri="/auth", workflow_timeout=300) asfquart.generics.enforce_login(app, redirect_uri="/auth")
callback_host is a parameter of setup_oauth() that pins the host the OAuth provider sends the browser back to. Without it, the callback URL is built from the request's Host header, so it relies on the proxy in front of the app passing that header through unchanged (ProxyPreserveHost On for httpd). The ASF provider only accepts pre-approved callback URLs, which limits what a forged header can do, but setting this removes the dependency.
Its default is asfquart.generics.OAUTH_CALLBACK_HOST, so an app that uses asfquart.construct() can set that instead:
import asfquart import asfquart.generics asfquart.generics.OAUTH_CALLBACK_HOST = "https://myapp.apache.org" app = asfquart.construct("myapp")
It must be https:// and a host name (with a port if needed), and nothing else - a bad value raises a ValueError when the app is set up. IP address literals in brackets, such as https://[::1], aren't accepted. Users have to log in on that host, as the cookie that ties a login to the browser is set for the host the login started on.
asfquart.generics.STATE_COOKIE_SAMESITE defaults to "Lax". This is required when the OAuth provider is on a different site from the app, because the callback is a top-level navigation from the provider and browsers do not send Strict cookies on it. Deployments where the provider and the app share a site may set it to "Strict".
asfquart does not keep an audit trail of logins. It logs a warning when the exchange with the OAuth provider fails, so that the failure can be diagnosed (see above), but it does not log successful logins, or who logged in from where. Which decisions to record, in what format, and where the record goes are for the application to decide.
There are two places where an application can record them. Use the session store if the application has one, and the hook for what the store does not see.
| Session store | after_request hook | |
|---|---|---|
| Needs | Server-side sessions (app.sessions) | Nothing |
| Successful logins | Yes | Yes |
| Logouts | Yes | No |
| Failed logins | No: they never reach the store | Yes |
| How it knows | asfquart calls it once the login has succeeded | It works it out from the request and the response |
An application that keeps its sessions in a store of its own (app.sessions) is told about each login: asfquart calls the store's create() when the OAuth callback has succeeded, and not before. It calls destroy() when the user logs out. Those two methods, in the store that the application already has, are where to log:
log = logging.getLogger("myapp.audit") class Store: async def create(self, hsid, session_data): ... # save the session, as the store does now log.info("login succeeded: uid=%s remote=%s", session_data.get("uid"), quart.request.remote_addr) async def destroy(self, hsid): session = ... # find the session and delete it, as the store does now if session is not None: log.info("logout: uid=%s", session.uid)
This shows where the two log lines go, and nothing more. It is not a store to copy: the rest of a store (validate(), and replace() or register() if the application uses asfquart.session.areplace()) is left out, and is the application's own.
What to know about it:
create() is called, so a record of a login cannot be made up by sending the application a request. That is the advantage over the hook below.create() has returned, so an error raised after saving leaves a session in the store that no cookie leads to. That is why the line above reads the user id with session_data.get("uid"), and not with session_data["uid"].create() is called by asfquart.session.awrite(). The OAuth callback calls that after a successful login, and so does the application whenever its own code calls awrite(). If the application changes a session that way, the old session is destroyed and a new one created, which this logging would record as a logout and a login.asfquart.session.areplace() changes a session without calling create(). What it does call depends on the store. If the store has a replace() method, that is all it calls, and nothing is logged. If it has not, it calls destroy() for the old session and then register() for the new one, and this logging would record a logout that did not happen. A store with neither method cannot be used with areplace() at all.destroy() is also called for the previous session of a user who logs in again without logging out first. This logging then records a logout followed by a login.hsid. It is a hash of the session id, but it is still what finds the session.quart.request.remote_addr is the address of the reverse proxy when there is one. The address of the client is then in a header such as X-Forwarded-For, which is only to be trusted in the conditions set out at the end of the next section; otherwise it is whatever the client sent.after_request hookAn application without a session store, where the session is kept in the cookie, has no create() to log in. It can record logins with an after_request hook, which also sees the logins that fail. An application that logs successful logins in its store, and adds this hook for the failed ones, should leave the log.info line out of the hook, or each successful login is recorded twice.
import logging import asfquart import quart app = asfquart.construct("myapp") log = logging.getLogger("myapp.audit") @app.after_request async def log_logins(response): request = quart.request args = request.args # Only the callback from the OAuth provider is a login decision. The OAuth endpoint treats a # request as the callback when it has a code and a state, neither of them empty, and is not # a login or logout request. Anything else must not be logged as a login: with an empty code, # for instance, the endpoint shows the session of a user who is logged in already. is_callback = ( request.path == "/auth" and args.get("code") and args.get("state") and not args.get("login") and not args.get("logout") ) if is_callback: # The callback answers 200 only when it has created the session. Read it back all the # same, and count the login as failed if it is not there. session = await asfquart.session.read() if response.status_code == 200 else None if session is not None: log.info("login succeeded: uid=%s remote=%s", session.uid, request.remote_addr) else: log.warning("login failed: status=%s remote=%s", response.status_code, request.remote_addr) return response
How this works:
code and a state, neither of them empty, and without login or logout. Every other request to the endpoint (starting a login, logging out, showing the current session) is not a login decision. The test for the callback has to be this exact: anybody can send any query string, and /auth?code=&state= from a user who is logged in already answers 200 with their session, which a looser test would record as a login that never happened.POST as well as by GET, and a login that is completed by POST is still a login. A hook that recorded GET requests only would let one go by unrecorded.asfquart.session.read() without naming an app, and that is deliberate. The OAuth endpoint writes the session for asfquart.APP, the application that was constructed last, and read() with no app looks in the same place. A process is expected to hold one application, and then there is nothing to choose. If a process does construct more than one, passing the hook's own app to read() would look for the session where the endpoint did not put it, and a login that worked would be recorded as one that failed.200 when the login succeeded, and by then the session has been created, so the hook can read the user id from it. The hook still allows for the session not being there, and then records a failed login, so that it cannot raise an error of its own on a response that was otherwise fine. The callback answers 403 when the login was refused: the state was unknown, expired, already used or from another browser, or the OAuth provider did not accept the code. See the callback consistency rules above.code or the state, and so do not log the query string of the callback. The code is a credential until it has been exchanged.request.remote_addr is the address of the proxy. Use the header that your proxy sets (X-Forwarded-For, say) if you want the address of the client, but only if the proxy overwrites that header and the application cannot be reached except through the proxy. Otherwise the header is whatever the client chose to send, and an audit trail built on it can be forged. This goes for the session store above as well. See the security notes in MDN's page on X-Forwarded-For./auth, compare with the path that was given to asfquart.construct(..., oauth=...) or setup_oauth().The hook has to follow the rules of the OAuth endpoint, so tests/oauth_logging.py runs it against the endpoint, including the requests that must not be logged. If the endpoint changes, those tests say that this section has to change with it. The same file has a small store with the two log lines above in it, and checks what they record.
OAuth state parameters are stored in a process-local dictionary in src/asfquart/generics.py:
pending_states = {} # keeps track of pending states and their expiry
In a multi-instance or load-balanced deployment, if the OAuth callback is routed to a different instance than the one that initiated the flow, the state lookup will fail because pending_states is not shared across processes.
See ASVS report
A login is pending from the moment it starts (/auth?login) until the browser comes back from the OAuth provider, or until the workflow timeout (15 minutes by default) has passed. asfquart keeps a small record of each pending login, with its state, in the memory of the process that started it. Nothing is written to disk.
When the application restarts, its pending logins are lost. There is no way to keep them across a restart.
state that the application no longer knows, and gets 403 Invalid or expired OAuth state provided. Starting the login again works.app.sessions), and neither is touched by a restart, provided that the application keeps its secret (the token_file of asfquart.construct()) and that the session store, if there is one, keeps its sessions.The same goes for a reload during development, and for the multi-instance case above, where the callback reaches a process other than the one that started the login.
Pending logins that are never completed do not accumulate:
403 above and have to start again. In normal use the limit is never reached; it is there for a client that starts logins without end.More than 10000 OAuth logins are pending; 37 dropped since the last warning, oldest first. The number is how many were dropped since the warning before. One warning with a small number is a burst. A warning every minute, with large numbers, is pressure that lasts, and may be an attack.Somebody who starts logins fast enough can push real ones out before their users come back from the OAuth provider. With the default limit, and a login that takes half a minute to complete, that needs more than 300 new logins a second, kept up for as long as the attack is to last. A real login that is pushed out fails with the 403 above, and works when tried again once the flood has stopped.
No limit on a table of pending logins avoids this altogether. The alternative, refusing new logins while the table is full, would let the same flood lock everybody out for the whole workflow timeout after it had stopped. Dropping the oldest ends the damage when the flood ends.
asfquart does not limit how fast one client may start logins. It cannot tell clients apart reliably: behind a reverse proxy every request comes from the proxy's address. The place to limit the rate of requests to /auth?login is the reverse proxy, which does know the client. Raising max_pending_logins also raises the rate that a flood has to keep up.
To use another limit, either set asfquart.generics.DEFAULT_MAX_PENDING_LOGINS before calling asfquart.construct(), or set the endpoint up yourself:
import asfquart import asfquart.generics app = asfquart.construct("myapp", oauth=False) asfquart.generics.setup_oauth(app, max_pending_logins=1000)