All files / components SessionDialogUI.jsx

35.63% Statements 93/261
100% Branches 1/1
0% Functions 0/13
35.63% Lines 93/261

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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 2621x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                       1x 1x 1x 1x 1x 1x 1x                         1x 1x                       1x 1x                                                           1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x                       1x 1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x           1x 1x 1x 1x 1x           1x 1x 1x 1x 1x                   1x 1x 1x 1x 1x             1x 1x                                                                                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import cookie from 'cookie';
import retry from 'retry';
import jwtDecode from 'jwt-decode';
 
import Modal from './Modal';
import RefreshSession from '../session/refresh';
import LogoutSession from '../session/logout';
import fetchTtl from '../session/ttl';
 
const fetchTtlIntervalMs = 60000;
 
export default class SessionDialogUI extends Component {
  /**
   * Create a SessionDialogUI component.
   * @param {object} props - Configuration options. See SessionDialog for more
   */
  constructor(props) {
    super(props);

    this.handleOpenModal = this.handleOpenModal.bind(this);
    this.handleCloseModal = this.handleCloseModal.bind(this);
    this.refreshSession = this.refreshSession.bind(this);
    this.signOut = this.signOut.bind(this);
    this.startTimer = this.startTimer.bind(this);
    this.shouldShowDialog = this.shouldShowDialog.bind(this);
    this.shouldSignOut = this.shouldSignOut.bind(this);
    this.minutesUntil = this.minutesUntil.bind(this);
    this.checkExpiry = this.checkExpiry.bind(this);
    this.onInterval = this.onInterval.bind(this);

    if (props.showImmediate === true) {
      this.state = {
        showModal: true,
        showError: false,
      };
    } else {
      this.state = {
        showModal: false,
        showError: false,
      };

      this.startTimer();
    }
  }
 
  /**
   * Extracts the token exp value as a JavaScript date.
   * @param {Object} cookies - An object representation of the cookie store
   * @returns {(date|null)} The token expiration date, if found
   */
  getTokenExpiry(cookies) {
    let expiry = null;

    if (cookies.hasOwnProperty('qpp_auth_token') && cookies.qpp_auth_token) {
      // See https://tools.ietf.org/html/rfc7519
      // `exp` is a NumericDate representing "seconds since the epoch"
      // and it needs to be milliseconds to get a JavaScript date from it.
      const { exp } = jwtDecode(cookies.qpp_auth_token);
      expiry = new Date(exp * 1000);
    }

    return expiry;
  }
 
  checkExpiry(expiry) {
    if (expiry) {
      if (this.shouldShowDialog(expiry)) {
        this.handleOpenModal();
      } else if (this.shouldSignOut(expiry)) {
        this.signOut();
        clearInterval(this.warningInterval);
      }
    } else {
      clearInterval(this.warningInterval);
    }
  }
 
  onInterval() {
    const cookies = cookie.parse(document.cookie);

    if (cookies.hasOwnProperty('qpp_auth_token') && cookies.qpp_auth_token) {
      const operation = retry.operation({
        retries: 5,
        factor: 1,
        minTimeout: 1000,
        maxTimeout: 1000,
      });

      operation.attempt(() => {
        fetchTtl(cookies.qpp_auth_token)
          .then(this.checkExpiry)
          .catch((error) => {
            if (operation.retry(error)) {
              // try again before giving up
              return;
            }
            clearInterval(this.warningInterval);
            // sign an inactive user out
            this.forceTimeoutHandle = setTimeout(this.signOut, 120 * 1000);
            this.setState({
              showModal: false,
              showError: true,
            });
          });
      });
    }
  }
 
  /**
   * Checks whether the user is within a specified number of minutes before
   * their session token expires. If so, show a warning dialog. If the
   * expiration has been reached, sign the user out.
   */
  startTimer() {
    clearInterval(this.warningInterval);

    this.warningInterval = setInterval(this.onInterval, fetchTtlIntervalMs);
  }
 
  /**
   * Returns whether the current time is within the timeout range of expiry.
   * @param {date|number} expiry - Expiration date to check against
   * @returns {boolean}
   */
  shouldShowDialog(expiry) {
    if (typeof expiry === 'number') {
      const minutesLeft = expiry / 60;

      return !this.state.showModal && minutesLeft <= this.props.warningTimeout;
    } else {
      return (
        !this.state.showModal &&
        this.minutesUntil(expiry) <= this.props.warningTimeout
      );
    }
  }
 
  /**
   * Returns whether the current time has reached the expiration date.
   * @param {date|number} expiry - Expiration date to check against
   * @returns {boolean}
   */
  shouldSignOut(expiry) {
    if (typeof expiry === 'number') {
      return expiry <= 1;
    } else {
      return this.minutesUntil(expiry) <= 0;
    }
  }
 
  /**
   * Returns number of minutes until the expiration date.
   * @param {date} expiry - Expiration date to check against
   * @returns {number}
   */
  minutesUntil(expiry) {
    return (expiry - new Date().getTime()) / 60000;
  }
 
  /**
   * Sets the modal state to visible.
   */
  handleOpenModal() {
    this.setState({
      showModal: true,
      showError: false,
    });
  }
 
  /**
   * Sets the modal state to hidden.
   */
  handleCloseModal() {
    this.setState({
      showModal: false,
      showError: false,
    });
  }
 
  /**
   * Handles the user requesting their session be extended.
   */
  refreshSession() {
    RefreshSession({
      window,
      onSuccess: () => {
        this.startTimer();
      },
    });

    this.handleCloseModal();
  }
 
  /**
   * Handles the user requesting their session be ended.
   */
  signOut() {
    if (this.forceTimeoutHandle) {
      clearTimeout(this.forceTimeoutHandle);
    }
    LogoutSession(window);
    this.handleCloseModal();
  }
 
  render() {
    return (
      <React.Fragment>
        <Modal
          useDesignSystem
          title="You will be signed out within two minutes"
          isOpen={this.state.showModal}
          primary={{
            title: 'Keep me signed in',
            onClick: this.refreshSession,
          }}
          secondary={{
            title: 'Sign me out now',
            onClick: this.signOut,
          }}
          onRequestClose={this.handleCloseModal}
        >
          <p>
            You have been inactive for thirty minutes. For your security, we
            will sign you out automatically.
          </p>
        </Modal>

        <Modal
          useDesignSystem
          title="Technical Difficulties"
          isOpen={this.state.showError}
          primary={{
            title: 'Sign Out',
            onClick: this.signOut,
          }}
          onRequestClose={this.signOut}
        >
          <p>
            We’re experiencing technical difficulties. Please log in again to
            continue. For your security, we will sign you out automatically
            within two minutes.
          </p>
        </Modal>
      </React.Fragment>
    );
  }
}
 
SessionDialogUI.propTypes = {
  showImmediate: PropTypes.bool,
  warningTimeout: PropTypes.number,
  appElement: PropTypes.string,
};
 
SessionDialogUI.defaultProps = {
  showImmediate: false,
  warningTimeout: 2,
  appElement: undefined,
};