package member import ( "context" "crypto/rsa" "fmt" "io/ioutil" "time" "encoding/json" "git.loafle.net/overflow/overflow_server_app/commons" "git.loafle.net/overflow/overflow_server_app/external/grpc" jwt "github.com/dgrijalva/jwt-go" "github.com/valyala/fasthttp" ) var ( verifyKey *rsa.PublicKey signKey *rsa.PrivateKey ) func init() { signBytes, err := ioutil.ReadFile("overFlow-private.key") if nil != err { panic(err) } signKey, err = jwt.ParseRSAPrivateKeyFromPEM(signBytes) if nil != err { panic(err) } verifyBytes, err := ioutil.ReadFile("overFlow-public.pem") if nil != err { panic(err) } verifyKey, err = jwt.ParseRSAPublicKeyFromPEM(verifyBytes) if nil != err { panic(err) } } func SignIn(ctx *fasthttp.RequestCtx) { var err error var webParams map[string]interface{} webBytes := ctx.PostBody() err = json.Unmarshal(webBytes, &webParams) if err != nil { commons.SendRESTError(ctx, fasthttp.StatusBadRequest, err) // fmt.Fprintf(ctx, "Err!!!!: %s\n", err) return } length := len(webParams) if length < 0 { fmt.Println("eeee") } signinId := webParams["signinId"].(string) signinPw := webParams["signinPw"].(string) params := []string{signinId, signinPw} gRPCCtx := context.Background() r, err := grpc.Exec(gRPCCtx, "MemberService.signin", params) if nil != err { commons.SendRESTError(ctx, fasthttp.StatusBadRequest, err) return } token := jwt.New(jwt.SigningMethodRS512) /* Create a map to store our claims */ claims := token.Claims.(jwt.MapClaims) /* Set token claims */ claims["iss"] = "overFlow" claims["iat"] = time.Now().Unix() claims["exp"] = time.Now().Add(time.Hour * 24).Unix() claims["aud"] = "www.overflow.cloud" claims["sub"] = signinId /* Sign the token with our secret */ tokenString, _ := token.SignedString(signKey) var jwtCookie fasthttp.Cookie jwtCookie.SetKey("AuthToken") jwtCookie.SetValue(tokenString) ctx.Response.Header.SetCookie(&jwtCookie) commons.SendRESTResponse(ctx, r) }