-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.go
111 lines (100 loc) · 3.09 KB
/
middleware.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
package ssojwt
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/golang-jwt/jwt/v4"
)
func MakeAccessTokenMiddleware(config SSOConfig, key string) func(nextHandler http.Handler) http.Handler {
return func(nextHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authorization := r.Header.Get("Authorization")
AuthorizationMap := strings.Split(authorization, " ")
if len(AuthorizationMap) != 2 {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\": \"invalid_token\"}")
return
}
tokenString := AuthorizationMap[1]
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
return []byte(config.AccessTokenSecretKey), nil
})
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\": \"invalid_token\"}")
return
}
ctx := r.Context()
claims, ok := token.Claims.(jwt.MapClaims)
if ok && token.Valid {
ctx = context.WithValue(ctx, key, claims)
}
nextHandler.ServeHTTP(w, r.WithContext(ctx))
})
}
}
func MakeRefreshTokenMiddleware(config SSOConfig) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authorization := r.Header.Get("Authorization")
AuthorizationMap := strings.Split(authorization, " ")
if len(AuthorizationMap) != 2 {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\": \"invalid_token\"}")
return
}
tokenString := AuthorizationMap[1]
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
return []byte(config.RefreshTokenSecretKey), nil
})
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\": \"invalid_token\"}")
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if ok && token.Valid {
jurusan := claims["jurusan"].(map[string]interface{})
newClaims := ServiceResponse{
AuthenticationSuccess: AuthenticationSuccess{
User: claims["user"].(string),
Attributes: Attributes{
Nama: claims["nama"].(string),
Npm: claims["npm"].(string),
Jurusan: Jurusan{
Faculty: jurusan["faculty"].(string),
ShortFaculty: jurusan["shortFaculty"].(string),
Major: jurusan["major"].(string),
Program: jurusan["program"].(string),
},
},
},
}
accessToken, err := CreateAccessToken(config, newClaims)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\": \"internal_server_error\"}")
return
}
refreshToken, err := CreateRefreshToken(config, newClaims)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "{\"error\": \"internal_server_error\"}")
return
}
res := LoginResponse{
AccessToken: accessToken,
RefreshToken: refreshToken,
Fakultas: nil,
}
w.WriteHeader(http.StatusOK)
resJson, _ := json.Marshal(res)
fmt.Fprintf(w, "%s", resJson)
return
}
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "{\"error\": \"invalid_token\"}")
return
})
}