<commit_before>import background from 'ringcentral-integration/lib/background';
import proxify from 'ringcentral-integration/lib/proxy/proxify';
import { Module } from 'ringcentral-integration/lib/di';
import ensureExist from 'ringcentral-integration/lib/ensureExist';
import url from 'url';
import uuid from 'uuid';
import actionTypes from './actionTypes';
import getProxyFrameOAuthReducer from './getProxyFrameOAuthReducer';

import OAuthBase from '../../lib/OAuthBase';

const DEFAULT_PROXY_RETRY = 5000;

@Module({
  name: 'OAuth',
  deps: [
    { dep: 'OAuthOptions', optional: true },
  ],
})
export default class ProxyFrameOAuth extends OAuthBase {
  constructor({
    redirectUri = './redirect.html',
    proxyUri = './proxy.html',
    defaultProxyRetry = DEFAULT_PROXY_RETRY,
    ...options
  }) {
    super({
      redirectUri,
      ...options,
    });
    this._uuid = uuid.v4();
    this._proxyUri = ensureExist(proxyUri, 'proxyUri');
    this._defaultProxyRetry = defaultProxyRetry;

    this._reducer = getProxyFrameOAuthReducer(this.actionTypes);

    this._loggedIn = false;
  }

  _onStateChange() {
    super._onStateChange();
    if (this._auth.loggedIn === this._loggedIn) {
      return;
    }
    this._loggedIn = this._auth.loggedIn;
    if (this._loggedIn && this._auth.isImplicit) {
      console.log('new login, start refresh token timeout');
      this._createImplicitRefreshTimeout();
    }
    if (!this._loggedIn && this._auth.isImplicit) {
      this._clearImplicitRefreshIframe();
      if (this._implicitRefreshTimeoutId) {
        clearTimeout(this._implicitRefreshTimeoutId);
      }
    }
  }

  async _handleCallbackUri(options) {
    await super._handleCallbackUri(options);
    if (this._auth.isImplicit && this._auth.loggedIn) {
      this._createImplicitRefreshTimeout();
    }
  }

  get name() {
    return 'proxyFrameOAuth';
  }

  get _actionTypes() {
    return actionTypes;
  }

  get proxyUri() {
    return `${url.resolve(window.location.href, this._proxyUri)}?hash=${encodeURIComponent(btoa(this._uuid))}&prefix=${encodeURIComponent(this.prefix)}`;
  }

  get proxyRetryCount() {
    return this.state.proxyRetryCount;
  }

