Issues (23)

src/pipes/Smile.js (6 issues)

1
const httpProcessor = require('../HttpProcessor');
2
const services = require('../config/services');
3
const md5 = require('md5');
4
const { randomNumber }= require('../classes/Helper');
5
const crypto = require('crypto');
6
7
8
class Smile
9
{
10
11
    constructor() {
12
        this.client = services.smile.client;
13
        this.apiKey = services.smile.api_key;
14
        this.baseUrl = services.smile.api_url;
15
        this.partnerId = services.smile.partner_id;
16
17
    }
18
    
19
20
    /**
21
    * Process axios calls
22
    * 
23
    * @param {string} method The call method get|post|put|delete|patch
24
    * @param {string} url The url to call
25
    * @param {object|formData} payload The payload data to send with the call
26
    */
27
    process(method, url, payload) {
28
        //HttpProcessor class to handle axios calls
29
        let processor = new httpProcessor(this.baseUrl, this.apiKey, this.client);
0 ignored issues
show
Coding Style Best Practice introduced by
By convention, constructors like httpProcessor should be capitalized.
Loading history...
30
        
31
        return processor.process(method, url, payload)
32
    }
33
34
    /**
35
    * Handles the ID request
36
    *
37
    * @param {object} IdFilter
38
    * @return {object}
39
    */
40
    async handle(IdFilter)
41
    {
42
        if (!IdFilter.isSuccessful()) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if !IdFilter.isSuccessful() is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
43
            const generatedKey = this.generateSecretKey(this.partnerId, this.apiKey);
44
            const jobId = md5(Date.now() + randomNumber(1000000000));
45
46
            const idType = IdFilter.getIDType();
47
            const country = IdFilter.getCountry();
48
            const url = '/v1/id_verification';
49
50
            const idNumber =  IdFilter.getIDNumber();
51
            const firstName =  IdFilter.getFirstName();
52
            const lastName =  IdFilter.getLastName();
53
            const middleName =  IdFilter.getMiddleName();
54
            const dateOfBirth =  IdFilter.getDOB();
55
            const phone =  IdFilter.getPhoneNumber();
56
            const userId = IdFilter.getUserId();
57
            const company = IdFilter.getCompany();
58
59
            const body = {
60
                'partner_id' : this.partnerId,
61
                'timestamp' : generatedKey.timestamp,
62
                'sec_key' : generatedKey.secretKey,
63
                'country'  :  country.toUpperCase(),
64
                'id_type'  :  idType,
65
                'id_number' : idNumber,
66
                'middle_name' : middleName,
67
                'first_name' : firstName,
68
                'last_name' : lastName,
69
                'phone_number' : phone,
70
                'dob' : dateOfBirth,
71
                'company' : company,
72
                'partner_params' : {
73
                    'job_id' : jobId,
74
                    'job_type' : 5,
75
                    'user_id' : userId
76
                }
77
78
            };
79
80
            try {
81
                const response = await this.process('POST', url, body);
82
                
83
                IdFilter.confirmSuccess();
84
85
                IdFilter.setHandler(this.client);
86
87
                IdFilter.setData({
88
                    'handler' : IdFilter.getHandler(),
89
                    'country' : IdFilter.getCountry().toUpperCase(),
90
                    'message' : idType + ' Verified' + ' Successfully',
91
                    'data' : response
92
                });
93
94
                return IdFilter.getData();
95
                
96
            } catch (error) {
97
                IdFilter.setError({'error' : error});
98
        
99
                return IdFilter.getError();
100
            }
101
        }
102
103
    }
104
105
    /**
106
    * Generate Secret Key
107
    *
108
    * @param {string} partnerId
109
    * @param {string} apiKey
110
    * @return {object}
111
    */
112
    generateSecretKey(partnerId, apiKey){
113
        // Calculating outgoing signature:
114
        const timestamp = Date.now();
115
116
        const hash = crypto.createHash('sha256').update(parseInt(partnerId, 10) + ":" + timestamp).digest('hex');
117
        
118
        const encrypted = crypto.publicEncrypt({
119
          key: Buffer.from(apiKey, 'base64'),
0 ignored issues
show
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
120
          padding: crypto.constants.RSA_PKCS1_PADDING
121
        }, Buffer.from(hash)).toString('base64');
122
123
        const secretKey = [encrypted, hash].join('|');
124
125
        return {'secretKey' : secretKey, 'timestamp' :timestamp };
126
     }
127
128
    /**
129
    * Confirm Secret Key
130
    *
131
    * @param {string} timestamp
132
    * @param {string} secretKey
133
    * @return boolean
134
    */
135
    confirmSecretKey(timestamp, secretKey){
136
137
        var encrypted, hashed;
138
        [encrypted, hashed] = secretKey.split("|")
0 ignored issues
show
The variable hashed seems to be never initialized.
Loading history...
The variable encrypted seems to be never initialized.
Loading history...
139
140
        var hash = crypto.createHash('sha256').update(parseInt(this.partnerId) + ":" + timestamp).digest('hex');
141
        
142
        var decrypted = crypto.publicDecrypt({
143
            key: Buffer.from(Buffer.from(this.apiKey, 'base64')),
0 ignored issues
show
The variable Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
144
            padding: crypto.constants.RSA_PKCS1_PADDING
145
        }, Buffer.from(encrypted, 'base64'));
146
147
        var success = decrypted == hashed && hashed == hash;
148
149
        return success;
150
     }
151
152
}
153
154
module.exports = Smile;