@@ -14,17 +14,13 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func CNRidMD5AliEnc(kyc *KycInfo) (*AliCloudAuth, error) {
|
||||
if kyc.Type != "Chinese" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func CNRidMD5AliEnc(kyc *CNRidInfo) (*AliCloudAuth, error) {
|
||||
// MD5 Legal Name rule: First Chinese char md5enc, remaining plain, at least 2 Chinese chars
|
||||
if len(kyc.CNRidInfo.LegalName) < 2 || utf8.RuneCountInString(kyc.CNRidInfo.LegalName) < 2 {
|
||||
if len(kyc.LegalName) < 2 || utf8.RuneCountInString(kyc.LegalName) < 2 {
|
||||
return nil, fmt.Errorf("input string must have at least 2 Chinese characters")
|
||||
}
|
||||
|
||||
lnFirstRune, size := utf8.DecodeRuneInString(kyc.CNRidInfo.LegalName)
|
||||
lnFirstRune, size := utf8.DecodeRuneInString(kyc.LegalName)
|
||||
if lnFirstRune == utf8.RuneError {
|
||||
return nil, fmt.Errorf("invalid first character")
|
||||
}
|
||||
@@ -33,18 +29,18 @@ func CNRidMD5AliEnc(kyc *KycInfo) (*AliCloudAuth, error) {
|
||||
lnHash.Write([]byte(string(lnFirstRune)))
|
||||
lnFirstHash := hex.EncodeToString(lnHash.Sum(nil))
|
||||
|
||||
lnRemaining := kyc.CNRidInfo.LegalName[size:]
|
||||
lnRemaining := kyc.LegalName[size:]
|
||||
|
||||
ln := lnFirstHash + lnRemaining
|
||||
|
||||
// MD5 Resident Id rule: First 6 char plain, middle birthdate md5enc, last 4 char plain, at least 18 chars
|
||||
if len(kyc.CNRidInfo.ResidentId) < 18 {
|
||||
if len(kyc.ResidentId) < 18 {
|
||||
return nil, fmt.Errorf("input string must have at least 18 characters")
|
||||
}
|
||||
|
||||
ridPrefix := kyc.CNRidInfo.ResidentId[:6]
|
||||
ridSuffix := kyc.CNRidInfo.ResidentId[len(kyc.CNRidInfo.ResidentId)-4:]
|
||||
ridMiddle := kyc.CNRidInfo.ResidentId[6 : len(kyc.CNRidInfo.ResidentId)-4]
|
||||
ridPrefix := kyc.ResidentId[:6]
|
||||
ridSuffix := kyc.ResidentId[len(kyc.ResidentId)-4:]
|
||||
ridMiddle := kyc.ResidentId[6 : len(kyc.ResidentId)-4]
|
||||
|
||||
ridHash := md5.New()
|
||||
ridHash.Write([]byte(ridMiddle))
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func EncodeAES(kyc *KycInfo) (*string, error) {
|
||||
func EncodeAES(kyc any) (*string, error) {
|
||||
plainJson, err := json.Marshal(kyc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -23,14 +23,14 @@ func EncodeAES(kyc *KycInfo) (*string, error) {
|
||||
return &encrypted, nil
|
||||
}
|
||||
|
||||
func DecodeAES(cipherStr string) (*KycInfo, error) {
|
||||
func DecodeAES(cipherStr string) (any, error) {
|
||||
aesKey := viper.GetString("secrets.kyc_info_key")
|
||||
plainBytes, err := cryptography.AESCBCDecrypt(cipherStr, []byte(aesKey))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var kycInfo KycInfo
|
||||
var kycInfo any
|
||||
if err := json.Unmarshal(plainBytes, &kycInfo); err != nil {
|
||||
return nil, errors.New("[KYC] invalid decrypted json")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package kyc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -11,78 +12,80 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func CreateSession() (*PassportReaderSessionResponse, error) {
|
||||
const (
|
||||
StateCreated = "CREATED"
|
||||
StateInitiated = "INITIATED"
|
||||
StateFailed = "FAILED"
|
||||
StateAborted = "ABORTED"
|
||||
StateCompleted = "COMPLETED"
|
||||
StateRejected = "REJECTED"
|
||||
StateApproved = "APPROVED"
|
||||
)
|
||||
|
||||
func doPassportRequest(ctx context.Context, method, path string, body any, target any) error {
|
||||
publicKey := viper.GetString("kyc.passport_reader_public_key")
|
||||
secret := viper.GetString("kyc.passport_reader_secret")
|
||||
baseURL := "https://passportreader.app/api/v1"
|
||||
|
||||
apiURL := "https://passportreader.app/api/v1/session.create"
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
var bodyReader io.Reader
|
||||
if body != nil {
|
||||
jsonData, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal request failed: %w", err)
|
||||
}
|
||||
bodyReader = bytes.NewBuffer(jsonData)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", apiURL, nil)
|
||||
req, err := http.NewRequestWithContext(ctx, method, baseURL+path, bodyReader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
return fmt.Errorf("create request failed: %w", err)
|
||||
}
|
||||
|
||||
req.SetBasicAuth(publicKey, secret)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request failed: %w", err)
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return nil, fmt.Errorf("api returned error: %d, body: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var sessionResp PassportReaderSessionResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&sessionResp); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
|
||||
return &sessionResp, nil
|
||||
}
|
||||
|
||||
func GetSessionDetails(sessionID int) (*PassportReaderSessionDetailResponse, error) {
|
||||
publicKey := viper.GetString("kyc.passport_reader_public_key")
|
||||
secret := viper.GetString("kyc.passport_reader_secret")
|
||||
|
||||
apiURL := "https://passportreader.app/api/v1/session.get"
|
||||
|
||||
reqPayload := PassportReaderGetSessionRequest{ID: sessionID}
|
||||
jsonData, err := json.Marshal(reqPayload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal request: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
|
||||
req.SetBasicAuth(publicKey, secret)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{Timeout: 15 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request failed: %w", err)
|
||||
return fmt.Errorf("http request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return nil, fmt.Errorf("api error: %d, response: %s", resp.StatusCode, string(body))
|
||||
respBody, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("api error: status %d, body %s", resp.StatusCode, string(respBody))
|
||||
}
|
||||
|
||||
var detailResp PassportReaderSessionDetailResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&detailResp); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
if target != nil {
|
||||
if err := json.NewDecoder(resp.Body).Decode(target); err != nil {
|
||||
return fmt.Errorf("decode response failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &detailResp, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateSession(ctx context.Context) (*PassportReaderSessionResponse, error) {
|
||||
var resp PassportReaderSessionResponse
|
||||
err := doPassportRequest(ctx, "POST", "/session.create", nil, &resp)
|
||||
return &resp, err
|
||||
}
|
||||
|
||||
func GetSessionState(ctx context.Context, sessionID int) (string, error) {
|
||||
payload := PassportReaderGetSessionRequest{ID: sessionID}
|
||||
var resp PassportReaderStateResponse
|
||||
err := doPassportRequest(ctx, "POST", "/session.state", payload, &resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return resp.State, nil
|
||||
}
|
||||
|
||||
func GetSessionDetails(ctx context.Context, sessionID int) (*PassportReaderSessionDetailResponse, error) {
|
||||
payload := PassportReaderGetSessionRequest{ID: sessionID}
|
||||
var resp PassportReaderSessionDetailResponse
|
||||
err := doPassportRequest(ctx, "POST", "/session.get", payload, &resp)
|
||||
return &resp, err
|
||||
}
|
||||
|
||||
@@ -1,17 +1,15 @@
|
||||
package kyc
|
||||
|
||||
type KycInfo struct {
|
||||
Type string `json:"type"` // cnrid/passport
|
||||
CNRidInfo *CNRidInfo `json:"CNRodInfo"`
|
||||
PassportInfo *PassportInfo `json:"passport_info"`
|
||||
}
|
||||
|
||||
type CNRidInfo struct {
|
||||
LegalName string `json:"legal_name"`
|
||||
ResidentId string `json:"resident_id"`
|
||||
}
|
||||
|
||||
type PassportInfo struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type PassportResp struct {
|
||||
GivenNames string `json:"given_names"`
|
||||
Surname string `json:"surname"`
|
||||
Nationality string `json:"nationality"`
|
||||
@@ -36,6 +34,10 @@ type PassportReaderGetSessionRequest struct {
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
type PassportReaderStateResponse struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
|
||||
type PassportReaderSessionDetailResponse struct {
|
||||
State string `json:"state"`
|
||||
GivenNames string `json:"given_names"`
|
||||
|
||||
Reference in New Issue
Block a user