Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 6x 4x 4x 11x 4x 4x 4x 1x 1x 1x 1x 3x 10x 10x 10x 10x 10x 4x 4x 4x 4x 6x 6x 6x 6x 6x 6x 6x 6x 3x 6x 6x 6x 6x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x | import createError from 'http-errors'; import { IncomingMessage, ServerResponse } from 'http'; import dbg from 'debug'; import { sspi, AcceptSecurityContextInput } from '../../lib/api'; import { hexDump, getMessageType, encode, decode } from './misc'; import { SSO } from './SSO'; import { ServerContextHandleManager } from './schm/ServerContextHandleManager'; import { AuthOptions, MessageType, Middleware, NextFunction, SSOMethod, SSOObject, } from './interfaces'; import { getStatusInfo } from './status'; import { getKerberosDetails, getKerberosResponseDetails } from './kerberos'; const DEBUG_KEY = 'node-expose-sspi:auth'; const debug = dbg(DEBUG_KEY); const isDbgEnabled = dbg.enabled(DEBUG_KEY); const WWW_AUTHENTICATE = 'WWW-Authenticate'; /** * Tries to get SSO information from browser. If success, the SSO info * is stored under req.sso * * @export * @param {AuthOptions} [options={}] * @returns {RequestHandler} */ export function auth(options: Partial<AuthOptions> = {}): Middleware { const opts: AuthOptions = { useActiveDirectory: true, useGroups: true, useOwner: false, groupFilterRegex: '.*', allowsGuest: false, allowsAnonymousLogon: false, useSession: false, forceNTLM: false, ...options, }; const authenticationType = opts.forceNTLM ? 'NTLM' : 'Negotiate'; const packageName = 'Negotiate'; let { credential, tsExpiry } = sspi.AcquireCredentialsHandle({ packageName, }); const checkCredentials = (): void => { Iif (tsExpiry < new Date()) { // renew server credentials sspi.FreeCredentialsHandle(credential); const renewed = sspi.AcquireCredentialsHandle({ packageName, }); credential = renewed.credential; tsExpiry = renewed.tsExpiry; } }; const schManager = new ServerContextHandleManager(); // returns the node middleware. return ( req: IncomingMessage, res: ServerResponse, next: NextFunction ): void => { if (opts.useSession) { const session = ((req as unknown) as { session?: { sso: SSOObject } }) .session; debug('check the session: ', session); if (session?.sso) { session.sso.cached = true; req.sso = session.sso; next(); return; } debug('no session.sso'); } (async (): Promise<void> => { let messageType: MessageType = 'Unknown'; try { const authorization = req.headers.authorization; if (!authorization) { debug('no authorization key in header'); res.statusCode = 401; res.setHeader(WWW_AUTHENTICATE, authenticationType); return res.end(); } Iif (!authorization.startsWith(authenticationType + ' ')) { res.statusCode = 400; return res.end(`Malformed authentication token: ${authorization}`); } checkCredentials(); const token = authorization.substring( (authenticationType + ' ').length ); messageType = getMessageType(token); debug('messageType: ', messageType); const buffer = decode(token); debug(hexDump(buffer)); // test if first token if ( messageType === 'NTLM_NEGOTIATE_01' || messageType === 'Kerberos_1' ) { schManager.release(req); } // kerberos token case Iif (isDbgEnabled && messageType === 'Kerberos_1') { debug('Kerberos_1 details: ', getKerberosDetails(buffer)); } const input: AcceptSecurityContextInput = { credential, SecBufferDesc: { ulVersion: 0, buffers: [buffer], }, }; const serverContextHandle = schManager.get(req); if (serverContextHandle) { debug('adding to input a serverContextHandle (not first exchange)'); input.contextHandle = serverContextHandle; } debug('input just before calling AcceptSecurityContext', input); const serverSecurityContext = sspi.AcceptSecurityContext(input); debug( 'serverSecurityContext just after AcceptSecurityContext', serverSecurityContext ); Iif (isDbgEnabled && messageType.startsWith('Kerberos')) { debug( 'Kerberos output details: ', getKerberosResponseDetails( serverSecurityContext.SecBufferDesc.buffers[0] ) ); } const failed = !['SEC_E_OK', 'SEC_I_CONTINUE_NEEDED'].includes( serverSecurityContext.SECURITY_STATUS ); Iif (failed) { schManager.release(req); // 'SEC_I_COMPLETE_AND_CONTINUE', 'SEC_I_COMPLETE_NEEDED' are considered as errors because it is used // only by 'Digest' SSP. (not by Negotiate, Kerberos or NTLM) if (serverSecurityContext.SECURITY_STATUS === 'SEC_E_LOGON_DENIED') { next( createError( 401, `SEC_E_LOGON_DENIED. (incorrect login/password, or account disabled, or locked, etc.). Protocol Message = ${messageType}.` ) ); return; } throw new Error( 'AcceptSecurityContext error: ' + serverSecurityContext.SECURITY_STATUS ); } schManager.set(req, serverSecurityContext.contextHandle); debug('AcceptSecurityContext output buffer'); debug(hexDump(serverSecurityContext.SecBufferDesc.buffers[0])); if (serverSecurityContext.SECURITY_STATUS === 'SEC_I_CONTINUE_NEEDED') { res.statusCode = 401; res.setHeader( WWW_AUTHENTICATE, authenticationType + ' ' + encode(serverSecurityContext.SecBufferDesc.buffers[0]) ); return res.end(); } const lastServerContextHandle = serverSecurityContext.contextHandle; Iif (!lastServerContextHandle) { throw new Error('cannot get the server context handle'); } const method: SSOMethod = messageType.startsWith('NTLM') ? 'NTLM' : 'Kerberos'; const sso = new SSO(lastServerContextHandle, method); sso.setOptions(opts); await sso.load(); req.sso = sso.getJSON(); if (opts.useSession) { debug('session case'); const session = ((req as unknown) as { session?: { sso: SSOObject } }) .session; debug('session: ', session); Eif (session) { req.sso.cached = false; session.sso = req.sso; } debug('session again: ', session); } sspi.DeleteSecurityContext(lastServerContextHandle); schManager.release(req); // check if user is allowed. Iif ( !opts.allowsAnonymousLogon && req.sso?.user?.name === 'ANONYMOUS LOGON' ) { res.statusCode = 401; return res.end('Anonymous login not authorized.'); } Iif (!opts.allowsGuest && req.sso?.user?.name === 'Guest') { res.statusCode = 401; return res.end('Guest not authorized.'); } // user authenticated and allowed. res.setHeader( WWW_AUTHENTICATE, authenticationType + ' ' + encode(serverSecurityContext.SecBufferDesc.buffers[0]) ); return next(); } catch (e) { schManager.release(req); console.error(e); console.error('statusInfo: ', getStatusInfo()); console.error('messageType: ', messageType); next(createError(401, `Error while doing SSO: ${e.message}`)); } })(); }; } |