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