-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
145 lines (129 loc) · 3.66 KB
/
util.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package util
import (
"context"
"crypto/rand"
"fmt"
"net/http"
"net/smtp"
"strings"
"time"
"github.com/geekcamp-vol11-team30/backend/appcontext"
"github.com/geekcamp-vol11-team30/backend/config"
"github.com/geekcamp-vol11-team30/backend/entity"
"github.com/labstack/echo/v4"
"github.com/oklog/ulid/v2"
)
func GenerateULID(ctx context.Context) ulid.ULID {
actx := appcontext.Extract(ctx)
id, _ := ulid.New(ulid.Timestamp(actx.Now), rand.Reader)
return id
}
func GenerateULIDNow() ulid.ULID {
id, _ := ulid.New(ulid.Timestamp(time.Now()), rand.Reader)
return id
}
func ULIDFromString(id string) (ulid.ULID, error) {
id = strings.ToUpper(id)
ulid, err := ulid.Parse(id)
if err != nil {
return ulid, err
}
return ulid, nil
}
func ULIDToString(id ulid.ULID) string {
return strings.ToLower(id.String())
}
func JSONResponse(c echo.Context, code int, data any) error {
return c.JSON(code, echo.Map{
"statusCode": code,
"data": data,
})
}
func SetTokenCookie(c echo.Context, cfg config.Config, token entity.Token) {
c.SetCookie(&http.Cookie{
Name: "accessToken",
Value: token.AccessToken,
Path: "/",
// Domain: "",
Expires: token.AccessTokenExpiredAt,
// RawExpires: "",
// MaxAge: 0,
Secure: cfg.Env != "dev",
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
// Raw: "",
// Unparsed: []string{},
})
c.SetCookie(&http.Cookie{
Name: "refreshToken",
Value: token.RefreshToken,
Path: "/",
// Domain: "",
Expires: token.RefreshTokenExpiredAt,
// RawExpires: "",
// MaxAge: 0,
Secure: cfg.Env != "dev",
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
// Raw: "",
// Unparsed: []string{},
})
}
func DeleteTokenCookie(c echo.Context, cfg config.Config) {
c.SetCookie(&http.Cookie{
Name: "accessToken",
Value: "",
Path: "/",
// Expires: time.Now(),
Secure: cfg.Env != "dev",
HttpOnly: true,
SameSite: http.SameSite(cfg.TokenSameSite),
MaxAge: -1,
})
c.SetCookie(&http.Cookie{
Name: "refreshToken",
Value: "",
Path: "/",
// Expires: time.Now(),
Secure: cfg.Env != "dev",
HttpOnly: true,
SameSite: http.SameSite(cfg.TokenSameSite),
MaxAge: -1,
})
}
func MakeRandomStr(digit int) (string, error) {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// 乱数を生成
b := make([]byte, digit)
if _, err := rand.Read(b); err != nil {
return "", err
}
// letters からランダムに取り出して文字列を生成
var result string
for _, v := range b {
// index が letters の長さに収まるように調整
result += string(letters[int(v)%len(letters)])
}
return result, nil
}
func SendMail(cfg config.Config, targetAddrs string, title string, body string) error {
fmt.Println("send email called!!!!!!", targetAddrs, title, body)
hostname := cfg.SMTP.Host // SMTPサーバーのホスト名
port := cfg.SMTP.Port // SMTPサーバーのポート番号
password := cfg.SMTP.Password
// from := cfg.SMTP.Email // 送信者のメールアドレス
// from = "マジスケ<noreply@magi-sche.net>"
username := cfg.SMTP.User
recipients := []string{targetAddrs} // 送信先のメールアドレス
// auth := smtp.PlainAuth("", username, password, hostname)
auth := smtp.PlainAuth("", username, password, hostname)
msg := []byte(strings.ReplaceAll(fmt.Sprintf(
"From: マジスケ<noreply@magi-sche.net>\nTo: %s\nSubject: %s\n\n%s", strings.Join(recipients, ","), title, body),
"\n", "\r\n"))
// メール送信
err := smtp.SendMail(fmt.Sprintf("%s:%d", hostname, port), auth, "noreply@magi-sche.net", recipients, msg)
if err != nil {
return fmt.Errorf("failed to send email: %w", err)
}
return nil
}