Auth
Full attribute and method reference for the auth providers, generated from their docstrings. For a task-oriented walkthrough, see Authentication.
starlette_admin.auth.base.BaseAuthProvider
Bases: ABC
Minimal contract every auth provider must satisfy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
login_path
|
str
|
Path for the login page/redirect. |
'/login'
|
logout_path
|
str
|
Path for the logout endpoint. |
'/logout'
|
allow_routes
|
Sequence[str] | None
|
Route names that bypass the auth check. |
None
|
Source code in starlette_admin/auth/base.py
authenticate(request)
abstractmethod
async
Authenticate the current request.
Returns:
| Type | Description |
|---|---|
AdminUser | None
|
An |
AdminUser | None
|
|
AdminUser | None
|
|
Source code in starlette_admin/auth/base.py
starlette_admin.auth.password.AuthProvider
Bases: BaseAuthProvider
Subclass to use the built-in username/password login page.
Implement login, logout, and authenticate only;
the framework owns the form, rendering, and redirect.
Source code in starlette_admin/auth/password.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
login(username, password, remember_me, request)
async
Validate credentials and persist auth state (e.g. write to request.session).
Raise LoginFailed for wrong credentials, or FormValidationError to mark
specific form fields invalid.
Returns:
| Type | Description |
|---|---|
Response | None
|
A |
Response | None
|
default redirect ( |
Examples:
async def login(self, username, password, remember_me, request):
user = await db.get_user(username)
if user and verify_password(password, user.hashed_password):
request.session["user_id"] = user.id
return
raise LoginFailed("Invalid username or password")
Source code in starlette_admin/auth/password.py
logout(request)
async
Clear auth state (e.g. request.session.clear()).
Returns:
| Type | Description |
|---|---|
Response | None
|
A |
Response | None
|
default redirect (the admin index). |
Examples:
Source code in starlette_admin/auth/password.py
render_login(request, templates)
async
Render and handle the built-in username/password login page.
Source code in starlette_admin/auth/password.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
render_logout(request)
async
Call logout(), then redirect to the admin index.
Source code in starlette_admin/auth/password.py
starlette_admin.auth.oauth.OAuthProvider
Bases: BaseAuthProvider
Subclass to use an OAuth2/OIDC flow.
Implement redirect_to_provider, handle_callback, and authenticate.
get_routes() returns the login redirect, logout, and callback routes.
The callback is automatically added to allow_routes so the middleware
never blocks it.
Requires SessionMiddleware: the OAuth client stores the state nonce in the
session between the redirect and the callback.
Examples:
from authlib.integrations.starlette_client import OAuth
oauth = OAuth()
oauth.register(
"provider",
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
client_kwargs={"scope": "openid profile email"},
server_metadata_url=SERVER_METADATA_URL,
)
class OIDCAuthProvider(OAuthProvider):
async def redirect_to_provider(self, request: Request, callback_url: str) -> Response:
client = oauth.create_client("provider")
return await client.authorize_redirect(request, callback_url)
async def handle_callback(self, request: Request) -> None:
client = oauth.create_client("provider")
token = await client.authorize_access_token(request)
# Persist userinfo in the encrypted session cookie.
request.session["user"] = dict(token["userinfo"])
async def authenticate(self, request: Request) -> AdminUser | None:
user = request.session.get("user")
if user:
return AdminUser(
username=user.get("name") or user.get("email"),
photo_url=user.get("picture"),
)
return None
async def logout(self, request: Request) -> Response | None:
request.session.clear()
# Return a Response to redirect to the provider's end_session_endpoint,
# or None to fall back to the default admin index redirect.
return None
Source code in starlette_admin/auth/oauth.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
handle_callback(request)
abstractmethod
async
logout(request)
async
Clear auth state on logout.
Returns:
| Type | Description |
|---|---|
Response | None
|
A |
Response | None
|
|
Source code in starlette_admin/auth/oauth.py
redirect_to_provider(request, callback_url)
abstractmethod
async
render_callback(request)
async
Handle the OAuth callback, then redirect to next or the admin index.
Source code in starlette_admin/auth/oauth.py
render_login(request)
async
Redirect to the OAuth provider's authorization URL.
Source code in starlette_admin/auth/oauth.py
render_logout(request)
async
Call logout(), then redirect to its result or to the admin index.
Source code in starlette_admin/auth/oauth.py
starlette_admin.auth.base.AdminUser
dataclass
starlette_admin.auth.base.AuthMiddleware
Bases: BaseHTTPMiddleware
Source code in starlette_admin/auth/base.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
dispatch(request, call_next)
async
Enforce authentication for every request that passes through this middleware.
Allows the request through when one of the following holds: the provider
authenticates the user (authenticate() returns an AdminUser), the matched
route name is in allow_routes, or the endpoint is decorated with
@login_not_required. Otherwise, redirects to the login page.
Source code in starlette_admin/auth/base.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
starlette_admin.auth.base.login_not_required(endpoint)
Mark an endpoint as exempt from the authentication check.
Apply this decorator to a route handler to let unauthenticated requests reach it.
AuthMiddleware checks for the _login_not_required attribute set here before
redirecting to the login page.