1 // Copyright 2023 Google LLC
2 //
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 //    https://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 #ifndef ANONYMOUS_TOKENS_CPP_CLIENT_ANONYMOUS_TOKENS_REDEMPTION_CLIENT_H_
16 #define ANONYMOUS_TOKENS_CPP_CLIENT_ANONYMOUS_TOKENS_REDEMPTION_CLIENT_H_
17 
18 #include <cstdint>
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "absl/container/flat_hash_map.h"
24 #include "absl/status/statusor.h"
25 #include "anonymous_tokens/proto/anonymous_tokens.pb.h"
26 
27 namespace anonymous_tokens {
28 
29 // This class generates AnonymousTokens Redemption request using the anonymous
30 // tokens, their respective plaintext messages and (optional) public metadata.
31 //
32 // A new instance of the AnonymousTokensRedemptionClient is needed for each
33 // redemption request created.
34 //
35 // This class is not thread-safe.
36 class AnonymousTokensRedemptionClient {
37  public:
38   AnonymousTokensRedemptionClient(const AnonymousTokensRedemptionClient&) =
39       delete;
40   AnonymousTokensRedemptionClient& operator=(
41       const AnonymousTokensRedemptionClient&) = delete;
42 
43   // Creates AnonymousTokensRedemptionClient for a valid use case and key
44   // version.
45   static absl::StatusOr<std::unique_ptr<AnonymousTokensRedemptionClient>>
46   Create(AnonymousTokensUseCase use_case, uint64_t key_version);
47 
48   // Creates a redemption request for anonymous tokens against plaintext
49   // messages and public metadatas (if they are set).
50   absl::StatusOr<AnonymousTokensRedemptionRequest>
51   CreateAnonymousTokensRedemptionRequest(
52       const std::vector<RSABlindSignatureTokenWithInput>& tokens_with_inputs);
53 
54   // This method is used to process AnonymousTokensRedemptionResponse and
55   // outputs a comprehensive redemption result.
56   absl::StatusOr<std::vector<RSABlindSignatureRedemptionResult>>
57   ProcessAnonymousTokensRedemptionResponse(
58       const AnonymousTokensRedemptionResponse& redemption_response);
59 
60  private:
61   // Saves plaintext message, public metadata along with the mask to use for
62   // validity checks on the server response as well as correct final processing
63   // of the redemption result.
64   struct RedemptionInfo {
65     PlaintextMessageWithPublicMetadata input;
66     std::string mask;
67   };
68 
69   // Takes in AnonymousTokensUseCase and a key version where the former must not
70   // be undefined and the latter must be greater than 0.
71   //
72   // This constructor is only called from
73   // AnonymousTokensRedemptionClient::Create method.
74   AnonymousTokensRedemptionClient(AnonymousTokensUseCase use_case,
75                                   uint64_t key_version);
76 
77   const std::string use_case_;
78   const uint64_t key_version_;
79   absl::flat_hash_map<std::string, RedemptionInfo> token_to_input_map_;
80 };
81 
82 }  // namespace anonymous_tokens
83 
84 #endif  // ANONYMOUS_TOKENS_CPP_CLIENT_ANONYMOUS_TOKENS_REDEMPTION_CLIENT_H_
85