github.com/cozy/cozy-stack@v0.0.0-20240327093429-939e4a21320e/model/instance/lifecycle/two_factor_auth.go (about)

     1  package lifecycle
     2  
     3  import (
     4  	"github.com/cozy/cozy-stack/model/instance"
     5  	"github.com/cozy/cozy-stack/pkg/emailer"
     6  )
     7  
     8  // SendTwoFactorPasscode sends by mail the two factor secret to the owner of
     9  // the instance. It returns the generated token.
    10  func SendTwoFactorPasscode(inst *instance.Instance) ([]byte, error) {
    11  	token, passcode, err := inst.GenerateTwoFactorSecrets()
    12  	if err != nil {
    13  		return nil, err
    14  	}
    15  	err = emailer.SendEmail(inst, &emailer.TransactionalEmailCmd{
    16  		TemplateName:   "two_factor",
    17  		TemplateValues: map[string]interface{}{"TwoFactorPasscode": passcode},
    18  	})
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	return token, nil
    23  }
    24  
    25  // SendMailConfirmationCode send a code to validate the email of the instance
    26  // in order to activate 2FA.
    27  func SendMailConfirmationCode(inst *instance.Instance) error {
    28  	passcode, err := inst.GenerateMailConfirmationCode()
    29  	if err != nil {
    30  		return err
    31  	}
    32  	return emailer.SendEmail(inst, &emailer.TransactionalEmailCmd{
    33  		TemplateName:   "two_factor_mail_confirmation",
    34  		TemplateValues: map[string]interface{}{"TwoFactorActivationPasscode": passcode},
    35  	})
    36  }