def check_employee(self, email, password): try: employee = self.storage.find_by_email(email) check_password = self.hash_maker.check_with_hash(password, employee.password) if not check_password: raise AuthenticationError() if not employee.activated: raise DeactivatedEmployeeError() token_data = {'email': email, 'password': employee.password} return self.tokenizer.get_token_by_data(token_data) except NotFoundError: raise AuthenticationError()
def _make_requests_request(self, method, url_suffix, data): full_url = '%s%s' % (self._api_base_url, url_suffix) r = self._method_fn_map.get(method)(url=full_url, data=data, auth=(self._api_key, '')) content = json.loads(r.content) error = content.get('error', content.get('errors', [])) # if any error if r.status_code == 200: return content elif r.status_code in [400, 422]: raise InvalidRequestError(error=error, http_body=r.text, http_status=r.status_code, json_body=content) elif r.status_code == 401: raise AuthenticationError(error=error, http_body=r.text, http_status=r.status_code, json_body=content) elif r.status_code == 404: raise APIConnectionError(error=error, http_body=r.text, http_status=r.status_code, json_body=content) else: raise APIError(error=error, http_body=r.text, http_status=r.status_code, json_body=content)
def request(self, entity, method, entityId=None, params=None, data=None): if not self.logged_in: _loginResponse = self.login() if not _loginResponse: raise AuthenticationError(errors=_loginResponse.text) _method = method.lower() json_payload = data if _method == "get": if entityId: url = '%s/%s/%s' % (self.base_url, entity, entityId) else: url = '%s/%s' % (self.base_url, entity) response = requests.get(url, headers=self.headers, params=params) elif _method == "post": response = requests.post('%s/%s' % (self.base_url, entity), headers=self.headers, data=json.dumps(json_payload)) elif _method == "put": response = requests.put('%s/%s/%s' % (self.base_url, entity, entityId), headers=self.headers, data=json.dumps(json_payload)) #print response.url return response.json()
def get_new_token(self, email=None, employee_id=None): if email: employee = self.storage.find_by_email(email) elif employee_id: employee = self.storage.find_by_id(employee_id) else: raise AuthenticationError() token_data = {'email': employee.email, 'password': employee.password} return self.tokenizer.get_token_by_data(token_data)
def on_connect(self): "Initialize the connection, authenticate and select a database" self._parser.on_connect(self) # if a password is specified, authenticate if self.password: self.send_command('AUTH', self.password) if nativestr(self.read_response()) != 'OK': raise AuthenticationError('Invalid Password') # if a database is specified, switch to it if self.db: self.send_command('SELECT', self.db) if nativestr(self.read_response()) != 'OK': raise ConnectionError('Invalid Database')
def _authenticate(self): """ Gets and stores session identifier to be used for each subequent request """ if self.authenticated: return data = ( ('action', 'authenticate'), ('format', 'php'), ('username', self.username), ('password', self.password), ) con = urllib2.urlopen(self.url, urllib.urlencode(data)) php = con.read() result = phpserialize.loads(php) if result['success'] == 'true': self.authenticated = True self.token = result['session'] else: raise AuthenticationError(self.username, self.password)
def send_email(email, password, resource='Summary'): pdf = helper.create_report(resource) filename = 'report_'+datetime.now().strftime("%Y-%m-%d_%H-%M-%S")+'.pdf' settings_file = open(os.path.join( os.path.expanduser('~'), '.sms/settings.json')) settings = json.load(settings_file) settings_file.close() receivers = list(settings['email'].keys()) assert len( receivers) > 0, '\nNo receivering emails present. Update settings to add people to emailing list\n' # Setup the MIME message = MIMEMultipart() message['From'] = 'System Monitoring System' message['To'] = ', '.join(receivers) message['Subject'] = 'Report - System Monitoring System' body = """ Hi, Please find attached the report you requested. Regards, System Monitoring System Team """ message.attach(MIMEText(body, 'plain')) payload = MIMEBase('application', 'octate-stream', Name=filename) payload.set_payload(pdf.output(filename, 'S')) # enconding the binary into base64 encoders.encode_base64(payload) # add header with pdf name payload.add_header('Content-Decomposition', 'attachment', filename=filename) message.attach(payload) # use gmail with port session = smtplib.SMTP('smtp.gmail.com', 587) # enable security session.starttls() # login with mail_id and password try: session.login(email, password) except smtplib.SMTPAuthenticationError: msg = """ Either of the following error occured: 1. Username or password do not match to an existing Google account. 2. Less secure app access is turned off. """ raise AuthenticationError(msg) text = message.as_string() try: session.sendmail(email, receivers, text) except smtplib.SMTPSenderRefused: msg = """ Either of the following error occured: 1. Username or password do not match to an existing Google account. 2. Less secure app access is turned off. """ raise AuthenticationError(msg) session.quit() return filename