Beispiel #1
0
 def test_raise_http_request(self, urlopen):
     error = http.HTTPError('http://example.com/', 404, 'Not Found', None,
                            None)
     error.read = lambda: b'o'
     urlopen.side_effect = error
     resp, content = OAuthRemoteApp.http_request('http://example.com')
     assert resp.code == 404
     assert b'o' in content
Beispiel #2
0
    def get_user_details(self, provider: OAuthRemoteApp) -> Tuple[str, dict]:
        """
        For the given ``provider``, return the user's email address and any
        extra data to create the user model with.
        """
        if provider.name == 'amazon':
            data = provider.get('/user/profile').data
            return data['email'], {}

        elif provider.name == 'github':
            data = provider.get('/user').data
            return data['email'], {}
        elif provider.name == 'gitlab':
            data = provider.get('user').data
            return data['email'], {}

        raise NotImplementedError(f'Unknown OAuth remote app: {provider.name}')
Beispiel #3
0
    def test_http_request(self, urlopen):
        urlopen.return_value = Response(b'{"foo": "bar"}',
                                        headers={'status-code': 200})

        resp, content = OAuthRemoteApp.http_request('http://example.com')
        assert resp.code == 200
        assert b'foo' in content

        resp, content = OAuthRemoteApp.http_request(
            'http://example.com/', method='GET', data={'wd': 'flask-oauthlib'})
        assert resp.code == 200
        assert b'foo' in content

        resp, content = OAuthRemoteApp.http_request(
            'http://example.com/', data={'wd': 'flask-oauthlib'})
        assert resp.code == 200
        assert b'foo' in content
 def test_raise_http_request(self, urlopen):
     error = http.HTTPError(
         'http://example.com/', 404, 'Not Found', None, None
     )
     error.read = lambda: b'o'
     urlopen.side_effect = error
     resp, content = OAuthRemoteApp.http_request('http://example.com')
     assert resp.code == 404
     assert b'o' in content
    def test_http_request(self, urlopen):
        urlopen.return_value = Response(
            b'{"foo": "bar"}', headers={'status-code': 200}
        )

        resp, content = OAuthRemoteApp.http_request('http://example.com')
        assert resp.code == 200
        assert b'foo' in content

        resp, content = OAuthRemoteApp.http_request(
            'http://example.com/',
            method='GET',
            data={'wd': 'flask-oauthlib'}
        )
        assert resp.code == 200
        assert b'foo' in content

        resp, content = OAuthRemoteApp.http_request(
            'http://example.com/',
            data={'wd': 'flask-oauthlib'}
        )
        assert resp.code == 200
        assert b'foo' in content
    def test_raise_http_request(self, urlopen):
        error = http.HTTPError('http://example.com/', 404, 'Not Found', None,
                               None)
        error.read = lambda: b'o'

        class _Fake(object):
            def close(self):
                return 0

        class _Faker(object):
            _closer = _Fake()

        error.file = _Faker()

        urlopen.side_effect = error
        resp, content = OAuthRemoteApp.http_request('http://example.com')
        assert resp.code == 404
        assert b'o' in content
    def test_raise_http_request(self, urlopen):
        error = http.HTTPError(
            'http://example.com/', 404, 'Not Found', None, None
        )
        error.read = lambda: b'o'

        class _Fake(object):
            def close(self):
                return 0

        class _Faker(object):
            _closer = _Fake()

        error.file = _Faker()

        urlopen.side_effect = error
        resp, content = OAuthRemoteApp.http_request('http://example.com')
        assert resp.code == 404
        assert b'o' in content
Beispiel #8
0
 def __init__(self, oauth, name, **kwargs):
     # constructor expects some values, so make it happy..
     kwargs['consumer_key'] = 'not-needed-here'
     kwargs['consumer_secret'] = 'not-needed-here'
     OAuthRemoteApp.__init__(self, oauth, name, **kwargs)
Beispiel #9
0
 def test_not_raise_init(self):
     OAuthRemoteApp('oauth', 'twitter', app_key='foo')
Beispiel #10
0
 def test_raise_init(self):
     OAuthRemoteApp('oauth', 'twitter')
Beispiel #11
0
 def __init__(self, id, module, install, *args, **kwargs):
     BaseRemoteApp.__init__(self, None, **kwargs)
     self.id = id
     self.module = module
Beispiel #12
0
 def __init__(self, id, module, install, *args, **kwargs):
     BaseRemoteApp.__init__(self, None, **kwargs)
     self.id = id
     self.module = module
 def __init__(self, oauth, name, **kwargs):
     # constructor expects some values, so make it happy..
     kwargs['consumer_key'] = 'not-needed-here'
     kwargs['consumer_secret'] = 'not-needed-here'
     OAuthRemoteApp.__init__(self, oauth, name, **kwargs)
Beispiel #14
0
 def __init__(self, app):
     self.app = app
     self.remote = OAuthRemoteApp(self, self.name, **self.config)
     self.key = '%s_oauthtok' % self.name
Beispiel #15
0
class OAuthProvider(Provider):

    name = ""
    always_callback = False
    config = {}

    def __init__(self, app):
        self.app = app
        self.remote = OAuthRemoteApp(self, self.name, **self.config)
        self.key = '%s_oauthtok' % self.name

    def register(self, doc):
        callback = None
        if request.args.get('next') or self.always_callback:
            callback = url_for('verify', hashkey=doc["_id"],
                               _external=True,
                               next=request.args.get('next') or "/v1/verified")

        resp = self.remote.authorize(callback=callback)

        if self.key in session:
            doc[self.key] = session.pop(self.key)

        return {"goto": resp.headers['Location']}

    def verify(self, doc):

        if self.key in doc:
            session[self.key] = doc.pop(self.key)

        if 'oauth_verifier' in request.args:
            try:
                data = self.remote.handle_oauth1_response()
            except OAuthException as e:
                if self.app.debug: raise
                return json_exception(e)
        elif 'code' in request.args:
            try:
                data = self.remote.handle_oauth2_response()
            except OAuthException as e:
                if self.app.debug: raise
                return json_exception(e)
        else:
            return json_error(400, "missing_code",
                    "You need to provide either oauth_verifier or code")

        print data

        def getter():
            return data

        self.remote.tokengetter(getter)

        try:
            if not self.confirm(doc, data):
                return json_error(400, "wrong_user",
                        "The user doesn't match. Sorry")
        except Exception, e:
            if self.app.debug: raise
            return json_exception(e, 500)

        doc["status"] = "confirmed"