liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/**
 * Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
 
var should = require("should");
var sinon = require('sinon');
 
var NR_TEST_UTILS = require("nr-test-utils");
 
var strategies = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/strategies");
var Users = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/users");
var Tokens = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/tokens");
var Clients = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/clients");
 
describe("api/auth/strategies", function() {
    describe("Password Token Exchange", function() {
        var userAuthentication;
        afterEach(function() {
            if (userAuthentication) {
                userAuthentication.restore();
                userAuthentication = null;
            }
        });
 
        it('Handles authentication failure',function(done) {
            userAuthentication = sinon.stub(Users,"authenticate").callsFake(function(username,password) {
                return Promise.resolve(null);
            });
 
            strategies.passwordTokenExchange({},"user","password","scope",function(err,token) {
                try {
                    should.not.exist(err);
                    token.should.be.false();
                    done();
                } catch(e) {
                    done(e);
                }
            });
        });
 
        it('Handles scope overreach',function(done) {
            userAuthentication = sinon.stub(Users,"authenticate").callsFake(function(username,password) {
                return Promise.resolve({username:"user",permissions:"read"});
            });
 
            strategies.passwordTokenExchange({},"user","password","*",function(err,token) {
                try {
                    should.not.exist(err);
                    token.should.be.false();
                    done();
                } catch(e) {
                    done(e);
                }
            });
        });
 
        it('Creates new token on authentication success',function(done) {
            userAuthentication = sinon.stub(Users,"authenticate").callsFake(function(username,password) {
                return Promise.resolve({username:"user",permissions:"*"});
            });
            var tokenDetails = {};
            var tokenCreate = sinon.stub(Tokens,"create").callsFake(function(username,client,scope) {
                tokenDetails.username = username;
                tokenDetails.client = client;
                tokenDetails.scope = scope;
                return Promise.resolve({accessToken: "123456"});
            });
 
            strategies.passwordTokenExchange({id:"myclient"},"user","password","read",function(err,token) {
                try {
                    should.not.exist(err);
                    token.should.equal("123456");
                    tokenDetails.should.have.property("username","user");
                    tokenDetails.should.have.property("client","myclient");
                    tokenDetails.should.have.property("scope","read");
                    done();
                } catch(e) {
                    done(e);
                } finally {
                    tokenCreate.restore();
                }
            });
        });
 
        it('Uses provided token on authentication success and token provided',function(done) {
            userAuthentication = sinon.stub(Users,"authenticate").callsFake(function(username,password) {
                return Promise.resolve({username:"user",permissions:"*",token:"123456"});
            });
 
            strategies.passwordTokenExchange({id:"myclient"},"user","password","read",function(err,token) {
                try {
                    should.not.exist(err);
                    token.should.equal("123456");
                    done();
                } catch(e) {
                    done(e);
                }
            });
        
        });
    });
 
    describe("Anonymous Strategy", function() {
        it('Succeeds if anon user enabled',function(done) {
            var userDefault = sinon.stub(Users,"default").callsFake(function() {
                return Promise.resolve("anon");
            });
            strategies.anonymousStrategy._success = strategies.anonymousStrategy.success;
            strategies.anonymousStrategy.success = function(user) {
                user.should.equal("anon");
                strategies.anonymousStrategy.success = strategies.anonymousStrategy._success;
                delete strategies.anonymousStrategy._success;
                done();
            };
            strategies.anonymousStrategy.authenticate({});
        });
        it('Fails if anon user not enabled',function(done) {
            var userDefault = sinon.stub(Users,"default").callsFake(function() {
                return Promise.resolve(null);
            });
            strategies.anonymousStrategy._fail = strategies.anonymousStrategy.fail;
            strategies.anonymousStrategy.fail = function(err) {
                err.should.equal(401);
                strategies.anonymousStrategy.fail = strategies.anonymousStrategy._fail;
                delete strategies.anonymousStrategy._fail;
                done();
            };
            strategies.anonymousStrategy.authenticate({});
        });
        afterEach(function() {
            Users.default.restore();
        })
    });
 
    describe("Tokens Strategy", function() {
        it('Succeeds if tokens user enabled custom header',function(done) {
            var userTokens = sinon.stub(Users,"tokens").callsFake(function(token) {
                return Promise.resolve("tokens-"+token);
            });
            var userTokenHeader = sinon.stub(Users,"tokenHeader").callsFake(function(token) {
                return "x-test-token";
            });
            strategies.tokensStrategy._success = strategies.tokensStrategy.success;
            strategies.tokensStrategy.success = function(user) {
                user.should.equal("tokens-1234");
                strategies.tokensStrategy.success = strategies.tokensStrategy._success;
                delete strategies.tokensStrategy._success;
                done();
            };
            strategies.tokensStrategy.authenticate({headers:{"x-test-token":"1234"}});
        });
        it('Succeeds if tokens user enabled default header',function(done) {
            var userTokens = sinon.stub(Users,"tokens").callsFake(function(token) {
                return Promise.resolve("tokens-"+token);
            });
            var userTokenHeader = sinon.stub(Users,"tokenHeader").callsFake(function(token) {
                return "authorization";
            });
            strategies.tokensStrategy._success = strategies.tokensStrategy.success;
            strategies.tokensStrategy.success = function(user) {
                user.should.equal("tokens-1234");
                strategies.tokensStrategy.success = strategies.tokensStrategy._success;
                delete strategies.tokensStrategy._success;
                done();
            };
            strategies.tokensStrategy.authenticate({headers:{"authorization":"Bearer 1234"}});
        });
        it('Fails if tokens user not enabled',function(done) {
            var userTokens = sinon.stub(Users,"tokens").callsFake(function() {
                return Promise.resolve(null);
            });
            var userTokenHeader = sinon.stub(Users,"tokenHeader").callsFake(function(token) {
                return "authorization";
            });
            strategies.tokensStrategy._fail = strategies.tokensStrategy.fail;
            strategies.tokensStrategy.fail = function(err) {
                err.should.equal(401);
                strategies.tokensStrategy.fail = strategies.tokensStrategy._fail;
                delete strategies.tokensStrategy._fail;
                done();
            };
            strategies.tokensStrategy.authenticate({headers:{"authorization":"Bearer 1234"}});
        });
        afterEach(function() {
            Users.tokens.restore();
            Users.tokenHeader.restore();
        })
    });
 
    describe("Bearer Strategy", function() {
        it('Rejects invalid token',function(done) {
            var getToken = sinon.stub(Tokens,"get").callsFake(function(token) {
                return Promise.resolve(null);
            });
 
            strategies.bearerStrategy("1234",function(err,user) {
                try {
                    should.not.exist(err);
                    user.should.be.false();
                    done();
                } catch(e) {
                    done(e);
                } finally {
                    getToken.restore();
                }
            });
        });
        it('Accepts valid token',function(done) {
            var getToken = sinon.stub(Tokens,"get").callsFake(function(token) {
                return Promise.resolve({user:"user",scope:"scope"});
            });
            var getUser = sinon.stub(Users,"get").callsFake(function(username) {
                return Promise.resolve("aUser");
            });
 
            strategies.bearerStrategy("1234",function(err,user,opts) {
                try {
                    should.not.exist(err);
                    user.should.equal("aUser");
                    opts.should.have.a.property("scope","scope");
                    done();
                } catch(e) {
                    done(e);
                } finally {
                    getToken.restore();
                    getUser.restore();
                }
            });
        });
        it('Fail if no user for token',function(done) {
            var getToken = sinon.stub(Tokens,"get").callsFake(function(token) {
                return Promise.resolve({user:"user",scope:"scope"});
            });
            var getUser = sinon.stub(Users,"get").callsFake(function(username) {
                return Promise.resolve(null);
            });
 
            strategies.bearerStrategy("1234",function(err,user,opts) {
                try {
                    should.not.exist(err);
                    user.should.equal(false);
                    should.not.exist(opts);
                    done();
                } catch(e) {
                    done(e);
                } finally {
                    getToken.restore();
                    getUser.restore();
                }
            });
        });
    });
 
    describe("Client Password Strategy", function() {
        it('Accepts valid client',function(done) {
            var testClient = {id:"node-red-editor",secret:"not_available"};
            var getClient = sinon.stub(Clients,"get").callsFake(function(client) {
                return Promise.resolve(testClient);
            });
 
            strategies.clientPasswordStrategy(testClient.id,testClient.secret,function(err,client) {
                try {
                    should.not.exist(err);
                    client.should.eql(testClient);
                    done();
                } catch(e) {
                    done(e);
                } finally {
                    getClient.restore();
                }
            });
        });
        it('Rejects invalid client secret',function(done) {
            var testClient = {id:"node-red-editor",secret:"not_available"};
            var getClient = sinon.stub(Clients,"get").callsFake(function(client) {
                return Promise.resolve(testClient);
            });
 
            strategies.clientPasswordStrategy(testClient.id,"invalid_secret",function(err,client) {
                try {
                    should.not.exist(err);
                    client.should.be.false();
                    done();
                } catch(e) {
                    done(e);
                } finally {
                    getClient.restore();
                }
            });
        });
        it('Rejects invalid client id',function(done) {
            var getClient = sinon.stub(Clients,"get").callsFake(function(client) {
                return Promise.resolve(null);
            });
            strategies.clientPasswordStrategy("invalid_id","invalid_secret",function(err,client) {
                try {
                    should.not.exist(err);
                    client.should.be.false();
                    done();
                } catch(e) {
                    done(e);
                } finally {
                    getClient.restore();
                }
            });
        });
 
        var userAuthentication;
        it('Blocks after 5 failures',function(done) {
            userAuthentication = sinon.stub(Users,"authenticate").callsFake(function(username,password) {
                return Promise.resolve(null);
            });
            for (var z=0; z<5; z++) {
                strategies.passwordTokenExchange({},"user","badpassword","scope",function(err,token) {
                });
            }
            strategies.passwordTokenExchange({},"user","badpassword","scope",function(err,token) {
                try {
                    err.toString().should.equal("Error: Too many login attempts. Wait 10 minutes and try again");
                    token.should.be.false();
                    done();
                } catch(e) {
                    done(e);
                } finally {
                    userAuthentication.restore();
                }
            });
        });
 
    });
});