token_info_iterator.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #include "../../types/token_info.h"
  3. #include <flipper_format/flipper_format.h>
  4. #include "constants.h"
  5. typedef int TotpIteratorUpdateTokenResult;
  6. typedef TotpIteratorUpdateTokenResult (
  7. *TOTP_ITERATOR_UPDATE_TOKEN_ACTION)(TokenInfo* const token_info, const void* context);
  8. typedef struct TokenInfoIteratorContext TokenInfoIteratorContext;
  9. enum TotpIteratorUpdateTokenResults {
  10. /**
  11. * @brief Token successfully updated
  12. */
  13. TotpIteratorUpdateTokenResultSuccess = 0,
  14. /**
  15. * @brief An error ocurred during updating config file
  16. */
  17. TotpIteratorUpdateTokenResultFileUpdateFailed = -1
  18. };
  19. /**
  20. * @brief Initializes a new token info iterator
  21. * @param storage storage reference
  22. * @param config_file config file to use
  23. * @param iv initialization vector (IV) to be used for encryption\decryption
  24. * @return Token info iterator context
  25. */
  26. TokenInfoIteratorContext*
  27. totp_token_info_iterator_alloc(Storage* storage, FlipperFormat* config_file, uint8_t* iv);
  28. /**
  29. * @brief Navigates iterator to the token with given index
  30. * @param context token info iterator context
  31. * @param token_index token index to navigate to
  32. * @return \c true if navigation succeeded; \c false otherwise
  33. */
  34. bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t token_index);
  35. /**
  36. * @brief Moves current token to a given new index
  37. * @param context token info iterator context
  38. * @param new_index new token index to move current token to
  39. * @return \c true if operation succeeded; \c false otherwise
  40. */
  41. bool totp_token_info_iterator_move_current_token_info(
  42. TokenInfoIteratorContext* context,
  43. size_t new_index);
  44. /**
  45. * @brief Updates current token info using given update action
  46. * @param context token info iterator context
  47. * @param update action which is responsible to make all the necessary updates to token info
  48. * @param update_context update action context
  49. * @return \c true if operation succeeded; \c false otherwise
  50. */
  51. TotpIteratorUpdateTokenResult totp_token_info_iterator_update_current_token(
  52. TokenInfoIteratorContext* context,
  53. TOTP_ITERATOR_UPDATE_TOKEN_ACTION update,
  54. const void* update_context);
  55. /**
  56. * @brief Adds new token info to the end of the list using given update action
  57. * @param context token info iterator context
  58. * @param update action which is responsible to make all the necessary updates to token info
  59. * @param update_context update action context
  60. * @return \c true if operation succeeded; \c false otherwise
  61. */
  62. TotpIteratorUpdateTokenResult totp_token_info_iterator_add_new_token(
  63. TokenInfoIteratorContext* context,
  64. TOTP_ITERATOR_UPDATE_TOKEN_ACTION update,
  65. const void* update_context);
  66. /**
  67. * @brief Remvoves current token info
  68. * @param context token info iterator context
  69. * @return \c true if operation succeeded; \c false otherwise
  70. */
  71. bool totp_token_info_iterator_remove_current_token_info(TokenInfoIteratorContext* context);
  72. /**
  73. * @brief Disposes token info iterator and releases all the resources
  74. * @param context token info iterator context
  75. */
  76. void totp_token_info_iterator_free(TokenInfoIteratorContext* context);
  77. /**
  78. * @brief Gets current token info
  79. * @param context token info iterator context
  80. * @return current token info
  81. */
  82. const TokenInfo*
  83. totp_token_info_iterator_get_current_token(const TokenInfoIteratorContext* context);
  84. /**
  85. * @brief Gets current token info index
  86. * @param context token info iterator context
  87. * @return current token info index
  88. */
  89. size_t totp_token_info_iterator_get_current_token_index(const TokenInfoIteratorContext* context);
  90. /**
  91. * @brief Gets total amount of token infos found
  92. * @param context token info iterator context
  93. * @return amount of token infos found
  94. */
  95. size_t totp_token_info_iterator_get_total_count(const TokenInfoIteratorContext* context);
  96. /**
  97. * @brief Attaches token info iterator to another config file
  98. * @param context token info iterator context
  99. * @param config_file config file reference to attach token info iterator to
  100. */
  101. void totp_token_info_iterator_attach_to_config_file(
  102. TokenInfoIteratorContext* context,
  103. FlipperFormat* config_file);