-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vtexid.js
147 lines (120 loc) · 3.34 KB
/
Vtexid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
const axios = require('axios');
const path = require('path');
const message = require('./utils/cli-colors');
const jsonfile = require('jsonfile');
const qs = require('qs');
const DIRNAME = __dirname;
const PROJECTDIR = process.cwd();
class VtexId {
constructor(account = null) {
this.account = account;
this.uri = `https://${this.account}.vtexcommercestable.com.br/api/vtexid/pub/authentication`;
this.token = null;
this.authCookie = null;
this.endpoints = {
getToken: '/start',
authenticateByEmailKey: '/accesskey/validate',
getEmailAcessKey: '/accesskey/send'
};
this.userInfos = {
initialCallback: {
callbackUrl: `${this.uri}/finish`,
user: null,
locale: 'pt-BR',
accountName: this.account
}
};
this.storeAuthCookiePath = path.resolve(PROJECTDIR, 'xvtx.auth.json');
};
setAccount(account) {
this.account = account;
this.uri = `http://${account}.vtexcommercestable.com.br/api/vtexid/pub/authentication`;
};
setAuthCookie(authCookie) {
this.authCookie = authCookie;
}
getToken() {
return axios
.get(`${this.uri}${this.endpoints.getToken}`, {
params: {
...this.userInfos.initialCallback,
appStart: true,
}
})
.then(( { data } ) => {
if(data && data.authenticationToken) {
this.token = data.authenticationToken;
return data.authenticationToken;
}
})
.catch(err => {
message('error', err);
throw new Error(err);
});
};
getEmailAccessKey(email) {
if(!this.token) {
return this.getToken()
.then(token => this._getEmailAccessKey(email, token))
} else {
return this._getEmailAccessKey(email);
}
};
_getEmailAccessKey(email, authenticationToken = this.token) {
return axios
.post(`${this.uri}${this.endpoints.getEmailAcessKey}`, qs.stringify({
email,
authenticationToken
}))
.catch(err => {
message('error', err);
throw new Error(err);
});
};
authenticateByEmailKey(login, accesskey) {
return axios
.post(`${this.uri}${this.endpoints.authenticateByEmailKey}`, qs.stringify({
login,
accesskey,
authenticationToken: this.token
}))
.then(( { data } ) => {
this.authCookie = data.authCookie.Value;
return data.authCookie.Value;
})
.catch(err => {
message('error', err);
throw new Error(err);
});
};
checkAuthStore(account, email, writeAuthStore) {
const storeAuthCookie = jsonfile.readFileSync(this.storeAuthCookiePath, { throws: false });
if(!storeAuthCookie) {
if(writeAuthStore) jsonfile.writeFileSync(this.storeAuthCookiePath, {});
return false;
}
if(storeAuthCookie[account]
&& storeAuthCookie[account][email]
&& this._checkDiffDate(storeAuthCookie[account][email].lastUpdate)) return storeAuthCookie[account][email].authCookie;
return false;
}
writeAuthStore(account, email, authCookie) {
const storeAuthCookie = jsonfile.readFileSync(this.storeAuthCookiePath, { throws: false });
const newStoreAuthCookie = {
...storeAuthCookie,
[account]: {
...storeAuthCookie[account],
[email]: {
authCookie,
lastUpdate: new Date()
}
}
};
jsonfile.writeFileSync(this.storeAuthCookiePath, newStoreAuthCookie, {spaces: 4});
}
_checkDiffDate(storeDate) {
return Math.round(Math.abs(new Date() - new Date(storeDate)) / 36e5) < 8;
}
};
module.exports = VtexId;