Skip to content

Commit

Permalink
Add 2fa code for upcomming project
Browse files Browse the repository at this point in the history
  • Loading branch information
funniray committed Dec 18, 2019
1 parent dbc15ae commit 29c3221
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 2fa/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
body {
text-align: center;
}

#qrcode > * {
width: 25%;
height: 25%;
margin: auto;
}
13 changes: 13 additions & 0 deletions 2fa/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<head>
<script src="jquery.min.js"></script>
<script src="qrcode.min.js"></script>
<script src="index.js"></script>
<link href="index.css" rel="stylesheet">
</head>
<body>
<p>Enter this code</p>
<h1 id="code"></h1>
<p>Or scan this qrcode</p>
<div id="qrcode"></div>
</body>
7 changes: 7 additions & 0 deletions 2fa/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$(document).ready(()=>{
const qrcode = document.getElementById("qrcode");
const urlParams = new URLSearchParams(window.location.search);
const name = 'MCTest';
new QRCode(qrcode,`otpauth://totp/${name}:${urlParams.get('username')}?secret=${urlParams.get('secret')}&issuer=MCTest`);
document.getElementById("code").innerText = urlParams.get('secret').replace('=','');
});
33 changes: 33 additions & 0 deletions 2fa/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import com.eatthepath.otp.TimeBasedOneTimePasswordGenerator;
import org.apache.commons.codec.binary.Base32;

import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;

public class Main {

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException {
genKey();
//TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator();
//Base32 b32 = new Base32();
//byte [] decodedKey = b32.decode("ZYA4K57WZ5GIO33R5J7MN37ZNA");
//Key key = new SecretKeySpec(decodedKey, 0, decodedKey.length, totp.getAlgorithm());
//Instant now = Instant.now();
//System.out.println(totp.generateOneTimePassword(key,now));
}

public static void genKey() throws NoSuchAlgorithmException, InvalidKeyException {
TimeBasedOneTimePasswordGenerator totp = new TimeBasedOneTimePasswordGenerator();
Base32 b32 = new Base32();
KeyGenerator keyGen = KeyGenerator.getInstance(totp.getAlgorithm());
keyGen.init(128);
Key key = keyGen.generateKey();
Instant now = Instant.now();
System.out.println(b32.encodeToString(key.getEncoded()));
System.out.println(totp.generateOneTimePassword(key,now));
}
}
36 changes: 36 additions & 0 deletions 2fa/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.funniray</groupId>
<artifactId>2fa-test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.eatthepath</groupId>
<artifactId>java-otp</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
</dependencies>

</project>
2 changes: 2 additions & 0 deletions 2fa/jquery.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions 2fa/qrcode.min.js

Large diffs are not rendered by default.

0 comments on commit 29c3221

Please sign in to comment.