ing
This commit is contained in:
parent
c38073b8d0
commit
920f9f8c30
|
@ -9,8 +9,16 @@ import (
|
||||||
func SendRESTResponse(ctx *fasthttp.RequestCtx, response interface{}) {
|
func SendRESTResponse(ctx *fasthttp.RequestCtx, response interface{}) {
|
||||||
ctx.SetContentType("application/json")
|
ctx.SetContentType("application/json")
|
||||||
|
|
||||||
jRes, _ := json.Marshal(response)
|
var body []byte
|
||||||
ctx.SetBody(jRes)
|
|
||||||
|
switch response.(type) {
|
||||||
|
default:
|
||||||
|
body, _ = json.Marshal(response)
|
||||||
|
case string:
|
||||||
|
body = []byte(response.(string))
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.SetBody(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendRESTError(ctx *fasthttp.RequestCtx, statusCode int, err error) {
|
func SendRESTError(ctx *fasthttp.RequestCtx, statusCode int, err error) {
|
||||||
|
|
3
main.go
3
main.go
|
@ -31,7 +31,8 @@ func main() {
|
||||||
external.ExternalInit()
|
external.ExternalInit()
|
||||||
|
|
||||||
s := server.New()
|
s := server.New()
|
||||||
s.Route("POST", "/account/signin", member.SignIn)
|
s.Route("POST", "/account/signin", member.Signin)
|
||||||
|
s.Route("POST", "/account/signin_cookie", member.SigninByCookie)
|
||||||
s.Route("POST", "/account/signup", member.SignUp)
|
s.Route("POST", "/account/signup", member.SignUp)
|
||||||
s.Route("POST", "/account/forgot_password", member.ForgotPassword)
|
s.Route("POST", "/account/forgot_password", member.ForgotPassword)
|
||||||
s.Route("POST", "/account/reset_password", member.ResetPassword)
|
s.Route("POST", "/account/reset_password", member.ResetPassword)
|
||||||
|
|
|
@ -3,6 +3,7 @@ package member
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
@ -10,6 +11,7 @@ import (
|
||||||
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"git.loafle.net/commons_go/logging"
|
||||||
"git.loafle.net/overflow/overflow_server_app/commons"
|
"git.loafle.net/overflow/overflow_server_app/commons"
|
||||||
"git.loafle.net/overflow/overflow_server_app/external/grpc"
|
"git.loafle.net/overflow/overflow_server_app/external/grpc"
|
||||||
jwt "github.com/dgrijalva/jwt-go"
|
jwt "github.com/dgrijalva/jwt-go"
|
||||||
|
@ -43,7 +45,7 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SignIn(ctx *fasthttp.RequestCtx) {
|
func Signin(ctx *fasthttp.RequestCtx) {
|
||||||
var err error
|
var err error
|
||||||
var webParams map[string]interface{}
|
var webParams map[string]interface{}
|
||||||
|
|
||||||
|
@ -79,7 +81,7 @@ func SignIn(ctx *fasthttp.RequestCtx) {
|
||||||
/* Create a map to store our claims */
|
/* Create a map to store our claims */
|
||||||
claims := token.Claims.(jwt.MapClaims)
|
claims := token.Claims.(jwt.MapClaims)
|
||||||
|
|
||||||
expireTime := time.Now().Add(time.Hour * 24)
|
// expireTime := time.Now().Add(time.Hour * 24)
|
||||||
|
|
||||||
/* Set token claims */
|
/* Set token claims */
|
||||||
claims["iss"] = "overFlow"
|
claims["iss"] = "overFlow"
|
||||||
|
@ -93,14 +95,90 @@ func SignIn(ctx *fasthttp.RequestCtx) {
|
||||||
|
|
||||||
log.Printf("%s \n", tokenString)
|
log.Printf("%s \n", tokenString)
|
||||||
|
|
||||||
var jwtCookie fasthttp.Cookie
|
var domainMember interface{}
|
||||||
jwtCookie.SetKey("AuthToken")
|
err = json.Unmarshal([]byte(r), &domainMember)
|
||||||
jwtCookie.SetValue(tokenString)
|
|
||||||
// jwtCookie.SetHTTPOnly(true)
|
signInResult := &SignInResult{
|
||||||
jwtCookie.SetSecure(true)
|
AuthToken: tokenString,
|
||||||
jwtCookie.SetExpire(expireTime)
|
DomainMember: domainMember,
|
||||||
jwtCookie.SetPath("/")
|
}
|
||||||
ctx.Response.Header.SetCookie(&jwtCookie)
|
|
||||||
|
// var jwtCookie fasthttp.Cookie
|
||||||
|
// jwtCookie.SetKey("AuthToken")
|
||||||
|
// jwtCookie.SetValue(tokenString)
|
||||||
|
// // jwtCookie.SetHTTPOnly(true)
|
||||||
|
// // jwtCookie.SetSecure(true)
|
||||||
|
// // jwtCookie.SetDomain("127.0.0.1")
|
||||||
|
// jwtCookie.SetExpire(expireTime)
|
||||||
|
// jwtCookie.SetPath("/")
|
||||||
|
|
||||||
|
commons.SendRESTResponse(ctx, signInResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
type SignInResult struct {
|
||||||
|
AuthToken string `json:"authToken"`
|
||||||
|
DomainMember interface{} `json:"domainMember"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func SigninByCookie(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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
length := len(webParams)
|
||||||
|
|
||||||
|
if length < 0 {
|
||||||
|
fmt.Println("eeee")
|
||||||
|
}
|
||||||
|
|
||||||
|
authToken := webParams["authToken"].(string)
|
||||||
|
|
||||||
|
if authToken == "" {
|
||||||
|
err = errors.New("authToken is not exist")
|
||||||
|
commons.SendRESTError(ctx, fasthttp.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
token, err := jwt.Parse(authToken, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
// Don't forget to validate the alg is what you expect:
|
||||||
|
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
|
||||||
|
return nil, fmt.Errorf("Webapp: Unexpected signing method: %v", token.Header["alg"])
|
||||||
|
}
|
||||||
|
|
||||||
|
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
|
||||||
|
return verifyKey, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if nil != err {
|
||||||
|
commons.SendRESTError(ctx, fasthttp.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var ok bool
|
||||||
|
var claims jwt.MapClaims
|
||||||
|
if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid {
|
||||||
|
logging.Logger().Warn(fmt.Sprintf("Webapp: Token is not valid %v", token))
|
||||||
|
|
||||||
|
err = errors.New("authToken is invalid")
|
||||||
|
commons.SendRESTError(ctx, fasthttp.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
params := []string{claims["sub"].(string)}
|
||||||
|
|
||||||
|
gRPCCtx := context.Background()
|
||||||
|
r, err := grpc.Exec(gRPCCtx, "DomainMemberService.readByMemberEmail", params)
|
||||||
|
if nil != err {
|
||||||
|
commons.SendRESTError(ctx, fasthttp.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
commons.SendRESTResponse(ctx, r)
|
commons.SendRESTResponse(ctx, r)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user