GoogleAuthenticatorKey.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2014, Enrico Maria Crisostomo
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * * Neither the name of the author nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package com.warrenstrange.googleauth;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. /**
  34. * This class is a JavaBean used by the GoogleAuthenticator library to represent
  35. * a secret key.
  36. * <p/>
  37. * This class is immutable.
  38. * <p/>
  39. * Instance of this class should only be constructed by the GoogleAuthenticator
  40. * library.
  41. *
  42. * @author Enrico M. Crisostomo
  43. * @version 1.0
  44. * @see GoogleAuthenticator
  45. * @since 1.0
  46. */
  47. public final class GoogleAuthenticatorKey {
  48. /**
  49. * The format string to generate the URL of a Google-provided QR bar code.
  50. *
  51. * @deprecated Use GoogleAuthenticatorQRGenerator instead.
  52. */
  53. private static final String QR_FORMAT =
  54. "https://chart.googleapis.com/chart?chs=200x200&chld=M%%7C0&cht=qr&"
  55. + "chl=otpauth://totp/%s@%s%%3Fsecret%%3D%s";
  56. /**
  57. * The secret key in Base32 encoding.
  58. */
  59. private final String key;
  60. /**
  61. * The verification code at time = 0 (the UNIX epoch).
  62. */
  63. private final int verificationCode;
  64. /**
  65. * The list of scratch codes.
  66. */
  67. private final List<Integer> scratchCodes;
  68. /**
  69. * The constructor with package visibility.
  70. *
  71. * @param secretKey the secret key in Base32 encoding.
  72. * @param code the verification code at time = 0 (the UNIX epoch).
  73. * @param scratchCodes the list of scratch codes.
  74. */
  75. /* package */ GoogleAuthenticatorKey(
  76. String secretKey, int code,
  77. List<Integer> scratchCodes) {
  78. key = secretKey;
  79. verificationCode = code;
  80. this.scratchCodes = new ArrayList<Integer>(scratchCodes);
  81. }
  82. /**
  83. * Returns the URL of a Google-provided QR barcode to be loaded into the
  84. * Google Authenticator application. The user scans this bar code with the
  85. * application on their smart phones or manually enter the secret manually.
  86. *
  87. * @param user the user to assign the secret key to.
  88. * @param host the host to assign the secret key to.
  89. * @param secret the secret key in Base32 encoding.
  90. * @return the URL of a Google-provided QR barcode to be loaded into the
  91. * Google Authenticator application.
  92. * @deprecated Use GoogleAuthenticatorQRGenerator##getOtpAuthURL instead.
  93. */
  94. @SuppressWarnings("deprecation, unused")
  95. public static String getQRBarcodeURL(String user, String host, String secret) {
  96. return String.format(QR_FORMAT, user, host, secret);
  97. }
  98. /**
  99. * Get the list of scratch codes.
  100. *
  101. * @return the list of scratch codes.
  102. */
  103. public List<Integer> getScratchCodes() {
  104. return scratchCodes;
  105. }
  106. /**
  107. * Returns the secret key in Base32 encoding.
  108. *
  109. * @return the secret key in Base32 encoding.
  110. */
  111. public String getKey() {
  112. return key;
  113. }
  114. /**
  115. * Returns the verification code at time = 0 (the UNIX epoch).
  116. *
  117. * @return the verificationCode at time = 0 (the UNIX epoch).
  118. */
  119. public int getVerificationCode() {
  120. return verificationCode;
  121. }
  122. }