Skip to content

Commit dbc8f36

Browse files
committed
Added functionality to renew jwt tokens
1 parent 8d37e61 commit dbc8f36

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ func (self *Api) GetPlugins() []string {
4444
type Plugin interface {
4545
ParseConfig(path string) error
4646
Process(data map[string]interface{}, arg map[string]interface{}) *plugins.Response
47-
ProcessBeforeHook(data map[string]interface{}, r *http.Request)
47+
ProcessBeforeHook(data map[string]interface{}, r *http.Request) *plugins.Response
4848
}

main.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func handler(api *Api, route *Route, version int) func(http.ResponseWriter, *htt
8282
data := make(map[string]interface{})
8383
data["params"] = params
8484

85-
runBeforeHooks(api, data, r)
85+
runBeforeHooks(api, data, r, w)
8686
sql, err := route.Sql(data, apiVersion)
8787
if err != nil && sql != "" {
8888
w.WriteHeader(http.StatusBadRequest)
@@ -218,10 +218,17 @@ func goThroughPipelines(api *Api,
218218
return nil
219219
}
220220

221-
func runBeforeHooks(api *Api, data map[string]interface{}, r *http.Request) {
221+
func runBeforeHooks(api *Api, data map[string]interface{}, r *http.Request, w http.ResponseWriter) {
222222
plugins := api.GetPlugins()
223223
for _, name := range plugins {
224224
plugin := api.GetPlugin(name)
225-
plugin.ProcessBeforeHook(data, r)
225+
response := plugin.ProcessBeforeHook(data, r)
226+
if response != nil && response.Headers != nil {
227+
for name, values := range response.Headers {
228+
for _, value := range values {
229+
w.Header().Set(name, value)
230+
}
231+
}
232+
}
226233
}
227234
}

plugins/jwt/jwt.go

+29-8
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ func (self *JWT) Process(data map[string]interface{}, arg map[string]interface{}
6868

6969
func (self *JWT) GenerateToken(payload map[string]interface{}) ([]byte, error) {
7070
claims := jws.Claims{}
71+
for key, value := range payload {
72+
claims.Set(key, value)
73+
}
7174
if self.Issuer != "" {
7275
claims.SetIssuer(self.Issuer)
7376
}
7477
if self.ExpirationTime.Duration > 0 {
7578
claims.SetExpiration(time.Now().Add(self.ExpirationTime.Duration))
7679
}
77-
for key, value := range payload {
78-
claims.Set(key, value)
79-
}
8080
token := jws.NewJWT(claims, crypto.SigningMethodHS256)
8181
serializedToken, err := token.Serialize([]byte(self.Secret))
8282
if err != nil {
@@ -85,22 +85,43 @@ func (self *JWT) GenerateToken(payload map[string]interface{}) ([]byte, error) {
8585
return serializedToken, nil
8686
}
8787

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 {
8989
headerValue := r.Header.Get("Authorization")
9090
if headerValue == "" {
91-
return
91+
return nil
9292
}
9393
if !strings.HasPrefix(headerValue, "Bearer ") {
94-
return
94+
return nil
9595
}
9696
headerValue = strings.Replace(headerValue, "Bearer ", "", 1)
9797
token, err := jws.ParseJWT([]byte(headerValue))
9898
if err != nil {
99-
return
99+
return nil
100100
}
101101
err = token.Validate([]byte(self.Secret), crypto.SigningMethodHS256)
102102
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+
}
104124
}
105125
data["jwt"] = token.Claims()
126+
return nil
106127
}

0 commit comments

Comments
 (0)