Database Reference
In-Depth Information
Notice the difference in this case. Because we are setting static class members, there is no this to
refer to- this is a reference to the current instance, but static classes are not instantiated. So we use this
syntax to set static class member variables from a method arguments:
Class.varName = varName;
Format the User-Input Two-Factor Code
We introduced the checkFormat2Factor() method in the last chapter, but we didn't discuss its
implementation. We want to assure that users who make an effort to enter the two-factor code get some
leniency with regard to format. This is especially important in this chapter, and from now on because we
are delivering the application ID along with the two-factor code to the user's cell phone and other
devices.
We want to assure that we always deliver the two-factor code as the first item in our message. If the
user includes the application name after that, we should find it easy to truncate the two-factor code at
the appropriate place. If extra data comes as separate arguments on the command line, we ignore the
additional arguments.
Occasionally, an old-style pager may drop non-numeric characters from a message. As a general
rule, dashes are always included. Perhaps the pager might also drop spaces and include underscore
characters. In preparation for any of those exigencies, and without burdening the user with our format
requirements, we pass the two-factor code that comes to us in the setAppContext() method on to the
checkFormat2Factor() method in Listing 10-22.
Listing 10-22. Format User-Provided Two-Factor Authentication Code, checkFormat2Factor()
public static String checkFormat2Factor( String twoFactor ) {
String rtrnString = "";
if( null == twoFactor ) return rtrnString;
// Use only numeric values and insert dash after every 4 chars
StringBuffer sB = new StringBuffer();
int used = 0;
char testChar;
int twoFactLen = twoFactor.length();
for( int i = 0; i < twoFactLen; i++ ) {
testChar = twoFactor.charAt( i ) ;
if( Character.isDigit( testChar ) ) {
sB.append( testChar );
if( sB.length() == twoFactorLength ) {
rtrnString = sB.toString();
break;
}
// Insert dash if we have accepted a multiple of 4 chars
used++;
if( 0 == ( used % 4 ) ) sB.append( "-" ) ;
}
}
return rtrnString;
}
This method reads each character of the user-input two-factor code. If a character is not numeric, it
is discarded. Numeric characters are appended to a StringBuffer , and after each four characters, a dash
is appended. This continues until we run out of input or we reach the required two-factor code length,
 
Search WWH ::




Custom Search