| 1 | # GOTP - The Golang One-Time Password Library |
| 2 | |
| 3 | [![build-status][build-status]][build-status] ![MIT License][license-badge] |
| 4 | |
| 5 | GOTP is a Golang package for generating and verifying one-time passwords. It can be used to implement two-factor (2FA) or multi-factor (MFA) authentication methods in anywhere that requires users to log in. |
| 6 | |
| 7 | Open MFA standards are defined in [RFC 4226][RFC 4226] (HOTP: An HMAC-Based One-Time Password Algorithm) and in [RFC 6238][RFC 6238] (TOTP: Time-Based One-Time Password Algorithm). GOTP implements server-side support for both of these standards. |
| 8 | |
| 9 | GOTP was inspired by [PyOTP][PyOTP]. |
| 10 | |
| 11 | |
| 12 | ## Installation |
| 13 | |
| 14 | ``` |
| 15 | $ go get github.com/xlzd/gotp |
| 16 | ``` |
| 17 | |
| 18 | ## Usage |
| 19 | |
| 20 | Check API docs at https://godoc.org/github.com/xlzd/gotp |
| 21 | |
| 22 | ### Time-based OTPs |
| 23 | |
| 24 | ```Go |
| 25 | totp := gotp.NewDefaultTOTP("4S62BZNFXXSZLCRO") |
| 26 | totp.Now() // current otp '123456' |
| 27 | totp.At(1524486261) // otp of timestamp 1524486261 '123456' |
| 28 | |
| 29 | # OTP verified for a given timestamp |
| 30 | totp.Verify('492039', 1524486261) // true |
| 31 | totp.Verify('492039', 1520000000) // false |
| 32 | |
| 33 | // generate a provisioning uri |
| 34 | totp.ProvisioningUri("demoAccountName", "issuerName") |
| 35 | // otpauth://totp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&issuer=issuerName |
| 36 | ``` |
| 37 | |
| 38 | ### Counter-based OTPs |
| 39 | |
| 40 | ```Go |
| 41 | hotp := gotp.NewDefaultHOTP("4S62BZNFXXSZLCRO") |
| 42 | hotp.At(0) // '944181' |
| 43 | hotp.At(1) // '770975' |
| 44 | |
| 45 | # OTP verified for a given timestamp |
| 46 | hotp.Verify('944181', 0) // true |
| 47 | hotp.Verify('944181', 1) // false |
| 48 | |
| 49 | // generate a provisioning uri |
| 50 | hotp.ProvisioningUri("demoAccountName", "issuerName", 1) |
| 51 | // otpauth://hotp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&counter=1&issuer=issuerName |
| 52 | ``` |
| 53 | |
| 54 | ### Generate random secret |
| 55 | |
| 56 | ```Go |
| 57 | secretLength := 16 |
| 58 | gotp.RandomSecret(secretLength) // LMT4URYNZKEWZRAA |
| 59 | ``` |
| 60 | |
| 61 | ### Google Authenticator Compatible |
| 62 | |
| 63 | GOTP works with the Google Authenticator iPhone and Android app, as well as other OTP apps like Authy. |
| 64 | GOTP includes the ability to generate provisioning URIs for use with the QR Code |
| 65 | scanner built into these MFA client apps via `otpObj.ProvisioningUri` method: |
| 66 | |
| 67 | ```Go |
| 68 | gotp.NewDefaultTOTP("4S62BZNFXXSZLCRO").ProvisioningUri("demoAccountName", "issuerName") |
| 69 | // otpauth://totp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&issuer=issuerName |
| 70 | |
| 71 | |
| 72 | gotp.NewDefaultHOTP("4S62BZNFXXSZLCRO").ProvisioningUri("demoAccountName", "issuerName", 1) |
| 73 | // otpauth://hotp/issuerName:demoAccountName?secret=4S62BZNFXXSZLCRO&counter=1&issuer=issuerName |
| 74 | ``` |
| 75 | |
| 76 | This URL can then be rendered as a QR Code which can then be scanned and added to the users list of OTP credentials. |
| 77 | |
| 78 | ### Working example |
| 79 | |
| 80 | Scan the following barcode with your phone's OTP app (e.g. Google Authenticator): |
| 81 | |
| 82 |  |
| 83 | |
| 84 | Now run the following and compare the output: |
| 85 | |
| 86 | ```Go |
| 87 | package main |
| 88 | |
| 89 | import ( |
| 90 | "fmt" |
| 91 | "github.com/xlzd/gotp" |
| 92 | ) |
| 93 | |
| 94 | func main() { |
| 95 | fmt.Println("Current OTP is", gotp.NewDefaultTOTP("4S62BZNFXXSZLCRO").Now()) |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | ## License |
| 100 | |
| 101 | GOTP is licensed under the [MIT License][License] |
| 102 | |
| 103 | |
| 104 | [build-status]: https://travis-ci.org/xlzd/gotp.svg?branch=master |
| 105 | [license-badge]: https://img.shields.io/badge/license-MIT-000000.svg |
| 106 | [RFC 4226]: https://tools.ietf.org/html/rfc4226 "RFC 4226" |
| 107 | [RFC 6238]: https://tools.ietf.org/html/rfc6238 "RFC 6238" |
| 108 | [PyOTP]: https://github.com/pyotp/pyotp |
| 109 | [License]: https://github.com/xlzd/gotp/blob/master/LICENSE |