class Token(object): schema = TokenSchema() auth = { 'backend': BasicAuthBackend(Authenticator.instance) } def on_get(self, req, resp): # type: (falcon.Request, falcon.Response) -> None token, expiry = Authenticator.instance.new_token(req.context['user']) req.context['result'] = { 'token': token, 'expiry': expiry }
def test_falcon_request_logs_user(user): user_dict = dict([(user.key or 'username', user.name)]) basic_auth = BasicAuthBackend(lambda username, password: user_dict) app = falcon.API(middleware=[ falcon_logging.LoggingMiddleware(), FalconAuthMiddleware(basic_auth) ]) app.add_route('/test/path', TestResource()) args = [app, user.key] if user.key else [app] _set_up_falcon_logging(*args) expected = {'remote_user': v_str(user.name)} _check_falcon_request_log( app, {'Authorization': str(auth_basic(user.name, 'pass'))}, expected)
class authUser(object): auth = {'backend': BasicAuthBackend(user_loader=loadUserPass)} cors_enabled = False def on_get(self, req, resp): user = req.context['user'] if user.remote: resp.status = falcon.HTTP_401 resp.body = json.dumps({"Error": "Remote user"}) token = Token.create(user=user) payload = {"token": token.key} resp.status = falcon.HTTP_200 resp.body = json.dumps({"token": auth_backend.get_auth_token(payload)})
import falcon from models import * from playhouse.shortcuts import model_to_dict import json import uuid from g.auth import Auth from falcon_auth import FalconAuthMiddleware, BasicAuthBackend user_loader = lambda username, password: {'username': username} auth_backend = BasicAuthBackend(user_loader) auth_middleware = FalconAuthMiddleware(auth_backend, exempt_routes=['/exempt', '/jwt'], exempt_methods=['HEAD']) class MAuth(): auth = {'auth_disabled': False} def on_get(self, req, resp): if not 'user' in req.context: resp.status = falcon.HTTP_404 return user = req.context['user'] print(user) resp.body = "User Found: {}\n".format(user['username']) def on_head(self, req, resp): resp.body = '?'
def __init__(self, cfg): token_auth = TokenAuthBackend(User.get_by_token) basic_auth = BasicAuthBackend(User.get_by_basic_auth) multi_auth = MultiAuthBackend(token_auth, basic_auth) auth_middleware = FalconAuthMiddleware(multi_auth) super(AnalystService, self).__init__( middleware=[ CORSComponentMiddleware(), RequireJSONMiddleware(), auth_middleware, ] ) handlers = falcon.media.Handlers({"application/json": DateTimeJSONHandler()}) self.resp_options.media_handlers.update(handlers) self.resp_options.default_media_type = "application/json" self.router_options.converters["ipv4_addr"] = IPV4Converter self.router_options.converters[ "lowercase_alpha_num" ] = LowerCaseAlphaNumConverter self.cfg = cfg # Build an object to manage our db connections. self.manager = DBManager(self.cfg.db.file_path) self.manager.setup() # Build routes self.add_route(f"/api/{self.cfg.version}/init", users.InitResource()) self.add_route(f"/api/{self.cfg.version}/users", users.UsersResource()) self.add_route( f"/api/{self.cfg.version}/users/{{username:lowercase_alpha_num}}", users.UsersResource(), ) self.add_route( f"/api/{self.cfg.version}/tokens/{{username:lowercase_alpha_num}}", tokens.TokensResource(), ) self.add_route( f"/api/{self.cfg.version}/asn", asn.ASNResource(self.cfg.asn_path) ) self.add_route( f"/api/{self.cfg.version}/asn/{{ip:ipv4_addr}}", asn.ASNResource(self.cfg.asn_path), ) self.add_route( f"/api/{self.cfg.version}/geo", geo.GeoResource(self.cfg.geo_path) ) self.add_route( f"/api/{self.cfg.version}/geo/{{ip:ipv4_addr}}", geo.GeoResource(self.cfg.geo_path), ) self.add_route(f"/api/{self.cfg.version}/iplists", iplists.IPListResource()) self.add_route( f"/api/{self.cfg.version}/iplists/{{ip_list_name:lowercase_alpha_num}}", iplists.IPListResource(), ) self.add_route( f"/api/{self.cfg.version}/iplists/{{ip_list_name:lowercase_alpha_num}}/items", iplists.IPListItemResource(), )
def auth_tuple_to_middleware(auth_tuple): user_loader = lambda username, password: username if ( username, password) == auth_tuple else None auth_backend = BasicAuthBackend(user_loader) return FalconAuthMiddleware(auth_backend)