AuthResource¶
AuthResource
¶
Session-based authentication for PiKVM.
Source code in src/aiopikvm/resources/auth.py
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
login(user, passwd, totp=None, *, expire=0, timeout=None)
async
¶
Open a session and return its token.
kvmd reads this endpoint with aiohttp's form parser, so the
credentials travel as application/x-www-form-urlencoded. It
answers with an empty envelope and puts the token in a Set-Cookie:
auth_token header — that header is the only place it ever appears.
The token is also stored in PiKVM.cookies,
scoped to the host it came from, and sent with every later request.
Under auth="cookie" it is what those requests are authenticated
by, and what a socket handshake carries. Under auth="basic" it
takes over as well — kvmd reads the cookie before the Basic
credential — and nothing renews it, so a client that outlives the
session starts failing with a password that is still good. Only
auth="headers" leaves it deciding nothing, that pair being read
first; see PiKVM.cookies for what a
session token is good for in that case.
A token already in the jar is replaced. The session it belonged to stays open on the device until it expires or any session of that user is logged out, so keep it if it is still wanted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user
|
str
|
kvmd user name. Must match
|
required |
passwd
|
str
|
Password, printable ASCII only. |
required |
totp
|
str | None
|
Current TOTP code, appended to the password. kvmd validates the concatenation, so a code that has rotated since it was read fails like a wrong password. |
None
|
expire
|
int
|
Session lifetime in seconds. |
0
|
timeout
|
float | None
|
Per-call timeout in seconds. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The session token, 64 hexadecimal characters. Empty when kvmd runs with authentication switched off — there is no session to hand out, and every request would be served anyway. |
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If |
AuthError
|
The credentials were refused (HTTP 403), or kvmd's validators rejected the user name or password before checking them (HTTP 400). |
Source code in src/aiopikvm/resources/auth.py
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
check(*, timeout=None)
async
¶
Verify that the current credentials are accepted.
Returns nothing: kvmd answers with an empty envelope, and the whole result is whether it answered at all.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Per-call timeout in seconds. |
None
|
Raises:
| Type | Description |
|---|---|
AuthError
|
No credentials were accepted (HTTP 401), or the ones sent were rejected (HTTP 403) — including a session token that has expired or been logged out. |
Source code in src/aiopikvm/resources/auth.py
logout(token=None, *, timeout=None)
async
¶
Close the sessions of the user a token belongs to.
kvmd takes the token's owner and drops every session that user has, not just this one. A script tidying up after itself therefore signs the same account out of the web UI and invalidates any other token it holds.
The session is identified by the auth_token cookie alone; the
X-KVMD-* headers get the request past the auth chain but say
nothing about which session is meant, so a call with no cookie fails
with HTTP 400.
The cookie is dropped from PiKVM.cookies
once kvmd confirms. On failure the jar is left as it was found —
including when the token came from the argument rather than the jar —
because the session is then in an unknown state, and throwing a token
away is the one thing that cannot be undone. Clear it by hand —
kvm.cookies.delete("auth_token") — for a token known to be dead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str | None
|
Session token to drop, surrounding whitespace ignored.
Defaults to the one
|
None
|
timeout
|
float | None
|
Per-call timeout in seconds. |
None
|
Raises:
| Type | Description |
|---|---|
ConfigurationError
|
If no token was given and none is stored, if the token is not the 64 hexadecimal characters kvmd accepts, or if this client's base URL names no host to scope the cookie to — a URL no request could be built from either. |
AuthError
|
The credentials this client sends were refused
(HTTP 403). Since the call always carries a cookie, which
source kvmd answers from follows the auth mode: under
|