From 3ad163d31625c29f313871a49dc460a010c84c4e Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 5 Sep 2017 13:06:48 +0900 Subject: [PATCH] JWT --- glide.yaml | 2 ++ module/member/signin.go | 32 +++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/glide.yaml b/glide.yaml index 33f4555..ef07ca0 100644 --- a/glide.yaml +++ b/glide.yaml @@ -9,3 +9,5 @@ import: - package: git.loafle.net/overflow/overflow_api_server subpackages: - golang +- package: github.com/dgrijalva/jwt-go + version: v3.0.0 diff --git a/module/member/signin.go b/module/member/signin.go index b8f0efc..2aec844 100644 --- a/module/member/signin.go +++ b/module/member/signin.go @@ -2,16 +2,21 @@ package member import ( "fmt" + "time" "git.loafle.net/overflow/overflow_server_app/backend" "git.loafle.net/overflow/overflow_server_app/server" - "github.com/valyala/fasthttp" "encoding/json" "log" + + jwt "github.com/dgrijalva/jwt-go" + "github.com/valyala/fasthttp" ) +var ofSigningKey = []byte("secret") + func SignIn(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { grpcPool := sctx.Value("grpc").(backend.Pool) fmt.Fprintf(ctx, "avail: %d\n", grpcPool.Available()) @@ -36,7 +41,7 @@ func SignIn(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { length := len(webParams) - if length < 0{ + if length < 0 { fmt.Println("eeee") } @@ -44,10 +49,31 @@ func SignIn(sctx *server.ServerContext, ctx *fasthttp.RequestCtx) { signinId := jsonMap["signinId"].(string) signinPw := jsonMap["signinPw"].(string) - params := []string{signinId, signinPw} r, err := c.Exec("MemberService", "signin", params) + if nil != err { + fmt.Fprintf(ctx, "%v", err) + return + } + + token := jwt.New(jwt.SigningMethodRS256) + + /* Create a map to store our claims */ + claims := token.Claims.(jwt.MapClaims) + + /* Set token claims */ + + claims["uid"] = signinId + claims["exp"] = time.Now().Add(time.Hour * 24).Unix() + + /* Sign the token with our secret */ + tokenString, _ := token.SignedString(ofSigningKey) + + var jwtCookie fasthttp.Cookie + jwtCookie.SetKey("AuthToken") + jwtCookie.SetValue(tokenString) + ctx.Response.Header.SetCookie(&jwtCookie) //fmt.Fprintf(ctx, "Welcome!!!!: %s\n", r) ctx.SetContentType("application/javascript")