Eagerly check token validity and handle invalid tokens in flask helpers #205
+242
−154
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This changeset makes one primary breaking change and calls out another (more subtle) one as well.
The first and most notable change here is that
AuthState
now callsAuthState.introspect_token()
on init.This removes error behaviors which were previously lazy -- for example, the first access to
AuthState.effective_identity
could raise an error.Instead, users of
AuthState
should anticipate that the token has already been validated.This does not guarantee that no calls to the
AuthState
will do IO and result in errors -- notably the codepath which reaches out to Groups is intentionally left untouched here for two reasons:Secondarily, now that
AuthState(...)
can raise an error, it is clearly possible to handle that gracefully in theFlaskAuthStateBuilder
.That component now translates the generic errors which are used by
AuthState
into an appropriate error for the flask components, which then gets handled and translated into anUnauthorized
error in the default handler.Because
FlaskAuthStateBuilder
has new and different error raising behavior, it is also noted as a breaking change.In the course of testing this, I also addressed some unnecessarily aggressive mocking in the testsuite, which resulted in a number of tests evaluating against mocks when it is perfectly feasible for them to evaluate against the real
AuthState
object.NB: I assessed some other paths to resolving the issues with uncaught errors, like modifying the flask-blueprint error handler more extensively.
Although many such options are reasonable, my determination was that they do not solve the underlying issue, which is the ability of this object to make errors appear on property access at unpredictable times in the application lifecycle.