-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from tinh-tinh/feat/ren/3-allow-two2f
#3 feat: support two factor
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package twofa | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pquerna/otp" | ||
"github.com/pquerna/otp/totp" | ||
"github.com/tinh-tinh/tinhtinh/core" | ||
) | ||
|
||
type Totp struct{} | ||
|
||
func (tp *Totp) GenerateCode(secret string, t time.Time) (string, error) { | ||
return totp.GenerateCode(secret, t) | ||
} | ||
|
||
func (tp *Totp) GenerateCodeCustom(secret string, t time.Time, opts totp.ValidateOpts) (string, error) { | ||
return totp.GenerateCodeCustom(secret, t, opts) | ||
} | ||
|
||
func (tp *Totp) ValidateCustom(passcode string, secret string, t time.Time, opts totp.ValidateOpts) (bool, error) { | ||
return totp.ValidateCustom(passcode, secret, t, opts) | ||
} | ||
|
||
func (tp *Totp) Validate(passcode string, secret string) bool { | ||
return totp.Validate(passcode, secret) | ||
} | ||
|
||
func (tp *Totp) Generate(opts totp.GenerateOpts) (*otp.Key, error) { | ||
return totp.Generate(opts) | ||
} | ||
|
||
const TOTP core.Provide = "TOTP" | ||
|
||
func Register() core.Module { | ||
return func(module *core.DynamicModule) *core.DynamicModule { | ||
twoFAModule := module.New(core.NewModuleOptions{}) | ||
|
||
twoFAModule.NewProvider(core.ProviderOptions{ | ||
Name: TOTP, | ||
Value: &Totp{}, | ||
}) | ||
twoFAModule.Export(TOTP) | ||
return twoFAModule | ||
} | ||
} | ||
|
||
func Inject(module *core.DynamicModule) *Totp { | ||
val, ok := module.Ref(TOTP).(*Totp) | ||
if !ok { | ||
return nil | ||
} | ||
return val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package twofa_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/pquerna/otp/totp" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tinh-tinh/auth/twofa" | ||
"github.com/tinh-tinh/tinhtinh/core" | ||
) | ||
|
||
func TestModule(t *testing.T) { | ||
authController := func(module *core.DynamicModule) *core.DynamicController { | ||
ctrl := module.NewController("test") | ||
totpCode := twofa.Inject(module) | ||
|
||
ctrl.Post("", func(ctx core.Ctx) error { | ||
data, err := totpCode.Generate(totp.GenerateOpts{ | ||
Issuer: "Snake Oil", | ||
AccountName: "[email protected]", | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return ctx.JSON(core.Map{ | ||
"data": data.Secret(), | ||
}) | ||
}) | ||
|
||
ctrl.Get("", func(ctx core.Ctx) error { | ||
secret := ctx.Query("secret") | ||
code := ctx.Query("code") | ||
valid := totpCode.Validate(code, secret) | ||
return ctx.JSON(core.Map{ | ||
"data": valid, | ||
}) | ||
}) | ||
|
||
return ctrl | ||
} | ||
|
||
authModule := func(module *core.DynamicModule) *core.DynamicModule { | ||
mod := module.New(core.NewModuleOptions{ | ||
Controllers: []core.Controller{authController}, | ||
}) | ||
|
||
return mod | ||
} | ||
|
||
appModule := func() *core.DynamicModule { | ||
appMod := core.NewModule(core.NewModuleOptions{ | ||
Imports: []core.Module{ | ||
twofa.Register(), | ||
authModule, | ||
}, | ||
}) | ||
|
||
return appMod | ||
} | ||
|
||
app := core.CreateFactory(appModule) | ||
app.SetGlobalPrefix("api") | ||
|
||
testServer := httptest.NewServer(app.PrepareBeforeListen()) | ||
defer testServer.Close() | ||
|
||
testClient := testServer.Client() | ||
|
||
resp, err := testClient.Post(testServer.URL+"/api/test", "application/json", nil) | ||
require.Nil(t, err) | ||
require.Equal(t, 200, resp.StatusCode) | ||
|
||
data, err := io.ReadAll(resp.Body) | ||
require.Nil(t, err) | ||
|
||
type Response struct { | ||
Data interface{} `json:"data"` | ||
} | ||
|
||
var res Response | ||
require.Nil(t, json.Unmarshal(data, &res)) | ||
|
||
resp, err = testClient.Get(testServer.URL + "/api/test?secret=" + res.Data.(string) + "&code=123456") | ||
require.Nil(t, err) | ||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
data, err = io.ReadAll(resp.Body) | ||
require.Nil(t, err) | ||
require.Nil(t, json.Unmarshal(data, &res)) | ||
fmt.Println(res.Data) | ||
} |