unitoolkit 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ladok_vakt/__init__.py +7 -0
- ladok_vakt/__main__.py +51 -0
- ladok_vakt/core.py +553 -0
- ladok_vakt/login.py +232 -0
- tools.json +623 -0
- unitoolkit/__init__.py +1 -0
- unitoolkit/__main__.py +63 -0
- unitoolkit/agent.py +270 -0
- unitoolkit/browser.py +147 -0
- unitoolkit/canvas.py +189 -0
- unitoolkit/chainproxy.py +113 -0
- unitoolkit/citation.py +165 -0
- unitoolkit/databases.py +497 -0
- unitoolkit/docs.py +394 -0
- unitoolkit/fulltext.py +260 -0
- unitoolkit/koha.py +729 -0
- unitoolkit/mcp.py +85 -0
- unitoolkit/programme.py +147 -0
- unitoolkit/search.py +507 -0
- unitoolkit/serve.py +153 -0
- unitoolkit/sharepoint.py +86 -0
- unitoolkit/surface.py +86 -0
- unitoolkit/timeedit.py +209 -0
- unitoolkit/tools.py +555 -0
- unitoolkit-0.7.0.dist-info/METADATA +177 -0
- unitoolkit-0.7.0.dist-info/RECORD +30 -0
- unitoolkit-0.7.0.dist-info/WHEEL +5 -0
- unitoolkit-0.7.0.dist-info/entry_points.txt +4 -0
- unitoolkit-0.7.0.dist-info/licenses/LICENSE +21 -0
- unitoolkit-0.7.0.dist-info/top_level.txt +2 -0
ladok_vakt/login.py
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"""Optional re-login for Ladok through the browser's own saved password.
|
|
2
|
+
|
|
3
|
+
Off by default. Enable with `LADOK_AUTOLOGIN=1` (or one-off `--login`).
|
|
4
|
+
|
|
5
|
+
What it does and does not do, because this is the part worth auditing:
|
|
6
|
+
|
|
7
|
+
* It never reads, stores, prints or types a password. It does not touch the
|
|
8
|
+
password manager API either. It checks whether Chrome has filled the IdP form
|
|
9
|
+
(the `:-webkit-autofill` pseudo-class) and then clicks the submit button with a
|
|
10
|
+
real `Input.dispatchMouseEvent`. Chrome itself releases the saved value into
|
|
11
|
+
the page, because a user gesture is what makes autofill usable.
|
|
12
|
+
* No field value is ever evaluated: the checks read booleans, element names and
|
|
13
|
+
geometry only.
|
|
14
|
+
* It logs in as the human whose browser profile it is. There is no credential of
|
|
15
|
+
its own, and it cannot log in anywhere the human is not already saved.
|
|
16
|
+
|
|
17
|
+
The flow: Ladok -> Shibboleth -> the institution's IdP. Only the institution's
|
|
18
|
+
IdP can be automated this way, since that is where the browser's saved login
|
|
19
|
+
lives, so `LADOK_IDP_ENTITY` names it (SWAMID entityID). With a single sign-on
|
|
20
|
+
session still alive, the IdP also lets it back in without showing the form at
|
|
21
|
+
all, which is why the re-login usually takes a second.
|
|
22
|
+
"""
|
|
23
|
+
import asyncio
|
|
24
|
+
import json
|
|
25
|
+
import os
|
|
26
|
+
|
|
27
|
+
from .core import LADOK_ORIGIN, Fault
|
|
28
|
+
|
|
29
|
+
SHIB_LOGIN = ('https://student.ladok.se/Shibboleth.sso/Login'
|
|
30
|
+
'?entityID=%s&target=https%%3A%%2F%%2Fstudent.ladok.se%%2Fstudent%%2Fauth%%2Fswamid%%2Flogin')
|
|
31
|
+
|
|
32
|
+
# Read-only description of the login page: names, types, autofill state. No values.
|
|
33
|
+
STATE_JS = r"""JSON.stringify({host: location.host, path: location.pathname,
|
|
34
|
+
form: !!document.querySelector('#username') && !!document.querySelector('#password'),
|
|
35
|
+
filled: ['#username','#password'].every(function (s) { var e = document.querySelector(s);
|
|
36
|
+
return !!e && e.matches(':-webkit-autofill'); }),
|
|
37
|
+
error: (document.querySelector('.output--failure, .form-error, .alert-danger') || {}).innerText || ''})"""
|
|
38
|
+
BTN_JS = r"""(function () { var b = document.querySelector('button[name=_eventId_proceed]');
|
|
39
|
+
if (!b) return null; b.scrollIntoView({block: 'center'}); var r = b.getBoundingClientRect();
|
|
40
|
+
return JSON.stringify({x: r.left + r.width / 2, y: r.top + r.height / 2}); })()"""
|
|
41
|
+
# Logged out, the proxy answers 200 with the app's HTML shell, so the status alone proves
|
|
42
|
+
# nothing: count it as signed in only when the body is the student record (JSON).
|
|
43
|
+
AUTHED_JS = ("fetch('%s',{credentials:'include',headers:{accept:"
|
|
44
|
+
"'application/vnd.ladok-studiedeltagande+json'}}).then(function (r) {"
|
|
45
|
+
" return r.text().then(function (b) {"
|
|
46
|
+
" return (r.status === 200 && /^\\s*[{\\[]/.test(b)) ? 200 : (r.status === 200 ? 'html' : r.status);"
|
|
47
|
+
" }); })")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
STUDENTWEBB = 'https://student.ladok.se/student/app/studentwebb/'
|
|
51
|
+
|
|
52
|
+
# The human route: "Logga in via ditt lärosäte" -> SeamlessAccess -> your institution.
|
|
53
|
+
# Link text in both languages; the institution is matched on the domain the picker shows
|
|
54
|
+
# under its name ("Sophiahemmet University / shh.se"), which survives renames and language.
|
|
55
|
+
PICKER_LINK_JS = r"""(function () {
|
|
56
|
+
var a = [].slice.call(document.querySelectorAll('a,button')).filter(function (e) {
|
|
57
|
+
return /lärosäte|through your university/i.test(e.innerText || ''); })[0];
|
|
58
|
+
if (!a) return 'none'; a.click(); return 'clicked'; })()"""
|
|
59
|
+
PICK_IDP_JS = r"""(function (dom) {
|
|
60
|
+
var hit = [].slice.call(document.querySelectorAll('a.institution, a.identityprovider, li a'))
|
|
61
|
+
.filter(function (e) { return (e.innerText || '').toLowerCase().indexOf(dom) >= 0; })[0];
|
|
62
|
+
if (hit) { hit.click(); return 'picked'; }
|
|
63
|
+
var box = document.querySelector('#searchinput');
|
|
64
|
+
if (!box) return 'no picker';
|
|
65
|
+
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set.call(box, dom);
|
|
66
|
+
box.dispatchEvent(new Event('input', {bubbles: true}));
|
|
67
|
+
return 'searched'; })(%s)"""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def idp_domain(cfg):
|
|
71
|
+
"""'https://swamid2.shh.se/idp/shibboleth' -> 'shh.se'; LADOK_IDP_DOMAIN overrides."""
|
|
72
|
+
import os
|
|
73
|
+
from urllib.parse import urlparse
|
|
74
|
+
dom = os.environ.get('LADOK_IDP_DOMAIN', '').strip().lower()
|
|
75
|
+
if dom:
|
|
76
|
+
return dom
|
|
77
|
+
host = urlparse(cfg.idp_entity or '').hostname or ''
|
|
78
|
+
return '.'.join(host.split('.')[-2:])
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def login_url(cfg):
|
|
82
|
+
"""Direct SWAMID login for the configured institution (the fallback route)."""
|
|
83
|
+
if not cfg.idp_entity:
|
|
84
|
+
raise Fault('LADOK_IDP_ENTITY är inte satt: ange ditt lärosätes SWAMID entityID, '
|
|
85
|
+
't.ex. https://swamid2.shh.se/idp/shibboleth')
|
|
86
|
+
from urllib.parse import quote
|
|
87
|
+
return SHIB_LOGIN % quote(cfg.idp_entity, safe='')
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
async def _login(cfg, route='picker', timeout=45.0):
|
|
91
|
+
try:
|
|
92
|
+
import websockets
|
|
93
|
+
except ImportError:
|
|
94
|
+
raise Fault('websockets saknas: python3 -m pip install websockets')
|
|
95
|
+
import urllib.request
|
|
96
|
+
import datetime as dt
|
|
97
|
+
|
|
98
|
+
url = STUDENTWEBB if route == 'picker' else login_url(cfg)
|
|
99
|
+
domain = idp_domain(cfg)
|
|
100
|
+
try:
|
|
101
|
+
tab = json.load(urllib.request.urlopen(urllib.request.Request(
|
|
102
|
+
cfg.cdp + '/json/new?about:blank', method='PUT'), timeout=10))
|
|
103
|
+
except Exception as exc:
|
|
104
|
+
raise Fault('kan inte öppna en flik i Chrome (%s)' % exc)
|
|
105
|
+
|
|
106
|
+
deadline = dt.datetime.now() + dt.timedelta(seconds=timeout)
|
|
107
|
+
try:
|
|
108
|
+
async with websockets.connect(tab['webSocketDebuggerUrl'], max_size=None) as ws:
|
|
109
|
+
n = 0
|
|
110
|
+
|
|
111
|
+
async def call(method, params=None):
|
|
112
|
+
nonlocal n
|
|
113
|
+
n += 1
|
|
114
|
+
mine = n
|
|
115
|
+
await ws.send(json.dumps({'id': mine, 'method': method, 'params': params or {}}))
|
|
116
|
+
while True:
|
|
117
|
+
msg = json.loads(await ws.recv())
|
|
118
|
+
if msg.get('id') == mine:
|
|
119
|
+
return msg.get('result', msg)
|
|
120
|
+
|
|
121
|
+
async def ev(expr, gesture=False):
|
|
122
|
+
r = await call('Runtime.evaluate', {'expression': expr, 'awaitPromise': True,
|
|
123
|
+
'returnByValue': True, 'userGesture': gesture})
|
|
124
|
+
return (r.get('result') or {}).get('value')
|
|
125
|
+
|
|
126
|
+
await call('Page.enable')
|
|
127
|
+
# Treat the tab as focused even when the window is not (screen locked, other
|
|
128
|
+
# window on top), so autofill and focus behave as for a person at the desk.
|
|
129
|
+
await call('Emulation.setFocusEmulationEnabled', {'enabled': True})
|
|
130
|
+
# A tab opened over CDP starts in the background, and a background tab is not
|
|
131
|
+
# rendered: a mouse event by coordinates then lands nowhere and Login is never
|
|
132
|
+
# pressed. The autofill click needs the tab in front.
|
|
133
|
+
await call('Page.bringToFront')
|
|
134
|
+
await call('Page.navigate', {'url': url})
|
|
135
|
+
clicked = False
|
|
136
|
+
clicks = 0
|
|
137
|
+
clicked_at = None
|
|
138
|
+
link_clicked = False
|
|
139
|
+
picked = 0
|
|
140
|
+
state = {}
|
|
141
|
+
while dt.datetime.now() < deadline:
|
|
142
|
+
await asyncio.sleep(1.5)
|
|
143
|
+
try:
|
|
144
|
+
state = json.loads(await ev(STATE_JS) or '{}')
|
|
145
|
+
except Exception:
|
|
146
|
+
continue # page is navigating
|
|
147
|
+
host, path = state.get('host', ''), state.get('path', '')
|
|
148
|
+
if os.environ.get('LADOK_LOGIN_TRACE'):
|
|
149
|
+
print(' [%s] %s%s form=%s filled=%s' % (route, host, path, state.get('form'),
|
|
150
|
+
state.get('filled')), flush=True)
|
|
151
|
+
if host in ('student.ladok.se', 'www.student.ladok.se') and '/app/' in path:
|
|
152
|
+
code = await ev(AUTHED_JS % ('/student/proxy/%s/studiedeltagande/internal/'
|
|
153
|
+
'student/inloggad/som' % cfg.larosate))
|
|
154
|
+
if code == 200:
|
|
155
|
+
return 'ok'
|
|
156
|
+
# Logged out: the studentwebb start page. Take the human route once,
|
|
157
|
+
# then let the SPA finish rendering before judging again.
|
|
158
|
+
if route == 'picker' and not link_clicked:
|
|
159
|
+
if await ev(PICKER_LINK_JS) == 'clicked':
|
|
160
|
+
link_clicked = True
|
|
161
|
+
continue
|
|
162
|
+
if route == 'picker' and not clicked:
|
|
163
|
+
continue
|
|
164
|
+
raise Fault('tillbaka i Ladok men API svarar %s' % code)
|
|
165
|
+
if 'seamlessaccess' in host or '/ds/' in path:
|
|
166
|
+
if picked >= 3:
|
|
167
|
+
raise Fault('lärosätesväljaren hittar inte %s' % domain)
|
|
168
|
+
if await ev(PICK_IDP_JS % json.dumps(domain)) in ('picked', 'searched'):
|
|
169
|
+
picked += 1
|
|
170
|
+
continue
|
|
171
|
+
if 'swamid' in host or (state.get('form') and not clicked):
|
|
172
|
+
if state.get('error'):
|
|
173
|
+
raise Fault('IdP visar ett fel: sparat lösenord stämmer inte?')
|
|
174
|
+
stuck = clicked and clicks < 2 and (dt.datetime.now() - clicked_at).total_seconds() > 8
|
|
175
|
+
if (clicked and not stuck) or not state.get('filled'):
|
|
176
|
+
continue # let autofill / the redirect do their work
|
|
177
|
+
# Chrome hands an autofilled password to the page only after a user
|
|
178
|
+
# gesture; until then the field reads empty and Login posts nothing.
|
|
179
|
+
# The first try is a gesture-flagged focus and click inside the page: it
|
|
180
|
+
# needs no drawn window, so it works minimized, covered or with the screen
|
|
181
|
+
# off (the unattended 07:10 run, where a coordinate click landed nowhere).
|
|
182
|
+
# The second try is a real mouse click, for when the window is drawn.
|
|
183
|
+
if clicks == 0:
|
|
184
|
+
await ev("document.querySelector('#password').focus(); 1", gesture=True)
|
|
185
|
+
await asyncio.sleep(0.4)
|
|
186
|
+
await ev("document.querySelector('button[name=_eventId_proceed]').click(); 1",
|
|
187
|
+
gesture=True)
|
|
188
|
+
xy = 'gesture'
|
|
189
|
+
else:
|
|
190
|
+
await call('Page.bringToFront')
|
|
191
|
+
xy = json.loads(await ev(BTN_JS) or 'null')
|
|
192
|
+
if not xy:
|
|
193
|
+
raise Fault('hittade ingen Login-knapp på IdP-sidan')
|
|
194
|
+
await asyncio.sleep(0.4)
|
|
195
|
+
for kind in ('mouseMoved', 'mousePressed', 'mouseReleased'):
|
|
196
|
+
await call('Input.dispatchMouseEvent', {'type': kind, 'x': xy['x'],
|
|
197
|
+
'y': xy['y'], 'button': 'left', 'clickCount': 1})
|
|
198
|
+
if os.environ.get('LADOK_LOGIN_TRACE'):
|
|
199
|
+
print(' [%s] click %d at %s' % (route, clicks + 1, xy), flush=True)
|
|
200
|
+
clicked = True
|
|
201
|
+
clicks += 1
|
|
202
|
+
clicked_at = dt.datetime.now()
|
|
203
|
+
where = '%s%s' % (state.get('host', '?'), state.get('path', ''))
|
|
204
|
+
if not clicked:
|
|
205
|
+
raise Fault('Chrome fyllde aldrig i IdP-formuläret (stannade på %s): inget sparat '
|
|
206
|
+
'lösenord för den här sidan, eller så kräver inloggningen något mer '
|
|
207
|
+
'(2FA)' % where)
|
|
208
|
+
raise Fault('klickade Login men kom inte tillbaka till Ladok inom %ds (stannade på %s)'
|
|
209
|
+
% (timeout, where))
|
|
210
|
+
finally:
|
|
211
|
+
try:
|
|
212
|
+
urllib.request.urlopen(urllib.request.Request(
|
|
213
|
+
cfg.cdp + '/json/close/' + tab['id'], method='PUT'), timeout=10)
|
|
214
|
+
except Exception:
|
|
215
|
+
pass
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def login(cfg):
|
|
219
|
+
"""Try to restore the Ladok session. Returns a short status line, raises Fault.
|
|
220
|
+
|
|
221
|
+
First the route a human takes (studentwebb -> "Logga in via ditt lärosäte" ->
|
|
222
|
+
SeamlessAccess -> the institution), then the direct Shibboleth URL. Two routes because
|
|
223
|
+
each has failed alone: the direct URL returned exit 3 one morning while the picker worked,
|
|
224
|
+
and the picker is a third-party page that can change. The error names both.
|
|
225
|
+
"""
|
|
226
|
+
errors = []
|
|
227
|
+
for route in ('picker', 'direct'):
|
|
228
|
+
try:
|
|
229
|
+
return '%s (%s)' % (asyncio.run(_login(cfg, route=route)), route)
|
|
230
|
+
except Fault as exc:
|
|
231
|
+
errors.append('%s: %s' % (route, exc))
|
|
232
|
+
raise Fault(' | '.join(errors))
|