|
| 1 | +import { ScanBarCode } from "../utils/TokenUtils"; |
| 2 | +import { url } from "@kit.ArkTS"; |
| 3 | +import { otpType, TokenConfig } from "../utils/TokenConfig"; |
| 4 | +import { readFileContent, showSelectFilePicker } from "../utils/FileUtils"; |
| 5 | + |
| 6 | +@Entry |
| 7 | +@Preview |
| 8 | +@CustomDialog |
| 9 | +export struct URIConfigDialog { |
| 10 | + controller?: CustomDialogController; |
| 11 | + @State otp_uris: Array<string> = []; |
| 12 | + @State btn_camera_clicked: number = 0; |
| 13 | + @State btn_folder_clicked: number = 0; |
| 14 | + |
| 15 | + cancel?: () => void; |
| 16 | + confirm: (tokens: Array<TokenConfig>) => void = () => {}; |
| 17 | + |
| 18 | + createTokens(uris: Array<string>): Array<TokenConfig> { |
| 19 | + let tokens: Array<TokenConfig> = []; |
| 20 | + let otp_url: url.URL; |
| 21 | + |
| 22 | + uris.forEach((uri) => { |
| 23 | + let token = new TokenConfig(); |
| 24 | + try { |
| 25 | + otp_url = url.URL.parseURL(uri) |
| 26 | + } catch (error) { |
| 27 | + console.error('Invalid OTPAuth URL:', error); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + if (otp_url.protocol !== 'otpauth:') { |
| 32 | + console.error('Invalid protocol'); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const type = otp_url.host.toLowerCase(); |
| 37 | + |
| 38 | + if (type === 'totp') { |
| 39 | + token.TokenType = otpType.TOTP; |
| 40 | + } else if (type == 'hotp') { |
| 41 | + token.TokenType = otpType.HOTP; |
| 42 | + } else { |
| 43 | + console.error('Invalid type'); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const pathParts = otp_url.pathname.slice(1).split('/'); |
| 48 | + const labelParts = decodeURIComponent(pathParts[0]).split(':'); |
| 49 | + token.TokenIssuer = labelParts[0]; |
| 50 | + token.TokenName = labelParts[1]; |
| 51 | + |
| 52 | + const parameters: Record<string, string> = {}; |
| 53 | + const query = otp_url.href.split('?')[1].toLowerCase(); |
| 54 | + if (query) { |
| 55 | + query.split('&').forEach(part => { |
| 56 | + const kv: string[] = part.split('='); |
| 57 | + parameters[kv[0]] = kv[1]; |
| 58 | + }); |
| 59 | + } |
| 60 | + token.TokenSecret = parameters['secret']; |
| 61 | + token.TokenPeriod = parseInt(parameters['period'] ?? '30'); |
| 62 | + token.TokenCounter = parseInt(parameters['counter'] ?? '0'); |
| 63 | + token.TokenDigits = parseInt(parameters['digits'] ?? '6'); |
| 64 | + |
| 65 | + tokens.push(token); |
| 66 | + }); |
| 67 | + |
| 68 | + return tokens; |
| 69 | + } |
| 70 | + |
| 71 | + build() { |
| 72 | + Column({ space: 10 }) { |
| 73 | + Row({ space: 10 }) { |
| 74 | + Text($r('app.string.tab_token_add_uri_title')) |
| 75 | + .fontSize(20) |
| 76 | + .fontWeight(FontWeight.Bold) |
| 77 | + Blank() |
| 78 | + SymbolGlyph($r('sys.symbol.camera')) |
| 79 | + .fontSize(30) |
| 80 | + .fontColor([$r('app.color.item_fg')]) |
| 81 | + .fontWeight(FontWeight.Medium) |
| 82 | + .symbolEffect(new BounceSymbolEffect(EffectScope.WHOLE, EffectDirection.UP), |
| 83 | + this.btn_camera_clicked) |
| 84 | + .onClick(() => { |
| 85 | + this.btn_camera_clicked++ |
| 86 | + ScanBarCode().then((code) => { |
| 87 | + this.otp_uris.push(code); |
| 88 | + }); |
| 89 | + }) |
| 90 | + SymbolGlyph($r('sys.symbol.folder')) |
| 91 | + .fontSize(30) |
| 92 | + .fontColor([$r('app.color.item_fg')]) |
| 93 | + .fontWeight(FontWeight.Medium) |
| 94 | + .symbolEffect(new BounceSymbolEffect(EffectScope.WHOLE, EffectDirection.UP), |
| 95 | + this.btn_folder_clicked) |
| 96 | + .onClick(() => { |
| 97 | + this.btn_folder_clicked++ |
| 98 | + showSelectFilePicker(1, ['Text File|.txt,.json']).then((uris) => { |
| 99 | + readFileContent(uris[0]).then((str) => { |
| 100 | + this.otp_uris = this.otp_uris.concat(str.split('\n')) |
| 101 | + }) |
| 102 | + }); |
| 103 | + }) |
| 104 | + } |
| 105 | + .margin({ top: 10, left: 10, right: 10 }) |
| 106 | + .width('100%') |
| 107 | + .justifyContent(FlexAlign.SpaceAround) |
| 108 | + |
| 109 | + TextArea({ placeholder: $r('app.string.tab_token_add_uri_desc'), text: this.otp_uris.join('\n') }) |
| 110 | + .width('100%') |
| 111 | + .layoutWeight(1) |
| 112 | + .onChange((value: string) => { |
| 113 | + this.otp_uris = value.split('\n') |
| 114 | + }) |
| 115 | + Flex({ justifyContent: FlexAlign.SpaceAround }) { |
| 116 | + Button($r('app.string.dialog_btn_cancel')) |
| 117 | + .fontColor($r('app.color.item_fg')) |
| 118 | + .backgroundColor(Color.Transparent) |
| 119 | + .onClick(() => { |
| 120 | + if (this.controller != undefined) { |
| 121 | + this.controller.close() |
| 122 | + } |
| 123 | + }) |
| 124 | + .width('100%') |
| 125 | + Button($r('app.string.dialog_btn_confirm')) |
| 126 | + .fontColor(Color.Red) |
| 127 | + .backgroundColor(Color.Transparent) |
| 128 | + .onClick(() => { |
| 129 | + if (this.controller != undefined) { |
| 130 | + this.confirm(this.createTokens(this.otp_uris)) |
| 131 | + this.controller.close() |
| 132 | + } |
| 133 | + }) |
| 134 | + .width('100%') |
| 135 | + } |
| 136 | + } |
| 137 | + .height('50%') |
| 138 | + .margin(10) |
| 139 | + } |
| 140 | +} |
0 commit comments