Authentication¶
History: the dead password grant¶
Older clients (szkolny-android, lomber1/py-librus-api) log in with an OAuth
password grant:
POST https://api.librus.pl/OAuth/Token
grant_type=password&client_id=28&username=...&password=...
✅ Dead. Since around 2026-03-28 Librus answers this with HTTP 400
unsupported_grant_type, even with correct credentials. Do not use it.
Current flow (4 steps)¶
The current flow behaves like a browser logging in to the parent portal.
Everything is carried in cookies, so every request must use the same cookie
jar. Implemented in LibrusApiClient.async_login, and based on
emsi/librus_pyapi (MIT).
- Portal redirect.
GET https://synergia.librus.pl/loguj/portalRodzina(do not follow redirects). TheLocationheader points atapi.librus.pl/OAuth/Authorization?client_id=46&response_type=code&scope=mydata&state=.... - Authorization page.
GETthatLocationURL (do not follow redirects). This sets API-side session cookies. The body is not used. - Credentials.
POST https://api.librus.pl/OAuth/Authorization?client_id=46with the form bodyaction=login&login=<login>&pass=<password>and browser-like XHR headers:Origin/Refereronapi.librus.pl, theSec-Fetch-*headers,X-Requested-With: XMLHttpRequestand a desktop ChromeUser-Agent.
✅ On success the response is JSON:
{"status": "ok", "goTo": "/OAuth/Authorization/2FA?client_id=46"}
❓ A response without goTo is treated as wrong credentials. This was
never tested with a real wrong password, to avoid tripping abuse
detection on a real family's account.
4. Redirect chain.
Starting from goTo (resolved against the authorization URL), follow
Location headers manually, keeping cookies, until a response has no
Location (at most 10 hops). After that, an oauth_token cookie must
exist for synergia.librus.pl.
If any response body in steps 3–4 contains a captcha marker (captcha,
recaptcha, g-recaptcha, hcaptcha), the client raises
LibrusCaptchaRequiredError. ❓ This was never observed.
Cookies and session lifetime¶
| Cookie | Domain | Lifetime | Why it matters |
|---|---|---|---|
oauth_token |
synergia.librus.pl | ✅ Max-Age=600 per response; the session dies after ~10 min without a request |
The actual session. ✅ Idle expiry confirmed live 2026-11-14: 11.5 idle minutes after a login, Me answered 401. A request every few minutes keeps it alive; after a quiet spell, refreshToken brings it back (below). |
DZIENNIKSID, SDZIENNIKSID |
both | session | Session companions. |
DeviceCookie |
api.librus.pl, ✅ Path=/OAuth |
✅ 1 year (Max-Age=31536000) |
Marks a known device. ❓ Probably why normal logins get no captcha. Persist it, with its path. |
⚠️ Because DeviceCookie lives under /OAuth, asking a cookie jar for
"cookies for https://api.librus.pl/" does not return it. Walk the jar
(or filter for an /OAuth/... URL) when you persist the session.
- There is no OAuth refresh token to keep instead of the password. The
session cookies can be renewed with
refreshToken(next point), even afteroauth_tokenexpired from idling, but a session that is gone for good (logged out, cookies lost, many hours later ❓) needs a password login, so unattended long-running use needs the password stored. - ✅
GET https://synergia.librus.pl/refreshToken(with the session cookies) answers 200 with an empty body and a newoauth_tokencookie (and a newSDZIENNIKSID); the API keeps working afterwards (confirmed live 2026-10-07). ✅ It also works after the session idled out (confirmed live 2026-11-14): log in, wait 11.5 minutes,Meanswers 401,refreshTokenanswers 200 with new cookies,Meworks again - no password login. So the cheap way to recover from a 401 isasync_refresh_session()first and a password login only when it returns False -Librusdoes this since 0.3.31, the Home Assistant integration since 0.12.49. ❓ How long after the last request a refresh still works was not measured.Mealso reportsRefresh: 900.async_ensure_session_validcalls it (async_refresh_session) once the session is 2 hours old, and (since 0.3.32) when Synergia hasn't answered for 8 minutes or nothing is known sinceimport_session- before the next request, so it isn't rejected first. It falls back to a password login if the refresh fails. A refresh only counts when that very answer sets a newoauth_token(a 200 without one is a failure); after a failure the client doesn't tryrefreshTokenagain for 30 minutes. - ✅ The session dies after ~10 minutes without a request, long before the
20 h lifetime the client assumes. On an HTTP 401 from a data endpoint
Librusrenews the session once and retries:refreshTokenfirst, a password login only when that fails (since 0.3.31; before, it always logged in with the password, so a client polling less often than every 10 minutes did that on nearly every poll). When several requests hit the same dead session at once, they all wait for one renewal instead of each renewing it. - To resume later, persist
LibrusSessionData(viaexport_session()orLibrus.session_data) and pass it back viaimport_session()orLibrus(..., session_data=...).Librus.session_datacan still be read afterclose()(it holds the cookies the session had when it closed) and reading it never opens a new session.
One account per cookie jar¶
The session lives in the cookie jar, and a jar holds only one
oauth_token per domain. ✅ Two accounts sharing one
aiohttp.ClientSession will overwrite each other's session and receive
each other's data, intermittently. Use one session per account.