Beispiel #1
0
func OpenLogin(c context.Context, oa *OpenAccount) (*LoginResult, error) {
	switch {
	case len(oa.Name) == 0:
		return nil, errors.New("invalid name")
	case len(oa.OpenID) == 0:
		return nil, errors.New("no open_id")
	case len(oa.AccessToken) == 0:
		return nil, errors.New("no access_token")
	case len(oa.RefreshToken) == 0:
		return nil, errors.New("no refresh_token")
	case oa.ExpiredAt < time.Now().Unix():
		return nil, errors.New("invalid expired_at")
	}

	openID := fmt.Sprint(oa.Type) + "--" + oa.OpenID
	a, err := persistence.GetAccountByOpenID(openID)
	if err != nil {
		return nil, ErrServer
	}

	if a != nil {
		rs := &LoginResult{}
		rs.Account = &Account{}
		runtime.CopyFields(rs.Account, a)
		rs.LoginToken = a.LoginToken
		rs.Sid = GenerateSessionID(a.ID)
		return rs, nil
	}

	nick := oa.Name
	for i := 1; persistence.IsDupNick(nick, 0); i++ {
		nick = oa.Name + fmt.Sprint(i)
	}

	a = &persistence.Account{}
	a.ID = types.NewID()
	a.Nick = nick
	a.Avatar = oa.Avatar
	a.Gender = oa.Gender
	a.OpenID = openID
	a.Password = crypt.SHA1(oa.AccessToken + fmt.Sprint(a.ID))
	a.LoginToken = GenerateLoginToken(a.ID)
	err = persistence.InsertAccount(a)
	if err != nil {
		return nil, ErrServer
	}

	poa := &persistence.OpenAccount{}
	runtime.CopyFields(poa, oa)
	persistence.InsertOpenAccount(poa)

	rs := &LoginResult{}
	rs.Account = &Account{}
	runtime.CopyFields(rs.Account, a)
	rs.LoginToken = a.LoginToken
	rs.Sid = GenerateSessionID(a.ID)
	return rs, nil
}
Beispiel #2
0
func MobileRegister(c context.Context, regInfo *MobileRegInfo) (*LoginResult, error) {
	var e error
	switch {
	case !validation.IsMobile(regInfo.Mobile):
		e = errors.New("invalid mobile")
	case !checkCaptcha(c, regInfo.Mobile, regInfo.Captcha):
		e = errors.New("invalid captcha")
	case persistence.IsDupMobile(regInfo.Mobile, 0):
		e = ErrDupMobile
	case len(regInfo.Nick) < 2:
		e = errors.New("invalid nick")
	case persistence.IsDupNick(regInfo.Nick, 0):
		e = ErrDupNick
	case len(regInfo.Birthdate) > 0 && !validation.IsBirthdate(regInfo.Birthdate):
		e = errors.New("invalid birthdate")
	case len(regInfo.Password) == 0:
		e = errors.New("no password")
	}

	if e != nil {
		log.Error(e)
		return nil, e
	}

	a := &persistence.Account{}
	a.ID = types.NewID()
	a.Nick = regInfo.Nick
	a.Mobile = regInfo.Mobile
	a.Password = crypt.SHA1(regInfo.Password + fmt.Sprint(a.ID))
	a.Birthdate = regInfo.Birthdate
	a.Gender = regInfo.Gender
	a.Avatar = regInfo.Avatar
	a.LoginToken = crypt.SHA1(a.Password + fmt.Sprint(a.ID))
	e = persistence.InsertAccount(a)
	if e != nil {
		log.Error(e)
		return nil, ErrServer
	}

	rs := &LoginResult{}
	rs.Account = &Account{}
	runtime.CopyFields(rs.Account, a)
	rs.LoginToken = a.LoginToken
	rs.Sid = GenerateSessionID(a.ID)
	return rs, nil
}
Beispiel #3
0
package persistence

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/justintan/gox/types"
	"testing"
	"time"
)

var testAcc = &Account{
	ID:         types.NewID(),
	OpenID:     types.NewUUID(),
	Mobile:     "138" + types.NewUUID()[:8],
	Email:      types.NewUUID()[:6] + "@test.com",
	Password:   "******",
	LoginToken: types.NewUUID(),

	Nick:      "Nick" + types.NewUUID()[:6],
	Gender:    1,
	Avatar:    "http://image.a.com/" + types.NewUUID(),
	Birthdate: "1980-03-21",
	CreatedAt: time.Now().Unix(),
	Status:    3,
}

func TestInsertAccount(t *testing.T) {
	err := InsertAccount(testAcc)
	if err != nil {
		t.Error(err)
		t.Fail()
	}
Beispiel #4
0
func AddMsg(c *Context, params *NewMsgParams) (*Msg, *Error) {
	switch {
	case c.UserID <= 0:
		return nil, NewError1(ErrAuth)
	case params.ReceiverID <= 0:
		return nil, NewError(ErrBadRequest, "check receiver_id")
	case params.Content == nil:
		return nil, NewError(ErrBadRequest, "check content")
	case params.LocalID <= 0:
		return nil, NewError(ErrBadRequest, "check local_id")
	case params.Type != DirectChat && params.Type != GroupChat:
		return nil, NewError(ErrBadRequest, "check type")
	}

	m := &Msg{}
	m.ID = types.NewID()
	m.SenderID = c.UserID
	m.ChatType = params.Type
	m.LocalID = params.LocalID
	m.Content = params.Content
	m.ReceiverID = params.ReceiverID
	m.AtUserIDs = params.AtUserIDs

	fm, err := globalContext.MsgRepo.FindMsg(m)
	if err != nil {
		return nil, NewError1(ErrServer)
	}

	if fm != nil {
		return fm, nil
	}

	switch m.ChatType {
	case DirectChat:
		err = c.MsgRepo.SaveDirectMsg(m)
	case GroupChat:
		members, e := c.GroupRepo.GetMembers(m.ReceiverID)
		if e != nil {
			return nil, NewError1(ErrServer)
		}

		ids := make([]types.ID, len(members))
		for i, mem := range members {
			ids[i] = mem.UserID
		}
		err = c.MsgRepo.SaveGroupMsg(m, ids)
	default:
		return nil, NewError1(ErrBadRequest)
	}
	if err != nil {
		return nil, NewError1(ErrServer)
	}

	var userIDs []types.ID
	switch m.ChatType {
	case DirectChat:
		userIDs = append(userIDs, m.ReceiverID)
	case GroupChat:
		var members []*Member
		members, err = c.GroupRepo.GetMembers(m.ReceiverID)
		for _, u := range members {
			userIDs = append(userIDs, u.UserID)
		}
	}
	go c.EventBus.Post(&NewMsgEvent{Msg: m, UserIDs: userIDs})
	return m, nil
}