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