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 ) { // 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 ; } // 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 .