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 }
func EmailLogin(c context.Context, email string, password string) (*LoginResult, error) { a, err := persistence.GetAccountByEmail(email) if err != nil { log.Error(email, password) return nil, ErrServer } if a == nil { log.Error(email, password) return nil, ErrLogin } password = crypt.SHA1(password + fmt.Sprint(a.ID)) if password != a.Password { return nil, ErrLogin } rs := &LoginResult{} rs.Account = &Account{} runtime.CopyFields(rs.Account, a) rs.LoginToken = a.LoginToken rs.Sid = GenerateSessionID(a.ID) return rs, nil }
func UpdateEmail(context context.Context, id types.ID, mobile string) (*Account, error) { loginID, _ := context.Value("login_id").(types.ID) if loginID <= 0 { return nil, ErrAuth } if !validation.IsMobile(mobile) { return nil, errors.New("invalid mobile") } if loginID != id { return nil, ErrForbidden } a, err := persistence.GetAccount(id) if err != nil { return nil, ErrServer } if a == nil { return nil, ErrNotFound } if a.Mobile == mobile { acc := &Account{} runtime.CopyFields(acc, a) return acc, nil } if persistence.IsDupMobile(mobile, a.ID) { return nil, ErrDupNick } a.Mobile = mobile err = persistence.UpdateAccount(a) if err != nil { return nil, ErrServer } acc := &Account{} runtime.CopyFields(acc, a) return acc, nil }
func UpdateNick(context context.Context, id types.ID, nick string) (*Account, error) { loginID, _ := context.Value("login_id").(types.ID) if loginID <= 0 { return nil, ErrAuth } if len(nick) < 2 { return nil, errors.New("invalid nick") } if loginID != id { return nil, ErrForbidden } a, err := persistence.GetAccount(id) if err != nil { return nil, ErrServer } if a == nil { return nil, ErrNotFound } if a.Nick == nick { acc := &Account{} runtime.CopyFields(acc, a) return acc, nil } if persistence.IsDupNick(nick, a.ID) { return nil, ErrDupNick } a.Nick = nick err = persistence.UpdateAccount(a) if err != nil { return nil, ErrServer } acc := &Account{} runtime.CopyFields(acc, a) return acc, nil }
func AutoLogin(c context.Context, id types.ID, loginToken string) (*LoginResult, error) { a, err := persistence.GetAccountByLoginToken(loginToken) if err != nil { return nil, ErrServer } else if a == nil { return nil, ErrLogin } else { rs := &LoginResult{} rs.Account = &Account{} runtime.CopyFields(rs.Account, a) rs.LoginToken = a.LoginToken rs.Sid = GenerateSessionID(a.ID) return rs, nil } }
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 }
func (as *AccountsService) MobileRegister(c context.Context, req *pb.MobileRegisterRequest) (*pb.LoginResponse, error) { regInfo := &services.MobileRegInfo{} runtime.CopyFields(regInfo, req) return as.handleLoginResult(services.MobileRegister(c, regInfo)) }
func (rs *AccountsService) OpenLogin(c context.Context, req *pb.OpenLoginRequest) (*pb.LoginResponse, error) { oa := &services.OpenAccount{} runtime.CopyFields(oa, req) return rs.handleLoginResult(services.OpenLogin(c, oa)) }