/
qr.ts
48 lines (46 loc) · 1.77 KB
/
qr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
import { WebUntisSecretAuth } from './secret';
import type { Authenticator } from './secret';
import type { URL } from 'url';
/**
* @private
*/
export type URLClass = typeof URL;
export class WebUntisQR extends WebUntisSecretAuth {
/**
* Use the data you get from a WebUntis QR code
* @constructor
* @param {string} QRCodeURI A WebUntis uri. This is the data you get from the QR Code from the webuntis webapp under profile->Data access->Display
* @param {string} [identity="Awesome"] A identity like: MyAwesomeApp
* @param {Object} authenticator Custom otplib v12 instance. Default will use the default otplib configuration.
* @param {Object} URL Custom whatwg url implementation. Default will use the nodejs implementation.
* @param {boolean} [disableUserAgent=false] If this is true, axios will not send a custom User-Agent
*/
constructor(
QRCodeURI: string,
identity: string,
authenticator: Authenticator,
URL?: URLClass,
disableUserAgent = false,
) {
let URLImplementation = URL;
if (!URL) {
if ('import' in globalThis) {
throw new Error(
'You need to provide the URL object by yourself. We can not eval the require in ESM mode.',
);
}
// React-Native will not eval this expression
URLImplementation = eval("require('url').URL") as URLClass;
}
const uri = new URLImplementation!(QRCodeURI);
super(
uri.searchParams.get('school')!,
uri.searchParams.get('user')!,
uri.searchParams.get('key')!,
uri.searchParams.get('url')!,
identity,
authenticator,
disableUserAgent,
);
}
}