-
Notifications
You must be signed in to change notification settings - Fork 283
/
authentication.go
93 lines (82 loc) · 2.58 KB
/
authentication.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
package middleware
import (
/* #nosec */
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/eleme/lindb/models"
"github.com/dgrijalva/jwt-go"
)
// UserAuthentication represents authentication param
type UserAuthentication struct {
user models.User
}
// CustomClaims represents jwt custom claims param
// need username and password and some standard claims
type CustomClaims struct {
jwt.StandardClaims
UserName string `json:"username"`
Password string `json:"password"`
}
// Valid rewrites jwt.Claims valid method return nil
func (*CustomClaims) Valid() error {
return nil
}
// NewUserAuthentication creates authentication api instance
func NewUserAuthentication(user models.User) *UserAuthentication {
return &UserAuthentication{
user: user,
}
}
// ValidateTokenMiddleware creates middleware for user permissions validation by request header Authorization
// if not authorization throw error
// else perform the next action
func (u *UserAuthentication) ValidateMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if len(token) == 0 {
err := errors.New("header cannot have authorization")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
b, _ := json.Marshal(err.Error())
_, _ = w.Write(b)
return
}
claims, _ := ParseToken(token, u.user)
if claims.UserName == u.user.UserName && claims.Password == u.user.Password {
next.ServeHTTP(w, r)
}
})
}
// ParseToken returns jwt claims by token
// get secret key use Md5Encrypt method with username and password
// then jwt parse token by secret key
func ParseToken(tokenString string, user models.User) (*CustomClaims, error) {
claims := CustomClaims{}
cid := Md5Encrypt(user)
_, _ = jwt.ParseWithClaims(tokenString, &claims, func(token *jwt.Token) (interface{}, error) {
return cid, nil
})
return &claims, nil
}
// CreateLToken returns token use jwt with custom claims
func CreateToken(user models.User) (string, error) {
claims := CustomClaims{
UserName: user.UserName,
Password: user.Password,
}
cid := Md5Encrypt(user)
token := jwt.NewWithClaims(jwt.SigningMethodHS256, &claims)
return token.SignedString([]byte(cid))
}
// Md5Encrypt returns secret key use Mk5 encryption with username and password
func Md5Encrypt(user models.User) string {
/* #nosec */
md5Encrypt := md5.New()
key := fmt.Sprintf("%s/%s", user.UserName, user.Password)
_, _ = md5Encrypt.Write([]byte(key))
cipher := md5Encrypt.Sum(nil)
return string(cipher)
}