implemented phone checks and sms checks

This commit is contained in:
heyethereum
2024-08-17 12:49:19 +08:00
parent d24ece60fd
commit e8836f1b5e
4 changed files with 58 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ package com.safeqr.app.qrcode.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -30,4 +31,8 @@ public class PhoneEntity {
private UUID qrCodeId;
private String phone;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Column(name = "remarks")
private String remarks;
}

View File

@@ -38,6 +38,6 @@ public final class PhoneModel extends QRCodeModel<PhoneEntity> {
}
@Override
public String retrieveClassification() {
return "";
return phoneVerificationService.checkPhoneNumber(details);
}
}

View File

@@ -11,6 +11,8 @@ import org.springframework.stereotype.Service;
import java.util.UUID;
import static com.safeqr.app.constants.CommonConstants.*;
@Service
public class PhoneVerificationService {
private final PhoneRepository phoneRepository;
@@ -48,4 +50,30 @@ public class PhoneVerificationService {
}
public String checkPhoneNumber(PhoneEntity phoneEntity) {
// Remove any spaces, dashes, parentheses, and trim the ends
String phoneNumber = phoneEntity.getPhone().replaceAll("[\\s\\-()]", "").trim();
// Check if the number starts with +65 or just 65
if (phoneNumber.startsWith("+65")) {
phoneNumber = phoneNumber.substring(3); // Remove the "+65"
} else if (phoneNumber.startsWith("65")) {
phoneNumber = phoneNumber.substring(2); // Remove the "65"
}
// Check if it's a valid Singapore mobile or landline number
if (phoneNumber.matches("^[689]\\d{7}$")) {
if (phoneNumber.startsWith("8") || phoneNumber.startsWith("9")) {
phoneEntity.setRemarks("Singapore mobile number - This number has not been scanned for scam. Please do not divulge your personal information.");
} else if (phoneNumber.startsWith("6")) {
phoneEntity.setRemarks("Singapore landline number - This phone number has not been scanned for scam. Please do not divulge your personal information.");
}
return CLASSIFY_UNKNOWN;
}
// If it doesn't match mobile or landline pattern
phoneEntity.setRemarks("Warning: This is either an overseas number or an invalid Singapore number. Please exercise caution.");
return CLASSIFY_WARNING;
}
}

View File

@@ -2,6 +2,7 @@ package com.safeqr.app.qrcode.service;
import com.safeqr.app.exceptions.InvalidFormatExceptions;
import com.safeqr.app.exceptions.ResourceNotFoundExceptions;
import com.safeqr.app.qrcode.entity.PhoneEntity;
import com.safeqr.app.qrcode.entity.SMSEntity;
import com.safeqr.app.qrcode.repository.SMSRepository;
import org.slf4j.Logger;
@@ -12,8 +13,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import static com.safeqr.app.constants.CommonConstants.CLASSIFY_SAFE;
import static com.safeqr.app.constants.CommonConstants.CLASSIFY_WARNING;
import static com.safeqr.app.constants.CommonConstants.*;
@Service
public class SMSVerificationService {
@@ -84,7 +84,6 @@ public class SMSVerificationService {
throw new InvalidFormatExceptions("Invalid SMSTO format. Expected format: SMSTO:<phone>:<message>");
}
}
@Transactional
public String getClassification (SMSEntity smsEntity) {
String lowerCaseSms = smsEntity.getMessage().toLowerCase();
@@ -105,8 +104,28 @@ public class SMSVerificationService {
}
}
// If no phishing keywords are found
return CLASSIFY_SAFE;
// If no phishing keywords are found, sent for local phone number checks
return checkPhoneNumber(smsEntity.getPhone());
}
private String checkPhoneNumber(String phoneNumber) {
// Remove any spaces, dashes, parentheses, and trim the ends
phoneNumber = phoneNumber.replaceAll("[\\s\\-()]", "").trim();
// Check if the number starts with +65 or just 65
if (phoneNumber.startsWith("+65")) {
phoneNumber = phoneNumber.substring(3); // Remove the "+65"
} else if (phoneNumber.startsWith("65")) {
phoneNumber = phoneNumber.substring(2); // Remove the "65"
}
// Check if it's a valid Singapore mobile or landline number
if (phoneNumber.matches("^[689]\\d{7}$") && (phoneNumber.startsWith("8") || phoneNumber.startsWith("9"))) {
return CLASSIFY_SAFE;
}
// If it doesn't match mobile
return CLASSIFY_UNSAFE;
}
}