Skip to content

Commit

Permalink
Initial Code
Browse files Browse the repository at this point in the history
  • Loading branch information
ekojs committed May 9, 2022
0 parents commit 56d5cdb
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.gitattributes export-ignore
.gitignore export-ignore
.gitreview export-ignore
.travis.yml export-ignore
phpunit.xml export-ignore
tests/ export-ignore
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
composer.lock
vendor/
build/
.phpunit.result.cache
.phpunit.cache
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Eko Junaidi Salam

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ejsotp
TOTP dan HOTP Library with backup codes, compatible with google authenticator
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "ekojs/otp",
"description": "TOTP dan HOTP Library with backup codes, compatible with google authenticator",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Ekojs\\Otp\\": "src/"
}
},
"authors": [
{
"name": "Eko Junaidi Salam",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"require": {
"spomky-labs/otphp": "^10.0",
"endroid/qr-code": "^4.4",
"furqansiddiqui/bip39-mnemonic-php": "^0.1.4"
}
}
66 changes: 66 additions & 0 deletions src/HOTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php declare(strict_types=1);

/**
* TOTP dan HOTP Library with backup codes, compatible with google authenticator
*
* @author Eko Junaidi Salam <[email protected]>
*/

namespace Ekojs\Otp;

use OTPHP\HOTP as LabHOTP;
use \FurqanSiddiqui\BIP39\BIP39;
use \FurqanSiddiqui\BIP39\Wordlist;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Color\Color;

class HOTP {
public static $instance;
public $otp;
protected $writer;
protected $bip39;
public $parameters = [];

public function __construct(?array $params=null) {
$this->parameters = $params;
$this->otp = LabHOTP::create($params["secret"] ?? null, $params["counter"] ?? 0, $params["digest"] ?? 'sha1', $params["digits"] ?? 6);
$this->writer = new PngWriter();
$this->bip39 = new BIP39();
self::$instance = $this;
}

public static function getInstance(?array $params=null) {
if (self::$instance === null) {
self::$instance = new self($params);
}
return self::$instance;
}

public function getParams(): array {
return $this->parameters;
}

public function generateQr(?string $logo=null, bool $setLabel=false, int $size=200) {
$label = null;
$qrCode = QrCode::create($this->otp->getProvisioningUri());
$qrCode->setSize($size);

if(!empty($logo)) $logo = Logo::create($logo)->setResizeToWidth(50);
if(!empty($this->otp->getLabel()) && $setLabel) $label = Label::create($this->otp->getLabel())->setTextColor(new Color(0, 0, 255));

return $this->writer->write($qrCode, $logo, $label);
}

public function generateBackupCodes(string $entropy, ?WordList $wordList=null) {
$wordList = $wordList ?? WordList::English();
return $this->bip39->wordlist($wordList)->useEntropy(hash("md5",$entropy))->mnemonic();
}

public function reverseMnemonic($words) {
return $this->bip39::Words($words);
}
}

67 changes: 67 additions & 0 deletions src/TOTP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);

/**
* TOTP dan HOTP Library with backup codes, compatible with google authenticator
*
* @author Eko Junaidi Salam <[email protected]>
*/

namespace Ekojs\Otp;

use OTPHP\TOTP as LabTOTP;
use \FurqanSiddiqui\BIP39\BIP39;
use \FurqanSiddiqui\BIP39\Wordlist;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Color\Color;

class TOTP {
public static $instance;
private $mode;
public $otp;
protected $writer;
protected $bip39;
public $parameters = [];

public function __construct(?array $params=null) {
$this->parameters = $params;
$this->otp = LabTOTP::create($params["secret"] ?? null, $params["period"] ?? 30, $params["digest"] ?? 'sha1', $params["digits"] ?? 6, $params["epoch"] ?? 0);
$this->writer = new PngWriter();
$this->bip39 = new BIP39();
self::$instance = $this;
}

public static function getInstance(?array $params=null) {
if (self::$instance === null) {
self::$instance = new self($params);
}
return self::$instance;
}

public function getParams(): array {
return $this->parameters;
}

public function generateQr(?string $logo=null, bool $setLabel=false, int $size=200) {
$label = null;
$qrCode = QrCode::create($this->otp->getProvisioningUri());
$qrCode->setSize($size);

if(!empty($logo)) $logo = Logo::create($logo)->setResizeToWidth(50);
if(!empty($this->otp->getLabel()) && $setLabel) $label = Label::create($this->otp->getLabel())->setTextColor(new Color(0, 0, 255));

return $this->writer->write($qrCode, $logo, $label);
}

public function generateBackupCodes(string $entropy, ?WordList $wordList=null) {
$wordList = $wordList ?? WordList::English();
return $this->bip39->wordlist($wordList)->useEntropy(hash("md5",$entropy))->mnemonic();
}

public function reverseMnemonic($words) {
return $this->bip39::Words($words);
}
}

0 comments on commit 56d5cdb

Please sign in to comment.