|
| 1 | +package biz |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + cv1 "common/api/common/v1" |
| 6 | + "common/pkg/constant" |
| 7 | + "context" |
| 8 | + "image/png" |
| 9 | + "time" |
| 10 | + "user/internal/biz/repo" |
| 11 | + "user/internal/data/ent" |
| 12 | + "user/internal/data/ent/gen" |
| 13 | + |
| 14 | + "github.com/pquerna/otp/totp" |
| 15 | +) |
| 16 | + |
| 17 | +type TwoFactorAuthenticationDomain struct { |
| 18 | + *BaseDomain |
| 19 | + userRepo repo.UserRepo |
| 20 | +} |
| 21 | + |
| 22 | +func NewTwoFactorAuthenticationDomain(base *BaseDomain, userRepo repo.UserRepo) (*TwoFactorAuthenticationDomain, error) { |
| 23 | + return &TwoFactorAuthenticationDomain{ |
| 24 | + BaseDomain: base, |
| 25 | + userRepo: userRepo, |
| 26 | + }, nil |
| 27 | +} |
| 28 | + |
| 29 | +func (d *TwoFactorAuthenticationDomain) Validate(ctx context.Context, secret string, code string) bool { |
| 30 | + return totp.Validate(code, secret) |
| 31 | +} |
| 32 | + |
| 33 | +func (d *TwoFactorAuthenticationDomain) Enable(ctx context.Context, name string) ([]byte, error) { |
| 34 | + buf := &bytes.Buffer{} |
| 35 | + generate, err := totp.Generate(totp.GenerateOpts{ |
| 36 | + Issuer: d.conf.Server.App, |
| 37 | + AccountName: name, |
| 38 | + }) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + err = d.redis.Client.SetEx(ctx, constant.GetKeyTwoFactorAuthentication(name), generate.Secret(), 5*time.Minute).Err() |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + image, err := generate.Image(256, 256) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + err = png.Encode(buf, image) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + return buf.Bytes(), nil |
| 57 | +} |
| 58 | + |
| 59 | +func (d *TwoFactorAuthenticationDomain) Disable(ctx context.Context, name string, secret string, code string) error { |
| 60 | + if !totp.Validate(code, secret) { |
| 61 | + return cv1.ErrorBadRequest("2FA code invalid") |
| 62 | + } |
| 63 | + err := ent.WithTx(ctx, d.db, func(tx *gen.Client) error { |
| 64 | + _, err := d.userRepo.DisableTwoFactorAuthentication(ctx, tx, name) |
| 65 | + return err |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (d *TwoFactorAuthenticationDomain) Confirm(ctx context.Context, name string, code string) error { |
| 74 | + secret, err := d.redis.Client.Get(ctx, constant.GetKeyTwoFactorAuthentication(name)).Result() |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + if !totp.Validate(code, secret) { |
| 79 | + return cv1.ErrorBadRequest("2FA code invalid") |
| 80 | + } |
| 81 | + err = ent.WithTx(ctx, d.db, func(tx *gen.Client) error { |
| 82 | + _, err = d.userRepo.EnableTwoFactorAuthentication(ctx, tx, name, secret) |
| 83 | + return err |
| 84 | + }) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + return nil |
| 89 | +} |
0 commit comments