MCPcopy Create free account
hub / github.com/gogs/gogs / Create

Method Create

internal/database/two_factors.go:46–69  ·  view source on GitHub ↗

Create creates a new 2FA token and recovery codes for given user. The "key" is used to encrypt and later decrypt given "secret", which should be configured in site-level and change of the "key" will break all existing 2FA tokens.

(ctx context.Context, userID int64, key, secret string)

Source from the content-addressed store, hash-verified

44// configured in site-level and change of the "key" will break all existing 2FA
45// tokens.
46func (s *TwoFactorsStore) Create(ctx context.Context, userID int64, key, secret string) error {
47 encrypted, err := cryptoutil.AESGCMEncrypt(cryptoutil.MD5Bytes(key), []byte(secret))
48 if err != nil {
49 return errors.Wrap(err, "encrypt secret")
50 }
51 tf := &TwoFactor{
52 UserID: userID,
53 Secret: base64.StdEncoding.EncodeToString(encrypted),
54 }
55
56 recoveryCodes, err := generateRecoveryCodes(userID, 10)
57 if err != nil {
58 return errors.Wrap(err, "generate recovery codes")
59 }
60
61 return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
62 err := tx.Create(tf).Error
63 if err != nil {
64 return err
65 }
66
67 return tx.Create(&recoveryCodes).Error
68 })
69}
70
71var _ errutil.NotFound = (*ErrTwoFactorNotFound)(nil)
72

Callers

nothing calls this directly

Calls 3

AESGCMEncryptFunction · 0.92
MD5BytesFunction · 0.92
generateRecoveryCodesFunction · 0.85

Tested by

no test coverage detected