Skip to content

Commit

Permalink
[ES-1325] (#237)
Browse files Browse the repository at this point in the history
* [ES-1325]

Signed-off-by: Balaji <[email protected]>

* updated a propertykey

Signed-off-by: Balaji <[email protected]>

---------

Signed-off-by: Balaji <[email protected]>
Signed-off-by: ase-101 <[email protected]>
Co-authored-by: ase-101 <[email protected]>
  • Loading branch information
balaji-alluru and ase-101 authored Sep 5, 2024
1 parent 4750de1 commit de79c90
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class IdentityData {

String pin;

List<LanguageValue> name;
List<LanguageValue> name;

List<LanguageValue> fullName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class KycAuthRequestDto {

private String transactionId;
private String individualId;
private String password;
private String otp;
private String pin;
private String biometrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@
import io.mosip.kernel.signature.dto.JWTSignatureResponseDto;
import io.mosip.kernel.signature.service.SignatureService;
import lombok.extern.slf4j.Slf4j;

import org.jose4j.jwe.ContentEncryptionAlgorithmIdentifiers;
import org.jose4j.jwe.JsonWebEncryption;
import org.jose4j.jwe.KeyManagementAlgorithmIdentifiers;
import org.json.simple.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
Expand Down Expand Up @@ -76,6 +80,9 @@ public class AuthenticationServiceImpl implements AuthenticationService {

@Value("${mosip.mock.ida.kyc.encrypt:false}")
private boolean encryptKyc;

@Value("${mosip.mock.ida.hash-algo:MD5}")
private String cryptoAlgo;

@Value("${mosip.mock.ida.kyc.default-language:eng}")
private String defaultLanguage;
Expand Down Expand Up @@ -314,6 +321,10 @@ private Boolean doKycAuthentication(KycAuthRequestDto kycAuthRequestDto,JsonNode
authStatus=validateKnowledgeBasedAuth(kycAuthRequestDto,identityData);
}

if(kycAuthRequestDto.getPassword()!=null){
authStatus=validatePasswordAuth(kycAuthRequestDto,identityData);
}

if (!CollectionUtils.isEmpty(kycAuthRequestDto.getTokens())) {
authStatus = !StringUtils.isEmpty(kycAuthRequestDto.getTokens().get(0));
if (!authStatus)
Expand Down Expand Up @@ -354,6 +365,12 @@ private boolean validateKnowledgeBasedAuth(KycAuthRequestDto kycAuthRequestDto,J
return true;
}

private boolean validatePasswordAuth(KycAuthRequestDto kycAuthRequestDto,JsonNode identityData){
String password=kycAuthRequestDto.getPassword();
String passwordHash = identityData.get("password").asText();
return checkPassword(password,passwordHash);
}

private String signKyc(Map<String, Object> kyc) throws JsonProcessingException {
String payload = objectMapper.writeValueAsString(kyc);
JWTSignatureRequestDto jwtSignatureRequestDto = new JWTSignatureRequestDto();
Expand Down Expand Up @@ -606,6 +623,27 @@ private Map<String, Object> getKycValues(List<String> locales, String claimName,
}
return map;
}

private boolean checkPassword(String password, String passwordHash) {
try
{
MessageDigest md = MessageDigest.getInstance(cryptoAlgo);
md.update(password.getBytes(StandardCharsets.UTF_8));
byte[] bytes = md.digest();

// This bytes[] has bytes in decimal format. Convert it to hexadecimal format
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}

// Get complete hashed password in hex format
return passwordHash.equals(sb.toString());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return false;
}

private boolean isClaimAvailable(String claim, JsonNode identityData) throws Exception {
return HelperUtil.getIdentityDataValue(identityData,claim,fieldLang)!=null;
Expand Down

0 comments on commit de79c90

Please sign in to comment.