class OAuthCodeExchangeHandler(webapp2.RequestHandler): """Request handler for OAuth 2.0 code exchange.""" @errors.error_aware def post(self): """Handle code exchange.""" self._process_request_body() self._exchange_code() user = self._create_user() util.write_response(self, user.to_dict()) def _process_request_body(self): """Parse the request body.""" try: request = json.loads(self.request.body) self._code = request.get('code') if not self._code: raise errors.BadRequestError('`code` attribute is required') self._timezone_offset = request.get( 'timezoneOffset', DEFAULT_TIMEZONE_OFFSET) except ValueError: raise errors.BadRequestError('Unsupported request body') def _exchange_code(self): """Retrieve credentials for the current user.""" oauth_flow = self._create_oauth_flow() # Perform the exchange of the code. try: creds = oauth_flow.step2_exchange(self._code) except FlowExchangeError, e: raise errors.InternalServerError('Unable to exchange code: ', e.message) # Verify that the token has been issued for our application. self._userid = util.verify_token(creds.access_token) if not self._userid: raise errors.BadRequestError('Unknown client ID') # Verify that we can retrieve valid refresh token for the current user. if creds.refresh_token: # Store the credentials in the data store using the userid as the key. StorageByKeyName( model.UserSettings, self._userid, 'credentials').put(creds) else: # Look for existing credentials in our datastore. creds = StorageByKeyName( model.UserSettings, self._userid, 'credentials').get() if not creds or not creds.refresh_token: raise errors.UnauthorizedError('No refresh token') return creds
async def servban_command(message, config): try: if not utils.check_if_authorized(message.author, config["json"]["moderatorRoles"]): raise err.UnauthorizedError("Vous n'êtes pas autorisé à utiliser cette commande") args_match = re.match(r"!servban ([\w]+#[0-9]{4}|\"[\w ]+#[0-9]{4}\")$", message.content, re.UNICODE) if not args_match: raise err.BadCommandSyntaxError("Syntaxe incorrecte : !servban <nomDuBatardAKick>") member_to_kick = message.server.get_member_named(args_match.group(1)) except Exception as e: client.send_message(message.channel, str(e))
async def on_message(message): if message.content.startswith('!ping'): await client.send_message(message.channel, 'PONG !') elif message.content.startswith('!kick'): await mod.kick_command(message) elif message.content.startswith("!reload"): try: global config if not utils.check_if_authorized( [role.id for role in message.author.roles], config["json"]["moderatorRoles"]): raise err.UnauthorizedError("Not authorized (Kick command)") config = reload_config(config) except err.UnauthorizedError: await client.send_message( message.channel, "Vous n'êtes pas autorisé à utiliser cette commande") elif message.content.startswith('!close'): await client.close()
def _check_response(response): if response.status_code == 403: raise errors.UnauthorizedError() if response.status_code == 404: raise errors.NotFoundError()
def get(self, url): if self.apikey is None: raise e.UnauthorizedError('A ProPublica API key is required') headers = {'X-API-Key': self.apikey} return requests.get(url, headers=headers).json()