  _callbackHandler = async ({ origin, data }) => {
    // TODO origin check
    if (data) {
      const {
        callbackUri,
        proxyLoaded,
      } = data;
      if (
        callbackUri
      ) {
        this._handleCallbackUri(callbackUri);
      } else if (proxyLoaded) {
        clearTimeout(this._retryTimeoutId);
        this._retryTimeoutId = null;
        this.store.dispatch({
          type: this.actionTypes.setupOAuth,
        });
      }
    }
  }
  _createProxyFrame = () => {
    this._proxyFrame = document.createElement('iframe');
    this._proxyFrame.src = this.proxyUri;
    this._proxyFrame.style.display = 'none';
    const isEdge = window && window.navigator && window.navigator.userAgent.indexOf('Edge') > -1;
    if (!isEdge) {
      this._proxyFrame.setAttribute('sandbox', [
        'allow-scripts',
        'allow-popups',
        'allow-same-origin',
        'allow-forms',
      ].join(' '));
    }
    document.body.appendChild(this._proxyFrame);
    window.addEventListener('message', this._callbackHandler);
    this._retryTimeoutId = setTimeout(() => {
      this._retrySetupProxyFrame();
    }, this._defaultProxyRetry);<commit_msg>Remove sandbox attribute for IE (#1335)<commit_after>import background from 'ringcentral-integration/lib/background';
import proxify from 'ringcentral-integration/lib/proxy/proxify';
import { Module } from 'ringcentral-integration/lib/di';
import ensureExist from 'ringcentral-integration/lib/ensureExist';
import url from 'url';
import uuid from 'uuid';
import actionTypes from './actionTypes';
import getProxyFrameOAuthReducer from './getProxyFrameOAuthReducer';

import OAuthBase from '../../lib/OAuthBase';

const DEFAULT_PROXY_RETRY = 5000;

@Module({
  name: 'OAuth',
  deps: [
    { dep: 'OAuthOptions', optional: true },
  ],
})
export default class ProxyFrameOAuth extends OAuthBase {
  constructor({
    redirectUri = './redirect.html',
    proxyUri = './proxy.html',
    defaultProxyRetry = DEFAULT_PROXY_RETRY,
    ...options
  }) {
    super({
      redirectUri,
      ...options,
    });
    this._uuid = uuid.v4();
    this._proxyUri = ensureExist(proxyUri, 'proxyUri');
    this._defaultProxyRetry = defaultProxyRetry;

    this._reducer = getProxyFrameOAuthReducer(this.actionTypes);

    this._loggedIn = false;
  }

  _onStateChange() {
    super._onStateChange();
    if (this._auth.loggedIn === this._loggedIn) {
      return;
    }
    this._loggedIn = this._auth.loggedIn;
    if (this._loggedIn && this._auth.isImplicit) {
      console.log('new login, start refresh token timeout');
      this._createImplicitRefreshTimeout();
    }
    if (!this._loggedIn && this._auth.isImplicit) {
      this._clearImplicitRefreshIframe();
      if (this._implicitRefreshTimeoutId) {
        clearTimeout(this._implicitRefreshTimeoutId);
      }
    }
  }

  async _handleCallbackUri(options) {
    await super._handleCallbackUri(options);
    if (this._auth.isImplicit && this._auth.loggedIn) {
      this._createImplicitRefreshTimeout();
    }
  }

  get name() {
    return 'proxyFrameOAuth';
  }

  get _actionTypes() {
    return actionTypes;
  }

  get proxyUri() {
    return `${url.resolve(window.location.href, this._proxyUri)}?hash=${encodeURIComponent(btoa(this._uuid))}&prefix=${encodeURIComponent(this.prefix)}`;
  }

  get proxyRetryCount() {
    return this.state.proxyRetryCount;
  }

  _callbackHandler = async ({ origin, data }) => {
    // TODO origin check
    if (data) {
      const {
        callbackUri,
        proxyLoaded,
      } = data;
      if (
        callbackUri
      ) {
        this._handleCallbackUri(callbackUri);
      } else if (proxyLoaded) {
        clearTimeout(this._retryTimeoutId);
        this._retryTimeoutId = null;
        this.store.dispatch({
          type: this.actionTypes.setupOAuth,
        });
      }
    }
  }
  _createProxyFrame = () => {
    this._proxyFrame = document.createElement('iframe');
    this._proxyFrame.src = this.proxyUri;
    this._proxyFrame.style.display = 'none';
    const isEdge = window && window.navigator && window.navigator.userAgent.indexOf('Edge') > -1;
    const isIE = window && window.navigator && /MSIE|Trident/i.test(window.navigator.userAgent);
    if (!isEdge && !isIE) {
      this._proxyFrame.setAttribute('sandbox', [
        'allow-scripts',
        'allow-popups',
        'allow-same-origin',
        'allow-forms',
      ].join(' '));
    }
    document.body.appendChild(this._proxyFrame);
    window.addEventListener('message', this._callbackHandler);
    this._retryTimeoutId = setTimeout(() => {
      this._retrySetupProxyFrame();
    }, this._defaultProxyRetry);
{'fraction_non_alphanumeric': 0.13367496339677892, 'fraction_numerical': 0.002342606149341142, 'mean_word_length': 3.1349878934624695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6719010', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2102, 'n_words': 366}