def _handle_authenticate(self, msg): if self.authentification is not None: return if self.dialog.password is None: raise ValueError('Password required for authentication') if msg.method.upper() == 'REGISTER': self.attempts -= 1 if self.attempts < 1: self._error(AuthentificationFailed('Too many unauthorized attempts!')) return username = msg.to_details['uri']['user'] elif msg.method.upper() == 'INVITE': self.attempts -= 1 if self.attempts < 1: self._error(AuthentificationFailed('Too many unauthorized attempts!')) return username = msg.from_details['uri']['user'] else: username = msg.from_details['uri']['user'] self.original_msg.cseq += 1 self.original_msg.headers['Authorization'] = str(Auth.from_authenticate_header( authenticate=msg.headers['WWW-Authenticate'], method=msg.method, uri=msg.to_details['uri'].short_uri(), username=username, password=self.dialog.password) ) self.dialog.transactions[self.original_msg.method][self.original_msg.cseq] = self self.authentification = asyncio.ensure_future(self._timer())
def _handle_proxy_authenticate(self, msg): if self.dialog.password is None: raise ValueError('Password required for authentication') self.attempts -= 1 if self.attempts < 1: self._error(AuthentificationFailed('Too many unauthorized attempts!')) return elif self.authentification: self.authentification.cancel() self.authentification = None if msg.method.upper() == 'REGISTER': username = msg.to_details['uri']['user'] else: username = msg.from_details['uri']['user'] self.original_msg.cseq += 1 auth = Auth.from_authenticate_header( authenticate=msg.headers['Proxy-Authenticate'], method=msg.method) proxy_auth_header = auth.generate_authorization( uri=msg.to_details['uri'].short_uri(), username=username, password=self.dialog.password) self.original_msg.headers['Proxy-Authorization'] = str(proxy_auth_header) self.dialog.transactions[self.original_msg.method][self.original_msg.cseq] = self self.authentification = asyncio.ensure_future(self._timer())
def _handle_proxy_authenticate(self, msg): self._handle_proxy_authenticate(msg) self.original_msg = self.original_msg.pop(msg.cseq) del (self.original_msg.headers['CSeq']) self.original_msg.headers['Proxy-Authorization'] = str(Auth.from_authenticate_header( authenticate=msg.headers['Proxy-Authenticate'], method=msg.method, uri=str(self.to_details), username=self.to_details['uri']['user'], password=self.dialog.password)) self.dialog.send_message(msg.method, headers=self.original_msg.headers, payload=self.original_msg.payload, future=self.futrue)
def receive_message(self, msg): if isinstance(msg, Response): if msg.cseq in self._msgs[msg.method]: if msg.status_code == 401: if msg.method.upper() == 'REGISTER': self.register_current_attempt -= 1 if self.register_current_attempt < 1: self._msgs[msg.method].pop(msg.cseq).future.set_exception(RegisterFailed('Too many unauthorized attempts !')) return original_msg = self._msgs[msg.method].pop(msg.cseq) del(original_msg.headers['CSeq']) original_msg.headers['Authorization'] = str(Auth.from_authenticate_header( authenticate=msg.headers['WWW-Authenticate'], method=msg.method, uri=str(self.to_details), username=self.to_details['uri']['user'], password=self.password)) self.send_message(msg.method, headers=original_msg.headers, payload=original_msg.payload, future=original_msg.future) else: if msg.method.upper() == 'REGISTER': self.register_current_attempt = None self._msgs[msg.method].pop(msg.cseq).future.set_result(msg) # Transaction end else: raise ValueError('This Response SIP message doesn\'t have Request: "%s"' % msg) else: hdrs = multidict.CIMultiDict() hdrs['Via'] = msg.headers['Via'] hdrs['To'] = msg.headers['To'] hdrs['From'] = msg.headers['From'] hdrs['CSeq'] = msg.headers['CSeq'] hdrs['Call-ID'] = msg.headers['Call-ID'] resp = Response(status_code=200, status_message='OK', # aiosip is in da place ! headers=hdrs, payload=None) self.app.send_message(type(self.protocol), self.local_addr, self.remote_addr, resp) for callback_info in self.callbacks[msg.method.upper()]: if asyncio.iscoroutinefunction(callback_info['callable']): fut = callback_info['callable'](*((self, msg,) + callback_info['args']), **callback_info['kwargs']) asyncio.async(fut) else: self.loop.call_soon(partial(callback_info['callable'], *((self, msg,) + callback_info['args']), **callback_info['kwargs']))
def feed_message(self, msg, original_msg=None): authenticate = msg.headers.get('WWW-Authenticate') if msg.status_code == 401 and authenticate: if msg.method.upper() == 'REGISTER': self.attempts -= 1 if self.attempts < 1: self.future.set_exception( RegisterFailed('Too many unauthorized attempts!') ) return username = msg.to_details['uri']['user'] elif msg.method.upper() == 'INVITE': self.attempts -= 1 if self.attempts < 1: self.future.set_exception( InviteFailed('Too many unauthorized attempts!') ) return username = msg.from_details['uri']['user'] hdrs = CIMultiDict() hdrs['From'] = msg.headers['From'] hdrs['To'] = msg.headers['To'] hdrs['Call-ID'] = msg.headers['Call-ID'] hdrs['CSeq'] = msg.headers['CSeq'].replace('INVITE', 'ACK') hdrs['Via'] = msg.headers['Via'] self.dialog.send_message(method='ACK', headers=hdrs) else: username = msg.from_details['uri']['user'] del(original_msg.headers['CSeq']) original_msg.headers['Authorization'] = str(Auth.from_authenticate_header( authenticate=authenticate, method=msg.method, uri=msg.to_details['uri'].short_uri(), username=username, password=self.password)) self.dialog.send_message(original_msg.method, to_details=original_msg.to_details, headers=original_msg.headers, payload=original_msg.payload, future=self.futrue) # for proxy authentication elif msg.status_code == 407: original_msg = self._msgs[msg.method].pop(msg.cseq) del(original_msg.headers['CSeq']) original_msg.headers['Proxy-Authorization'] = str(Auth.from_authenticate_header( authenticate=msg.headers['Proxy-Authenticate'], method=msg.method, uri=str(self.to_details), username=self.to_details['uri']['user'], password=self.password)) self.dialog.send_message(msg.method, headers=original_msg.headers, payload=original_msg.payload, future=self.futrue) elif 100 <= msg.status_code < 200: pass else: self.future.set_result(msg)
def receive_message(self, msg): if isinstance(msg, Response): if msg.cseq in self._msgs[msg.method]: if msg.status_code == 401: if msg.method.upper() == 'REGISTER': self.register_current_attempt -= 1 if self.register_current_attempt < 1: self._msgs[msg.method].pop(msg.cseq).future.set_exception(RegisterFailed('Too many unauthorized attempts !')) return username = self.to_details['uri']['user'] elif msg.method.upper() == 'INVITE': self.invite_current_attempt -= 1 if self.invite_current_attempt < 1: self._msgs[msg.method].pop(msg.cseq).future.set_exception(InviteFailed('Too many unauthorized attempts !')) return username = msg.from_details['uri']['user'] hdrs = CIMultiDict() hdrs['From'] = msg.headers['From'] hdrs['To'] = msg.headers['To'] hdrs['Call-ID'] = msg.headers['Call-ID'] hdrs['CSeq'] = msg.headers['CSeq'].replace('INVITE', 'ACK') hdrs['Via'] = msg.headers['Via'] self.send_message(method='ACK', headers=hdrs) original_msg = self._msgs[msg.method].pop(msg.cseq) del(original_msg.headers['CSeq']) original_msg.headers['Authorization'] = str(Auth.from_authenticate_header( authenticate=msg.headers['WWW-Authenticate'], method=msg.method, uri=self.to_details.from_repr(), username=username, password=self.password)) self.send_message(msg.method, headers=original_msg.headers, payload=original_msg.payload, future=original_msg.future) # for proxy authentication elif msg.status_code == 407: original_msg = self._msgs[msg.method].pop(msg.cseq) del(original_msg.headers['CSeq']) original_msg.headers['Proxy-Authorization'] = str(Auth.from_authenticate_header( authenticate=msg.headers['Proxy-Authenticate'], method=msg.method, uri=str(self.to_details), username=self.to_details['uri']['user'], password=self.password)) self.send_message(msg.method, headers=original_msg.headers, payload=original_msg.payload, future=original_msg.future) elif msg.status_code == 100: pass elif msg.status_code == 180: pass else: if msg.method.upper() == 'REGISTER': self.register_current_attempt = None if msg.method.upper() == 'INVITE': self.invite_current_attempt = None self._msgs[msg.method].pop(msg.cseq).future.set_result(msg) # Transaction end else: raise ValueError('This Response SIP message doesn\'t have Request: "%s"' % msg) else: if msg.method != 'ACK': hdrs = CIMultiDict() hdrs['Via'] = msg.headers['Via'] hdrs['CSeq'] = msg.headers['CSeq'] hdrs['Call-ID'] = msg.headers['Call-ID'] self.send_reply(status_code=200, status_message='OK', to_details=msg.to_details, from_details=msg.from_details, headers=hdrs, payload=None) for callback_info in self.callbacks[msg.method.upper()]: if asyncio.iscoroutinefunction(callback_info['callable']): fut = callback_info['callable'](*((self, msg,) + callback_info['args']), **callback_info['kwargs']) asyncio.async(fut) else: self.loop.call_soon(partial(callback_info['callable'], *((self, msg,) + callback_info['args']), **callback_info['kwargs']))