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 char EVENT_PUBLISH[] = "event publish"; 47 const char OHOS_ACCOUNT_EVENT_LOGIN[] = "Ohos.account.event.LOGIN"; 48 const char OHOS_ACCOUNT_EVENT_LOGOUT[] = "Ohos.account.event.LOGOUT"; 49 const char OHOS_ACCOUNT_EVENT_TOKEN_INVALID[] = "Ohos.account.event.TOKEN_INVALID"; 50 const char OHOS_ACCOUNT_EVENT_LOGOFF[] = "Ohos.account.event.LOGOFF"; 51 const char OPERATION_INIT_OPEN_FILE_TO_READ[] = "InitOpenFileToRead"; 52 const char OPERATION_REMOVE_FILE[] = "RemoveFile"; 53 const char OPERATION_OPEN_FILE_TO_READ[] = "OpenFileToRead"; 54 const char OPERATION_OPEN_FILE_TO_WRITE[] = "OpenFileToWrite"; 55 const char OPERATION_CHANGE_MODE_FILE[] = "ChangeModeFile"; 56 const char OPERATION_FORCE_CREATE_DIRECTORY[] = "ForceCreateDirectory"; 57 const char 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 char DEFAULT_OHOS_ACCOUNT_NAME[] = "ohosAnonymousName"; // default name 75 const char 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 constexpr std::int32_t ACCOUNT_VERSION_DEFAULT = 0; 80 constexpr std::int32_t ACCOUNT_VERSION_ANON = 1; 81 class OhosAccountInfo { 82 public: 83 std::string name_; 84 std::string uid_; 85 std::int32_t status_; 86 std::int32_t callingUid_ = DEFAULT_CALLING_UID; 87 std::string nickname_; 88 std::string avatar_; 89 AAFwk::Want scalableData_; 90 OhosAccountInfo(const std::string & name,const std::string & id,std::int32_t status)91 OhosAccountInfo(const std::string &name, const std::string &id, std::int32_t status) 92 : name_(name), uid_(id), status_(status), rawUid_(id) 93 { 94 nickname_ = ""; 95 avatar_ = ""; 96 scalableData_ = {}; 97 } 98 OhosAccountInfo()99 OhosAccountInfo() 100 { 101 name_ = ""; 102 uid_ = ""; 103 nickname_ = ""; 104 avatar_ = ""; 105 scalableData_ = {}; 106 status_ = ACCOUNT_STATE_UNBOUND; 107 } 108 ~OhosAccountInfo()109 ~OhosAccountInfo() {}; 110 111 // filtering the input scalableData only GetScalableDataString(const AAFwk::Want & scalableData)112 static std::string GetScalableDataString(const AAFwk::Want &scalableData) 113 { 114 std::string result = ""; 115 AAFwk::WantParams wantParams = scalableData.GetParams(); 116 for (auto it : wantParams.GetParams()) { 117 if (it.first != "moduleName") { 118 result += it.first; 119 int typeId = AAFwk::WantParams::GetDataType(it.second); 120 result += wantParams.GetStringByType(it.second, typeId); 121 } 122 } 123 return result; 124 } 125 IsValid()126 bool IsValid() const 127 { 128 std::string str = GetScalableDataString(scalableData_); 129 return (nickname_.size() <= Constants::NICKNAME_MAX_SIZE) && (avatar_.size() <= Constants::AVATAR_MAX_SIZE) && 130 (str.size() <= Constants::SCALABLEDATA_MAX_SIZE); 131 } 132 GetRawUid()133 std::string GetRawUid() const 134 { 135 return rawUid_; 136 } 137 SetRawUid(std::string rawUid)138 void SetRawUid(std::string rawUid) 139 { 140 rawUid_ = rawUid; 141 } 142 143 private: 144 std::string rawUid_; 145 }; 146 147 class AccountInfo { 148 public: 149 OhosAccountInfo ohosAccountInfo_; 150 std::time_t bindTime_; 151 std::int32_t userId_; 152 std::string digest_; 153 std::int32_t version_; AccountInfo()154 AccountInfo() 155 { 156 bindTime_ = 0; 157 userId_ = 0; 158 digest_ = ""; 159 version_ = ACCOUNT_VERSION_DEFAULT; 160 } 161 AccountInfo(const OhosAccountInfo & ohosAccountInfo)162 explicit AccountInfo(const OhosAccountInfo &ohosAccountInfo) 163 { 164 ohosAccountInfo_ = ohosAccountInfo; 165 bindTime_ = 0; 166 userId_ = 0; 167 digest_ = ""; 168 version_ = ACCOUNT_VERSION_DEFAULT; 169 } 170 171 bool operator==(const AccountInfo &info) 172 { 173 return (ohosAccountInfo_.uid_ == info.ohosAccountInfo_.uid_); 174 } 175 176 void clear(std::int32_t clrStatus = ACCOUNT_STATE_UNBOUND) 177 { 178 ohosAccountInfo_.name_ = DEFAULT_OHOS_ACCOUNT_NAME; 179 ohosAccountInfo_.uid_ = DEFAULT_OHOS_ACCOUNT_UID; 180 ohosAccountInfo_.status_ = clrStatus; 181 ohosAccountInfo_.nickname_ = ""; 182 ohosAccountInfo_.avatar_ = ""; 183 ohosAccountInfo_.scalableData_ = {}; 184 ohosAccountInfo_.callingUid_ = DEFAULT_CALLING_UID; 185 ohosAccountInfo_.SetRawUid(""); 186 digest_.clear(); 187 bindTime_ = 0; 188 } 189 ~AccountInfo()190 ~AccountInfo() {} 191 }; 192 } // namespace AccountSA 193 } // namespace OHOS 194 195 #endif // BASE_ACCOUNT_ACCOUNT_INFO_H 196