mirror of https://github.com/oxen-io/session-ios
registration validator
parent
cd70f9d0c3
commit
eb71c49794
@ -0,0 +1,53 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import SignalServiceKit
|
||||||
|
|
||||||
|
@objc
|
||||||
|
public enum ValidatedPhoneCountryCodes: UInt {
|
||||||
|
case unitedStates = 1
|
||||||
|
case brazil = 55
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc
|
||||||
|
public class PhoneNumberValidator: NSObject {
|
||||||
|
|
||||||
|
@objc
|
||||||
|
public func isValidForRegistration(phoneNumber: PhoneNumber) -> Bool {
|
||||||
|
guard let countryCode = phoneNumber.getCountryCode() else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let validatedCountryCode = ValidatedPhoneCountryCodes(rawValue: countryCode.uintValue) else {
|
||||||
|
// no extra validation for this country
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
switch validatedCountryCode {
|
||||||
|
case .brazil:
|
||||||
|
return isValidForBrazilRegistration(phoneNumber: phoneNumber)
|
||||||
|
case .unitedStates:
|
||||||
|
return isValidForUnitedStatesRegistration(phoneNumber: phoneNumber)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let validBrazilPhoneNumberRegex = try! NSRegularExpression(pattern: "^\\+55\\d{2}9?\\d{8}$", options: [])
|
||||||
|
private func isValidForBrazilRegistration(phoneNumber: PhoneNumber) -> Bool {
|
||||||
|
guard let e164 = phoneNumber.toE164() else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return validBrazilPhoneNumberRegex.hasMatch(input: e164)
|
||||||
|
}
|
||||||
|
|
||||||
|
let validUnitedStatesPhoneNumberRegex = try! NSRegularExpression(pattern: "^\\+1\\d{10}$", options: [])
|
||||||
|
private func isValidForUnitedStatesRegistration(phoneNumber: PhoneNumber) -> Bool {
|
||||||
|
guard let e164 = phoneNumber.toE164() else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return validUnitedStatesPhoneNumberRegex.hasMatch(input: e164)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import XCTest
|
||||||
|
import Signal
|
||||||
|
|
||||||
|
class PhoneNumberValidatorTest: SignalBaseTest {
|
||||||
|
|
||||||
|
func assertValid(e164: String,
|
||||||
|
file: StaticString = #file,
|
||||||
|
line: UInt = #line) {
|
||||||
|
let validator = PhoneNumberValidator()
|
||||||
|
guard let phoneNumber = PhoneNumber(fromE164: e164) else {
|
||||||
|
XCTFail("unparseable phone number", file: file, line: line)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let isValid = validator.isValidForRegistration(phoneNumber: phoneNumber)
|
||||||
|
XCTAssertTrue(isValid, file: file, line: line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertInvalid(e164: String,
|
||||||
|
file: StaticString = #file,
|
||||||
|
line: UInt = #line) {
|
||||||
|
let validator = PhoneNumberValidator()
|
||||||
|
guard let phoneNumber = PhoneNumber(fromE164: e164) else {
|
||||||
|
XCTFail("unparseable phone number", file: file, line: line)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let isValid = validator.isValidForRegistration(phoneNumber: phoneNumber)
|
||||||
|
XCTAssertFalse(isValid, file: file, line: line)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testUnitedStates() {
|
||||||
|
// valid us number
|
||||||
|
assertValid(e164: "+13235551234")
|
||||||
|
|
||||||
|
// too short
|
||||||
|
assertInvalid(e164: "+1323555123")
|
||||||
|
|
||||||
|
// too long
|
||||||
|
assertInvalid(e164: "+132355512345")
|
||||||
|
|
||||||
|
// not a US phone number
|
||||||
|
assertValid(e164: "+3235551234")
|
||||||
|
}
|
||||||
|
|
||||||
|
func testBrazil() {
|
||||||
|
// valid mobile
|
||||||
|
assertValid(e164: "+5532912345678")
|
||||||
|
|
||||||
|
// valid landline
|
||||||
|
assertValid(e164: "+553212345678")
|
||||||
|
|
||||||
|
// mobile length, but with out the leading '9'
|
||||||
|
assertInvalid(e164: "+5532812345678")
|
||||||
|
|
||||||
|
// too short
|
||||||
|
assertInvalid(e164: "+5532812345678")
|
||||||
|
|
||||||
|
// too long landline
|
||||||
|
assertInvalid(e164: "+5532123456789")
|
||||||
|
assertInvalid(e164: "+55321234567890")
|
||||||
|
|
||||||
|
// too long mobile
|
||||||
|
assertInvalid(e164: "+55329123456789")
|
||||||
|
assertInvalid(e164: "+553291234567890")
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
@objc
|
||||||
|
public extension NSRegularExpression {
|
||||||
|
|
||||||
|
@objc
|
||||||
|
public func hasMatch(input: String) -> Bool {
|
||||||
|
return self.firstMatch(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) != nil
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue