File: user.h

package info (click to toggle)
megacmd 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,592 kB
  • sloc: cpp: 326,437; ansic: 34,524; python: 4,630; java: 3,965; sh: 2,869; objc: 2,459; makefile: 197; xml: 113
file content (259 lines) | stat: -rw-r--r-- 9,493 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
 * @file mega/user.h
 * @brief Class for manipulating user / contact data
 *
 * (c) 2013-2014 by Mega Limited, Auckland, New Zealand
 *
 * This file is part of the MEGA SDK - Client Access Engine.
 *
 * Applications using the MEGA API must present a valid application key
 * and comply with the the rules set forth in the Terms of Service.
 *
 * The MEGA SDK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @copyright Simplified (2-clause) BSD License.
 *
 * You should have received a copy of the license along with this
 * program.
 */

#ifndef MEGA_USER_H
#define MEGA_USER_H 1

#include "attrmap.h"

namespace mega {

class UserAttribute;
class UserAttributeManager;

// user/contact
struct MEGA_API User : public Cacheable
{
    // user handle
    handle userhandle;

    // string identifier for API requests (either e-mail address or ASCII user
    // handle)
    string uid;

    // e-mail address
    string email;

    // visibility status
    visibility_t show;

    // shares by this user
    handle_set sharing;

    // contact establishment timestamp
    m_time_t ctime;

    BizMode mBizMode = BIZ_MODE_UNKNOWN;

    struct
    {
        bool keyring : 1;   // private keys
        bool authring : 1;  // authentication information of the contact (signing key)
        bool authcu255 : 1; // authentication information of the contact (Cu25519 key)
        bool lstint : 1;    // last interaction with the contact
        bool puEd255 : 1;   // public key for Ed25519
        bool puCu255 : 1;   // public key for Cu25519
        bool sigPubk : 1;   // signature for RSA public key
        bool sigCu255 : 1;  // signature for Cu255199 public key
        bool avatar : 1;    // avatar image
        bool firstname : 1;
        bool lastname : 1;
        bool country : 1;
        bool birthday : 1;      // wraps status of birthday, birthmonth, birthyear
        bool email : 1;
        bool language : 1;      // preferred language code
        bool pwdReminder : 1;   // password-reminder-dialog information
        bool disableVersions : 1;   // disable fileversioning
        bool noCallKit : 1;   // disable CallKit
        bool contactLinkVerification : 1; // Verify contact requests with contact links
        bool richPreviews : 1;  // enable messages with rich previews
        bool lastPsa : 1;
        bool rubbishTime : 1;   // days to keep nodes in rubbish bin before auto clean
        bool storageState : 1;  // state of the storage (0 = green, 1 = orange, 2 = red)
        bool geolocation : 1;   // enable send geolocations
        bool cameraUploadsFolder : 1;   // target folder for Camera Uploads
        bool myChatFilesFolder : 1;   // target folder for my chat files
        bool pushSettings : 1;  // push notification settings
        bool alias : 1; // user's aliases
        bool unshareablekey : 1;    // key to encrypt unshareable node attributes
        bool devicenames : 1; // device or external drive names
        bool myBackupsFolder : 1; // target folder for My Backups
        bool cookieSettings : 1; // bit map to indicate whether some cookies are enabled or not
        bool jsonSyncConfigData : 1;
        bool drivenames : 1;    // drive names
        bool keys : 1;
        bool aPrefs : 1;    // apps preferences
        bool ccPrefs : 1;   // content consumption preferences
        bool enableTestNotifications : 1; // list of IDs for enabled notifications
        bool lastReadNotification : 1; // ID of last read notification
        bool lastActionedBanner : 1; // ID of last actioner banner
        bool enableTestSurveys: 1; // list of handles for enabled test surveys
        bool recentClearTimestamp: 1; // timestamp of clearing recent action history
    } changed;

    // user's public key
    AsymmCipher pubk;

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4201) // nameless struct
#endif
    struct
    {
        bool pubkrequested : 1;
        bool isTemporary : 1;
    };
#ifdef _MSC_VER
#pragma warning(pop)
#endif

    // actions to take after arrival of the public key
    deque<std::unique_ptr<PubKeyAction>> pkrs;

private:
    std::unique_ptr<UserAttributeManager> mAttributeManager;

    // source tag
    int tag;

public:
    void set(visibility_t, m_time_t);

    bool serialize(string*) const override;
    static User* unserialize(class MegaClient *, string*);
    bool unserializeAttributes(const char*& from, const char* upTo, char formatVersion);

    void removepkrs(MegaClient*);

