perbaikan decript
This commit is contained in:
@@ -2,49 +2,110 @@ package services
|
||||
|
||||
import (
|
||||
helper "api-service/internal/helpers/bpjs"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
|
||||
lzstring "github.com/daku10/go-lz-string"
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
func min(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// ResponseVclaim decrypts the encrypted response from VClaim API
|
||||
func ResponseVclaim(encrypted string, key string) (string, error) {
|
||||
log.Println("ResponseVclaim: Starting decryption process")
|
||||
log.Printf("ResponseVclaim: Encrypted string: %s", encrypted)
|
||||
|
||||
// Pad the base64 string if needed
|
||||
if len(encrypted)%4 != 0 {
|
||||
padding := (4 - len(encrypted)%4) % 4
|
||||
for i := 0; i < padding; i++ {
|
||||
encrypted += "="
|
||||
}
|
||||
log.Printf("ResponseVclaim: Padded encrypted string: %s", encrypted)
|
||||
}
|
||||
|
||||
cipherText, err := base64.StdEncoding.DecodeString(encrypted)
|
||||
if err != nil {
|
||||
log.Printf("ResponseVclaim: Failed to decode base64: %v", err)
|
||||
return "", err
|
||||
}
|
||||
log.Printf("ResponseVclaim: Base64 decoded successfully, length: %d", len(cipherText))
|
||||
|
||||
hash := sha256.Sum256([]byte(key))
|
||||
|
||||
block, err := aes.NewCipher(hash[:])
|
||||
if err != nil {
|
||||
log.Printf("ResponseVclaim: Failed to create AES cipher: %v", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(cipherText) < aes.BlockSize {
|
||||
log.Println("ResponseVclaim: CipherText too short")
|
||||
return "", errors.New("cipherText too short")
|
||||
}
|
||||
|
||||
iv := hash[:aes.BlockSize]
|
||||
|
||||
if len(cipherText)%aes.BlockSize != 0 {
|
||||
log.Println("ResponseVclaim: CipherText not multiple of block size")
|
||||
return "", errors.New("cipherText is not a multiple of the block size")
|
||||
}
|
||||
|
||||
mode := cipher.NewCBCDecrypter(block, iv)
|
||||
mode.CryptBlocks(cipherText, cipherText)
|
||||
log.Println("ResponseVclaim: AES decryption completed")
|
||||
|
||||
// cipherText, _ = pkcs7.Unpad(cipherText, aes.BlockSize)
|
||||
cipherText = helper.RemovePKCS7Padding(cipherText)
|
||||
data, err := lzstring.DecompressFromEncodedURIComponent(string(cipherText))
|
||||
// data, err := helper.DecompressFromEncodedUriComponent(string(cipherText))
|
||||
log.Printf("ResponseVclaim: PKCS7 padding removed, length: %d", len(cipherText))
|
||||
|
||||
var data string
|
||||
|
||||
// Try gzip decompression first
|
||||
reader, err := gzip.NewReader(bytes.NewReader(cipherText))
|
||||
if err != nil {
|
||||
return "", err
|
||||
log.Printf("ResponseVclaim: Gzip decompression failed: %v, trying LZ-string decompression", err)
|
||||
// Try lz-string decompression using helper function
|
||||
data, err := helper.StringDecrypt(key, string(cipherText))
|
||||
if err != nil || len(data) == 0 {
|
||||
log.Printf("ResponseVclaim: Helper StringDecrypt failed or empty: %v, trying without decompression", err)
|
||||
// Try without decompression
|
||||
data = string(cipherText)
|
||||
log.Printf("ResponseVclaim: Using decrypted data without decompression, data length: %d", len(data))
|
||||
} else {
|
||||
log.Printf("ResponseVclaim: Helper StringDecrypt successful, data length: %d, data: %s", len(data), data[:min(100, len(data))])
|
||||
}
|
||||
} else {
|
||||
defer reader.Close()
|
||||
decompressed, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
log.Printf("ResponseVclaim: Failed to read gzip decompressed data: %v, trying LZ-string decompression", err)
|
||||
// Try lz-string decompression using helper function
|
||||
data, err := helper.StringDecrypt(key, string(cipherText))
|
||||
if err != nil || len(data) == 0 {
|
||||
log.Printf("ResponseVclaim: Helper StringDecrypt failed or empty: %v, using data without decompression", err)
|
||||
data = string(cipherText)
|
||||
log.Printf("ResponseVclaim: Using decrypted data without decompression, data length: %d", len(data))
|
||||
} else {
|
||||
log.Printf("ResponseVclaim: Helper StringDecrypt successful, data length: %d", len(data))
|
||||
}
|
||||
} else {
|
||||
data = string(decompressed)
|
||||
log.Printf("ResponseVclaim: Gzip decompression successful, data length: %d", len(data))
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("ResponseVclaim: Final data length: %d, data: %s", len(data), data[:min(100, len(data))])
|
||||
log.Println("ResponseVclaim: Decryption process completed successfully")
|
||||
return data, nil
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"api-service/internal/config"
|
||||
"api-service/internal/models/vclaim/peserta"
|
||||
|
||||
"github.com/mashingan/smapping"
|
||||
"github.com/rs/zerolog/log"
|
||||
@@ -198,41 +199,53 @@ func (s *Service) processResponse(res *http.Response) (*ResponDTOVclaim, error)
|
||||
return finalResp, nil
|
||||
}
|
||||
|
||||
// Decrypt response
|
||||
consID, secretKey, _, tstamp, _ := s.config.SetHeader()
|
||||
respDecrypt, err := ResponseVclaim(respMentah.Response, consID+secretKey+tstamp)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("meta_code", respMentah.MetaData.Code).
|
||||
Msg("Failed to decrypt response")
|
||||
return nil, fmt.Errorf("failed to decrypt response: %w", err)
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Str("encrypted_length", fmt.Sprintf("%d bytes", len(respMentah.Response))).
|
||||
Str("decrypted_length", fmt.Sprintf("%d bytes", len(respDecrypt))).
|
||||
Msg("Response decrypted successfully")
|
||||
|
||||
// Unmarshal decrypted response
|
||||
if respDecrypt != "" {
|
||||
if err := json.Unmarshal([]byte(respDecrypt), &finalResp.Response); err != nil {
|
||||
// If JSON unmarshal fails, store as string
|
||||
log.Warn().
|
||||
// Try to unmarshal response as JSON first (in case it's not encrypted)
|
||||
var tempResp interface{}
|
||||
if json.Unmarshal([]byte(respMentah.Response), &tempResp) == nil {
|
||||
log.Debug().Msg("Response is valid JSON, not encrypted")
|
||||
finalResp.Response = tempResp
|
||||
} else {
|
||||
log.Debug().Msg("Response is not valid JSON, trying to decrypt")
|
||||
// Decrypt response
|
||||
consID, secretKey, _, tstamp, _ := s.config.SetHeader()
|
||||
respDecrypt, err := ResponseVclaim(respMentah.Response, consID+secretKey+tstamp)
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Msg("Failed to unmarshal decrypted response, storing as string")
|
||||
finalResp.Response = respDecrypt
|
||||
} else {
|
||||
log.Debug().Msg("Decrypted response unmarshaled successfully")
|
||||
Str("meta_code", respMentah.MetaData.Code).
|
||||
Msg("Failed to decrypt response")
|
||||
return nil, fmt.Errorf("failed to decrypt response: %w", err)
|
||||
}
|
||||
|
||||
// Use gjson to extract and log some metadata from the response if it's JSON
|
||||
if jsonBytes, err := json.Marshal(finalResp.Response); err == nil {
|
||||
jsonStr := string(jsonBytes)
|
||||
// Extract some common fields using gjson
|
||||
if metaCode := gjson.Get(jsonStr, "metaData.code"); metaCode.Exists() {
|
||||
log.Info().
|
||||
Str("response_meta_code", metaCode.String()).
|
||||
Msg("Final response metadata")
|
||||
log.Debug().
|
||||
Str("encrypted_length", fmt.Sprintf("%d bytes", len(respMentah.Response))).
|
||||
Str("decrypted_length", fmt.Sprintf("%d bytes", len(respDecrypt))).
|
||||
Msg("Response decrypted successfully")
|
||||
|
||||
log.Debug().
|
||||
Str("decrypted_data", respDecrypt).
|
||||
Msg("Decrypted data")
|
||||
|
||||
// Unmarshal decrypted response
|
||||
if respDecrypt != "" {
|
||||
if err := json.Unmarshal([]byte(respDecrypt), &finalResp.Response); err != nil {
|
||||
// If JSON unmarshal fails, store as string
|
||||
log.Warn().
|
||||
Err(err).
|
||||
Msg("Failed to unmarshal decrypted response, storing as string")
|
||||
finalResp.Response = respDecrypt
|
||||
} else {
|
||||
log.Debug().Msg("Decrypted response unmarshaled successfully")
|
||||
|
||||
// Use gjson to extract and log some metadata from the response if it's JSON
|
||||
if jsonBytes, err := json.Marshal(finalResp.Response); err == nil {
|
||||
jsonStr := string(jsonBytes)
|
||||
// Extract some common fields using gjson
|
||||
if metaCode := gjson.Get(jsonStr, "metaData.code"); metaCode.Exists() {
|
||||
log.Info().
|
||||
Str("response_meta_code", metaCode.String()).
|
||||
Msg("Final response metadata")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -410,6 +423,20 @@ func mapToResult(resp *ResponDTOVclaim, result interface{}) error {
|
||||
return fmt.Errorf("failed to unmarshal to result: %w", err)
|
||||
}
|
||||
|
||||
// Handle BPJS peserta response structure
|
||||
if pesertaResp, ok := result.(*peserta.PesertaResponse); ok {
|
||||
if resp.Response != nil {
|
||||
if responseMap, ok := resp.Response.(map[string]interface{}); ok {
|
||||
if pesertaMap, ok := responseMap["peserta"]; ok {
|
||||
pesertaBytes, _ := json.Marshal(pesertaMap)
|
||||
var pd peserta.PesertaData
|
||||
json.Unmarshal(pesertaBytes, &pd)
|
||||
pesertaResp.Data = &pd
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user