(obj)
| 94 | * @returns {Object} The configured server object with authentication middleware |
| 95 | */ |
| 96 | export function auth (obj) { |
| 97 | const ssl = obj.ssl.cert && obj.ssl.key, |
| 98 | realm = `http${ssl ? S : EMPTY}://${obj.host}${obj.port !== INT_80 && obj.port !== INT_443 ? COLON + obj.port : EMPTY}`, |
| 99 | async = obj.auth.oauth2.enabled || obj.auth.saml.enabled, |
| 100 | stateless = obj.rate.enabled === false && obj.security.csrf === false, |
| 101 | authDelay = obj.auth.delay, |
| 102 | authMap = {}, |
| 103 | authUris = []; |
| 104 | |
| 105 | let sesh, fnCookie, fnSession, passportInit, passportSession; |
| 106 | |
| 107 | obj.ignore(asyncFlag); |
| 108 | |
| 109 | // Use cached regex compilation for auth patterns |
| 110 | for (const k of groups) { |
| 111 | obj.auth[k] = (obj.auth[k] || []).map(i => { |
| 112 | const pattern = convertPatternToRegex(i, obj.auth.uri.login); |
| 113 | |
| 114 | return createCachedRegex(pattern, I); |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | for (const i of Object.keys(obj.auth)) { |
| 119 | if (obj.auth[i].enabled) { |
| 120 | const uri = `${SLASH}${AUTH}${SLASH}${i}`; |
| 121 | |
| 122 | authMap[`${i}${UNDERSCORE}${URI}`] = uri; |
| 123 | authUris.push(uri); |
| 124 | obj.auth.protect.push(createCachedRegex(`^/auth/${i}(/|$)`)); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (stateless === false) { |
| 129 | const objSession = clone(obj.session); |
| 130 | |
| 131 | delete objSession.redis; |
| 132 | delete objSession.store; |
| 133 | |
| 134 | sesh = Object.assign({secret: uuid(), resave: false, saveUninitialized: false}, objSession); |
| 135 | |
| 136 | if (obj.session.store === REDIS) { |
| 137 | const client = redis.createClient(clone(obj.session.redis)); |
| 138 | |
| 139 | sesh.store = new RedisStore({client}); |
| 140 | } |
| 141 | |
| 142 | fnCookie = cookie(); |
| 143 | fnSession = session(sesh); |
| 144 | |
| 145 | obj.always(fnCookie).ignore(fnCookie); |
| 146 | obj.always(fnSession).ignore(fnSession); |
| 147 | obj.always(bypass).ignore(bypass); |
| 148 | |
| 149 | if (obj.security.csrf) { |
| 150 | obj.always(csrfWrapper).ignore(csrfWrapper); |
| 151 | } |
| 152 | } |
| 153 |
no test coverage detected