    // attribute methods: set/get/expire...
    void setAttribute(attr_t at, const string& value, const string& version);
    bool updateAttributeIfDifferentVersion(attr_t at, const string& value, const string& version);
    void setAttributeExpired(attr_t at);
    const UserAttribute* getAttribute(attr_t at) const;
    void removeAttribute(attr_t at);
    void removeAttributeUpdateVersion(attr_t at, const string& version); // remove in up2/upv V3 ?

    // Set this to avoid requesting attributes already known to not exist from server.
    void cacheNonExistingAttributes();

    static string attr2string(attr_t at);
    static string attr2longname(attr_t at);
    static attr_t string2attr(const char *name);
    static int needversioning(attr_t at);
    static char scope(attr_t at);
    static bool isAuthring(attr_t at);
    static size_t getMaxAttributeSize(attr_t at);

    enum {
        PWD_LAST_SUCCESS = 0x01,
        PWD_LAST_SKIPPED = 0x02,
        PWD_MK_EXPORTED = 0x04,
        PWD_DONT_SHOW = 0x08,
        PWD_LAST_LOGIN = 0x10
    };

    static const int PWD_SHOW_AFTER_ACCOUNT_AGE = 7 * 24 * 60 * 60;
    static const int PWD_SHOW_AFTER_LASTSUCCESS = 3 * 30 * 24 * 60 * 60;
    static const int PWD_SHOW_AFTER_LASTLOGIN = 14 * 24 * 60 * 60;
    static const int PWD_SHOW_AFTER_LASTSKIP = 3 * 30 * 24 * 60 * 60;
    static const int PWD_SHOW_AFTER_LASTSKIP_LOGOUT = 1 * 30 * 24 * 60 * 60;

    static bool mergePwdReminderData(int numDetails, const char *data, unsigned int size, string *newValue);
    static m_time_t getPwdReminderData(int numDetail, const char *data, unsigned int size);

    bool setChanged(attr_t at);

    void setTag(int newTag);
    int getTag();
    void resetTag();

    User(const char* = NULL);
    ~User() override;

    // merges the new values in the given destination. Returns true if it was changed.
    static bool mergeUserAttribute(attr_t type,
                                   const string_map& newValuesMap,
                                   string_map& destination);
    static string attributePrefixInTLV(attr_t type, bool modifier);
};

class AuthRing
{
public:
    // create authring of 'type' from the encrypted data
    AuthRing(attr_t type, const string_map& authring = {});

    // create authring of 'type' from the TLV value (undecrypted already, no Type nor Length)
    AuthRing(attr_t type, const string& authring);

    // return true if authring has changed (data can be pubKey or keySignature depending on authMethod)
    void add(handle uh, const std::string &fingerprint, AuthMethod authMethod);

    // assumes the key is already tracked for uh (otherwise, it will throw)
    void update(handle uh, AuthMethod authMethod);

    // return the authring as tlv container, ready to set as user's attribute [*!authring | *!authCu255 | *!authRSA]
    std::string *serialize(PrnGen &rng, SymmCipher &key) const;

    // return a binary buffer compatible with Webclient, to store authrings in user's attribute ^!keys
    std::string serializeForJS() const;

    // false if uh is not tracked in the authring
    bool isTracked(handle uh) const;

    // true for Cu25519 and RSA, false for Ed25519
    bool isSignedKey() const;

    // true if key is tracked and authentication method is fingerprint/signature-verified
    bool areCredentialsVerified(handle uh) const;

    // returns AUTH_METHOD_UNKNOWN if no authentication is found for the given user
    AuthMethod getAuthMethod(handle uh) const;

    // returns the fingerprint of the public key for a given user, or empty string if user is not found
    string getFingerprint(handle uh) const;

    // returns the list of tracked users
    vector<handle> getTrackedUsers() const;

    // returns most significant 160 bits from SHA256, whether in binary or hexadecimal
    static string fingerprint(const string &pubKey, bool hexadecimal = false);

    // returns the authring type for a given attribute type associated to a public key
    static attr_t keyTypeToAuthringType(attr_t at);

    // returns the authring type for a given attribute type associated to a signature
    static attr_t signatureTypeToAuthringType(attr_t at);

    // returns the attribute type associated to the corresponding signature for a given authring type
    static attr_t authringTypeToSignatureType(attr_t at);

    // returns a human-friendly string for a given authentication method
    static string authMethodToStr(AuthMethod authMethod);

    static string toString(const AuthRing& authRing);

    bool needsUpdate() const { return mNeedsUpdate; }

private:
    attr_t mType;
    map<handle, string> mFingerprint;
    map<handle, AuthMethod> mAuthMethod;

    // indicates if the authring has changed and needs to update value in server
    bool mNeedsUpdate = false;

    bool deserialize(const std::string &authValue);
};

} // namespace

#endif