2018-04-06 10:08:38 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-04-06 10:47:42 +00:00
|
|
|
"context"
|
2018-04-10 07:11:17 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-04-10 06:58:38 +00:00
|
|
|
"reflect"
|
2018-04-10 07:11:17 +00:00
|
|
|
"time"
|
2018-04-06 10:47:42 +00:00
|
|
|
|
2018-05-11 08:00:33 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-04-10 06:58:38 +00:00
|
|
|
cda "git.loafle.net/commons/di-go/annotation"
|
|
|
|
cdr "git.loafle.net/commons/di-go/registry"
|
2018-04-11 13:08:41 +00:00
|
|
|
"git.loafle.net/commons/logging-go"
|
2018-04-06 10:08:38 +00:00
|
|
|
"git.loafle.net/commons/server-go"
|
2018-05-11 08:00:33 +00:00
|
|
|
"git.loafle.net/overflow/external-go/grpc"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
2018-04-06 10:08:38 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2018-04-10 07:11:17 +00:00
|
|
|
|
2018-05-11 08:00:33 +00:00
|
|
|
// For annotation
|
|
|
|
_ "git.loafle.net/overflow/commons-go/core/annotation"
|
2018-04-11 14:23:47 +00:00
|
|
|
)
|
2018-04-06 10:08:38 +00:00
|
|
|
|
2018-04-10 06:58:38 +00:00
|
|
|
var MemberServiceType = reflect.TypeOf((*MemberService)(nil))
|
|
|
|
|
2018-04-11 14:23:47 +00:00
|
|
|
func init() {
|
2018-04-10 06:58:38 +00:00
|
|
|
cdr.RegisterType(MemberServiceType)
|
|
|
|
}
|
|
|
|
|
2018-04-06 10:08:38 +00:00
|
|
|
type MemberService struct {
|
2018-04-10 14:30:45 +00:00
|
|
|
cda.TypeAnnotation `annotation:"@overflow:RESTService()"`
|
|
|
|
|
2018-04-11 14:23:47 +00:00
|
|
|
VerifyKey *rsa.PublicKey `annotation:"@Resource(name='VerifyKey')"`
|
|
|
|
SignKey *rsa.PrivateKey `annotation:"@Resource(name='SignKey')"`
|
2018-04-11 13:08:41 +00:00
|
|
|
|
2018-04-11 14:23:47 +00:00
|
|
|
_Signin cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/signin', params='[signinID, signinPW]')"`
|
|
|
|
_SigninByCookie cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/signin_cookie', params='[authToken]')"`
|
2018-06-20 06:14:44 +00:00
|
|
|
_Signup cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/signup', params='[member, pw]')"`
|
2018-06-01 03:49:45 +00:00
|
|
|
_ConfirmEmail cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/confirm_email', params='[token]')"`
|
2018-05-11 08:00:33 +00:00
|
|
|
_SendEmailPw cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/send_email_pw', params='[signinID]')"`
|
2018-06-01 03:49:45 +00:00
|
|
|
_ConfirmResetPw cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/confirm_reset_pw', params='[token]')"`
|
2018-04-11 06:05:09 +00:00
|
|
|
|
2018-05-06 09:39:45 +00:00
|
|
|
_ResetPassword cda.MethodAnnotation `annotation:"@overflow:RequestMapping(method='POST', entry='/account/reset_password', params='[token, pw, confirmPw]')"`
|
2018-04-11 04:35:29 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 06:05:09 +00:00
|
|
|
type SigninResult struct {
|
|
|
|
AuthToken string `json:"authToken"`
|
|
|
|
DomainMember interface{} `json:"domainMember"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MemberService) Signin(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, signinID string, signinPW string) error {
|
2018-04-06 10:08:38 +00:00
|
|
|
|
2018-04-06 10:47:42 +00:00
|
|
|
gRPCCtx := context.Background()
|
2018-04-10 07:11:17 +00:00
|
|
|
r, err := grpc.Exec(gRPCCtx, "MemberService.signin", signinID, signinPW)
|
|
|
|
if nil != err {
|
2018-04-11 06:05:09 +00:00
|
|
|
return err
|
2018-04-10 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
token := jwt.New(jwt.SigningMethodRS512)
|
|
|
|
|
|
|
|
/* Create a map to store our claims */
|
|
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
|
|
|
|
|
|
// expireTime := time.Now().Add(time.Hour * 24)
|
|
|
|
|
|
|
|
/* 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 */
|
2018-04-11 13:08:41 +00:00
|
|
|
tokenString, err := token.SignedString(ms.SignKey)
|
2018-04-10 07:11:17 +00:00
|
|
|
if nil != err {
|
2018-04-11 06:05:09 +00:00
|
|
|
return err
|
2018-04-10 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var domainMember interface{}
|
|
|
|
err = json.Unmarshal([]byte(r), &domainMember)
|
|
|
|
if nil != err {
|
2018-04-11 06:05:09 +00:00
|
|
|
return err
|
2018-04-10 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 06:05:09 +00:00
|
|
|
signInResult := &SigninResult{
|
2018-04-10 07:11:17 +00:00
|
|
|
AuthToken: tokenString,
|
|
|
|
DomainMember: domainMember,
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := json.Marshal(signInResult)
|
|
|
|
if nil != err {
|
2018-04-11 06:05:09 +00:00
|
|
|
return err
|
2018-04-10 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 06:05:09 +00:00
|
|
|
ctx.SetBody(buf)
|
|
|
|
|
|
|
|
return nil
|
2018-04-10 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
2018-04-11 06:05:09 +00:00
|
|
|
func (ms *MemberService) SigninByCookie(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, authToken string) error {
|
2018-04-10 07:11:17 +00:00
|
|
|
|
|
|
|
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")
|
2018-04-11 13:08:41 +00:00
|
|
|
return ms.VerifyKey, nil
|
2018-04-10 07:11:17 +00:00
|
|
|
})
|
2018-04-06 10:47:42 +00:00
|
|
|
if nil != err {
|
2018-04-11 06:05:09 +00:00
|
|
|
return err
|
2018-04-06 10:47:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 07:11:17 +00:00
|
|
|
var ok bool
|
|
|
|
var claims jwt.MapClaims
|
|
|
|
if claims, ok = token.Claims.(jwt.MapClaims); !ok || !token.Valid {
|
|
|
|
logging.Logger().Warnf("Token is not valid %v", token)
|
2018-04-11 06:05:09 +00:00
|
|
|
return fmt.Errorf("authToken is not valid")
|
2018-04-10 07:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params := []string{claims["sub"].(string)}
|
|
|
|
|
|
|
|
gRPCCtx := context.Background()
|
|
|
|
r, err := grpc.Exec(gRPCCtx, "DomainMemberService.readByMemberEmail", params...)
|
2018-04-11 06:05:09 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-10 07:11:17 +00:00
|
|
|
|
2018-04-11 06:05:09 +00:00
|
|
|
ctx.SetBody([]byte(r))
|
2018-04-06 10:08:38 +00:00
|
|
|
|
2018-04-11 06:05:09 +00:00
|
|
|
return nil
|
2018-04-06 10:08:38 +00:00
|
|
|
}
|
2018-04-10 07:11:17 +00:00
|
|
|
|
2018-04-11 08:19:00 +00:00
|
|
|
func (ms *MemberService) Signup(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, member string, pw string) error {
|
|
|
|
|
|
|
|
gRPCCtx := context.Background()
|
|
|
|
r, err := grpc.Exec(gRPCCtx, "MemberService.signup", member, pw)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.SetBody([]byte(r))
|
2018-04-11 06:05:09 +00:00
|
|
|
|
|
|
|
return nil
|
2018-04-10 07:11:17 +00:00
|
|
|
}
|
2018-04-11 08:19:00 +00:00
|
|
|
|
2018-05-04 07:44:10 +00:00
|
|
|
func (ms *MemberService) SendEmailPw(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, signinID string) error {
|
2018-04-11 08:19:00 +00:00
|
|
|
|
|
|
|
gRPCCtx := context.Background()
|
|
|
|
r, err := grpc.Exec(gRPCCtx, "MemberService.sendEmailForPassword", signinID)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.SetBody([]byte(r))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 07:44:10 +00:00
|
|
|
func (ms *MemberService) ConfirmEmail(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, token string) error {
|
|
|
|
gRPCCtx := context.Background()
|
|
|
|
|
2018-06-01 03:49:45 +00:00
|
|
|
r, err := grpc.Exec(gRPCCtx, "EmailAuthService.readBySignupAuthKey", token)
|
2018-05-04 07:44:10 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-06 09:39:45 +00:00
|
|
|
|
2018-06-01 03:49:45 +00:00
|
|
|
//uri := ccm.NG_MEMBER_SERVER_URI + "/auth/signin"
|
|
|
|
//ctx.Redirect(uri, 302)
|
|
|
|
ctx.SetBody([]byte(r))
|
2018-05-04 07:44:10 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ms *MemberService) ConfirmResetPw(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, token string) error {
|
2018-04-11 08:19:00 +00:00
|
|
|
|
|
|
|
gRPCCtx := context.Background()
|
|
|
|
|
2018-06-01 03:49:45 +00:00
|
|
|
r, err := grpc.Exec(gRPCCtx, "EmailAuthService.readByPwAuthKey", token)
|
2018-05-06 09:39:45 +00:00
|
|
|
|
2018-04-11 08:19:00 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-01 03:49:45 +00:00
|
|
|
//uri := ccm.NG_MEMBER_SERVER_URI + "/auth/modify-password?token=" + token
|
|
|
|
//ctx.Redirect(uri, 302)
|
2018-05-06 09:39:45 +00:00
|
|
|
|
2018-06-01 03:49:45 +00:00
|
|
|
ctx.SetBody([]byte(r))
|
2018-04-11 08:19:00 +00:00
|
|
|
|
|
|
|
return nil
|
2018-04-11 14:23:47 +00:00
|
|
|
}
|
2018-05-04 07:44:10 +00:00
|
|
|
|
2018-05-06 09:39:45 +00:00
|
|
|
func (ms *MemberService) ResetPassword(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx, token string, pw string, confirmPw string) error {
|
2018-05-04 07:44:10 +00:00
|
|
|
|
|
|
|
gRPCCtx := context.Background()
|
|
|
|
|
2018-05-06 09:39:45 +00:00
|
|
|
if strings.Compare(pw, confirmPw) != 0 {
|
|
|
|
return fmt.Errorf("ResetPassword() password not match: %s, %s", pw, confirmPw)
|
|
|
|
}
|
2018-06-01 03:49:45 +00:00
|
|
|
r, err := grpc.Exec(gRPCCtx, "MemberService.resetPassword", token, pw)
|
2018-05-04 07:44:10 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-01 03:49:45 +00:00
|
|
|
//uri := ccm.NG_MEMBER_SERVER_URI + "/auth/signin"
|
|
|
|
//ctx.Redirect(uri, 302)
|
|
|
|
ctx.SetBody([]byte(r))
|
2018-05-04 07:44:10 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|