def test_parse_phone(self): parse_phone("+8613812345678") parse_phone("13812345678") with self.assertRaises(exceptions.InvalidPhone): parse_phone("13812345678", default_country=None) parse_phone("bad string")
def mutate(self, info, phone, provider=None, auth_code=None, user_info=None): try: phone = parse_phone(phone) except exceptions.InvalidPhone as e: raise exceptions.GQLError(e.message) try: shop_user = ShopUser.objects.get(phone=phone) except ShopUser.DoesNotExist: shop_user = ShopUser.objects.create_user(phone=phone) if provider and auth_code: try: shop_user.bind_third_account(provider, auth_code) except ( exceptions.AlreadyBinded, exceptions.DoNotSupportBindType, exceptions.CodeBeUsed, ) as e: raise exceptions.GQLError(e.message) if user_info: for k, v in user_info.items(): setattr(shop_user, k, v) shop_user.save(update_fields=[k]) token = get_token(shop_user.user) refresh_token = create_refresh_token(shop_user.user) return SignUp(me=shop_user, token=token, refresh_token=refresh_token)
def resolve_registered(self, info, phone): try: phone = parse_phone(phone, default_country=None) except exceptions.InvalidPhone as e: raise exceptions.GQLError(e.message) try: ShopUser.objects.get(user__username=phone) return True except ShopUser.DoesNotExist: return False
def request_verify_code(request, phone): phone = parse_phone(phone) if phone in settings.TEST_SMS_ACCOUNT: code = "123456" else: code = get_random_code() send_verify_code(request, phone, code) request.session["verify_code"] = { "phone": phone, "code": code, "retry": 0, "expired_at": time.time() + SMS_EXPIRATION, }
def verify_code(request, phone, code): session = request.session vc = session.get("verify_code", None) phone = parse_phone(phone) if not vc or phone != vc.get("phone", None): raise exceptions.WrongVerifyCode retry = vc.get("retry", 0) vc["retry"] = retry + 1 if retry > MAX_RETRY_VERIFY: raise exceptions.WrongVerifyCode("too_much_retry") if (vc.get("code", None) != code or not vc.get("expired_at", None) or vc.get("used", False) or time.time() > vc.get("expired_at", 0)): raise exceptions.WrongVerifyCode vc["used"] = True session["verified_phone"] = { "phone": phone, "expired_at": time.time() + SMS_EXPIRATION, }