Best Python code snippet using pytractor_python
token.js
Source:token.js  
...94	 * Refresh token method. Useful in a method form as it can be override in tests.95	 * @returns {Promise.<String>}96	 */97	refreshToken() {98		return this._refresh()99			.then( value => {100				this._validateTokenValue( value );101				this.set( 'value', value );102				if ( this._options.autoRefresh ) {103					this._registerRefreshTokenTimeout();104				}105			} )106			.then( () => this );107	}108	/**109	 * Destroys token instance. Stops refreshing.110	 */111	destroy() {112		clearTimeout( this._tokenRefreshTimeout );...server_status_processes.js
Source:server_status_processes.js  
...119        var label = PMA_messages.strStartRefresh;120        if (processList.autoRefresh) {121            img = 'pause.png';122            label = PMA_messages.strStopRefresh;123            processList.refresh();124        }125        $('a#toggleRefresh').html(PMA_getImage(img) + escapeHtml(label));126    },127    /**128     * Return the Url Parameters129     * for autorefresh request,130     * includes showExecuting if the filter is checked131     *132     * @return urlParams - url parameters with autoRefresh request133     */134    getUrlParams: function() {135        var urlParams = { 'ajax_request': true, 'refresh': true };136        if ($('#showExecuting').is(":checked")) {137            urlParams.showExecuting = true;138            return urlParams;139        }140        return urlParams;141    }142};143AJAX.registerOnload('server_status_processes.js', function() {144    processList.init();145    // Bind event handler for kill_process146    $('#tableprocesslist').on(147        'click',148        'a.kill_process',149        processList.killProcessHandler150    );151    // Bind event handler for toggling refresh of process list152    $('a#toggleRefresh').on('click', function(event) {153        event.preventDefault();154        processList.autoRefresh = !processList.autoRefresh;155        processList.setRefreshLabel();156    });157    // Bind event handler for change in refresh rate158    $('#id_refreshRate').on('change', function(event) {159        processList.refreshInterval = $(this).val();160        processList.refresh();161    });162    // Bind event handler for table header links163    $('#tableprocesslist').on('click', 'thead a', function() {164        processList.refreshUrl = $(this).attr('href');165    });166});167/**168 * Unbind all event handlers before tearing down a page169 */170AJAX.registerTeardown('server_status_processes.js', function() {171    $('#tableprocesslist').off('click', 'a.kill_process');172    $('a#toggleRefresh').off('click');173    $('#id_refreshRate').off('change');174    $('#tableprocesslist').off('click', 'thead a');...processes.js
Source:processes.js  
...73   * @param object the event object74   *75   * @return void76   */77  refresh: function refresh() {78    // abort any previous pending requests79    // this is necessary, it may go into80    // multiple loops causing unnecessary81    // requests even after leaving the page.82    processList.abortRefresh(); // if auto refresh is enabled83    if (processList.autoRefresh) {84      var interval = parseInt(processList.refreshInterval, 10) * 1000;85      var urlParams = processList.getUrlParams();86      processList.refreshRequest = $.post(processList.refreshUrl, urlParams, function (data) {87        if (data.hasOwnProperty('success') && data.success) {88          var $newTable = $(data.message);89          $('#tableprocesslist').html($newTable.html());90          Functions.highlightSql($('#tableprocesslist'));91        }92        processList.refreshTimeout = setTimeout(processList.refresh, interval);93      });94    }95  },96  /**97   * Stop current request and clears timeout98   *99   * @return void100   */101  abortRefresh: function abortRefresh() {102    if (processList.refreshRequest !== null) {103      processList.refreshRequest.abort();104      processList.refreshRequest = null;105    }106    clearTimeout(processList.refreshTimeout);107  },108  /**109   * Set label of refresh button110   * change between play & pause111   *112   * @return void113   */114  setRefreshLabel: function setRefreshLabel() {115    var img = 'play';116    var label = Messages.strStartRefresh;117    if (processList.autoRefresh) {118      img = 'pause';119      label = Messages.strStopRefresh;120      processList.refresh();121    }122    $('a#toggleRefresh').html(Functions.getImage(img) + Functions.escapeHtml(label));123  },124  /**125   * Return the Url Parameters126   * for autorefresh request,127   * includes showExecuting if the filter is checked128   *129   * @return urlParams - url parameters with autoRefresh request130   */131  getUrlParams: function getUrlParams() {132    var urlParams = {133      'server': CommonParams.get('server'),134      'ajax_request': true,135      'refresh': true,136      'full': $('input[name="full"]').val(),137      'order_by_field': $('input[name="order_by_field"]').val(),138      'column_name': $('input[name="column_name"]').val(),139      'sort_order': $('input[name="sort_order"]').val()140    };141    if ($('#showExecuting').is(':checked')) {142      urlParams.showExecuting = true;143      return urlParams;144    }145    return urlParams;146  }147};148AJAX.registerOnload('server/status/processes.js', function () {149  processList.init(); // Bind event handler for kill_process150  $('#tableprocesslist').on('click', 'a.kill_process', processList.killProcessHandler); // Bind event handler for toggling refresh of process list151  $('a#toggleRefresh').on('click', function (event) {152    event.preventDefault();153    processList.autoRefresh = !processList.autoRefresh;154    processList.setRefreshLabel();155  }); // Bind event handler for change in refresh rate156  $('#id_refreshRate').on('change', function () {157    processList.refreshInterval = $(this).val();158    processList.refresh();159  }); // Bind event handler for table header links160  $('#tableprocesslist').on('click', 'thead a', function () {161    processList.refreshUrl = $(this).attr('href');162  });163});164/**165 * Unbind all event handlers before tearing down a page166 */167AJAX.registerTeardown('server/status/processes.js', function () {168  $('#tableprocesslist').off('click', 'a.kill_process');169  $('a#toggleRefresh').off('click');170  $('#id_refreshRate').off('change');171  $('#tableprocesslist').off('click', 'thead a'); // stop refreshing further172  processList.abortRefresh();...jwt.js
Source:jwt.js  
1const jwt = require('jsonwebtoken');2const { v1: uuidv1 } = require('uuid');3const mockDB = require('./data.mock');4const jwtSecretString = 'everybody loves ice cream';5function getAccessToken(payload) {6  return jwt.sign({user: payload}, jwtSecretString, { expiresIn: '15min' });7}8function getRefreshToken(payload) {9  // get all user's refresh tokens from DB10  const userRefreshTokens = mockDB.tokens.filter(token => token.userId === payload.id);11  // check if there are 5 or more refresh tokens,12  // which have already been generated. In this case we should13  // remove all this refresh tokens and leave only new one for security reason14  if (userRefreshTokens.length >= 5) {15    mockDB.tokens = mockDB.tokens.filter(token => token.userId !== payload.id);16  }17  const refreshToken = jwt.sign({user: payload}, jwtSecretString, { expiresIn: '30d' });18  mockDB.tokens.push({19    id: uuidv1(),20    userId: payload.id,21    refreshToken22  });23  return refreshToken;24}25function verifyJWTToken(token) {26  return new Promise((resolve, reject) => {27    if (!token.startsWith('Bearer')) {28      // Reject if there is no Bearer in the token29      return reject('Token is invalid');30    }31    // Remove Bearer from string32    token = token.slice(7, token.length);33    jwt.verify(token, jwtSecretString, (err, decodedToken) => {34      if (err) {35        return reject(err.message);36      }37      // Check the decoded user38      if (!decodedToken || !decodedToken.user) {39        return reject('Token is invalid');40      }41      resolve(decodedToken.user);42    })43  });44}45function refreshToken(token) {46  // get decoded data47  const decodedToken = jwt.verify(token, jwtSecretString);48  // find the user in the user table49  const user = mockDB.users.find(user => user.id = decodedToken.user.id);50  if (!user) {51    throw new Error(`Access is forbidden`);52  }53  // get all user's refresh tokens from DB54  const allRefreshTokens = mockDB.tokens.filter(refreshToken => refreshToken.userId === user.id);55  if (!allRefreshTokens || !allRefreshTokens.length) {56    throw new Error(`There is no refresh token for the user with`);57  }58  const currentRefreshToken = allRefreshTokens.find(refreshToken => refreshToken.refreshToken === token);59  if (!currentRefreshToken) {60    throw new Error(`Refresh token is wrong`);61  }62  // user's data for new tokens63  const payload = {64    id : user.id,65    email: user.email,66    username: user.username67  };68  // get new refresh and access token69  const newRefreshToken = getUpdatedRefreshToken(token, payload);70  const newAccessToken = getAccessToken(payload);71  return {72    accessToken: newAccessToken,73    refreshToken: newRefreshToken74  };75}76function getUpdatedRefreshToken(oldRefreshToken, payload) {77  // create new refresh token78  const newRefreshToken = jwt.sign({user: payload}, jwtSecretString, { expiresIn: '30d' });79  // replace current refresh token with new one80  mockDB.tokens = mockDB.tokens.map(token => {81    if (token.refreshToken === oldRefreshToken) {82      return {83        ...token,84        refreshToken: newRefreshToken85      };86    }87    return token;88  });89  return newRefreshToken;90}91module.exports = {92  getAccessToken,93  getRefreshToken,94  verifyJWTToken,95  refreshToken...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
