1/** 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import picker from '@ohos.file.picker'; 17import router from '@ohos.router'; 18import certManagerModel from '../model/CertMangerModel'; 19import FileIoModel from '../model/FileIoModel'; 20import { CMModelErrorCode, CMModelOptType } from '../model/CertMangerModel'; 21import { BusinessError } from '@ohos.base'; 22import { RouterFileVo, RouterAppUidVo } from '../model/CertManagerVo/RouterInfoVo'; 23import checkUserAuthModel from '../model/CheckUserAuthModel'; 24import CmInstallPresenter from './CmInstallPresenter'; 25 26const TAG = 'CMFaPresenter: '; 27const gridCountNum: number = 4; 28 29export default class CmFaPresenter { 30 private static sInstance: CmFaPresenter; 31 32 public static getInstance(): CmFaPresenter { 33 if (CmFaPresenter.sInstance == null) { 34 CmFaPresenter.sInstance = new CmFaPresenter(); 35 } 36 return CmFaPresenter.sInstance; 37 } 38 39 onAboutToAppear(): void { 40 41 } 42 43 aboutToDisappear(): void { 44 } 45 46 unrecognizedFileTips(): void { 47 AlertDialog.show({ 48 message: $r('app.string.Install_Error_NOT_FOUND'), 49 autoCancel: true, 50 alignment: DialogAlignment.Bottom, 51 offset: { 52 dx: $r('app.float.wh_value_0'), dy: $r('app.float.wh_value_0') 53 }, 54 gridCount: gridCountNum, 55 primaryButton: { 56 value: $r('app.string.OK'), 57 action: () => { 58 } 59 }, 60 }) 61 } 62 63 routeToNextInstallCert(fileUri: string): void { 64 FileIoModel.getMediaFileSuffix(fileUri, (suffix: string | undefined) => { 65 if (suffix !== undefined) { 66 console.debug(TAG, 'suffix = ', suffix); 67 if ((suffix === 'cer') || (suffix === 'pem')) { 68 CmInstallPresenter.getInstance().installCert(fileUri, '', suffix, true); 69 } else { 70 this.unrecognizedFileTips(); 71 } 72 } 73 }) 74 } 75 76 routeToNextInstallEvidence(fileUri: string): void { 77 FileIoModel.getMediaFileSuffix(fileUri, (suffix: string | undefined) => { 78 if (suffix !== undefined) { 79 console.debug(TAG, 'suffix = ', suffix); 80 if ((suffix === 'p12') || (suffix === 'pfx')) { 81 let fileInfo = new RouterFileVo(fileUri, suffix); 82 router.pushUrl({ 83 url: 'pages/certPwdInput', 84 params: fileInfo 85 }) 86 } else { 87 this.unrecognizedFileTips(); 88 } 89 } 90 }) 91 } 92 93 startInstallCert(): void { 94 try { 95 let documentSelectOptions = new picker.DocumentSelectOptions(); 96 let documentPicker = new picker.DocumentViewPicker(); 97 console.info(TAG + 'start documentPicker.select'); 98 documentPicker.select(documentSelectOptions).then((documentSelectResult) => { 99 if (documentSelectResult.length >= 1) { 100 this.routeToNextInstallCert(String(documentSelectResult[0])) 101 } else { 102 console.error(TAG + 'documentPicker.select length invalid:' + documentSelectResult.length); 103 } 104 }).catch((err: BusinessError) => { 105 console.error(TAG + 'documentPicker.select failed with err, message: ' + err.message + ', code: ' + err.code); 106 }); 107 } catch (err) { 108 let e: BusinessError = err as BusinessError; 109 console.error(TAG + 'DocumentViewPicker failed with err, message: ' + e.message + ', code: ' + e.code); 110 } 111 } 112 113 startInstallEvidence(): void { 114 try { 115 let documentSelectOptions = new picker.DocumentSelectOptions(); 116 let documentPicker = new picker.DocumentViewPicker(); 117 console.info(TAG + 'start documentPicker.select'); 118 documentPicker.select(documentSelectOptions).then((documentSelectResult) => { 119 if (documentSelectResult.length >= 1) { 120 this.routeToNextInstallEvidence(String(documentSelectResult[0])) 121 } else { 122 console.error(TAG + 'documentPicker.select length invalid:' + documentSelectResult.length); 123 } 124 }).catch((err: BusinessError) => { 125 console.error(TAG + 'documentPicker.select failed with err, message: ' + err.message + ', code: ' + err.code); 126 }); 127 } catch (err) { 128 let e: BusinessError = err as BusinessError; 129 console.error(TAG + 'DocumentViewPicker failed with err, message: ' + e.message + ', code: ' + e.code); 130 } 131 } 132 133 startRequestAuth(uri: string): void { 134 let appUidInfo = new RouterAppUidVo(uri); 135 router.pushUrl({ 136 url: 'pages/requestAuth', 137 params: appUidInfo 138 }); 139 } 140 141 uninstallAllCert(): void { 142 certManagerModel.delAllCertOrCred(CMModelOptType.CM_MODEL_OPT_USER_CA, (errCode: CMModelErrorCode) => { 143 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 144 console.info(TAG + 'uninstallAllCert CM_MODEL_OPT_USER_CA success'); 145 } else { 146 console.error(TAG + 'uninstallAllCert CM_MODEL_OPT_USER_CA failed'); 147 } 148 }); 149 150 certManagerModel.delAllCertOrCred(CMModelOptType.CM_MODEL_OPT_APP_CRED, (errCode: CMModelErrorCode) => { 151 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 152 console.info(TAG + 'uninstallAllCert CM_MODEL_OPT_APP_CRED success'); 153 } else { 154 console.error(TAG + 'uninstallAllCert CM_MODEL_OPT_APP_CRED failed'); 155 } 156 }); 157 } 158 159 startInstall(installType: string, fileUri: string) { 160 if (installType === 'CACert') { 161 let titleStr = getContext().resourceManager.getStringSync($r('app.string.Identity_Authentication')); 162 checkUserAuthModel.auth(titleStr, (authResult: boolean) => { 163 if (authResult) { 164 console.info('checkUserAuth success'); 165 this.routeToNextInstallCert(fileUri); 166 } 167 }) 168 } else if (installType === 'userCred') { 169 AppStorage.setOrCreate('installSystemCred', false); 170 AppStorage.setOrCreate('installUserCred', true); 171 this.routeToNextInstallEvidence(fileUri); 172 } else if (installType === 'systemCred') { 173 AppStorage.setOrCreate('installSystemCred', true); 174 AppStorage.setOrCreate('installUserCred', false); 175 this.routeToNextInstallEvidence(fileUri); 176 } else { 177 console.error(TAG, 'The installType is not supported'); 178 } 179 } 180}