In this two-part series, we are going to build a web application and a REST API service.
After finishing this series hopefully, we get a better understanding of what TOTP is and how to implement it.
We are going to build the REST API using Java with Micronaut framework and the Web App using the good old html and jQuery.
What is a Time-Based One-Time Password (TOTP)?
Simply put TOTP is a mechanism in which the user is required to enter a token (usually a six-digit numeric), the token itself will refresh after a set of interval defined by the service.
The user can generate the token using 3rd party application such as Google Authenticator.
Rest API
Start a micronaut project using this command
1
mn create-app totp-service --features=yaml
--features=yaml is telling micronaut that we are using the YAML format for our configuration file.
here is what our project’s structure will look like.
for (inti=7; i >= 0; i--) { // Extract the least significant byte from timeInterval timeIntervalBytes[i] = (byte) (timeInterval & 0xFF); // Right shift to process the next byte timeInterval >>= 8; }
/* * The line offset = hash[hash.length - 1] & 0xF; is used to determine the offset into the HMAC hash * from which a 4-byte dynamic binary code will be extracted to generate the TOTP. * This method of determining the offset is specified in the TOTP (RFC 6238) and HOTP (RFC 4226) standards. */ intoffset= hash[hash.length - 1] & 0xF;
/* * The expression hash[offset] & 0x7F uses the hexadecimal value 0x7F to mask * the most significant bit (MSB) of the byte at hash[offset], * ensuring it's set to 0. The reason for this is to make sure that the resulting 32-bit integer * (binaryCode) is treated as a positive number. Reference TOTP (RFC 6238) */ longmostSignificantByte= (hash[offset] & 0x7F) << 24; longsecondMostSignificantByte= (hash[offset + 1] & 0xFF) << 16; longthirdMostSignificantByte= (hash[offset + 2] & 0xFF) << 8; longleastSignificantByte= hash[offset + 3] & 0xFF;