Skip to content

Commit

Permalink
Add backwards compatible phone number normalization for Estonian and …
Browse files Browse the repository at this point in the history
…Lithuanian phone numbers
  • Loading branch information
ErkoRisthein committed Mar 23, 2019
1 parent 64009f3 commit e01bd8d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
22 changes: 21 additions & 1 deletion src/com/codeborne/security/mobileid/MobileIDAuthenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.rmi.RemoteException;
import java.util.Base64;
import java.util.List;
import java.util.stream.Stream;

import static com.codeborne.security.AuthenticationException.Code.valueOf;

Expand Down Expand Up @@ -155,7 +156,26 @@ private void validateServiceUrl() {
}

String normalizePhoneNumber(String phone) {
return phone != null && phone.startsWith("+") ? phone.substring(1) : phone;
if (phone != null) {
if (phone.startsWith("+"))
phone = phone.substring(1);
if (isLithuanian(phone)) {
return "370" + phone;
} else if (isEstonian(phone)) {
return "372" + phone;
}
}
return phone;
}

private boolean isEstonian(String phone) {
// https://en.wikipedia.org/wiki/Telephone_numbers_in_Estonia
return Stream.of("5", "81", "82", "83", "84", "870", "871").anyMatch(phone::startsWith);
}

private boolean isLithuanian(String phone) {
// https://en.wikipedia.org/wiki/Telephone_numbers_in_Lithuania
return phone.startsWith("86");
}

private void validateOkResult(StringHolder result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,20 @@ public void loginByPhone() throws RemoteException {
}

@Test
public void removesPlusFromPhoneNumber() {
assertThat(mid.normalizePhoneNumber("+37255667788"), equalTo("37255667788"));

public void normalizesEstonianAndLithuanianPhoneNumbers() {
assertThat(mid.normalizePhoneNumber(null), nullValue());
assertThat(mid.normalizePhoneNumber(""), equalTo(""));
assertThat(mid.normalizePhoneNumber("+37255667788"), equalTo("37255667788"));
assertThat(mid.normalizePhoneNumber("37255667788"), equalTo("37255667788"));
assertThat(mid.normalizePhoneNumber("55667788"), equalTo("55667788"));
assertThat(mid.normalizePhoneNumber("5xxxxxx"), equalTo("3725xxxxxx"));
assertThat(mid.normalizePhoneNumber("5xxxxxxx"), equalTo("3725xxxxxxx"));
assertThat(mid.normalizePhoneNumber("81xxxxxx"), equalTo("37281xxxxxx"));
assertThat(mid.normalizePhoneNumber("82xxxxxx"), equalTo("37282xxxxxx"));
assertThat(mid.normalizePhoneNumber("83xxxxxx"), equalTo("37283xxxxxx"));
assertThat(mid.normalizePhoneNumber("84xxxxxx"), equalTo("37284xxxxxx"));
assertThat(mid.normalizePhoneNumber("870xxxxxxxxx"), equalTo("372870xxxxxxxxx"));
assertThat(mid.normalizePhoneNumber("871xxxxxxxxx"), equalTo("372871xxxxxxxxx"));
assertThat(mid.normalizePhoneNumber("86xxxxxxx"), equalTo("37086xxxxxxx"));
}

@Test
Expand Down
9 changes: 5 additions & 4 deletions test/com/codeborne/security/mobileid/MobileIDSignerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -30,8 +29,8 @@ public void setUp() {
}

@Test
@Ignore
public void mobileIdSignatureFlow() throws IOException {
@Ignore
public void mobileIdSignatureFlow() throws Exception {
// startSession
int sessCode = signer.startSession();
assertThat(sessCode, is(not(0)));
Expand Down Expand Up @@ -59,6 +58,7 @@ public void mobileIdSignatureFlow() throws IOException {
assertThat(challenge, is(notNullValue()));

// getStatusInfo
// set a breakpoint here and resume after typing in the PIN2 on your phone
String status = signer.getStatusInfo(sessCode);
assertThat(status, is("SIGNATURE"));

Expand All @@ -76,14 +76,15 @@ public void mobileIdSignatureFlow() throws IOException {

@Test
@Ignore
public void compactMobileIdSignatureFlow() throws Exception {
public void compactMobileIdSignatureFlow() {
List<SignatureFile> files = asList(
new SignatureFile("test1.txt", "text/plain", "Test1".getBytes()),
new SignatureFile("test2.txt", "text/plain", "Test2".getBytes())
);

MobileIdSignatureSession session = signer.startSign(files, "38112310010", "55555555");

// set a breakpoint here and resume after typing in the PIN2 on your phone
byte[] signedFile = signer.getSignedFile(session);
assertThat(signedFile, is(notNullValue()));
}
Expand Down

0 comments on commit e01bd8d

Please sign in to comment.