def run_auth_server(): try: client_store = ClientStore() client_store.add_client( client_id="abc", client_secret="xyz", redirect_uris=["http://localhost:8081/callback"]) token_store = TokenStore() auth_controller = Provider(access_token_store=token_store, auth_code_store=token_store, client_store=client_store, site_adapter=TestSiteAdapter(), token_generator=Uuid4()) auth_controller.add_grant(AuthorizationCodeGrant()) app = Wsgi(server=auth_controller) httpd = make_server('', 8080, app, handler_class=OAuthRequestHandler) print( "Starting implicit_grant oauth2 server on http://localhost:8080/..." ) httpd.serve_forever() except KeyboardInterrupt: httpd.server_close()
def test_call(self): body = "body" headers = {"header": "value"} path = "/authorize" status_code = 200 http_code = "200 OK" environment = {"PATH_INFO": path, "myvar": "value"} request_mock = Mock(spec=Request) request_class_mock = Mock(return_value=request_mock) response_mock = Mock(spec=Response) response_mock.body = body response_mock.headers = headers response_mock.status_code = status_code server_mock = Mock(spec=Provider) server_mock.dispatch.return_value = response_mock start_response_mock = Mock() wsgi = Wsgi(server=server_mock, authorize_uri=path, request_class=request_class_mock, env_vars=["myvar"]) result = wsgi(environment, start_response_mock) request_class_mock.assert_called_with(environment) server_mock.dispatch.assert_called_with(request_mock, {"myvar": "value"}) start_response_mock.assert_called_with(http_code, list(headers.items())) self.assertEqual(result, [body.encode('utf-8')])
def main(): client = MongoClient() db = client.testdb access_token_store = AccessTokenStore(collection=db["access_tokens"]) auth_code_store = AuthCodeStore(collection=db["auth_codes"]) client_store = ClientStore(collection=db["clients"]) provider = Provider(access_token_store=access_token_store, auth_code_store=auth_code_store, client_store=client_store, site_adapter=TestSiteAdapter(), token_generator=Uuid4()) provider.add_grant(AuthorizationCodeGrant()) provider.add_grant(ImplicitGrant()) provider.add_grant(ResourceOwnerGrant()) provider.add_grant(ClientCredentialsGrant()) provider.add_grant(RefreshToken(expires_in=600)) provider.enable_unique_tokens() app = Wsgi(server=provider) try: httpd = make_server('', 8888, app) print("Starting test auth server on port 8888...") httpd.serve_forever() except KeyboardInterrupt: httpd.server_close()
def run_provider(queue): try: redirect_uri = "http://127.0.0.1:15487/callback" stores = store_factory(client_identifier="abc", client_secret="xyz", redirect_uris=[redirect_uri]) provider = Provider( access_token_store=stores["access_token_store"], auth_code_store=stores["auth_code_store"], client_store=stores["client_store"], site_adapter=TestSiteAdapter(), token_generator=Uuid4()) provider.add_grant(AuthorizationCodeGrant(expires_in=120)) provider.add_grant(RefreshToken(expires_in=60)) app = Wsgi(server=provider) httpd = make_server('', 15486, app, handler_class=NoLoggingHandler) queue.put({"result": 0}) httpd.serve_forever() except Exception as e: queue.put({"result": 1, "error_message": str(e)})
def main(): parser = argparse.ArgumentParser(description="python-oauth2 test provider") parser.add_argument("--store", dest="store", type=str, default="mongodb", help="The store adapter to use. Can one of 'mongodb'"\ "(default), 'mysql'") args = parser.parse_args() if args.store == "mongodb": print("Using mongodb stores...") client = MongoClient() db = client.testdb access_token_store = AccessTokenStore(collection=db["access_tokens"]) auth_code_store = AuthCodeStore(collection=db["auth_codes"]) client_store = ClientStore(collection=db["clients"]) elif args.store == "mysql": print("Using mysql stores...") connection = mysql.connector.connect(host="127.0.0.1", user="******", passwd="", db="testdb") access_token_store = MysqlAccessTokenStore(connection=connection) auth_code_store = MysqlAuthCodeStore(connection=connection) client_store = MysqlClientStore(connection=connection) else: raise Exception("Unknown store") provider = Provider(access_token_store=access_token_store, auth_code_store=auth_code_store, client_store=client_store, site_adapter=TestSiteAdapter(), token_generator=Uuid4()) provider.add_grant(AuthorizationCodeGrant(expires_in=120)) provider.add_grant(ImplicitGrant()) provider.add_grant(ResourceOwnerGrant()) provider.add_grant(ClientCredentialsGrant()) provider.add_grant(RefreshToken(expires_in=60)) app = Wsgi(server=provider) try: httpd = make_server('', 8888, app) print("Starting test auth server on port 8888...") httpd.serve_forever() except KeyboardInterrupt: httpd.server_close()