Esempio n. 1
0
    def test_fetch_by_code_no_data(self):
        self.collection_mock.find_one.return_value = None

        store = AuthCodeStore(collection=self.collection_mock)

        with self.assertRaises(AuthCodeNotFound):
            store.fetch_by_code(code="abcd")
Esempio n. 2
0
    def test_fetch_by_code_no_data(self):
        self.collection_mock.find_one.return_value = None

        store = AuthCodeStore(collection=self.collection_mock)

        with self.assertRaises(AuthCodeNotFound):
            store.fetch_by_code(code="abcd")
Esempio n. 3
0
    def test_save_code(self):
        self.auth_code_data["code"] = "abcd"

        auth_code = AuthorizationCode(**self.auth_code_data)

        store = AuthCodeStore(collection=self.collection_mock)
        store.save_code(auth_code)

        self.collection_mock.insert.assert_called_with(self.auth_code_data)
Esempio n. 4
0
    def test_save_code(self):
        self.auth_code_data["code"] = "abcd"

        auth_code = AuthorizationCode(**self.auth_code_data)

        store = AuthCodeStore(collection=self.collection_mock)
        store.save_code(auth_code)

        self.collection_mock.insert.assert_called_with(self.auth_code_data)
Esempio n. 5
0
    def test_fetch_by_code(self):
        code = "abcd"

        self.collection_mock.find_one.return_value = self.auth_code_data

        self.auth_code_data["code"] = "abcd"

        store = AuthCodeStore(collection=self.collection_mock)
        auth_code = store.fetch_by_code(code=code)

        self.collection_mock.find_one.assert_called_with({"code": "abcd"})
        self.assertTrue(isinstance(auth_code, AuthorizationCode))
        self.assertDictEqual(auth_code.__dict__, self.auth_code_data)
Esempio n. 6
0
    def test_fetch_by_code(self):
        code = "abcd"

        self.collection_mock.find_one.return_value = self.auth_code_data

        self.auth_code_data["code"] = "abcd"

        store = AuthCodeStore(collection=self.collection_mock)
        auth_code = store.fetch_by_code(code=code)

        self.collection_mock.find_one.assert_called_with({"code": "abcd"})
        self.assertTrue(isinstance(auth_code, AuthorizationCode))
        self.assertDictEqual(auth_code.__dict__, self.auth_code_data)
def run_auth_server():
    try:
        client = MongoClient('localhost', 27017)

        db = client.test_database

        client_store = ClientStore(collection=db["clients"])

        token_store = AccessTokenStore(collection=db["access_tokens"])
        code_store = AuthCodeStore(collection=db["auth_codes"])

        provider = Provider(
            access_token_store=token_store,
            auth_code_store=code_store,
            client_store=client_store,
            token_generator=Uuid4())
        provider.add_grant(
            AuthorizationCodeGrant(site_adapter=TestSiteAdapter(), scopes=["basic", "big", "long"],
                                   unique_token=True,
                                   expires_in=20
                                   )
        )

        provider.add_grant(
            RefreshToken(scopes=["basic", "big", "long"], expires_in=2592000, reissue_refresh_tokens=True)
        )

        app = Application(provider=provider)

        httpd = make_server('', 8080, app, handler_class=OAuthRequestHandler)

        print("Starting OAuth2 server on http://localhost:8080/...")
        httpd.serve_forever()
    except KeyboardInterrupt:
        httpd.server_close()
Esempio n. 8
0
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()
Esempio n. 9
0
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()
Esempio n. 10
0
def run_auth_server():
    try:
        client = MongoClient('localhost', 27017)

        db = client.test_database

        client_store = ClientStore(collection=db["clients"])

        # memory
        # client_store = ClientStore()
        # client_store.add_client(client_id="abc", client_secret="xyz",
        #                         redirect_uris=["http://localhost:8081/callback"])
        #
        # token_store = TokenStore()

        token_store = AccessTokenStore(collection=db["access_tokens"])
        code_store = AuthCodeStore(collection=db["auth_codes"])

        provider = Provider(access_token_store=token_store,
                            auth_code_store=code_store,
                            client_store=client_store,
                            token_generator=Uuid4())
        provider.add_grant(
            AuthorizationCodeGrant(site_adapter=TestSiteAdapter(),
                                   scopes=["test", "test2"],
                                   unique_token=True,
                                   expires_in=1))
        # auth_controller.add_grant_type(ResourceOwnerGrant(tokens_expire=600))
        provider.add_grant(
            RefreshToken(scopes=["test", "test2"],
                         expires_in=2592000,
                         reissue_refresh_tokens=True))
        # auth_controller.add_grant_type(RefreshToken(tokens_expire=1200))
        app = Application(provider=provider)

        httpd = make_server('', 8080, app, handler_class=OAuthRequestHandler)

        print("Starting OAuth2 server on http://localhost:8080/...")
        httpd.serve_forever()
    except KeyboardInterrupt:
        httpd.server_close()