private UserTOTPKey getKey(UserContext context,
            String username) throws GuacamoleException {

        // Retrieve attributes from current user
        User self = context.self();
        Map<String, String> attributes = context.self().getAttributes();

        // If no key is defined, attempt to generate a new key
        String secret = attributes.get(TOTPUser.TOTP_KEY_SECRET_ATTRIBUTE_NAME);
        
        if (secret == null || secret.isEmpty()) 
            return generateKey(context, username);

        // Parse retrieved base32 key value
        byte[] key;
        try {
            key = BASE32.decode(secret);
        }

        // If key is not valid base32, warn but otherwise pretend the key does
        // not exist
        catch (IllegalArgumentException e) {
            logger.warn("TOTP key of user \"{}\" is not valid base32.", self.getIdentifier());
            logger.debug("TOTP key is not valid base32.", e);
            return null;
        }

        // Otherwise, parse value from attributes
        boolean confirmed = "true".equals(attributes.get(TOTPUser.TOTP_KEY_CONFIRMED_ATTRIBUTE_NAME));
        return new UserTOTPKey(username, key, confirmed);

    }
Retrieves and decodes the base32-encoded TOTP key associated with user
having the given UserContext. If no TOTP key is associated with the user,
a random key is generated and associated with the user. If the extension
storing the user does not support storage of the TOTP key, null is
returned.

@param context
    The UserContext of the user whose TOTP key should be retrieved.

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

@return
    The TOTP key associated with the user having the given UserContext,
    or null if the extension storing the user does not support storage
    of the TOTP key.

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