Flask
masks.flask adds masks to a Flask app. Python apps explains the settings,
the handshake, sessions, and privileges that every framework shares.
Setting up
Section titled “Setting up”-
Install the package with the
flaskextra:Terminal window pip install "masks-client[flask]" -
Create
Maskswith the app and its settings:import osfrom flask import Flaskfrom masks.flask import Masks, maskedapp = Flask(__name__)app.secret_key = os.environ["SESSION_SECRET"]auth = Masks(app, issuer=os.environ["MASKS_ISSUER"], resource="https://app.example.com")@app.get("/settings")@auth.required()def settings():return {"name": masked.name}
Masks registers the routes as a blueprint. It takes the settings as keywords or a Config, as
Masks(app, config). Keywords that leave out secret use app.secret_key. A Config needs its
own secret. Masks() followed by init_app(app) works as well.
masked is the actor for the current request. The decorators take
privileges.
init_app raises these errors:
ValueErrorfor a privilege thatprivilegesdoes not name, even in a decorator applied beforeinit_appran.TypeErrorfor an async session store orstore.
The session is read the first time a request needs it, so a view that never asks for the actor reads nothing from a session store.
Reference
Section titled “Reference”masked |
The actor for the current request. |
@auth.required(*privileges, tokens=False) |
Needs a sign-in holding each privilege. With tokens=True, it checks a token when the request has an Authorization header or no session. |
@auth.protected(*privileges) |
Needs an access token holding each privilege. |
auth.authorize(*privileges, tokens=False) |
required inside a view. Raises masks.web.Refusal. |
auth.actor |
The actor, the same as masked. |
auth.client() |
A masks.Client with the app’s credentials. |
auth.start_delegation(provider, prompt=None, max_age=None) |
Starts a delegation. Returns the redirect. |
auth.finish_delegation() |
Finishes it. Returns a masks.HeldConnection. |
auth.login_url(return_to="", organization="", privilege=()) |
A link that starts a sign-in. |
auth.logout_url() |
The path a sign-out form posts to. |
auth.registered() |
Whether masks still knows the app. |
auth.connected() |
Whether the app holds credentials. |
auth.session() |
The session, a mutable mapping. |