예제 #1
0
    def test_fetch_by_client_id_no_data(self):
        collection_mock = Mock(spec=["find_one"])
        collection_mock.find_one.return_value = None

        store = ClientStore(collection=collection_mock)

        with self.assertRaises(ClientNotFoundError):
            store.fetch_by_client_id(client_id="testclient")
예제 #2
0
    def test_fetch_by_client_id_no_data(self):
        collection_mock = Mock(spec=["find_one"])
        collection_mock.find_one.return_value = None

        store = ClientStore(collection=collection_mock)

        with self.assertRaises(ClientNotFoundError):
            store.fetch_by_client_id(client_id="testclient")
예제 #3
0
    def test_fetch_by_client_id(self):
        client_data = {"identifier": "testclient", "secret": "k#4g6", "redirect_uris": ["https://redirect"]}

        collection_mock = Mock(spec=["find_one"])
        collection_mock.find_one.return_value = client_data

        store = ClientStore(collection=collection_mock)
        client = store.fetch_by_client_id(client_id=client_data["identifier"])

        collection_mock.find_one.assert_called_with({"identifier": client_data["identifier"]})
        self.assertTrue(isinstance(client, Client))
        self.assertDictEqual(client.__dict__, client_data)
예제 #4
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()
예제 #5
0
    def test_fetch_by_client_id(self):
        client_data = {
            "identifier": "testclient",
            "secret": "k#4g6",
            "redirect_uris": ["https://redirect"]
        }

        collection_mock = Mock(spec=["find_one"])
        collection_mock.find_one.return_value = client_data

        store = ClientStore(collection=collection_mock)
        client = store.fetch_by_client_id(client_id=client_data["identifier"])

        collection_mock.find_one.assert_called_with(
            {"identifier": client_data["identifier"]})
        self.assertTrue(isinstance(client, Client))
        self.assertDictEqual(client.__dict__, client_data)
예제 #6
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()
예제 #7
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()
from oauth2.grant import AuthorizationCodeGrant
from oauth2.store.mongodb import MongodbStore, AccessTokenStore, AuthCodeStore, ClientStore
from oauth2.datatype import AccessToken, AuthorizationCode, Client
from oauth2.error import AccessTokenNotFound, AuthCodeNotFound, \
    ClientNotFoundError
# from pymongo import MongoClient
from bson.objectid import ObjectId
import pymongo
import bz2
#
# client = pymongo.MongoClient('localhost', 27017)
#
# db = client.test_database
db = pymongo.MongoClient('localhost', 27017).test_database
# access_token_store = ClientStore(collection=db["clients"])
access_token_store = ClientStore(collection=db["user"])
password = hash("2415")
# print "ddddddddddddddddddddddd"
# print "ddddddddddddddddddddddd"
# print "ddddddddddddddddddddddd"
# print "ddddddddddddddddddddddd"
# print "ddddddddddddddddddddddd"
# print password

# client_id = db["user"].insert({
#             "username": "******",
#             "password": password,
#         })
# print db.collection_names()
# print db["clients"].find({"identifier": "abc"})