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
16/**
17 * @file attributes.h
18 *
19 * @brief Attributes enum define.
20 * @since 3.1
21 * @version 3.2
22 */
23
24#ifndef IAM_ATTRIBUTES_H
25#define IAM_ATTRIBUTES_H
26
27#include <memory>
28#include <string>
29#include <vector>
30
31namespace OHOS {
32namespace UserIam {
33namespace UserAuth {
34class Attributes final {
35public:
36    /**
37     * @brief The key to set attribute.
38     */
39    enum AttributeKey : uint32_t {
40        /** Root tag, the value type is std::vector<uint8_t>. */
41        ATTR_ROOT = 100000,
42        /** Result code, the value type is int32_t. */
43        ATTR_RESULT_CODE = 100001,
44        /** Signature, the value type is std::vector<uint8_t>. */
45        ATTR_SIGNATURE = 100004,
46        /** Identify mode, the value type is uint32_t. */
47        ATTR_IDENTIFY_MODE = 100005,
48        /** Template ID, the value type is uint64_t. */
49        ATTR_TEMPLATE_ID = 100006,
50        /** Template ID list, the value type is std::vector<uint64_t>. */
51        ATTR_TEMPLATE_ID_LIST = 100007,
52        /** Attribute error count, the value type is int32_t. */
53        ATTR_ERROR_COUNT = 100008,
54        /** Remain time, the value type is int32_t. */
55        ATTR_REMAIN_TIMES = 100009,
56        ATTR_REMAIN_ATTEMPTS = ATTR_REMAIN_TIMES,
57        /** Freezing time, the value type is int32_t. */
58        ATTR_FREEZING_TIME = 100010,
59        ATTR_LOCKOUT_DURATION = ATTR_FREEZING_TIME,
60        /** Session ID, the value type is uint64_t. */
61        ATTR_SESSION_ID = 100014,
62        /** Caller package name, the value type is string. */
63        ATTR_CALLER_NAME = 100015,
64        /** Schedule version, the value type is uint32_t. */
65        ATTR_SCHEDULE_VERSION = 100016,
66        /** Schedule ID, the value type is uint64_t. */
67        ATTR_SCHEDULE_ID = 100017,
68        /** Locked templates, the value type is std::vector<uint64_t>. */
69        ATTR_LOCKED_TEMPLATES = 100018,
70        /** Unlocked templates, the value type is std::vector<uint64_t>. */
71        ATTR_UNLOCKED_TEMPLATES = 100019,
72        /** Data, the value type is std::vector<uint8_t>. */
73        ATTR_DATA = 100020,
74        /** Pin subtype, the value type is int32_t. */
75        ATTR_PIN_SUB_TYPE = 100021,
76        /** Schedule mode, the value type is int32_t. */
77        ATTR_SCHEDULE_MODE = 100022,
78        /** Property mode, the value type is uint32_t. */
79        ATTR_PROPERTY_MODE = 100023,
80        /** Authenticate type, the value type is int32_t. */
81        ATTR_AUTH_TYPE = 100024,
82        /** Credential ID, the value type is uint64_t. */
83        ATTR_CREDENTIAL_ID = 100025,
84        /** Caller UID, the value type is uint64_t. */
85        ATTR_CALLER_UID = 100027,
86        /** Tag of result, the value type is std::vector<uint8_t>. */
87        ATTR_RESULT = 100028,
88        /** Capability level, the value type is uint64_t. */
89        ATTR_CAPABILITY_LEVEL = 100029,
90        /** Algorithm information, the value type is uint64_t. */
91        ATTR_ALGORITHM_INFO = 100030,
92        /** Timer stamp, the value type is uint64_t. */
93        ATTR_TIME_STAMP = 100031,
94        /** Root secret, the value type is std::vector<uint8_t>. */
95        ATTR_ROOT_SECRET = 100032,
96        /** Auth token, the value type is std::vector<uint8_t>. */
97        ATTR_AUTH_TOKEN = 100033,
98        /** Security user ID return when add pin credential, the value type is uint64_t. */
99        ATTR_SEC_USER_ID = 100034,
100        /** Enroll progress, the value type is string. */
101        ATTR_ENROLL_PROGRESS = 100035,
102        /** Sensor info, the value type is string. */
103        ATTR_SENSOR_INFO = 100036,
104        /** Key list, the value type is std::vector<uint32_t>. */
105        ATTR_KEY_LIST = 100037,
106        /** End after first fail, the value type is boolean. */
107        ATTR_END_AFTER_FIRST_FAIL = 100038,
108        /** Tip info, the value type is int32_t. */
109        ATTR_TIP_INFO = 100039,
110        /** Old root secret, the value type is std::vector<uint8_t>. */
111        ATTR_OLD_ROOT_SECRET = 100040,
112        /** Old credential ID, the value type is uint64_t. */
113        ATTR_OLD_CREDENTIAL_ID = 100041,
114        /** Source role, the value type is int32_t. */
115        ATTR_SRC_ROLE = 100042,
116        /** User ID, the value type is int32_t. */
117        ATTR_USER_ID = 100043,
118        /** Extra information, the value type is std::vector<uint8_t>. */
119        ATTR_EXTRA_INFO = 100044,
120        /** Executor ID, the value type is uint64_t. */
121        ATTR_EXECUTOR_INDEX = 100045,
122        /** Executor sensor hint, the value type is uint32_t. */
123        ATTR_EXECUTOR_SENSOR_HINT = 100046,
124        /** Executor matcher, the value type is uint32_t. */
125        ATTR_EXECUTOR_MATCHER = 100047,
126        /** Access token ID, the value type is uint32_t. */
127        ATTR_ACCESS_TOKEN_ID = 100048,
128        /** Template change reason, the value type is string. */
129        ATTR_TEMPLATE_CHANGE_REASON = 100049,
130        /** Credential digest, the value type is uint16_t. */
131        ATTR_CREDENTIAL_DIGEST = 100050,
132        /** Credential count, the value type is uint16_t. */
133        ATTR_CREDENTIAL_COUNT = 100051,
134        /** Message sequence number, the value type is uint32_t. */
135        ATTR_MSG_SEQ_NUM = 100052,
136        /** Reply message, the value type is bool. */
137        ATTR_MSG_ACK = 100053,
138        /** Message source UDID, the value type is string. */
139        ATTR_MSG_SRC_UDID = 100054,
140        /** Source of message, the value type is string. */
141        ATTR_MSG_SRC_END_POINT = 100055,
142        /** Destination of message, the value type is string. */
143        ATTR_MSG_DEST_END_POINT = 100056,
144        /** Connection name, the value type is string. */
145        ATTR_CONNECTION_NAME = 100057,
146        /** Message version, the value type is uint32_t. */
147        ATTR_MSG_VERSION = 100058,
148        /** Message type, the value type is int32_t. */
149        ATTR_MSG_TYPE = 100059,
150        /** Message reply sequence number, the value type is uint32_t. */
151        ATTR_MSG_REPLY_SEQ_NUM = 100060,
152        /** Context ID, the value type is uint64_t. */
153        ATTR_CONTEXT_ID = 100061,
154        /** Collector info, the value type is std::vector<uint8_t>. */
155        ATTR_COLLECTOR_INFO = 100062,
156        /** Executor role, the value type is int32_t. */
157        ATTR_EXECUTOR_ROLE = 100063,
158        /** Schedule data, the value type is std::vector<uint8_t>. */
159        ATTR_SCHEDULE_DATA = 100064,
160        /** Signed authentication result, the value type is std::vector<uint8_t>. */
161        ATTR_SIGNED_AUTH_RESULT = 100065,
162        /** Destination role, the value type is int32_t. */
163        ATTR_DEST_ROLE = 100066,
164        /** Local UDID, the value type is string. */
165        ATTR_LOCAL_UDID = 100067,
166        /** Peer UDID, the value type is string. */
167        ATTR_PEER_UDID = 100068,
168        /** Public key, the value type is std::vector<uint8_t>. */
169        ATTR_PUBLIC_KEY = 100069,
170        /** Executor info list, the value type is std::vector<uint8_t>. */
171        ATTR_EXECUTOR_REGISTER_INFO_LIST = 100070,
172        /** Executor secure level, the value type is int32_t. */
173        ATTR_ESL = 100071,
174        /** Challenge, the value type is std::vector<uint8_t>. */
175        ATTR_CHALLENGE = 100072,
176        /** Remote executor info, the value type is std::vector<uint8_t>. */
177        ATTR_REMOTE_EXECUTOR_INFO = 100073,
178        /** Authentication types, the value type is std::vector<int32_t>. */
179        ATTR_AUTH_TYPES = 100074,
180        /** Authentication trust level, the value type is int32_t. */
181        ATTR_AUTH_TRUST_LEVEL = 100075,
182        /** Device UDID, the value type is string. */
183        ATTR_DEVICE_UDID = 100080,
184        /** Collector network id. */
185        ATTR_COLLECTOR_NETWORK_ID = 100081,
186        /** Collector token id. */
187        ATTR_COLLECTOR_TOKEN_ID = 100082,
188        /** Pin expired info, the value type is int64_t and it's max value is 2^50. */
189        ATTR_PIN_EXPIRED_INFO = 100083,
190        /** next fail lockout duration, the value type is int32_t. */
191        ATTR_NEXT_FAIL_LOCKOUT_DURATION = 100084,
192        /** caller type, the value type is int32_t. */
193        ATTR_CALLER_TYPE = 100085,
194        /* auth intention */
195        ATTR_AUTH_INTENTION = 100086,
196        /** Supported message version, the value type is std::vector<uint32_t>. */
197        ATTR_SUPPORTED_MSG_VERSION = 100087,
198    };
199
200    /**
201     * @brief Default constructor.
202     */
203    Attributes();
204
205    /**
206     * @brief Overload constructor.
207     *
208     * This constructor prohibits implicit type conversion of input parameters.
209     *
210     * @param raw The value to be passed in when defining Attribute.
211     */
212    explicit Attributes(const std::vector<uint8_t> &raw);
213
214    /**
215     * @brief Overload constructor.
216     *
217     * This constructor is used to define constant Attribute type.
218     *
219     * @param other The value to be passed in when defining Attribute.
220     */
221    Attributes(const Attributes &other) = delete;
222
223    /**
224     * @brief Overload operator.
225     *
226     * @param other The value to be compared.
227     */
228    Attributes &operator=(const Attributes &other) = delete;
229
230    /**
231     * @brief Overload constructor.
232     *
233     * @param other The value to be passed in when defining Attribute.
234     */
235    Attributes(Attributes &&other) noexcept;
236
237    /**
238     * @brief Overload operator.
239     *
240     * @param other The value to be compared.
241     */
242    Attributes &operator=(Attributes &&other) noexcept;
243
244    /**
245     * @brief Deconstructor.
246     */
247    virtual ~Attributes();
248
249    /**
250     * @brief Set bool value.
251     *
252     * @param key The attribute key.
253     * @param value The bool value.
254     * @return Return success or not(true:success; false:failed).
255     */
256    bool SetBoolValue(AttributeKey key, bool value);
257
258    /**
259     * @brief Set uint64 value.
260     *
261     * @param key The attribute key.
262     * @param value The uint64_t value.
263     * @return Return success or not(true:success; false:failed).
264     */
265    bool SetUint64Value(AttributeKey key, uint64_t value);
266
267    /**
268     * @brief Set uint32_t value.
269     *
270     * @param key The attribute key.
271     * @param value The uint32_t value.
272     * @return Return success or not(true:success; false:failed).
273     */
274    bool SetUint32Value(AttributeKey key, uint32_t value);
275
276    /**
277     * @brief Set uint16_t value.
278     *
279     * @param key The attribute key.
280     * @param value The uint16_t value.
281     * @return Return success or not(true:success; false:failed).
282     */
283    bool SetUint16Value(AttributeKey key, uint16_t value);
284
285    /**
286     * @brief Set uint8_t value.
287     *
288     * @param key The attribute key.
289     * @param value The uint8_t value.
290     * @return Return success or not(true:success; false:failed).
291     */
292    bool SetUint8Value(AttributeKey key, uint8_t value);
293
294    /**
295     * @brief Set int32_t value.
296     *
297     * @param key The attribute key.
298     * @param value The int32_t value.
299     * @return Return success or not(true:success; false:failed).
300     */
301    bool SetInt32Value(AttributeKey key, int32_t value);
302
303    /**
304     * @brief Set int64_t value.
305     *
306     * @param key The attribute key.
307     * @param value The int64_t value.
308     * @return Return success or not(true:success; false:failed).
309     */
310    bool SetInt64Value(AttributeKey key, int64_t value);
311
312    /**
313     * @brief Set string value.
314     *
315     * @param key The attribute key.
316     * @param value The string.
317     * @return Return success or not(true:success; false:failed).
318     */
319    bool SetStringValue(AttributeKey key, const std::string &value);
320
321    /**
322     * @brief Set Attributes value.
323     *
324     * @param key The attribute key.
325     * @param value The attributes type value.
326     * @return Return success or not(true:success; false:failed).
327     */
328    bool SetAttributesValue(AttributeKey key, const Attributes &value);
329    bool SetAttributesArrayValue(AttributeKey key, const std::vector<Attributes> &array);
330
331    /**
332     * @brief Set vector<uint64_t> value.
333     *
334     * @param key The attribute key.
335     * @param value The vector<uint64_t> value.
336     * @return Return success or not(true:success; false:failed).
337     */
338    bool SetUint64ArrayValue(AttributeKey key, const std::vector<uint64_t> &value);
339
340    /**
341     * @brief Set vector<uint32_t> value.
342     *
343     * @param key The attribute key.
344     * @param value The vector<uint32_t> value.
345     * @return Return success or not(true:success; false:failed).
346     */
347    bool SetUint32ArrayValue(AttributeKey key, const std::vector<uint32_t> &value);
348
349    /**
350     * @brief Set vector<uint32_t> value.
351     *
352     * @param key The attribute key.
353     * @param value The vector<uint32_t> value.
354     * @return Return success or not(true:success; false:failed).
355     */
356    bool SetInt32ArrayValue(AttributeKey key, const std::vector<int32_t> &value);
357
358    /**
359     * @brief Set vector<uint16_t> value.
360     *
361     * @param key The attribute key.
362     * @param value The vector<uint16_t> value.
363     * @return Return success or not(true:success; false:failed).
364     */
365    bool SetUint16ArrayValue(AttributeKey key, const std::vector<uint16_t> &value);
366
367    /**
368     * @brief Set vector<uint8_t> value.
369     *
370     * @param key The attribute key.
371     * @param value The vector<uint8_t> value.
372     * @return Return success or not(true:success; false:failed).
373     */
374    bool SetUint8ArrayValue(AttributeKey key, const std::vector<uint8_t> &value);
375
376    /**
377     * @brief Get bool value.
378     *
379     * @param key The attribute key.
380     * @param value Return bool value corresponding to key.
381     * @return Return success or not(true:success; false:failed).
382     */
383    bool GetBoolValue(AttributeKey key, bool &value) const;
384
385    /**
386     * @brief Get uint64_t value.
387     *
388     * @param key The attribute key.
389     * @param value Return uint64_t value corresponding to key.
390     * @return Return success or not(true:success; false:failed).
391     */
392    bool GetUint64Value(AttributeKey key, uint64_t &value) const;
393
394    /**
395     * @brief Get uint32_t value.
396     *
397     * @param key The attribute key.
398     * @param value Return uint32_t value corresponding to key.
399     * @return Return success or not(true:success; false:failed).
400     */
401    bool GetUint32Value(AttributeKey key, uint32_t &value) const;
402
403    /**
404     * @brief Get uint16_t value.
405     *
406     * @param key The attribute key.
407     * @param value Return uint16_t value corresponding to key.
408     * @return Return success or not(true:success; false:failed).
409     */
410    bool GetUint16Value(AttributeKey key, uint16_t &value) const;
411
412    /**
413     * @brief Get uint8_t value.
414     *
415     * @param key The attribute key.
416     * @param value Return uint8_t value corresponding to key.
417     * @return Return success or not(true:success; false:failed).
418     */
419    bool GetUint8Value(AttributeKey key, uint8_t &value) const;
420
421    /**
422     * @brief Get int32_t value.
423     *
424     * @param key The attribute key.
425     * @param value Return int32_t value corresponding to key.
426     * @return Return success or not(true:success; false:failed).
427     */
428    bool GetInt32Value(AttributeKey key, int32_t &value) const;
429
430    /**
431     * @brief Get int64_t value.
432     *
433     * @param key The attribute key.
434     * @param value Return int64_t value corresponding to key.
435     * @return Return success or not(true:success; false:failed).
436     */
437    bool GetInt64Value(AttributeKey key, int64_t &value) const;
438
439    /**
440     * @brief Get string value.
441     *
442     * @param key The attribute key.
443     * @param value Return string corresponding to key.
444     * @return Return success or not(true:success; false:failed).
445     */
446    bool GetStringValue(AttributeKey key, std::string &value) const;
447
448    /**
449     * @brief Get vector<uint64_t> value.
450     *
451     * @param key The attribute key.
452     * @param value Return vector<uint64_t> value corresponding to key.
453     * @return Return success or not(true:success; false:failed).
454     */
455    bool GetUint64ArrayValue(AttributeKey key, std::vector<uint64_t> &value) const;
456
457    /**
458     * @brief Get vector<uint32_t> value.
459     *
460     * @param key The attribute key.
461     * @param value Return vector<uint32_t> value corresponding to key.
462     * @return Return success or not(true:success; false:failed).
463     */
464    bool GetUint32ArrayValue(AttributeKey key, std::vector<uint32_t> &value) const;
465
466    /**
467     * @brief Get vector<uint32_t> value.
468     *
469     * @param key The attribute key.
470     * @param value Return vector<uint32_t> value corresponding to key.
471     * @return Return success or not(true:success; false:failed).
472     */
473    bool GetInt32ArrayValue(AttributeKey key, std::vector<int32_t> &value) const;
474
475    /**
476     * @brief Get vector<uint16_t> value.
477     *
478     * @param key The attribute key.
479     * @param value Return vector<uint16_t> value corresponding to key.
480     * @return Return success or not(true:success; false:failed).
481     */
482    bool GetUint16ArrayValue(AttributeKey key, std::vector<uint16_t> &value) const;
483
484    /**
485     * @brief Get vector<uint8_t> value.
486     *
487     * @param key The attribute key.
488     * @param value Return vector<uint8_t> value corresponding to key.
489     * @return Return success or not(true:success; false:failed).
490     */
491    bool GetUint8ArrayValue(AttributeKey key, std::vector<uint8_t> &value) const;
492
493    /**
494     * @brief Get Attributes value.
495     *
496     * @param key The attribute key.
497     * @param value Return Attributes value corresponding to key.
498     * @return Return success or not(true:success; false:failed).
499     */
500    bool GetAttributesValue(AttributeKey key, Attributes &value) const;
501
502    bool GetAttributesArrayValue(AttributeKey key, std::vector<Attributes> &array) const;
503
504    /**
505     * @brief Serialize the Attribute object.
506     *
507     * @return Return serialized Attribute object.
508     */
509    std::vector<uint8_t> Serialize() const;
510
511    /**
512     * @brief Get all keys of Attribute.
513     *
514     * @return Return all keys of Attribute.
515     */
516    std::vector<AttributeKey> GetKeys() const;
517
518private:
519    class Impl;
520    std::unique_ptr<Impl> impl_ {nullptr};
521};
522} // namespace UserAuth
523} // namespace UserIam
524} // namespace OHOS
525
526#endif // IAM_ATTRIBUTES_H
527