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'; 25import FilterParams from '../common/constants/FileFilterParams'; 26 27const TAG = 'CMFaPresenter: '; 28const gridCountNum: number = 4; 29 30export default class CmFaPresenter { 31 private static sInstance: CmFaPresenter; 32 33 public static getInstance(): CmFaPresenter { 34 if (CmFaPresenter.sInstance == null) { 35 CmFaPresenter.sInstance = new CmFaPresenter(); 36 } 37 return CmFaPresenter.sInstance; 38 } 39 40 onAboutToAppear(): void { 41 42 } 43 44 aboutToDisappear(): void { 45 } 46 47 unrecognizedFileTips(): void { 48 AlertDialog.show({ 49 message: $r('app.string.Install_Error_NOT_FOUND'), 50 autoCancel: true, 51 alignment: DialogAlignment.Bottom, 52 offset: { 53 dx: $r('app.float.wh_value_0'), dy: $r('app.float.wh_value_0') 54 }, 55 gridCount: gridCountNum, 56 primaryButton: { 57 value: $r('app.string.OK'), 58 action: () => { 59 } 60 }, 61 }) 62 } 63 64 routeToNextInstallCert(fileUri: string): void { 65 FileIoModel.getMediaFileSuffix(fileUri, (suffix: string | undefined) => { 66 if (suffix !== undefined) { 67 console.debug(TAG, 'suffix = ', suffix); 68 if ((suffix === 'cer') || (suffix === 'pem') || (suffix === 'crt') || (suffix === 'der') || 69 (suffix === 'p7b') || (suffix === 'spc')) { 70 CmInstallPresenter.getInstance().installCert(fileUri, '', suffix, true); 71 } else { 72 this.unrecognizedFileTips(); 73 } 74 } 75 }) 76 } 77 78 routeToNextInstallEvidence(fileUri: string): void { 79 FileIoModel.getMediaFileSuffix(fileUri, (suffix: string | undefined) => { 80 if (suffix !== undefined) { 81 console.debug(TAG, 'suffix = ', suffix); 82 if ((suffix === 'p12') || (suffix === 'pfx')) { 83 let fileInfo = new RouterFileVo(fileUri, suffix); 84 router.pushUrl({ 85 url: 'pages/certPwdInput', 86 params: fileInfo 87 }) 88 } else { 89 this.unrecognizedFileTips(); 90 } 91 } 92 }) 93 } 94 95 startInstallCert(context: Context): void { 96 if (context === undefined || context === null) { 97 console.error(TAG + 'startInstallCert, context is undefined'); 98 return; 99 } 100 try { 101 let documentSelectOptions = new picker.DocumentSelectOptions(); 102 documentSelectOptions.maxSelectNumber = FilterParams.MAX_SELECT_FILE_NUM; 103 documentSelectOptions.fileSuffixFilters = FilterParams.CERT_FILE_SUFFIX; 104 let documentPicker = new picker.DocumentViewPicker(context); 105 console.info(TAG + 'start documentPicker.select'); 106 documentPicker.select(documentSelectOptions).then((documentSelectResult) => { 107 if (documentSelectResult.length >= 1) { 108 this.routeToNextInstallCert(String(documentSelectResult[0])) 109 } else { 110 console.error(TAG + 'documentPicker.select length invalid:' + documentSelectResult.length); 111 } 112 }).catch((err: BusinessError) => { 113 console.error(TAG + 'documentPicker.select failed with err, message: ' + err.message + ', code: ' + err.code); 114 }); 115 } catch (err) { 116 let e: BusinessError = err as BusinessError; 117 console.error(TAG + 'DocumentViewPicker failed with err, message: ' + e.message + ', code: ' + e.code); 118 } 119 } 120 121 startInstallEvidence(context: Context): void { 122 if (context === undefined || context === null) { 123 console.error(TAG + 'startInstallEvidence, context is undefined'); 124 return; 125 } 126 try { 127 let documentSelectOptions = new picker.DocumentSelectOptions(); 128 documentSelectOptions.maxSelectNumber = FilterParams.MAX_SELECT_FILE_NUM; 129 documentSelectOptions.fileSuffixFilters = FilterParams.CREDENTIAL_FILE_SUFFIX; 130 let documentPicker = new picker.DocumentViewPicker(context); 131 console.info(TAG + 'start documentPicker.select'); 132 documentPicker.select(documentSelectOptions).then((documentSelectResult) => { 133 if (documentSelectResult.length >= 1) { 134 this.routeToNextInstallEvidence(String(documentSelectResult[0])) 135 } else { 136 console.error(TAG + 'documentPicker.select length invalid:' + documentSelectResult.length); 137 } 138 }).catch((err: BusinessError) => { 139 console.error(TAG + 'documentPicker.select failed with err, message: ' + err.message + ', code: ' + err.code); 140 }); 141 } catch (err) { 142 let e: BusinessError = err as BusinessError; 143 console.error(TAG + 'DocumentViewPicker failed with err, message: ' + e.message + ', code: ' + e.code); 144 } 145 } 146 147 startRequestAuth(uri: string): void { 148 let appUidInfo = new RouterAppUidVo(uri); 149 router.pushUrl({ 150 url: 'pages/requestAuth', 151 params: appUidInfo 152 }); 153 } 154 155 uninstallAllCert(): void { 156 certManagerModel.delAllCertOrCred(CMModelOptType.CM_MODEL_OPT_USER_CA, (errCode: CMModelErrorCode) => { 157 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 158 console.info(TAG + 'uninstallAllCert CM_MODEL_OPT_USER_CA success'); 159 } else { 160 console.error(TAG + 'uninstallAllCert CM_MODEL_OPT_USER_CA failed'); 161 } 162 }); 163 164 certManagerModel.delAllCertOrCred(CMModelOptType.CM_MODEL_OPT_APP_CRED, (errCode: CMModelErrorCode) => { 165 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 166 console.info(TAG + 'uninstallAllCert CM_MODEL_OPT_APP_CRED success'); 167 } else { 168 console.error(TAG + 'uninstallAllCert CM_MODEL_OPT_APP_CRED failed'); 169 } 170 }); 171 } 172 173 startInstall(installType: string, fileUri: string) { 174 if (installType === 'CACert') { 175 let titleStr = getContext().resourceManager.getStringSync($r('app.string.Identity_Authentication')); 176 checkUserAuthModel.auth(titleStr, (authResult: boolean) => { 177 if (authResult) { 178 console.info('checkUserAuth success'); 179 this.routeToNextInstallCert(fileUri); 180 } 181 }) 182 } else if (installType === 'userCred') { 183 AppStorage.setOrCreate('installSystemCred', false); 184 AppStorage.setOrCreate('installUserCred', true); 185 this.routeToNextInstallEvidence(fileUri); 186 } else if (installType === 'systemCred') { 187 AppStorage.setOrCreate('installSystemCred', true); 188 AppStorage.setOrCreate('installUserCred', false); 189 this.routeToNextInstallEvidence(fileUri); 190 } else { 191 console.error(TAG, 'The installType is not supported'); 192 } 193 } 194}