def delete(self, user): ''' Delete the user on Github Enterprise ''' # Initialize the PhantomJS selenium driver driver = PhantomJS() driver.implicitly_wait(10) driver.set_window_size(1400, 850) # Login as the admin user driver.get('https://%s/login' % (self.ghe_host)) driver.find_element_by_name('login').send_keys(self.ghe_user) driver.find_element_by_name('password').send_keys(self.ghe_pass) driver.find_element_by_name('commit').click() # Check for two-factor auth code request if driver.current_url == 'https://%s/sessions/two-factor' % self.ghe_host: if self.ghe_totp: base = '.auth-form-body input' u = driver.find_element_by_css_selector('%s[name=utf8]' % base) t = driver.find_element_by_css_selector('%s[name=authenticity_token]' % base) otp = pyotp.TOTP(self.ghe_totp) driver.request('POST', 'https://%s/sessions/two-factor' % self.ghe_host, data={ 'utf8': u.get_attribute('value'), 'otp': otp.now(), 'authenticity_token': t.get_attribute('value') } ) else: print('Two-Factor authentication required.') sys.exit() # Retrieve the admin page for the designated user to be deleted driver.get('https://%s/stafftools/users/%s/admin' % (self.ghe_host, user)) # Ensure that we were able to access the requested admin page if 'Page not found' in driver.title or user.lower() not in driver.title.lower(): print('User not found, or insufficient access rights.') sys.exit() # Locate the necessary inputs to be able to delete a user base = '#confirm_deletion form input' u = driver.find_element_by_css_selector('%s[name=utf8]' % base) m = driver.find_element_by_css_selector('%s[name=_method]' % base) t = driver.find_element_by_css_selector('%s[name=authenticity_token]' % base) # Send the delete user request driver.request('POST', 'https://%s/stafftools/users/%s' % (self.ghe_host, user), data={ 'utf8': u.get_attribute('value'), '_method': m.get_attribute('value'), 'authenticity_token': t.get_attribute('value') } )
def update(self, user, email): ''' Reset the users email address on Github Enterprise ''' # Initialize the PhantomJS selenium driver driver = PhantomJS() driver.implicitly_wait(10) driver.set_window_size(1400, 850) # Login as the admin user driver.get('https://%s/login' % (self.ghe_host)) driver.find_element_by_name('login').send_keys(self.ghe_user) driver.find_element_by_name('password').send_keys(self.ghe_pass) driver.find_element_by_name('commit').click() # Check for two-factor auth code request if driver.current_url == 'https://%s/sessions/two-factor' % self.ghe_host: if self.ghe_totp: base = '.auth-form-body input' u = driver.find_element_by_css_selector('%s[name=utf8]' % base) t = driver.find_element_by_css_selector( '%s[name=authenticity_token]' % base) otp = pyotp.TOTP(self.ghe_totp) driver.request( 'POST', 'https://%s/sessions/two-factor' % self.ghe_host, data={ 'utf8': u.get_attribute('value'), 'otp': otp.now(), 'authenticity_token': t.get_attribute('value') }) else: print('Two-Factor authentication required.') sys.exit() # Retrieve the email admin page for the designated user to be updated driver.get('https://%s/stafftools/users/%s/emails' % (self.ghe_host, user)) # Ensure that we were able to access the requested admin page if 'Page not found' in driver.title or user.lower( ) not in driver.title.lower(): print('User not found, or insufficient access rights.') sys.exit() # Locate the necessary inputs to be able to add an email address base = 'form[action="/stafftools/users/%s/emails"] input' % user u = driver.find_element_by_css_selector('%s[name=utf8]' % base) t = driver.find_element_by_css_selector('%s[name=authenticity_token]' % base) # Send the add email address request driver.request('POST', 'https://%s/stafftools/users/%s/emails' % (self.ghe_host, user), data={ 'utf8': u.get_attribute('value'), 'email': email, 'authenticity_token': t.get_attribute('value') }) # Send password reset to new email address base = 'form[action="/stafftools/users/%s/password/send_reset_email"] input' % user u = driver.find_element_by_css_selector('%s[name=utf8]' % base) t = driver.find_element_by_css_selector('%s[name=authenticity_token]' % base) m = driver.find_element_by_css_selector('%s[name=_method]' % base) driver.request( 'POST', 'https://%s/stafftools/users/%s/password/send_reset_email' % (self.ghe_host, user), data={ 'utf8': u.get_attribute('value'), 'email': email, 'authenticity_token': t.get_attribute('value'), '_method': m.get_attribute('value') }) # Get password reset link and display to console driver.get('https://%s/stafftools/users/%s/emails' % (self.ghe_host, user)) if email in driver.page_source: print('Email added and password reset email sent.') else: print( 'New email not showing up on user page; please check manually.' )