@@ -68,15 +68,15 @@ func (self *JWT) Process(data map[string]interface{}, arg map[string]interface{}
68
68
69
69
func (self * JWT ) GenerateToken (payload map [string ]interface {}) ([]byte , error ) {
70
70
claims := jws.Claims {}
71
+ for key , value := range payload {
72
+ claims .Set (key , value )
73
+ }
71
74
if self .Issuer != "" {
72
75
claims .SetIssuer (self .Issuer )
73
76
}
74
77
if self .ExpirationTime .Duration > 0 {
75
78
claims .SetExpiration (time .Now ().Add (self .ExpirationTime .Duration ))
76
79
}
77
- for key , value := range payload {
78
- claims .Set (key , value )
79
- }
80
80
token := jws .NewJWT (claims , crypto .SigningMethodHS256 )
81
81
serializedToken , err := token .Serialize ([]byte (self .Secret ))
82
82
if err != nil {
@@ -85,22 +85,43 @@ func (self *JWT) GenerateToken(payload map[string]interface{}) ([]byte, error) {
85
85
return serializedToken , nil
86
86
}
87
87
88
- func (self * JWT ) ProcessBeforeHook (data map [string ]interface {}, r * http.Request ) {
88
+ func (self * JWT ) ProcessBeforeHook (data map [string ]interface {}, r * http.Request ) * plugins. Response {
89
89
headerValue := r .Header .Get ("Authorization" )
90
90
if headerValue == "" {
91
- return
91
+ return nil
92
92
}
93
93
if ! strings .HasPrefix (headerValue , "Bearer " ) {
94
- return
94
+ return nil
95
95
}
96
96
headerValue = strings .Replace (headerValue , "Bearer " , "" , 1 )
97
97
token , err := jws .ParseJWT ([]byte (headerValue ))
98
98
if err != nil {
99
- return
99
+ return nil
100
100
}
101
101
err = token .Validate ([]byte (self .Secret ), crypto .SigningMethodHS256 )
102
102
if err != nil {
103
- return
103
+ return nil
104
+ }
105
+ expiration , ok := token .Claims ().Expiration ()
106
+ if ! ok {
107
+ return nil
108
+ }
109
+ if expiration .Unix () < time .Now ().Unix () {
110
+ return nil
111
+ }
112
+ if time .Now ().Add (self .RotationDeadline .Duration ).Unix () > expiration .Unix () {
113
+ token , err := self .GenerateToken (token .Claims ())
114
+ response := & plugins.Response {}
115
+ if err != nil {
116
+ response .ResponseCode = 500
117
+ response .Error = err .Error ()
118
+ return response
119
+ }
120
+ if len (token ) > 0 {
121
+ response .Headers = make (map [string ][]string )
122
+ response .Headers ["Authorization" ] = []string {"Bearer " + string (token )}
123
+ }
104
124
}
105
125
data ["jwt" ] = token .Claims ()
126
+ return nil
106
127
}
0 commit comments