Skip to content

Commit

Permalink
modify request for jwt user
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Lin committed Jul 24, 2018
1 parent 4f0d660 commit 0711cd0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
17 changes: 1 addition & 16 deletions src/server/handler_authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package server

import (
"strings"
"time"

"github.com/dgrijalva/jwt-go"
response "github.com/linkernetworks/vortex/src/net/http"
"github.com/linkernetworks/vortex/src/web"
)
Expand All @@ -15,7 +13,7 @@ type UserCredentials struct {
Password string `json:"password"`
}

func authenticateHandler(ctx *web.Context) {
func loginHandler(ctx *web.Context) {
_, req, resp := ctx.ServiceProvider, ctx.Request, ctx.Response
user := UserCredentials{}
if err := req.ReadEntity(&user); err != nil {
Expand All @@ -40,16 +38,3 @@ func authenticateHandler(ctx *web.Context) {
Message: tokenString,
})
}

func generateToken(userUUID string) (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = jwt.MapClaims{
// issuer of the claim
"exp": time.Now().Add(time.Hour * time.Duration(1)).Unix(),
// issued-at time
"iat": time.Now().Unix(),
// the subject of this token. This is the user associated with the relevant action
"sub": userUUID,
}
return token.SignedString([]byte(SecretKey))
}
20 changes: 20 additions & 0 deletions src/server/jwt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package server

import (
"time"

"github.com/dgrijalva/jwt-go"
)

func generateToken(userUUID string) (string, error) {
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = jwt.MapClaims{
// issuer of the claim
"exp": time.Now().Add(time.Hour * time.Duration(1)).Unix(),
// issued-at time
"iat": time.Now().Unix(),
// the subject of this token. This is the user associated with the relevant action
"sub": userUUID,
}
return token.SignedString([]byte(SecretKey))
}
13 changes: 13 additions & 0 deletions src/server/jwt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package server

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGenerateToken(t *testing.T) {
tokenString, err := generateToken("234243353535330")
assert.NotNil(t, tokenString)
assert.NoError(t, err)
}
4 changes: 2 additions & 2 deletions src/server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func newVersionService(sp *serviceprovider.Container) *restful.WebService {

func newAuthenticateService(sp *serviceprovider.Container) *restful.WebService {
webService := new(restful.WebService)
webService.Path("/v1/auth").Consumes(restful.MIME_JSON, restful.MIME_JSON).Produces(restful.MIME_JSON, restful.MIME_JSON)
webService.Route(webService.POST("/").To(handler.RESTfulServiceHandler(sp, authenticateHandler)))
webService.Path("/v1/login").Consumes(restful.MIME_JSON, restful.MIME_JSON).Produces(restful.MIME_JSON, restful.MIME_JSON)
webService.Route(webService.POST("/").To(handler.RESTfulServiceHandler(sp, loginHandler)))
return webService
}

Expand Down
7 changes: 2 additions & 5 deletions src/server/route_filter.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package server

import (
"fmt"
"net/http"

"github.com/dgrijalva/jwt-go"
Expand All @@ -10,9 +9,6 @@ import (
"github.com/linkernetworks/logger"
)

// will be the cookie name defined in the http header
const SessionKey = "ses"

// FIXME using ldconfig go build to give a secretkey
const (
SecretKey = "linkernetworks"
Expand All @@ -31,7 +27,8 @@ func validateTokenMiddleware(req *restful.Request, resp *restful.Response, chain

if err == nil {
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
fmt.Println(claims["sub"])
// save to requests attributes
req.SetAttribute("UserID", claims["sub"])
chain.ProcessFilter(req, resp)
} else {
resp.WriteHeader(http.StatusUnauthorized)
Expand Down

0 comments on commit 0711cd0

Please sign in to comment.