class TokenManager(object): def __init__(self, token_endpoint, client_id, client_secret, http_options=None): self._token_endpoint = token_endpoint self._client_id = client_id self._client_secret = client_secret self._token = Token() self._http_options = http_options if http_options else {} self._http_client = AsyncHTTPClient() def _has_token(self): return self._token.is_valid() @gen.coroutine def get_token(self): if not self._has_token(): yield self._update_token() raise gen.Return(self._token.access_token) @gen.coroutine def _get_token_data(self): token_data = yield self._request_token() raise gen.Return(token_data) def reset_token(self): self._token = Token() @gen.coroutine def _update_token(self): token_data = yield self._get_token_data() self._token = Token(token_data.get('access_token', ''), token_data.get('expires_in', 0)) @gen.coroutine def _request_token(self): if not self._token_endpoint: raise TokenError('Missing token endpoint') token_data = yield self._fetch( url=self._token_endpoint, method="POST", auth=(self._client_id, self._client_secret), data={'grant_type': 'client_credentials'} ) raise gen.Return(token_data) @gen.coroutine def _fetch(self, url, method="GET", data=None, auth=None): if type(data) == dict: data = urlencode(data) request_data = dict( url=url, headers={}, method=method, body=data ) if auth is not None: try: passhash = b64encode(':'.join(auth).encode('ascii')) except TypeError as e: raise TokenError('Missing credentials (client_id:client_secret)', str(e)) request_data['headers']['Authorization'] = 'Basic %s' % passhash request_data.update(self._http_options) request = HTTPRequest(**request_data) logging.info('request:%s %s\n%s\n%s' % (request.method, request.url, request.headers, request.body)) try: response = yield self._http_client.fetch(request) except HTTPError as e: raise TokenError('Failed to request token', str(e)) result = json.loads(response.body) raise gen.Return(result)
def test_should_know_when_it_has_expired(self): token = Token(access_token='access_token', expires_in=0) self.assertFalse(token.is_valid())
def test_should_know_when_it_is_valid(self): token = Token(access_token='access_token', expires_in=10) self.assertTrue(token.is_valid())
class TokenManager(object): def __init__(self, token_endpoint, client_id, client_secret, http_options=None): self._token_endpoint = token_endpoint self._client_id = client_id self._client_secret = client_secret self._token = Token() self._http_options = http_options if http_options else {} self._http_client = AsyncHTTPClient() def _has_token(self): return self._token.is_valid() @gen.coroutine def get_token(self): if not self._has_token(): yield self._update_token() raise gen.Return(self._token.access_token) @gen.coroutine def _get_token_data(self): token_data = yield self._request_token() raise gen.Return(token_data) def reset_token(self): self._token = Token() @gen.coroutine def _update_token(self): token_data = yield self._get_token_data() self._token = Token(token_data.get('access_token', ''), token_data.get('expires_in', 0)) @gen.coroutine def _request_token(self): if not self._token_endpoint: raise TokenError('Missing token endpoint') token_data = yield self._fetch( url=self._token_endpoint, method="POST", auth=(self._client_id, self._client_secret), data={'grant_type': 'client_credentials'}) raise gen.Return(token_data) @gen.coroutine def _fetch(self, url, method="GET", data=None, auth=None): if type(data) == dict: data = urlencode(data) request_data = dict(url=url, headers={}, method=method, body=data) if auth is not None: try: passhash = b64encode(':'.join(auth).encode('ascii')) except TypeError as e: raise TokenError( 'Missing credentials (client_id:client_secret)', str(e)) request_data['headers']['Authorization'] = ( 'Basic %s' % passhash.decode('utf-8')) request_data.update(self._http_options) request = HTTPRequest(**request_data) logger.debug('Request: %s %s', request.method, request.url) for header in request.headers: logger.debug('Header %s: %s', header, request.headers[header]) try: response = yield self._http_client.fetch(request) except HTTPError as e: raise TokenHTTPError('Failed to request token', e.response) result = json.loads(response.body.decode("utf-8")) raise gen.Return(result)
class TokenManager(object): def __init__(self, token_endpoint, client_id, client_secret): self._token_endpoint = token_endpoint self._client_id = client_id self._client_secret = client_secret self._token = Token() self._http_client = AsyncHTTPClient() def _has_token(self): return self._token.is_valid() @gen.coroutine def get_token(self): if not self._has_token(): yield self._update_token() raise gen.Return(self._token.access_token) @gen.coroutine def _get_token_data(self): token_data = yield self._request_token() raise gen.Return(token_data) def reset_token(self): self._token = Token() @gen.coroutine def _update_token(self): token_data = yield self._get_token_data() self._token = Token(token_data.get('access_token', ''), token_data.get('expires_in', 0)) @gen.coroutine def _request_token(self): token_data = yield self._fetch( url=self._token_endpoint, method="POST", auth=(self._client_id, self._client_secret), data={'grant_type': 'client_credentials'} ) raise gen.Return(token_data) @gen.coroutine def _fetch(self, url, method="GET", data=None, auth=None): if type(data) == dict: data = urlencode(data) request_data = dict( url=url, # headers={'Content-Type': 'application/x-www-form-urlencoded'}, headers={}, method=method, body=data ) if auth is not None: passhash = b64encode(':'.join(auth)) request_data['headers']['Authorization'] = 'Basic %s' % passhash request = HTTPRequest(**request_data) print "request:%s %s\n%s\n%s" % (request.method, request.url, request.headers, request.body) try: response = yield self._http_client.fetch(request) except HTTPError, e: raise TokenError('Failed to request token', e) result = json.loads(response.body) raise gen.Return(result)
class TokenManager(object): def __init__(self, token_endpoint, client_id, client_secret): self._token_endpoint = token_endpoint self._client_id = client_id self._client_secret = client_secret self._token = Token() self._http_client = AsyncHTTPClient() def _has_token(self): return self._token.is_valid() @gen.coroutine def get_token(self): if not self._has_token(): yield self._update_token() raise gen.Return(self._token.access_token) @gen.coroutine def _get_token_data(self): token_data = yield self._request_token() raise gen.Return(token_data) def reset_token(self): self._token = Token() @gen.coroutine def _update_token(self): token_data = yield self._get_token_data() self._token = Token(token_data.get('access_token', ''), token_data.get('expires_in', 0)) @gen.coroutine def _request_token(self): token_data = yield self._fetch( url=self._token_endpoint, method="POST", auth=(self._client_id, self._client_secret), data={'grant_type': 'client_credentials'}) raise gen.Return(token_data) @gen.coroutine def _fetch(self, url, method="GET", data=None, auth=None): if type(data) == dict: data = urlencode(data) request_data = dict( url=url, # headers={'Content-Type': 'application/x-www-form-urlencoded'}, headers={}, method=method, body=data) if auth is not None: passhash = b64encode(':'.join(auth)) request_data['headers']['Authorization'] = 'Basic %s' % passhash request = HTTPRequest(**request_data) print "request:%s %s\n%s\n%s" % (request.method, request.url, request.headers, request.body) try: response = yield self._http_client.fetch(request) except HTTPError, e: raise TokenError('Failed to request token', e) result = json.loads(response.body) raise gen.Return(result)