GithubHelp home page GithubHelp logo

tominolp / 2fa-api Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 31 KB

A simple API to generate QR codes and validate the user input

License: Apache License 2.0

Java 100.00%
2fa 2fa-client 2fa-security 2factor api java otp-verification otpauth

2fa-api's Introduction

2FA-API

Documentation Status

Introduction

The 2FA-API is a Java library for generating QR codes and validating codes for Two-factor authentication. The library supports both the TOTP (Time-based One-Time Password) algorithm and SHA1 hashing. The library generates a secret key that can be stored securely on the client-side and a QR code that can be displayed to the user.


Usage

You can use the TWOFA-API library in your Java application by following these steps:

  1. Generate a secret key:
String secretKey = AuthSys.generateSecretKey();
  1. Generate a QR code image or bufferd image:
BufferedImage qrCodeImage = AuthSys.generateQrCodeData(secretKey, "YourApp", "username");
Image qrCodeImage = AuthSys.generateQrCode(secretKey, "YourApp", "username");

you can also use the method generateLink to get the link to the QR code image:

String link = AuthSys.generateLink(secretKey, "YourApp", "username");
  1. Display the QR code to the user, so they can scan it with their authenticator app, such as Google Authenticator

  2. Validate the code entered by the user:

boolean isValid = AuthSys.validateCode(secretKey, userEnteredCode);

if you wish to save the QR code image to a file, you can use the method saveQrCode:

AuthSys.saveQrCode(qrCodeImage, fileformat ,"path/to/file");

That's it! You can now use the TWOFA-API library to add Two-factor authentication to your Java application.

NEW!

you can now also generate TOTP codes with the method generateCode:

HashMap<String, String> secretKeys = TOTP.generateCode(timeInSecondsAsInt)

the method returns a HashMap with the secret key as key and the code as value.

now you can send the code to the user via email or something else.

that you can validate the code with the method validateTOTPCode:

boolean isValid = AuthSys.validateTOTPCode(secretKey, code);

you can also get the ExpirationDate of the code with the method getExpirationDate:

Date expirationDate = TOTP.getExpirationDate(secretKey);

Example

import java.awt.*;
import java.awt.image.BufferedImage;

public class Example {
    public static void main(String[] args) {
        String secretKey = AuthSys.generateSecretKey();
        String issuer = "Example Company";
        String account = "[email protected]";

        // Generate the QR code as a BufferedImage
        Image qrCodeImage = AuthSys.generateQrCode(secretKey, issuer, account);

        // Display the QR code image or save it to a file

        // Validate a code
        String code = "123456"; // The code entered by the user
        boolean isValid = AuthSys.validateCode(secretKey, code);
        if (isValid) {
            System.out.println("Code is valid!");
        } else {
            System.out.println("Code is not valid.");
        }
    }
}

Installation

You can install the TWOFA-API library via jitpack by adding the following to your Maven pom.xml file:

Maven

<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>

<dependency>
    <groupId>com.github.TominoLP</groupId>
    <artifactId>2FA-API</artifactId>
    <version>1.1</version>
</dependency>

Gradle

Or, if you're using Gradle, add the following to your build.gradle file:

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.TominoLP:2FA-API:1.1'
}

License

This API is licensed under the Apache License 2.0. click here to read the full license.


Support

add me on discord: Tomino#0101
or: Send an email


Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

2fa-api's People

Contributors

tominolp avatar zeusseingrossopa avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

2fa-api's Issues

Java Version

Could you please define the minimal Java version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.