Class TOTPService

java.lang.Object
de.bytestore.hostinger.security.service.TOTPService

@Component @Service public class TOTPService extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
    TOTPService(io.jmix.core.security.SystemAuthenticator systemAuthenticator)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    dev.samstevens.totp.qr.QrData
    Generates a QR code for the given user and adds it to the QR data.
    generateRecoveryCodes(User userIO, int amountIO)
    Generates recovery codes for a user.
    Generates a secret using the secret generator.
    getDataUriForImage(dev.samstevens.totp.qr.QrData codeIO)
    Retrieves the data URI for an image generated using the specified codeIO object.
    byte[]
    getImageSRC(dev.samstevens.totp.qr.QrData codeIO)
    Retrieves the image source data of a QR code generated using the specified codeIO object.
    Returns the MIME type of the image generated by the imageGenerator.
    Retrieves the recovery codes associated with a user.
    Retrieves the user with the given username or email from the database.
    boolean
    hasRecoveryCode(User userIO, String codeIO)
    Checks if the given user has a recovery code.
    boolean
    hasTOTP(User userIO)
    Checks if the given user has Time-based One-Time Password (TOTP) authentication enabled.
    boolean
    hasTOTP(String usernameIO)
    Checks if a user has Time-based One-Time Password (TOTP) authentication enabled.
    void
    onApplicationStarted(org.springframework.boot.context.event.ApplicationStartedEvent event)
     
    save(User userIO, dev.samstevens.totp.qr.QrData secretIO)
    Saves the user's information and secret for Time-based One-Time Password (TOTP) authentication.
    void
    saveRecoveryCodes(User userIO, String[] codesIO, TOTPStorage storageIO)
    Saves the recovery codes for a user.
    void
    Updates the verifier for Time-based One-Time Password (TOTP) authentication.
    boolean
    verifyRecoveryCode(User userIO, String codeIO)
    Verifies the recovery code provided by the user.
    boolean
    verifyTOTP(User userIO, Integer otpIO)
    Verifies the Time-based One-Time Password (TOTP) entered by the user.
    boolean
    verifyTOTP(User userIO, Integer otpIO, String secretIO)
    Verifies the Time-based One-Time Password (TOTP) entered by the user.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TOTPService

      public TOTPService(io.jmix.core.security.SystemAuthenticator systemAuthenticator)
  • Method Details

    • onApplicationStarted

      @EventListener public void onApplicationStarted(org.springframework.boot.context.event.ApplicationStartedEvent event)
    • updateVerifier

      public void updateVerifier()
      Updates the verifier for Time-based One-Time Password (TOTP) authentication.

      This method sets the time period and time period discrepancy of the TOTP verifier based on the configuration settings in the TOTPAppSetting class. It logs the changes made to the verifier.

      See Also:
      • TOTPAppSetting#setAllowedTimePeriodDiscrepancy(Integer)
      • TOTPAppSetting#setPeriod(Integer)
      • TOTPAppSetting#getAllowedTimePeriodDiscrepancy()
      • TOTPAppSetting#getPeriod()
    • generateSecret

      public String generateSecret()
      Generates a secret using the secret generator.
      Returns:
      the generated secret.
    • generateCode

      public dev.samstevens.totp.qr.QrData generateCode(User userIO) throws TOTPMailUnset
      Generates a QR code for the given user and adds it to the QR data.
      Parameters:
      userIO - the user object for which the QR code is generated
      Returns:
      the QR data object containing the generated QR code
      Throws:
      TOTPMailUnset
    • getImageSRC

      public byte[] getImageSRC(dev.samstevens.totp.qr.QrData codeIO) throws dev.samstevens.totp.exceptions.QrGenerationException
      Retrieves the image source data of a QR code generated using the specified codeIO object.
      Parameters:
      codeIO - the QR data object representing the QR code
      Returns:
      the byte array containing the image source data
      Throws:
      dev.samstevens.totp.exceptions.QrGenerationException - if there is an error in generating the QR code image
    • getDataUriForImage

      public String getDataUriForImage(dev.samstevens.totp.qr.QrData codeIO) throws dev.samstevens.totp.exceptions.QrGenerationException
      Retrieves the data URI for an image generated using the specified codeIO object.
      Parameters:
      codeIO - the QR data object representing the QR code
      Returns:
      the data URI for the image
      Throws:
      dev.samstevens.totp.exceptions.QrGenerationException - if there is an error in generating the QR code image
    • getMimeType

      public String getMimeType()
      Returns the MIME type of the image generated by the imageGenerator.
      Returns:
      the MIME type of the image
    • verifyTOTP

      public boolean verifyTOTP(User userIO, Integer otpIO)
      Verifies the Time-based One-Time Password (TOTP) entered by the user.
      Parameters:
      userIO - the User object for which the TOTP is to be verified
      otpIO - the TOTP entered by the user
      Returns:
      true if the TOTP is valid, false otherwise
    • verifyTOTP

      public boolean verifyTOTP(User userIO, Integer otpIO, String secretIO)
      Verifies the Time-based One-Time Password (TOTP) entered by the user.
      Parameters:
      userIO - the User object for which the TOTP is to be verified
      otpIO - the TOTP entered by the user
      secretIO - the secret associated with the TOTP (optional, can be null)
      Returns:
      true if the TOTP is valid, false otherwise
    • generateRecoveryCodes

      public String[] generateRecoveryCodes(User userIO, int amountIO)
      Generates recovery codes for a user.
      Parameters:
      userIO - the User object for which recovery codes are generated
      amountIO - the amount of recovery codes to generate
      Returns:
      an array of generated recovery codes
    • saveRecoveryCodes

      public void saveRecoveryCodes(User userIO, String[] codesIO, TOTPStorage storageIO)
      Saves the recovery codes for a user.
      Parameters:
      userIO - The User object representing the user for which recovery codes are saved.
      codesIO - An array of recovery codes to be saved.
    • verifyRecoveryCode

      public boolean verifyRecoveryCode(User userIO, String codeIO)
      Verifies the recovery code provided by the user. This method checks if the recovery code is valid by comparing it with the recovery codes associated with the user.
      Parameters:
      userIO - the User object for which the recovery code is to be verified
      codeIO - the recovery code provided by the user
      Returns:
      true if the recovery code is valid and has been successfully redeemed, false otherwise
    • hasRecoveryCode

      public boolean hasRecoveryCode(User userIO, String codeIO)
      Checks if the given user has a recovery code.
      Parameters:
      userIO - the User object for which the recovery code is to be checked
      codeIO - the recovery code to check
      Returns:
      true if the user has the recovery code, false otherwise
    • getRecoveryCodes

      public String[] getRecoveryCodes(User userIO)
      Retrieves the recovery codes associated with a user.
      Parameters:
      userIO - the User object for which the recovery codes are retrieved
      Returns:
      an array of recovery codes
    • save

      public TOTPStorage save(User userIO, dev.samstevens.totp.qr.QrData secretIO)
      Saves the user's information and secret for Time-based One-Time Password (TOTP) authentication.
      Parameters:
      userIO - the User object representing the user's information
      secretIO - the QrData object containing the secret for TOTP authentication
      Returns:
    • hasTOTP

      public boolean hasTOTP(String usernameIO)
      Checks if a user has Time-based One-Time Password (TOTP) authentication enabled.
      Parameters:
      usernameIO - the username of the user to check for TOTP authentication
      Returns:
      true if the user has TOTP enabled, false otherwise
    • hasTOTP

      public boolean hasTOTP(User userIO)
      Checks if the given user has Time-based One-Time Password (TOTP) authentication enabled.
      Parameters:
      userIO - the User object to check for TOTP authentication
      Returns:
      true if the user has TOTP enabled, false otherwise
    • getUserByUsername

      public User getUserByUsername(String usernameIO)
      Retrieves the user with the given username or email from the database.
      Parameters:
      usernameIO - the username or email of the user to retrieve
      Returns:
      the User object representing the retrieved user, or null if no user is found