| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- /*
- * Project: MFA Google Auth Plugin
- *
- * Class: TOTPUtil
- *
- * Utility class for handling Time-based One-Time Password (TOTP) operations
- * using Google Authenticator library. Provides methods to generate secrets,
- * create otpauth URLs for QR codes, and verify TOTP codes.
- *
- * Author: Allan Barcelos
- * Date: 2025-07-17
- */
- package io.jenkins.plugins;
- import com.warrenstrange.googleauth.GoogleAuthenticator;
- import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
- import hudson.util.Secret;
- public class TOTPUtil {
- public static GoogleAuthenticatorKey generateSecret() {
- GoogleAuthenticator gAuth = new GoogleAuthenticator();
- return gAuth.createCredentials();
- }
- public static String getQRBarcodeURL(String user, String host, String secret) {
- String issuer = host;
- return String.format("otpauth://totp/%s@%s?secret=%s&issuer=%s", user, host, secret, issuer);
- }
- public static boolean verifyCode(Secret secret, String code) {
- try {
- return verifyCode(secret.getPlainText(), code);
- } catch (Exception e) {
- return false;
- }
- }
- public static boolean verifyCode(String secret, String code) {
- try {
- int codeInt = Integer.parseInt(code);
- GoogleAuthenticator gAuth = new GoogleAuthenticator();
- return gAuth.authorize(secret, codeInt);
- } catch (NumberFormatException e) {
- return false;
- }
- }
- }
|