LCOV - code coverage report
Current view: top level - server/mail - EmailTokenVerifier.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 10 13 76.9 %
Date: 2022-11-19 15:00:39 Functions: 5 7 71.4 %

          Line data    Source code
       1             : // Copyright (C) 2012 The Android Open Source Project
       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             : // 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             : package com.google.gerrit.server.mail;
      16             : 
      17             : import com.google.gerrit.entities.Account;
      18             : import com.google.gerrit.server.account.AuthRequest;
      19             : import com.google.gerrit.server.mail.send.RegisterNewEmailSender;
      20             : 
      21             : /** Verifies the token sent by {@link RegisterNewEmailSender}. */
      22             : public interface EmailTokenVerifier {
      23             :   /**
      24             :    * Construct a token to verify an email address for a user.
      25             :    *
      26             :    * @param accountId the caller that wants to add an email to their account.
      27             :    * @param emailAddress the address to add.
      28             :    * @return an unforgeable string to email to {@code emailAddress}. Presenting the string provides
      29             :    *     proof the user has the ability to read messages sent to that address. Must not be null.
      30             :    */
      31             :   String encode(Account.Id accountId, String emailAddress);
      32             : 
      33             :   /**
      34             :    * Decode a token previously created.
      35             :    *
      36             :    * @param tokenString the string created by encode. Never null.
      37             :    * @return a pair of account id and email address.
      38             :    * @throws InvalidTokenException the token is invalid, expired, malformed, etc.
      39             :    */
      40             :   ParsedToken decode(String tokenString) throws InvalidTokenException;
      41             : 
      42             :   /** Exception thrown when a token does not parse correctly. */
      43             :   class InvalidTokenException extends Exception {
      44             :     private static final long serialVersionUID = 1L;
      45             : 
      46             :     public InvalidTokenException() {
      47           0 :       super("Invalid token");
      48           0 :     }
      49             : 
      50             :     public InvalidTokenException(Throwable cause) {
      51           2 :       super("Invalid token", cause);
      52           2 :     }
      53             :   }
      54             : 
      55             :   /** Pair returned from decode to provide the data used during encode. */
      56             :   class ParsedToken {
      57             :     private final Account.Id accountId;
      58             :     private final String emailAddress;
      59             :     private final AuthRequest.Factory authRequestFactory;
      60             : 
      61             :     public ParsedToken(
      62           2 :         Account.Id accountId, String emailAddress, AuthRequest.Factory authRequestFactory) {
      63           2 :       this.accountId = accountId;
      64           2 :       this.emailAddress = emailAddress;
      65           2 :       this.authRequestFactory = authRequestFactory;
      66           2 :     }
      67             : 
      68             :     public Account.Id getAccountId() {
      69           2 :       return accountId;
      70             :     }
      71             : 
      72             :     public String getEmailAddress() {
      73           2 :       return emailAddress;
      74             :     }
      75             : 
      76             :     public AuthRequest toAuthRequest() {
      77           1 :       return authRequestFactory.createForEmail(getEmailAddress());
      78             :     }
      79             : 
      80             :     @Override
      81             :     public String toString() {
      82           0 :       return accountId + " adds " + emailAddress;
      83             :     }
      84             :   }
      85             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750