https://github.com/leslie-tsang/lua-resty-otp
OTP for OpenResty
https://github.com/leslie-tsang/lua-resty-otp
lua lua-library luajit openresty openresty-lua openresty-module otp otp-library otp-verification otpauth resty
Last synced: about 1 month ago
JSON representation
OTP for OpenResty
- Host: GitHub
- URL: https://github.com/leslie-tsang/lua-resty-otp
- Owner: leslie-tsang
- License: apache-2.0
- Created: 2021-10-30T14:15:53.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-03-13T01:11:50.000Z (2 months ago)
- Last Synced: 2025-04-15T04:17:25.506Z (about 1 month ago)
- Topics: lua, lua-library, luajit, openresty, openresty-lua, openresty-module, otp, otp-library, otp-verification, otpauth, resty
- Language: Lua
- Homepage:
- Size: 24.4 KB
- Stars: 9
- Watchers: 1
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
Name
====lua-resty-otp - Lua OTP lib for OpenResty
Table of Contents
=================* [Name](#name)
* [Status](#status)
* [Usage](#usage)
* [Calculate OTP token](#calculate-otp-token)
* [Generate QR Code](#generate-qr-code)
* [Verify OTP](#verify-otp)
* [Bugs and Patches](#bugs-and-patches)Status
======[](https://github.com/leslie-tsang/lua-resty-otp/actions/workflows/ci-unit-test.yml)
This library is already usable though still highly experimental.
The Lua API is still in flux and may change in the near future without notice.
[Back to TOC](#table-of-contents)
Usage
================
## Calculate OTP token
```lua
local lib_otp = require ("resty.otp")
local TOTP = lib_otp.totp_init("JBSWY3DPEHPK3PXP")
-- UTC format of date `20200101 00:00:00` -> 946656000
-- use `ngx.time()` instead of `946656000` in prod env
ngx.say("TOTP_Token -> ", TOTP:calc_token(946656000))
```> Output
```bash
458138
```[Back to TOC](#table-of-contents)
## Generate QR Code
```lua
local lib_otp = require ("resty.otp")
local TOTP = lib_otp.totp_init("JBSWY3DPEHPK3PXP")
local url = TOTP:get_qr_url('OpenResty-TOTP', '[email protected]')
local html = [[![]()
]]html = string.format(html, url)
ngx.header['Content-Type'] = 'text/html'
ngx.say(html)
```> Output

Scan the QR Code with Google Authenticator
[Back to TOC](#table-of-contents)
## Verify OTP
```lua
local lib_otp = require ("resty.otp")
local TOTP = lib_otp.totp_init("JBSWY3DPEHPK3PXP")
local token = ngx.var.arg_otp
ngx.say("Verify Token : ", TOTP:verify_token(token))
```Use OTP from Google Authenticator
```bash
curl localhost/check?otp=734923
```> Output
```bash
Verify Token : true
```[Back to TOC](#table-of-contents)
Bugs and Patches
================Please report bugs or submit patches by
1. Creating a ticket on the [GitHub Issue Tracker](https://github.com/leslie-tsang/lua-resty-otp/issues).
[Back to TOC](#table-of-contents)