Esempio n. 1
0
class Client(object):
    login_view = LoginView
    authenticate_view = AuthenticateView
    backend = "%s.%s" % (ModelBackend.__module__, ModelBackend.__name__)
    user_extra_data = None

    def __init__(self, server_url, public_key, private_key, user_extra_data=None):
        self.server_url = server_url
        self.public_key = public_key
        self.private_key = private_key
        self.consumer = SyncConsumer(self.server_url, self.public_key, self.private_key)
        if user_extra_data:
            self.user_extra_data = user_extra_data

    @classmethod
    def from_dsn(cls, dsn):
        parse_result = urlparse.urlparse(dsn)
        public_key = parse_result.username
        private_key = parse_result.password
        netloc = parse_result.hostname
        if parse_result.port:
            netloc += ":%s" % parse_result.port
        server_url = urlparse.urlunparse(
            (
                parse_result.scheme,
                netloc,
                parse_result.path,
                parse_result.params,
                parse_result.query,
                parse_result.fragment,
            )
        )
        return cls(server_url, public_key, private_key)

    def get_request_token(self, redirect_to):
        return self.consumer.consume("/request-token/", {"redirect_to": redirect_to})["request_token"]

    def get_user(self, access_token):
        data = {"access_token": access_token}
        if self.user_extra_data:
            data["extra_data"] = self.user_extra_data
        user_data = self.consumer.consume("/verify/", data)
        user = self.build_user(user_data)
        return user

    def build_user(self, user_data):
        try:
            user = User.objects.get(username=user_data["username"])
        except User.DoesNotExist:
            user = User(**user_data)
        user.set_unusable_password()
        user.save()
        return user

    def get_urls(self):
        return patterns(
            "",
            url(r"^$", self.login_view.as_view(client=self), name="simple-sso-login"),
            url(r"^authenticate/$", self.authenticate_view.as_view(client=self), name="simple-sso-authenticate"),
        )
Esempio n. 2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('public_key')
    parser.add_argument('private_key')
    parser.add_argument('name')
    parser.add_argument('--host', default='http://localhost:8000/')
    args = parser.parse_args()
    consumer = SyncConsumer(args.host, args.public_key, args.private_key)
    print consumer.consume('/', {'name': args.name})['greeting']
Esempio n. 3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('public_key')
    parser.add_argument('private_key')
    parser.add_argument('name')
    parser.add_argument('--host', default='http://localhost:8000/')
    args = parser.parse_args()
    consumer = SyncConsumer(args.host, args.public_key, args.private_key)
    print consumer.consume('/', {'name': args.name})['greeting']
Esempio n. 4
0
class Client(object):
    login_view = LoginView
    authenticate_view = AuthenticateView
    backend = "%s.%s" % (ModelBackend.__module__, ModelBackend.__name__)
    user_extra_data = None

    def __init__(self, server_url, public_key, private_key,
                 user_extra_data=None):
        self.server_url = server_url
        self.public_key = public_key
        self.private_key = private_key
        self.consumer = SyncConsumer(self.server_url, self.public_key, self.private_key)
        if user_extra_data:
            self.user_extra_data = user_extra_data

    @classmethod
    def from_dsn(cls, dsn):
        parse_result = urlparse(dsn)
        public_key = parse_result.username
        private_key = parse_result.password
        netloc = parse_result.hostname
        if parse_result.port:
            netloc += ':%s' % parse_result.port
        server_url = urlunparse((
            parse_result.scheme, netloc, parse_result.path, parse_result.params, parse_result.query,
            parse_result.fragment))
        return cls(server_url, public_key, private_key)

    def get_request_token(self, redirect_to):
        return self.consumer.consume('/request-token/', {'redirect_to': redirect_to})['request_token']

    def get_user(self, access_token):
        data = {'access_token': access_token}
        if self.user_extra_data:
            data['extra_data'] = self.user_extra_data
        user_data = self.consumer.consume('/verify/', data)
        user = self.build_user(user_data)
        return user

    def build_user(self, user_data):
        try:
            user = User.objects.get(username=user_data['username'])
        except User.DoesNotExist:
            user = User(**user_data)
        # user.set_unusable_password()
        user.save()
        return user

    def get_login_urls(self):
        return [
            url(r'^$', self.login_view.as_view(client=self), name='zjxl-sso-login'),
            url(r'^authenticate/$', self.authenticate_view.as_view(client=self), name='zjxl-sso-authenticate'),
        ]
Esempio n. 5
0
class Client(object):
    login_view = LoginView
    authenticate_view = AuthenticateView
    backend = "%s.%s" % (ModelBackend.__module__, ModelBackend.__name__)

    def __init__(self, server_url, public_key, private_key):
        self.server_url = server_url
        self.public_key = public_key
        self.private_key = private_key
        self.consumer = SyncConsumer(self.server_url, self.public_key, self.private_key)

    @classmethod
    def from_dsn(cls, dsn):
        parse_result = urlparse.urlparse(dsn)
        public_key = parse_result.username
        private_key = parse_result.password
        netloc = parse_result.hostname
        if parse_result.port:
            netloc += ':%s' % parse_result.port
        server_url = urlparse.urlunparse((parse_result.scheme, netloc, parse_result.path, parse_result.params, parse_result.query, parse_result.fragment))
        return cls(server_url, public_key, private_key)

    def get_request_token(self, redirect_to):
        return self.consumer.consume('/request-token/', {'redirect_to': redirect_to})['request_token']

    def get_user(self, access_token):
        user_data = self.consumer.consume('/verify/', {'access_token': access_token})
        user = self.build_user(user_data)
        return user

    def build_user(self, user_data):
        try:
            user = User.objects.get(username=user_data['username'])
        except User.DoesNotExist:
            user = User(**user_data)
        user.set_unusable_password()
        user.save()
        return user

    def get_urls(self):
        return patterns('',
            url(r'^$', self.login_view.as_view(client=self), name='simple-sso-login'),
            url(r'^authenticate/$', self.authenticate_view.as_view(client=self), name='simple-sso-authenticate'),
        )