|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const AppError = require('../../error'); |
| 8 | +const authMethods = require('../../authMethods'); |
| 9 | +const { parseAuthorizationHeader } = require('./hawk-fxa-token'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Authentication strategy that validates a Hawk session token and ensures: |
| 13 | + * 1) account email is verified |
| 14 | + * 2) session token is verified (no tokenVerificationId) |
| 15 | + * 3) account AAL and session AAL match |
| 16 | + * |
| 17 | + * @param {Function} getCredentialsFunc - function to fetch a session token by id |
| 18 | + * @param {Object} db - database interface to fetch account and factors |
| 19 | + * @returns {Function} |
| 20 | + */ |
| 21 | +function strategy(getCredentialsFunc, db, config, statsd) { |
| 22 | + const tokenNotFoundError = () => { |
| 23 | + const error = AppError.unauthorized('Token not found'); |
| 24 | + error.isMissing = true; |
| 25 | + return error; |
| 26 | + }; |
| 27 | + |
| 28 | + // Extract regular expressions to allow for optional skipping of certain routes for certain checks. |
| 29 | + const verifiedSessionTokenConfig = |
| 30 | + config?.authStrategies?.verifiedSessionToken; |
| 31 | + |
| 32 | + const skipEmailVerifiedCheckForRoutes = |
| 33 | + verifiedSessionTokenConfig?.skipEmailVerifiedCheckForRoutes |
| 34 | + ? new RegExp(verifiedSessionTokenConfig.skipEmailVerifiedCheckForRoutes) |
| 35 | + : null; |
| 36 | + |
| 37 | + const skipTokenVerifiedCheckForRoutes = |
| 38 | + verifiedSessionTokenConfig?.skipTokenVerifiedCheckForRoutes |
| 39 | + ? new RegExp(verifiedSessionTokenConfig.skipTokenVerifiedCheckForRoutes) |
| 40 | + : null; |
| 41 | + |
| 42 | + const skipAalCheckForRoutes = |
| 43 | + verifiedSessionTokenConfig?.skipAalCheckForRoutes |
| 44 | + ? new RegExp(verifiedSessionTokenConfig.skipAalCheckForRoutes) |
| 45 | + : null; |
| 46 | + |
| 47 | + return function (server, options) { |
| 48 | + return { |
| 49 | + authenticate: async function (req, h) { |
| 50 | + const auth = req.headers.authorization; |
| 51 | + |
| 52 | + if (!auth) { |
| 53 | + // if this strategy is selected, auth *cannot* be optional |
| 54 | + // "optional" mode is not supported |
| 55 | + throw tokenNotFoundError(); |
| 56 | + } |
| 57 | + |
| 58 | + const parsedHeader = parseAuthorizationHeader(auth); |
| 59 | + let token; |
| 60 | + try { |
| 61 | + token = await getCredentialsFunc(parsedHeader.id); |
| 62 | + } catch (_) {} |
| 63 | + |
| 64 | + if (!token) { |
| 65 | + throw tokenNotFoundError(); |
| 66 | + } |
| 67 | + |
| 68 | + // Fetch the account for further checks |
| 69 | + const account = await db.account(token.uid); |
| 70 | + |
| 71 | + // 1) account email is verified |
| 72 | + if (!account?.primaryEmail?.isVerified) { |
| 73 | + if (skipEmailVerifiedCheckForRoutes?.test(req.route.path)) { |
| 74 | + // Important! Using req.route.path which has much lower cardinality than req.path |
| 75 | + statsd?.increment( |
| 76 | + 'verified_session_token.primary_email_not_verified.skipped', |
| 77 | + [`path:${req.route.path}`] |
| 78 | + ); |
| 79 | + } else { |
| 80 | + statsd?.increment( |
| 81 | + 'verified_session_token.primary_email_not_verified.error', |
| 82 | + [`path:${req.route.path}`] |
| 83 | + ); |
| 84 | + throw AppError.unverifiedAccount(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // 2) session token is verified |
| 89 | + if (token.tokenVerificationId || token.tokenVerified === false) { |
| 90 | + if (skipTokenVerifiedCheckForRoutes?.test(req.route.path)) { |
| 91 | + statsd?.increment('verified_session_token.token_verified.skipped', [ |
| 92 | + `path:${req.route.path}`, |
| 93 | + ]); |
| 94 | + } else { |
| 95 | + statsd?.increment('verified_session_token.token_verified.error', [ |
| 96 | + `path:${req.route.path}`, |
| 97 | + ]); |
| 98 | + throw AppError.unverifiedSession(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // 3) account AAL and session AAL match |
| 103 | + const accountAmr = await authMethods.availableAuthenticationMethods( |
| 104 | + db, |
| 105 | + account |
| 106 | + ); |
| 107 | + const accountAal = authMethods.maximumAssuranceLevel(accountAmr); |
| 108 | + const sessionAal = token.authenticatorAssuranceLevel; |
| 109 | + |
| 110 | + if (accountAal !== sessionAal) { |
| 111 | + if (skipAalCheckForRoutes?.test(req.route.path)) { |
| 112 | + statsd?.increment('verified_session_token.aal.skipped', [ |
| 113 | + `path:${req.route.path}`, |
| 114 | + ]); |
| 115 | + } else { |
| 116 | + statsd?.increment('verified_session_token.aal.error', [ |
| 117 | + `path:${req.route.path}`, |
| 118 | + ]); |
| 119 | + throw AppError.unauthorized('AAL mismatch'); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return h.authenticated({ |
| 124 | + credentials: token, |
| 125 | + }); |
| 126 | + }, |
| 127 | + }; |
| 128 | + }; |
| 129 | +} |
| 130 | + |
| 131 | +module.exports = { |
| 132 | + strategy, |
| 133 | +}; |
0 commit comments