@@ -7,6 +7,7 @@ import cloneDeep from 'lodash/cloneDeep'
7
7
import { User } from './user/index'
8
8
import error from './core/contentstackError'
9
9
import OAuthHandler from './core/oauthHandler'
10
+ import { authenticator } from 'otplib'
10
11
11
12
export default function contentstackClient ( { http } ) {
12
13
/**
@@ -16,7 +17,8 @@ export default function contentstackClient ({ http }) {
16
17
* @param {Object } parameters - login parameters
17
18
* @prop {string } parameters.email - email id for user to login
18
19
* @prop {string } parameters.password - password for user to login
19
- * @prop {string } parameters.token - token for user to login
20
+ * @prop {string } parameters.tfa_token - tfa token for user to login (2FA token)
21
+ * @prop {string } parameters.mfaSecret - TOTP secret key for generating 2FA token
20
22
* @returns {Promise }
21
23
* @example
22
24
* import * as contentstack from '@contentstack/management'
@@ -25,10 +27,23 @@ export default function contentstackClient ({ http }) {
25
27
* client.login({ email: <emailid>, password: <password> })
26
28
* .then(() => console.log('Logged in successfully'))
27
29
*
30
+ * @example
31
+ * client.login({ email: <emailid>, password: <password>, tfa_token: <tfa_token> })
32
+ * .then(() => console.log('Logged in successfully'))
33
+ *
34
+ * @example
35
+ * client.login({ email: <emailid>, password: <password>, mfaSecret: <mfa_secret> })
36
+ * .then(() => console.log('Logged in successfully'))
28
37
*/
29
- function login ( requestBody , params = { } ) {
38
+ function login ( requestBody = { } , params = { } ) {
30
39
http . defaults . versioningStrategy = 'path'
31
40
41
+ const { mfaSecret, ...credentials } = requestBody
42
+ requestBody = credentials
43
+
44
+ if ( ! requestBody . tfa_token && mfaSecret ) {
45
+ requestBody . tfa_token = authenticator . generate ( mfaSecret )
46
+ }
32
47
return http . post ( '/user-session' , { user : requestBody } , { params : params } )
33
48
. then ( ( response ) => {
34
49
if ( response . data . user != null && response . data . user . authtoken != null ) {
@@ -55,10 +70,9 @@ export default function contentstackClient ({ http }) {
55
70
*/
56
71
function getUser ( params = { } ) {
57
72
http . defaults . versioningStrategy = 'path'
58
- return http . get ( '/user' , { params : params } )
59
- . then ( ( response ) => {
60
- return new User ( http , response . data )
61
- } , error )
73
+ return http . get ( '/user' , { params : params } ) . then ( ( response ) => {
74
+ return new User ( http , response . data )
75
+ } , error )
62
76
}
63
77
/**
64
78
* @description Get Stack instance. A stack is a space that stores the content of a project.
@@ -127,13 +141,16 @@ export default function contentstackClient ({ http }) {
127
141
*/
128
142
function organization ( uid = null ) {
129
143
http . defaults . versioningStrategy = 'path'
130
- return new Organization ( http , uid !== null ? { organization : { uid : uid } } : null )
144
+ return new Organization (
145
+ http ,
146
+ uid !== null ? { organization : { uid : uid } } : null
147
+ )
131
148
}
132
149
133
150
/**
134
151
* @description The Log out of your account call is used to sign out the user of Contentstack account.
135
152
* @memberof ContentstackClient
136
- * @param {String } authtoken - Authtoken to logout from.
153
+ * @param {String } authtoken - Authtoken to logout from.
137
154
* @func logout
138
155
* @returns {Object } Response object.
139
156
*
@@ -152,25 +169,25 @@ export default function contentstackClient ({ http }) {
152
169
function logout ( authtoken ) {
153
170
http . defaults . versioningStrategy = 'path'
154
171
if ( authtoken !== undefined ) {
155
- return http . delete ( '/user-session' , {
156
- headers : {
157
- authtoken : authtoken
158
- }
159
- } )
172
+ return http
173
+ . delete ( '/user-session' , {
174
+ headers : {
175
+ authtoken : authtoken
176
+ }
177
+ } )
160
178
. then ( ( response ) => {
161
179
return response . data
162
180
} , error )
163
181
}
164
- return http . delete ( '/user-session' )
165
- . then ( ( response ) => {
166
- if ( http . defaults . headers . common ) {
167
- delete http . defaults . headers . common . authtoken
168
- }
169
- delete http . defaults . headers . authtoken
170
- delete http . httpClientParams . authtoken
171
- delete http . httpClientParams . headers . authtoken
172
- return response . data
173
- } , error )
182
+ return http . delete ( '/user-session' ) . then ( ( response ) => {
183
+ if ( http . defaults . headers . common ) {
184
+ delete http . defaults . headers . common . authtoken
185
+ }
186
+ delete http . defaults . headers . authtoken
187
+ delete http . httpClientParams . authtoken
188
+ delete http . httpClientParams . headers . authtoken
189
+ return response . data
190
+ } , error )
174
191
}
175
192
176
193
/**
@@ -201,7 +218,15 @@ export default function contentstackClient ({ http }) {
201
218
const responseType = params . responseType || 'code'
202
219
const scope = params . scope
203
220
const clientSecret = params . clientSecret
204
- return new OAuthHandler ( http , appId , clientId , redirectUri , clientSecret , responseType , scope )
221
+ return new OAuthHandler (
222
+ http ,
223
+ appId ,
224
+ clientId ,
225
+ redirectUri ,
226
+ clientSecret ,
227
+ responseType ,
228
+ scope
229
+ )
205
230
}
206
231
207
232
return {
0 commit comments