This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathperson.go
78 lines (62 loc) · 1.83 KB
/
person.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
69
70
71
72
73
74
75
76
77
78
package main
import (
"crypto/sha256"
"fmt"
"log"
"math/rand"
"os"
"time"
sg "github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
type Person struct {
// ID bson.ObjectID `bson:"_id,omitempty"`
Roll string
Email string
Verifier string
EmailToken string
LinkSuffix string
Step1Complete bool
Step1CompletedAt time.Time
Step2Complete bool
Step2CompletedAt time.Time
}
// ENHANCE: Improve the generation of the random seeds
func GetPerson(roll string, email string) Person {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
base := fmt.Sprintf("%s %s %v", roll, email, time.Now().UnixNano())
h := sha256.New()
h.Write([]byte(base))
h.Write([]byte(fmt.Sprintf("%d", r.Uint64())))
link_suffix := fmt.Sprintf("%x", h.Sum(nil))
h.Write([]byte(fmt.Sprintf("%d", r.Uint64())))
verifier := fmt.Sprintf("%x", h.Sum(nil))
h.Write([]byte(fmt.Sprintf("%d", r.Uint64())))
email_tok := fmt.Sprintf("%x", h.Sum(nil))
return Person{
roll,
email,
verifier[:HASH_LEN],
email_tok[:HASH_LEN],
link_suffix[:HASH_LEN],
false,
time.Now(),
false,
time.Now(),
}
}
// ENHANCE: Make the link clickable using HTML content with appropriate markup
func SendVerificationEmail(email string, subject string, suffix string) {
from := mail.NewEmail(os.Getenv("FROM_NAME"), os.Getenv("FROM_EMAIL"))
to := mail.NewEmail("", email)
plainTextContent := fmt.Sprintf("Please visit this URL in a web browser: %s/%s", os.Getenv("BASE_LINK"), suffix)
message := mail.NewSingleEmail(from, subject, to, plainTextContent, plainTextContent)
client := sg.NewSendClient(os.Getenv("SENDGRID_API_KEY"))
response, err := client.Send(message)
if err != nil {
log.Println(err)
} else {
log.Println(response.StatusCode)
log.Printf("Email sent to %s successfully!", email)
}
}