Skip to content

Commit bdf8b65

Browse files
committed
feat: login handler
1 parent 565d3a5 commit bdf8b65

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/handlers/login.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { GM_getValue } from '$'
2+
import { TOTP } from 'totp-generator'
3+
import { observe } from '../observer'
4+
import { writeToken } from '../utils'
5+
import { Handler } from './handler'
6+
7+
export class LoginHandler implements Handler {
8+
shouldLoad(): boolean {
9+
return false
10+
}
11+
12+
load(): void {
13+
const secret = GM_getValue('totpSecret', null)
14+
if (secret != null) {
15+
observe('#tokenValidationModal', () => {
16+
const { otp } = TOTP.generate(secret)
17+
writeToken(otp)
18+
})
19+
}
20+
}
21+
}

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Handler } from './handlers/handler'
2+
import { LoginHandler } from './handlers/login'
23

3-
const handlers: Handler[] = []
4+
const handlers: Handler[] = [new LoginHandler()]
45

56
handlers.forEach((h) => {
67
if (h.shouldLoad()) h.load()

src/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ import $ from 'jquery'
33
export function isTokenModelOpen(): boolean {
44
return $('#tokenValidationModal').css('display') !== 'none'
55
}
6+
7+
export function writeToken(token: string) {
8+
const input = $('input[name="token"]')
9+
input.val(token)
10+
$('button[sdaValidateToken=submit]')[0].click()
11+
}

0 commit comments

Comments
 (0)