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 CmInstallPresenter.getInstance().installCert(fileUri, '', suffix, true); 70 } else { 71 this.unrecognizedFileTips(); 72 } 73 } 74 }) 75 } 76 77 routeToNextInstallEvidence(fileUri: string): void { 78 FileIoModel.getMediaFileSuffix(fileUri, (suffix: string | undefined) => { 79 if (suffix !== undefined) { 80 console.debug(TAG, 'suffix = ', suffix); 81 if ((suffix === 'p12') || (suffix === 'pfx')) { 82 let fileInfo = new RouterFileVo(fileUri, suffix); 83 router.pushUrl({ 84 url: 'pages/certPwdInput', 85 params: fileInfo 86 }) 87 } else { 88 this.unrecognizedFileTips(); 89 } 90 } 91 }) 92 } 93 94 startInstallCert(context: Context): void { 95 if (context === undefined || context === null) { 96 console.error(TAG + 'startInstallCert, context is undefined'); 97 return; 98 } 99 try { 100 let documentSelectOptions = new picker.DocumentSelectOptions(); 101 documentSelectOptions.maxSelectNumber = FilterParams.MAX_SELECT_FILE_NUM; 102 documentSelectOptions.fileSuffixFilters = FilterParams.CERT_FILE_SUFFIX; 103 let documentPicker = new picker.DocumentViewPicker(context); 104 console.info(TAG + 'start documentPicker.select'); 105 documentPicker.select(documentSelectOptions).then((documentSelectResult) => { 106 if (documentSelectResult.length >= 1) { 107 this.routeToNextInstallCert(String(documentSelectResult[0])) 108 } else { 109 console.error(TAG + 'documentPicker.select length invalid:' + documentSelectResult.length); 110 } 111 }).catch((err: BusinessError) => { 112 console.error(TAG + 'documentPicker.select failed with err, message: ' + err.message + ', code: ' + err.code); 113 }); 114 } catch (err) { 115 let e: BusinessError = err as BusinessError; 116 console.error(TAG + 'DocumentViewPicker failed with err, message: ' + e.message + ', code: ' + e.code); 117 } 118 } 119 120 startInstallEvidence(context: Context): void { 121 if (context === undefined || context === null) { 122 console.error(TAG + 'startInstallEvidence, context is undefined'); 123 return; 124 } 125 try { 126 let documentSelectOptions = new picker.DocumentSelectOptions(); 127 documentSelectOptions.maxSelectNumber = FilterParams.MAX_SELECT_FILE_NUM; 128 documentSelectOptions.fileSuffixFilters = FilterParams.CREDENTIAL_FILE_SUFFIX; 129 let documentPicker = new picker.DocumentViewPicker(context); 130 console.info(TAG + 'start documentPicker.select'); 131 documentPicker.select(documentSelectOptions).then((documentSelectResult) => { 132 if (documentSelectResult.length >= 1) { 133 this.routeToNextInstallEvidence(String(documentSelectResult[0])) 134 } else { 135 console.error(TAG + 'documentPicker.select length invalid:' + documentSelectResult.length); 136 } 137 }).catch((err: BusinessError) => { 138 console.error(TAG + 'documentPicker.select failed with err, message: ' + err.message + ', code: ' + err.code); 139 }); 140 } catch (err) { 141 let e: BusinessError = err as BusinessError; 142 console.error(TAG + 'DocumentViewPicker failed with err, message: ' + e.message + ', code: ' + e.code); 143 } 144 } 145 146 startRequestAuth(uri: string): void { 147 let appUidInfo = new RouterAppUidVo(uri); 148 router.pushUrl({ 149 url: 'pages/requestAuth', 150 params: appUidInfo 151 }); 152 } 153 154 uninstallAllCert(): void { 155 certManagerModel.delAllCertOrCred(CMModelOptType.CM_MODEL_OPT_USER_CA, (errCode: CMModelErrorCode) => { 156 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 157 console.info(TAG + 'uninstallAllCert CM_MODEL_OPT_USER_CA success'); 158 } else { 159 console.error(TAG + 'uninstallAllCert CM_MODEL_OPT_USER_CA failed'); 160 } 161 }); 162 163 certManagerModel.delAllCertOrCred(CMModelOptType.CM_MODEL_OPT_APP_CRED, (errCode: CMModelErrorCode) => { 164 if (errCode === CMModelErrorCode.CM_MODEL_ERROR_SUCCESS) { 165 console.info(TAG + 'uninstallAllCert CM_MODEL_OPT_APP_CRED success'); 166 } else { 167 console.error(TAG + 'uninstallAllCert CM_MODEL_OPT_APP_CRED failed'); 168 } 169 }); 170 } 171 172 startInstall(installType: string, fileUri: string) { 173 if (installType === 'CACert') { 174 let titleStr = getContext().resourceManager.getStringSync($r('app.string.Identity_Authentication')); 175 checkUserAuthModel.auth(titleStr, (authResult: boolean) => { 176 if (authResult) { 177 console.info('checkUserAuth success'); 178 this.routeToNextInstallCert(fileUri); 179 } 180 }) 181 } else if (installType === 'userCred') { 182 AppStorage.setOrCreate('installSystemCred', false); 183 AppStorage.setOrCreate('installUserCred', true); 184 this.routeToNextInstallEvidence(fileUri); 185 } else if (installType === 'systemCred') { 186 AppStorage.setOrCreate('installSystemCred', true); 187 AppStorage.setOrCreate('installUserCred', false); 188 this.routeToNextInstallEvidence(fileUri); 189 } else { 190 console.error(TAG, 'The installType is not supported'); 191 } 192 } 193}