1 /* 2 * Copyright (c) 2021-2022 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 16 #ifndef BASE_ACCOUNT_ACCOUNT_INFO_H 17 #define BASE_ACCOUNT_ACCOUNT_INFO_H 18 19 #include <ctime> 20 #include <string> 21 #include "account_error_no.h" 22 #include "ohos_account_constants.h" 23 #include "string_ex.h" 24 #include "want.h" 25 26 namespace OHOS { 27 namespace AccountSA { 28 29 /** 30 * Account state type . 31 */ 32 typedef enum : std::int32_t { 33 // invalid state 34 ACCOUNT_STATE_INVALID = -1, 35 // no account bound 36 ACCOUNT_STATE_UNBOUND = 0, 37 // account login 38 ACCOUNT_STATE_LOGIN, 39 // account logout but not unbound 40 ACCOUNT_STATE_NOTLOGIN, 41 // account logoff, all the data of this account will be deleted in the network 42 ACCOUNT_STATE_LOGOFF, 43 } OHOS_ACCOUNT_STATE; 44 45 // event string 46 const std::string EVENT_PUBLISH = "event publish"; 47 const std::string OHOS_ACCOUNT_EVENT_LOGIN = "Ohos.account.event.LOGIN"; 48 const std::string OHOS_ACCOUNT_EVENT_LOGOUT = "Ohos.account.event.LOGOUT"; 49 const std::string OHOS_ACCOUNT_EVENT_TOKEN_INVALID = "Ohos.account.event.TOKEN_INVALID"; 50 const std::string OHOS_ACCOUNT_EVENT_LOGOFF = "Ohos.account.event.LOGOFF"; 51 const std::string OPERATION_INIT_OPEN_FILE_TO_READ = "InitOpenFileToRead"; 52 const std::string OPERATION_REMOVE_FILE = "RemoveFile"; 53 const std::string OPERATION_OPEN_FILE_TO_READ = "OpenFileToRead"; 54 const std::string OPERATION_OPEN_FILE_TO_WRITE = "OpenFileToWrite"; 55 const std::string OPERATION_CHANGE_MODE_FILE = "ChangeModeFile"; 56 const std::string OPERATION_FORCE_CREATE_DIRECTORY = "ForceCreateDirectory"; 57 const std::string OPERATION_CHANGE_MODE_DIRECTORY = "ChangeModeDirectory"; 58 /** 59 * Account operation events 60 */ 61 typedef enum : std::int32_t { 62 ACCOUNT_INVALID_EVT = -1, // invalid account event 63 ACCOUNT_BIND_SUCCESS_EVT = 0, // bind account successfully 64 ACCOUNT_BIND_FAILED_EVT, // bind account failed 65 ACCOUNT_AUTHENTICATE_SUCCESS_EVT, // authenticate account successfully 66 ACCOUNT_AUTHENTICATE_FAILED_EVT, // authenticate account failed 67 ACCOUNT_TOKEN_EXPIRED_EVT, // local token of account expired 68 ACCOUNT_PASSWORD_CHANGED_EVT, // account password changed in remount server 69 ACCOUNT_MANUAL_LOGOUT_EVT, // account logout manually 70 ACCOUNT_MANUAL_UNBOUND_EVT, // account unbound manually 71 ACCOUNT_MANUAL_LOGOFF_EVT, // account logoff manually 72 } ACCOUNT_INNER_EVENT_TYPE; 73 74 const std::string DEFAULT_OHOS_ACCOUNT_NAME = "ohosAnonymousName"; // default name 75 const std::string DEFAULT_OHOS_ACCOUNT_UID = "ohosAnonymousUid"; // default UID 76 constexpr std::int32_t UID_TRANSFORM_DIVISOR = 200000; // local account id = uid / UID_TRANSFORM_DIVISOR 77 constexpr std::int32_t MAIN_OS_ACCOUNT_LOCAL_ID = 100; // main os account local id = 100 78 constexpr std::int32_t DEFAULT_CALLING_UID = -1; // main os account local id = 100 79 80 class OhosAccountInfo { 81 public: 82 std::string name_; 83 std::string uid_; 84 std::int32_t status_; 85 std::int32_t callingUid_ = DEFAULT_CALLING_UID; 86 std::string nickname_; 87 std::string avatar_; 88 AAFwk::Want scalableData_; 89 OhosAccountInfo(const std::string & name,const std::string & id,std::int32_t status)90 OhosAccountInfo(const std::string &name, const std::string &id, std::int32_t status) 91 : name_(name), uid_(id), status_(status), rawUid_(id) 92 { 93 nickname_ = ""; 94 avatar_ = ""; 95 scalableData_ = {}; 96 } 97 OhosAccountInfo()98 OhosAccountInfo() 99 { 100 name_ = ""; 101 uid_ = ""; 102 nickname_ = ""; 103 avatar_ = ""; 104 scalableData_ = {}; 105 status_ = ACCOUNT_STATE_UNBOUND; 106 } 107 ~OhosAccountInfo()108 ~OhosAccountInfo() {}; 109 110 // filtering the input scalableData only GetScalableDataString(const AAFwk::Want & scalableData)111 static std::string GetScalableDataString(const AAFwk::Want &scalableData) 112 { 113 std::string result = ""; 114 AAFwk::WantParams wantParams = scalableData.GetParams(); 115 for (auto it : wantParams.GetParams()) { 116 if (it.first != "moduleName") { 117 result += it.first; 118 int typeId = AAFwk::WantParams::GetDataType(it.second); 119 result += wantParams.GetStringByType(it.second, typeId); 120 } 121 } 122 return result; 123 } 124 IsValid()125 bool IsValid() const 126 { 127 std::string str = GetScalableDataString(scalableData_); 128 return (nickname_.size() <= Constants::NICKNAME_MAX_SIZE) && (avatar_.size() <= Constants::AVATAR_MAX_SIZE) && 129 (str.size() <= Constants::SCALABLEDATA_MAX_SIZE); 130 } 131 GetRawUid()132 std::string GetRawUid() const 133 { 134 return rawUid_; 135 } 136 SetRawUid(std::string rawUid)137 void SetRawUid(std::string rawUid) 138 { 139 rawUid_ = rawUid; 140 } 141 142 private: 143 std::string rawUid_; 144 }; 145 146 class AccountInfo { 147 public: 148 OhosAccountInfo ohosAccountInfo_; 149 std::time_t bindTime_; 150 std::int32_t userId_; 151 std::string digest_; AccountInfo()152 AccountInfo() 153 { 154 bindTime_ = 0; 155 userId_ = 0; 156 digest_ = ""; 157 } 158 AccountInfo(const OhosAccountInfo & ohosAccountInfo)159 explicit AccountInfo(const OhosAccountInfo &ohosAccountInfo) 160 { 161 ohosAccountInfo_ = ohosAccountInfo; 162 bindTime_ = 0; 163 userId_ = 0; 164 digest_ = ""; 165 } 166 167 bool operator==(const AccountInfo &info) 168 { 169 return (ohosAccountInfo_.uid_ == info.ohosAccountInfo_.uid_); 170 } 171 172 void clear(std::int32_t clrStatus = ACCOUNT_STATE_UNBOUND) 173 { 174 ohosAccountInfo_.name_ = DEFAULT_OHOS_ACCOUNT_NAME; 175 ohosAccountInfo_.uid_ = DEFAULT_OHOS_ACCOUNT_UID; 176 ohosAccountInfo_.status_ = clrStatus; 177 ohosAccountInfo_.nickname_ = ""; 178 ohosAccountInfo_.avatar_ = ""; 179 ohosAccountInfo_.scalableData_ = {}; 180 ohosAccountInfo_.callingUid_ = DEFAULT_CALLING_UID; 181 ohosAccountInfo_.SetRawUid(""); 182 digest_.clear(); 183 bindTime_ = 0; 184 } 185 ~AccountInfo()186 ~AccountInfo() {} 187 }; 188 } // namespace AccountSA 189 } // namespace OHOS 190 191 #endif // BASE_ACCOUNT_ACCOUNT_INFO_H 192