-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
109 lines (94 loc) 路 2.63 KB
/
route.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
package auth
import (
"fmt"
"time"
"github.com/form3tech-oss/jwt-go"
"github.com/go-dawn/dawn/fiberx"
"github.com/gofiber/fiber/v2"
jwtware "github.com/gofiber/jwt/v2"
)
func (m module) jwt() fiber.Handler {
return jwtware.New(jwtware.Config{
SigningKey: []byte(m.SigningKey),
ErrorHandler: func(c *fiber.Ctx, err error) error {
if err.Error() == "Missing or malformed JWT" {
return fiberx.CodeErr(fiber.StatusBadRequest, err)
}
return fiberx.CodeErr(fiber.StatusBadRequest, err, "Invalid or expired JWT")
},
})
}
type loginForm struct {
// Username is account username, mobile number or email address
Username string `json:"username" validate:"required"`
// Type can be password, mobile or email
Type string `json:"type" validate:"required,oneof=password mobile email"`
// Code can be password, sms code or email code
Code string `json:"code" validate:"required"`
}
func (m module) login(c *fiber.Ctx) (err error) {
var (
data loginForm
id int
t string
)
if err = fiberx.ValidateBody(c, &data); err != nil {
return
}
if id, err = m.authFunc(data.Type)(data.Username, data.Code); err != nil {
return fiberx.CodeErr(fiber.StatusUnauthorized, err, "Failed to authenticate")
}
// Generate encoded token and send it as response.
if t, err = generateToken(m.SigningMethod, m.SigningKey, id, m.Expiration); err != nil {
return err
}
return fiberx.Data(c, t)
}
func (m module) authFunc(tpy string) authenticate {
switch tpy {
case "password":
return m.LoginByPassword
case "mobile":
return m.LoginByMobileCode
case "email":
return m.LoginByEmailCode
default:
return func(username, code string) (i int, err error) {
return 0, fmt.Errorf("auth: invalid authenticate type %s", tpy)
}
}
}
func signingMethod(method string) jwt.SigningMethod {
switch method {
case "", "HS256":
return jwt.SigningMethodHS256
case "HS384":
return jwt.SigningMethodHS384
case "HS512":
return jwt.SigningMethodHS512
case "ES256":
return jwt.SigningMethodES256
case "ES384":
return jwt.SigningMethodES384
case "ES512":
return jwt.SigningMethodES512
case "RS256":
return jwt.SigningMethodRS256
case "RS384":
return jwt.SigningMethodRS384
case "RS512":
return jwt.SigningMethodRS512
default:
return jwt.SigningMethodHS256
}
}
func generateToken(method, key string, id int, expiration time.Duration) (t string, err error) {
// Create token
token := jwt.New(signingMethod(method))
// Set claims
claims := token.Claims.(jwt.MapClaims)
claims["id"] = id
claims["exp"] = time.Now().Add(expiration).Unix()
// Generate encoded token and send it as response.
return token.SignedString([]byte(key))
}