API Reference
This document provides details on the core JavaScript modules and functions within GAuth. This information is primarily for developers looking to understand the application's internals or contribute to the project.
gauth.KeyUtilities
The KeyUtilities
module contains the core logic for generating Time-Based One-Time Passwords (TOTP).
generate(secret, [epoch])
Generates a 6-digit OTP code based on the provided secret and time.
-
Parameters:
secret
(String): The Base32 encoded secret key for the account.epoch
(Number, optional): The Unix epoch time in seconds. If not provided, it defaults to the current time.
-
Returns: (String) A zero-padded, 6-digit OTP code.
-
Example:
The following example demonstrates how to generate a code for a known secret and a fixed point in time, as used in the project's test suite.
// Requires jsSHA library to be loaded var keyUtils = new gauth.KeyUtilities(jsSHA); // Secret key for 'alice@google.com (demo account)' var secret = 'JBSWY3DPEHPK3PXP'; // Unix timestamp for 1981-01-01 00:00:00 UTC var epoch = Date.UTC(1981, 1, 1) / 1000.0; var code = keyUtils.generate(secret, epoch); console.log(code); // Outputs: "684675"
gauth.StorageService
This module is a simple wrapper around the browser's localStorage
API, designed to handle JSON object serialization and deserialization.
getObject(key)
Retrieves an item from localStorage
and parses it as JSON.
- Parameters:
key
(String): The key of the item to retrieve.
- Returns: (Object|null) The parsed JavaScript object, or
null
if the key doesn't exist.
setObject(key, value)
Serializes a JavaScript object to a JSON string and saves it to localStorage
.
- Parameters:
key
(String): The key under which to store the value.value
(Object): The JavaScript object to store.
isSupported()
Checks if the browser supports localStorage
.
- Returns: (Boolean)
true
iflocalStorage
is available, otherwisefalse
.
gauth.KeysController
This is the main application controller. It orchestrates the UI, handles user interactions, and uses KeyUtilities
and StorageService
to manage the application's state. It is not intended for external use but is the central point of the application's logic.