private UserTOTPKey generateKey(UserContext context, String username)
            throws GuacamoleException {
        
        // Generate random key for user
        TOTPGenerator.Mode mode = confService.getMode();
        UserTOTPKey generated = new UserTOTPKey(username,mode.getRecommendedKeyLength());
        if (setKey(context, generated))
            return generated;

        // Fail if key cannot be set
        return null;
        
    }
Generate and set a new key for the specified user and context, returning
the key if the set successfully or null if it fails.

@param context
    The UserContext of the user whose TOTP key should be generated and set.

@param username
    The username of the user associated with the given UserContext.

@return
    The generated and set key, or null if the operation failed.

@throws GuacamoleException
    If a new key is generated, but the extension storing the associated
    user fails while updating the user account, or if the configuration
    cannot be retrieved.
extensions/guacamole-auth-totp/src/main/java/org/apache/guacamole/auth/totp/user/UserVerificationService.java