Code Monkey home page Code Monkey logo

otp's Introduction

otp

build-img pkg-img reportcard-img coverage-img version-img

One time password for Go.

Features

  • Simple API.
  • Dependency-free.
  • Clean and tested code.
  • HOTP RFC 4226.
  • TOTP RFC 6238.

See GUIDE.md for more details.

Install

Go version 1.17+

go get github.com/cristalhq/otp

Example

secretInBase32 := "JBSWY3DPEHPK3PXP"

algo := otp.AlgorithmSHA1
digits := otp.Digits(10)
issuer := "cristalhq"

hotp, err := otp.NewHOTP(algo, digits, issuer)
checkErr(err)

code, err := hotp.GenerateCode(42, secretInBase32)
checkErr(err)

fmt.Println(code)

err = hotp.Validate(code, 42, secretInBase32)
checkErr(err)

// Output:
// 0979090604

Also see examples: examples_test.go.

Documentation

See these docs.

License

MIT License.

otp's People

Contributors

cristaloleg avatar dependabot[bot] avatar s3rj1k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

otp's Issues

Possible secret leak into error from `url.Parse`.

Possible secret leak into error from url.Parse.

https://github.com/cristalhq/otp/blob/main/otp.go#L79

In case of invalid url url.Parse returns error with full string in it, that can leak OTP secret into logs.

This either should be highlighted in docs or plugged-in with custom error messages instead of the one that comes from url.Parse.

Redacted example of error

parse "otpauth://totp/[email protected]:00000000-0000-0000-0000-000000000000?digits=6&secret=***&period=30&algorithm=SHA1&issuer=user%40domain.com\n": net/url: invalid control character in URL

Consider changing `Period` field type to uint in TOTPConfig for better API consistency.

Consider changing Period field type to uint64 (or vice-versa) in TOTPConfig for better API consistency.

https://github.com/cristalhq/otp/blob/main/totp.go#L19

	otpAuthParsed, err := otp.ParseKeyFromURL(otpAuthURL)
	if err != nil {
		return nil, fmt.Errorf("error parsing OTPAuthURL: %w", err)
	}

	totp, err := otp.NewTOTP(otp.TOTPConfig{
		Algo:   otpAuthParsed.Algorithm(),
		Digits: otpAuthParsed.Digits(),
		Issuer: otpAuthParsed.Issuer(),
		Period: uint(otpAuthParsed.Period()),
		Skew:   2,
	})

(P.S. Not big of a deal, will just look prettier)

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.