/
email.handlers.go
68 lines (52 loc) · 2.32 KB
/
email.handlers.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package handlers
import (
"fmt"
"log"
"github.com/harshsinghvi/golang-fido2-passkeys-api/models"
"github.com/harshsinghvi/golang-fido2-passkeys-api/utils"
"gorm.io/gorm"
)
func SendVerificationMail(db *gorm.DB, verification models.Verification) bool {
var updateVerification = func() bool {
// TODO: check if wokring
if res := db.Save(verification); res.RowsAffected == 0 || res.Error != nil {
log.Println("Error Saving Emai lMessageID, Error, ", res.Error)
return false
}
return true
}
verificationUrl, _ := utils.GenerateVerificationUrl(verification.ID.String(), verification.Code)
if utils.IsEmailDomainTesting(verification.Email) {
verification.EmailMessageID = verificationUrl
return updateVerification()
}
var toEmail, emailSubject, bodyHtml, bodyHtmlTemplate string
// TODO USE CONFIG TO STORE HTML BODY TEMPLATE
switch verification.Type {
case models.VerificationTypeNewUser:
emailSubject = "User Verification FIDO 2"
bodyHtmlTemplate = "<h2> Your User Verification URL : </h2> <a href=\"%s\">%s</a>"
case models.VerificationTypeNewPasskey:
emailSubject = "[Alert] New Passkey registration request"
bodyHtmlTemplate = "<h2> Your Passkey Authorisation URL : </h2> <a href=\"%s\">%s</a> <br> please do not authorize this request if yout have not requested this action."
case models.VerificationTypeDeleteUser:
emailSubject = "[Alert] User and Data Deletion Request"
bodyHtmlTemplate = "<h2> Your User and Data Deletion Request Authorisation URL : </h2> <a href=\"%s\">%s</a> <br> please do not authorize this request if yout have not requested this action."
default:
emailSubject = "[Alert/Verification/Authorization] Request from FIDO 2 System"
bodyHtmlTemplate = "<h2> YourAlert/Verification/Authorization URL : </h2> <a href=\"%s\">%s</a> <br> please do not authorize this request if yout have not requested this action."
}
toEmail = verification.Email
bodyHtml = fmt.Sprintf(bodyHtmlTemplate, verificationUrl, verificationUrl)
ok, messageId := utils.SendMailByElasticMail(toEmail, emailSubject, bodyHtml)
if !ok {
return false
}
verification.EmailMessageID = messageId
// TODO: check if wokring
if res := db.Save(verification); res.RowsAffected == 0 || res.Error != nil {
log.Println("Error Saving EmailMessageID, Error, ", res.Error)
return false
}
return updateVerification()
}