From 81eabdc0159a1c0c244b1b592a0c8d1a90870e80 Mon Sep 17 00:00:00 2001 From: liujian Date: Mon, 17 Jun 2024 13:01:29 +0800 Subject: [PATCH] Update authenticator --- src/authenticator/README.md | 41 +++++++++++++++++++ .../authenticator.go | 2 +- src/authenticator/authenticator_test.go | 34 +++++++++++++++ .../go.mod | 2 +- .../go.sum | 0 src/googleauthenticator/README.md | 41 ------------------- src/googleauthenticator/authenticator_test.go | 34 --------------- 7 files changed, 77 insertions(+), 77 deletions(-) create mode 100644 src/authenticator/README.md rename src/{googleauthenticator => authenticator}/authenticator.go (99%) create mode 100644 src/authenticator/authenticator_test.go rename src/{googleauthenticator => authenticator}/go.mod (86%) rename src/{googleauthenticator => authenticator}/go.sum (100%) delete mode 100644 src/googleauthenticator/README.md delete mode 100644 src/googleauthenticator/authenticator_test.go diff --git a/src/authenticator/README.md b/src/authenticator/README.md new file mode 100644 index 0000000..178e4c8 --- /dev/null +++ b/src/authenticator/README.md @@ -0,0 +1,41 @@ +> Developed by OpenMix: [https://openmix.org](https://openmix.org/mix-go) + +## Mix Authenticator + +Install + +``` +go get github.com/mix-go/authenticator@latest +``` + +Generate Secret + +```go +secret := authenticator.GenerateSecret() +``` + +Generate Code + +```go +code := authenticator.GenerateToken(secret) +``` + +Verify Code + +```go +ok := authenticator.VerifyToken(secret, code) +// or +ok := authenticator.VerifyTokenCustom(secret, code, 60) +``` + +Generate Url + +```go +uri := authenticator.GenerateTotpUri("Foo", "bar", secret) +// or +url := authenticator.GenerateQRCodeGoogleUrl("Foo", "bar", secret) +``` + +## License + +Apache License Version 2.0, http://www.apache.org/licenses/ diff --git a/src/googleauthenticator/authenticator.go b/src/authenticator/authenticator.go similarity index 99% rename from src/googleauthenticator/authenticator.go rename to src/authenticator/authenticator.go index 6afd7a9..706bc35 100644 --- a/src/googleauthenticator/authenticator.go +++ b/src/authenticator/authenticator.go @@ -1,4 +1,4 @@ -package googleauthenticator +package authenticator import ( "crypto/rand" diff --git a/src/authenticator/authenticator_test.go b/src/authenticator/authenticator_test.go new file mode 100644 index 0000000..db581dd --- /dev/null +++ b/src/authenticator/authenticator_test.go @@ -0,0 +1,34 @@ +package authenticator_test + +import ( + "fmt" + "github.com/mix-go/authenticator" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestVerifyToken(t *testing.T) { + a := assert.New(t) + + secret := authenticator.GenerateSecret() + code := authenticator.GenerateToken(secret) + ok := authenticator.VerifyToken(secret, code) + uri := authenticator.GenerateTotpUri("foo", "bar", secret) + url := authenticator.GenerateQRCodeGoogleUrl("foo", "bar", secret) + fmt.Printf("%v\n%s\n%s\n", ok, uri, url) + + a.Equal(ok, true) +} + +func TestPHPVerifyToken(t *testing.T) { + a := assert.New(t) + + secret := "OQB6ZZGYHCPSX4AK" + code := authenticator.GenerateToken(secret) + ok := authenticator.VerifyToken(secret, code) + uri := authenticator.GenerateTotpUri("foo", "bar", secret) + url := authenticator.GenerateQRCodeGoogleUrl("foo", "bar", secret) + fmt.Printf("%v\n%s\n%s\n", ok, uri, url) + + a.Equal(ok, true) +} diff --git a/src/googleauthenticator/go.mod b/src/authenticator/go.mod similarity index 86% rename from src/googleauthenticator/go.mod rename to src/authenticator/go.mod index 9ee7207..cb91aa7 100644 --- a/src/googleauthenticator/go.mod +++ b/src/authenticator/go.mod @@ -1,4 +1,4 @@ -module github.com/mix-go/googleauthenticator +module github.com/mix-go/authenticator go 1.20 diff --git a/src/googleauthenticator/go.sum b/src/authenticator/go.sum similarity index 100% rename from src/googleauthenticator/go.sum rename to src/authenticator/go.sum diff --git a/src/googleauthenticator/README.md b/src/googleauthenticator/README.md deleted file mode 100644 index 231a9a9..0000000 --- a/src/googleauthenticator/README.md +++ /dev/null @@ -1,41 +0,0 @@ -> Produced by OpenMix: [https://openmix.org](https://openmix.org/mix-go) - -## Mix Google Authenticator - -Install - -``` -go get github.com/mix-go/googleauthenticator@latest -``` - -Generate Secret - -```go -secret := googleauthenticator.GenerateSecret() -``` - -Generate Code - -```go -code := googleauthenticator.GenerateToken(secret) -``` - -Verify Code - -```go -ok := googleauthenticator.VerifyToken(secret, code) -// or -ok := googleauthenticator.VerifyTokenCustom(secret, code, 60) -``` - -Generate Url - -```go -uri := googleauthenticator.GenerateTotpUri("Foo", "bar", secret) -// or -url := googleauthenticator.GenerateQRCodeGoogleUrl("Foo", "bar", secret) -``` - -## License - -Apache License Version 2.0, http://www.apache.org/licenses/ diff --git a/src/googleauthenticator/authenticator_test.go b/src/googleauthenticator/authenticator_test.go deleted file mode 100644 index eae16c8..0000000 --- a/src/googleauthenticator/authenticator_test.go +++ /dev/null @@ -1,34 +0,0 @@ -package googleauthenticator_test - -import ( - "fmt" - "github.com/mix-go/googleauthenticator" - "github.com/stretchr/testify/assert" - "testing" -) - -func TestVerifyToken(t *testing.T) { - a := assert.New(t) - - secret := googleauthenticator.GenerateSecret() - code := googleauthenticator.GenerateToken(secret) - ok := googleauthenticator.VerifyToken(secret, code) - uri := googleauthenticator.GenerateTotpUri("foo", "bar", secret) - url := googleauthenticator.GenerateQRCodeGoogleUrl("foo", "bar", secret) - fmt.Printf("%v\n%s\n%s\n", ok, uri, url) - - a.Equal(ok, true) -} - -func TestPHPVerifyToken(t *testing.T) { - a := assert.New(t) - - secret := "OQB6ZZGYHCPSX4AK" - code := googleauthenticator.GenerateToken(secret) - ok := googleauthenticator.VerifyToken(secret, code) - uri := googleauthenticator.GenerateTotpUri("foo", "bar", secret) - url := googleauthenticator.GenerateQRCodeGoogleUrl("foo", "bar", secret) - fmt.Printf("%v\n%s\n%s\n", ok, uri, url) - - a.Equal(ok, true) -}