-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (83 loc) · 1.83 KB
/
index.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
const R = require('ramda');
/* TODO: distinguish between:
- login failed (bad credentials)
- request failed because of insufficient permissions (logged in but don't have access)
- request failed because not logged in
*/
const redFlags = [
'unauthorized',
'unauthorised',
'please log in to',
'you could not be logged in',
'secure zone access denied'
];
/**
* string -> string
*
* @param {String} s
* @returns {string}
*/
const toLowerCase = (s) => {
return String.prototype.toLowerCase.apply(s)
};
/**
* Does the text contain the flag? (case insensitive)
*
* Text -> Flag -> Bool
*
* @param {String} flag
* @param {String} text
* @returns {boolean}
*/
const flagPresent = R.curry((text, flag) => {
const lowercaseText = toLowerCase(text);
return lowercaseText.indexOf(flag) >= 0;
});
/**
*
* @param flags {Array<String>}
* @param text {String}
*/
const mapFlagPresence = (flags, text) => {
return R.map(flagPresent(text), flags)
};
/**
*
* (Flags, Text) -> Bool
*
* @param {Array<String>} flags
* @param {String} text
* @type {Function}
*/
const checkFlags = R.pipe(
mapFlagPresence,
R.any(R.equals(true))
);
/**
* Is the response BC's version of a '401 Unauthorized' error?
*
* Response -> Boolean
*
* @param response
* @returns {Boolean}
*/
const checkLoginResponse = function (response) {
return checkFlags(redFlags, response.body);
};
const patchAuthErrorResponses = (request) => {
const statusShouldBe401 = request.status === 200 && checkLoginResponse(request);
if (statusShouldBe401) {
request.status = 401;
request.statusText = 'Unauthorized';
return request;
}
return request;
};
module.exports = {
patchAuthErrorResponses,
checkLoginResponse,
toLowerCase,
flagPresent,
mapFlagPresence,
checkFlags
};