-
Notifications
You must be signed in to change notification settings - Fork 0
/
totp_setup.go
41 lines (33 loc) · 880 Bytes
/
totp_setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package actions
import (
"context"
"fmt"
"image"
"github.com/pquerna/otp/totp"
"github.com/alayton/papers"
)
type TOTPSetupResult struct {
Secret string
QR image.Image
}
// Generates a TOTP secret and QR code to start the TOTP enrollment process
func TOTPSetup(ctx context.Context, p *papers.Papers, user papers.User) (*TOTPSetupResult, error) {
if len(user.GetTOTPSecret()) > 0 {
return nil, papers.ErrTOTPAlreadySetup
}
key, err := totp.Generate(totp.GenerateOpts{
Issuer: p.Config.ApplicationName,
AccountName: user.GetEmail(),
})
if err != nil {
return nil, fmt.Errorf("%w: %v", papers.ErrTOTPGenerateError, err)
}
img, err := key.Image(p.Config.TOTPQRSize, p.Config.TOTPQRSize)
if err != nil {
return nil, fmt.Errorf("%w: %v", papers.ErrTOTPQRError, err)
}
return &TOTPSetupResult{
Secret: key.Secret(),
QR: img,
}, nil
}