generated from resonatecoop/user-api-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
email.go
59 lines (51 loc) · 1.45 KB
/
email.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
package model
import (
"time"
jwt "github.com/form3tech-oss/jwt-go"
uuid "github.com/google/uuid"
)
// EmailTokenModel is an abstract model which can be used for objects from which
// we derive redirect emails (email confirmation, password reset and such)
type EmailToken struct {
IDRecord
Reference uuid.UUID `bun:"type:uuid,notnull,default:uuid_generate_v4()"`
EmailSent bool `bun:",notnull,default:false"`
EmailSentAt *time.Time
ExpiresAt time.Time `bun:",notnull"`
}
type EmailTokenClaims struct {
Username string `json:"username"`
Reference string `json:"reference"`
jwt.StandardClaims
}
type Email struct {
Recipient string
Subject string
Template string
}
// NewEmailToken creates new OauthEmailToken instance
func NewOauthEmailToken(expiresIn *time.Duration) *EmailToken {
return &EmailToken{
EmailSent: false,
ExpiresAt: time.Now().UTC().Add(*expiresIn),
}
}
// NewOauthEmailTokenClaims creates new NewOauthEmailTokenClaims instance
func NewOauthEmailTokenClaims(email string, emailToken *EmailToken) *EmailTokenClaims {
return &EmailTokenClaims{
Username: email,
Reference: emailToken.Reference.String(),
StandardClaims: jwt.StandardClaims{
// In JWT, the expiry time is expressed as unix milliseconds
ExpiresAt: emailToken.ExpiresAt.Unix(),
},
}
}
// NewOauthEmail
func NewOauthEmail(email, subject, template string) *Email {
return &Email{
Recipient: email,
Subject: subject,
Template: template,
}
}