Esempio n. 1
0
// @Title LoginWithPhone
// @Description 通过手机号和密码获取Token(登录)
// @Param	phone		query 	string	true		"用户手机号"
// @Param	secret		query 	string	true		"加密处理后的密码,全部小写"
// @Success 201 {object} models.User
// @Failure 403 参数错误:缺失或格式错误
// @Failure 422 手机号未注册/密码错误
// @Failure 500 系统错误
// @router / [post]
func (this *TokensController) Post() {
	phone := this.GetString("phone")
	secret := this.GetString("secret")
	if utils.IsValidPhone(phone) && len(secret) == 40 {
		user, err := models.VerifyUserByPhone(&phone, secret)
		if err > 0 {
			this.Ctx.ResponseWriter.WriteHeader(422)
			this.Data["json"] = utils.Issue(err, this.Ctx.Request.URL.String())
		} else {
			this.Ctx.ResponseWriter.WriteHeader(201)
			this.Data["json"] = user
		}
	} else {
		this.Ctx.ResponseWriter.WriteHeader(403)
		this.Data["json"] = utils.Issue(utils.ERROR_CODE_PARAM_ERROR, this.Ctx.Request.URL.String())
	}
	this.ServeJSON()
}
Esempio n. 2
0
func TestVerifyUserByPhone(t *testing.T) {
	initORM()

	phone := "18801234567"
	secret := "8428d916f8cca9ba5971bf58b34d38da20bc3dff"
	password := "******"

	// insert one
	user, err := models.CreateUserByPhone(&phone, secret)
	assert.NotNil(t, user)
	assert.Zero(t, err)
	assert.Empty(t, user.Nickname)
	assert.True(t, user.Uid >= utils.USER_MIN_UID)
	assert.Equal(t, password, user.Password)
	assert.True(t, len(user.Token) == 40)
	assert.True(t, utils.AreStringEquals(user.Phone, &phone))

	// get it by phone
	getByPhone, err := models.GetUserByUid(user.Uid)
	assert.Zero(t, err)
	assertUserEquals(t, user, getByPhone)

	// verify by phone
	verifyByPhone, err := models.VerifyUserByPhone(&phone, secret)
	assert.Zero(t, err)
	user.UpdateAt = verifyByPhone.UpdateAt
	assertUserEqualsWithoutToken(t, user, verifyByPhone)

	// clean up
	deleteUser(t, user.Id)

	// no such user after delete
	user, err = models.GetUserByUid(user.Uid)
	assert.Nil(t, user)
	assert.Equal(t, utils.ERROR_CODE_USERS_USER_NOT_EXISTS, err)
}