예제 #1
0
파일: auth.py 프로젝트: CKiilu/rides
def revoke_access_token(credential):
    """Revoke an access token.

    All future requests with the access token will be invalid.

    Parameters
        credential (OAuth2Credential)
            An authorized user's OAuth 2.0 credentials.

    Raises
        ClientError (APIError)
            Thrown if there was an HTTP error.
    """
    url = build_url(auth.AUTH_HOST, auth.REVOKE_PATH)

    args = {
        'token': credential.access_token,
        'client_id': credential.client_id,
        'client_secret': credential.client_secret,
    }

    response = post(url=url, params=args)

    if response.status_code == codes.ok:
        return

    message = 'Failed to revoke access token: {}.'
    message = message.format(response.reason)
    raise ClientError(response, message)
예제 #2
0
파일: request.py 프로젝트: CKiilu/rides
    def _prepare(self):
        """Builds a URL and return a PreparedRequest.

        Returns
            (requests.PreparedRequest)

        Raises
            UberIllegalState (APIError)
        """
        if self.method not in http.ALLOWED_METHODS:
            raise UberIllegalState('Unsupported HTTP Method.')

        api_host = self.api_host
        headers = self._build_headers(self.method, self.auth_session)
        url = build_url(api_host, self.path)
        data, params = generate_data(self.method, self.args)

        return generate_prepared_request(
            self.method,
            url,
            headers,
            data,
            params,
            self.handlers,
        )
예제 #3
0
    def _prepare(self):
        """Builds a URL and return a PreparedRequest.

        Returns
            (requests.PreparedRequest)

        Raises
            UberIllegalState (APIError)
        """
        if self.method not in http.ALLOWED_METHODS:
            raise UberIllegalState('Unsupported HTTP Method.')

        api_host = self.api_host
        headers = self._build_headers(self.method, self.auth_session)
        url = build_url(api_host, self.path)
        data, params = generate_data(self.method, self.args)

        return generate_prepared_request(
            self.method,
            url,
            headers,
            data,
            params,
            self.handlers,
        )
예제 #4
0
파일: auth.py 프로젝트: CKiilu/rides
    def _build_authorization_request_url(
        self,
        response_type,
        redirect_url,
        state=None
    ):
        """Form URL to request an auth code or access token.

        Parameters
            response_type (str)
                Either 'code' (Authorization Code Grant) or
                'token' (Implicit Grant)
            redirect_url (str)
                The URL that the Uber server will redirect the user to after
                finishing authorization. The redirect must be HTTPS-based and
                match the URL you registered your application with. Localhost
                URLs are permitted and can be either HTTP or HTTPS.
            state (str)
                Optional CSRF State token to send to server.

        Returns
            (str)
                The fully constructed authorization request URL.

        Raises
            UberIllegalState (ApiError)
                Raised if response_type parameter is invalid.
        """
        if response_type not in auth.VALID_RESPONSE_TYPES:
            message = '{} is not a valid response type.'
            raise UberIllegalState(message.format(response_type))

        args = OrderedDict([
            ('scope', ' '.join(self.scopes)),
            ('state', state),
            ('redirect_uri', redirect_url),
            ('response_type', response_type),
            ('client_id', self.client_id),
        ])

        return build_url(auth.AUTH_HOST, auth.AUTHORIZE_PATH, args)
예제 #5
0
    def _build_authorization_request_url(
        self,
        response_type,
        redirect_url,
        state=None
    ):
        """Form URL to request an auth code or access token.

        Parameters
            response_type (str)
                Either 'code' (Authorization Code Grant) or
                'token' (Implicit Grant)
            redirect_url (str)
                The URL that the Uber server will redirect the user to after
                finishing authorization. The redirect must be HTTPS-based and
                match the URL you registered your application with. Localhost
                URLs are permitted and can be either HTTP or HTTPS.
            state (str)
                Optional CSRF State token to send to server.

        Returns
            (str)
                The fully constructed authorization request URL.

        Raises
            UberIllegalState (ApiError)
                Raised if response_type parameter is invalid.
        """
        if response_type not in auth.VALID_RESPONSE_TYPES:
            message = '{} is not a valid response type.'
            raise UberIllegalState(message.format(response_type))

        args = OrderedDict([
            ('scope', ' '.join(self.scopes)),
            ('state', state),
            ('redirect_uri', redirect_url),
            ('response_type', response_type),
            ('client_id', self.client_id),
        ])

        return build_url(auth.AUTH_HOST, auth.AUTHORIZE_PATH, args)
예제 #6
0
def test_build_url_no_params():
    """Build URL with no parameters."""
    url = build_url(HOST, DEFAULT_TARGET)
    assert url == DEFAULT_BASE_URL
예제 #7
0
def test_build_url_params(default_http_arguments_as_json):
    """Build URL with querystring parameters."""
    url = build_url(HOST, DEFAULT_TARGET, default_http_arguments_as_json)
    url_with_params = '{}?latitude={}&longitude={}'
    assert url == url_with_params.format(DEFAULT_BASE_URL, LAT, LNG)
예제 #8
0
def test_build_special_char_url():
    """Build URL special characters."""
    url = build_url(HOST, SPECIAL_CHAR_TARGET)
    assert url == 'https://api.uber.com/%7Eproducts'
예제 #9
0
def test_build_url_with_scheme():
    """Build URL with https scheme."""
    url = build_url(HTTPS_HOST, DEFAULT_TARGET)
    assert url == DEFAULT_BASE_URL
예제 #10
0
def _request_access_token(grant_type,
                          client_id=None,
                          client_secret=None,
                          scopes=None,
                          code=None,
                          redirect_url=None,
                          refresh_token=None):
    """Make an HTTP POST to request an access token.

    Parameters
        grant_type (str)
            Either 'client_credientials' (Client Credentials Grant)
            or 'authorization_code' (Authorization Code Grant).
        client_id (str)
            Your app's Client ID.
        client_secret (str)
            Your app's Client Secret.
        scopes (set)
            Set of permission scopes to request.
            (e.g. {'profile', 'history'})
        code (str)
            The authorization code to switch for an access token.
            Only used in Authorization Code Grant.
        redirect_url (str)
            The URL that the Uber server will redirect to.
        refresh_token (str)
            Refresh token used to get a new access token.
            Only used for Authorization Code Grant.

    Returns
        (requests.Response)
            Successful HTTP response from a 'POST' to request
            an access token.

    Raises
        ClientError (APIError)
            Thrown if there was an HTTP error.
    """
    url = build_url(auth.AUTH_HOST, auth.ACCESS_TOKEN_PATH)

    if isinstance(scopes, set):
        scopes = ' '.join(scopes)

    args = {
        'grant_type': grant_type,
        'client_id': client_id,
        'client_secret': client_secret,
        'scope': scopes,
        'code': code,
        'redirect_uri': redirect_url,
        'refresh_token': refresh_token,
    }

    response = post(url=url, data=args)

    if response.status_code == codes.ok:
        return response

    message = 'Failed to request access token: {}.'
    message = message.format(response.reason)
    raise ClientError(response, message)
예제 #11
0
	if DEBUG:
        	out_log.write(msg)

        try:

		query = "select uber_code from uber.access_token where uber_access_token is null";
		query_result = [i for i in execute_query_db(query)]
		uber_code = []
		for p in query_result: uber_code.append(p[0])
		
		for i in range(len(uber_code)):

			print uber_code[i]
			print i
		
			url = build_url(auth.AUTH_HOST, auth.ACCESS_TOKEN_PATH)


			args = {
	    		'grant_type': GRANT_TYPE,
	    		'client_id': CLIENT_ID,
	    		'client_secret': CLIENT_SECRET,    
	    		'code': uber_code[i],
	    		'redirect_uri': REDIRECT_URI,
			}

			response = post(url=url, data=args)
						
			#print response.status_code
			#print response.reason
			#print response.text
예제 #12
0
def test_build_url_no_params():
    """Build URL with no parameters."""
    url = build_url(HOST, DEFAULT_TARGET)
    assert url == DEFAULT_BASE_URL
예제 #13
0
def test_build_url_params(default_http_arguments_as_json):
    """Build URL with querystring parameters."""
    url = build_url(HOST, DEFAULT_TARGET, default_http_arguments_as_json)
    url_with_params = '{}?latitude={}&longitude={}'
    assert url == url_with_params.format(DEFAULT_BASE_URL, LAT, LNG)
예제 #14
0
def test_build_special_char_url():
    """Build URL special characters."""
    url = build_url(HOST, SPECIAL_CHAR_TARGET)
    assert url == 'https://api.uber.com/%7Eproducts'
예제 #15
0
def test_build_url_with_scheme():
    """Build URL with https scheme."""
    url = build_url(HTTPS_HOST, DEFAULT_TARGET)
    assert url == DEFAULT_BASE_URL
예제 #16
0
파일: auth.py 프로젝트: CKiilu/rides
def _request_access_token(
    grant_type,
    client_id=None,
    client_secret=None,
    scopes=None,
    code=None,
    redirect_url=None,
    refresh_token=None
):
    """Make an HTTP POST to request an access token.

    Parameters
        grant_type (str)
            Either 'client_credientials' (Client Credentials Grant)
            or 'authorization_code' (Authorization Code Grant).
        client_id (str)
            Your app's Client ID.
        client_secret (str)
            Your app's Client Secret.
        scopes (set)
            Set of permission scopes to request.
            (e.g. {'profile', 'history'})
        code (str)
            The authorization code to switch for an access token.
            Only used in Authorization Code Grant.
        redirect_url (str)
            The URL that the Uber server will redirect to.
        refresh_token (str)
            Refresh token used to get a new access token.
            Only used for Authorization Code Grant.

    Returns
        (requests.Response)
            Successful HTTP response from a 'POST' to request
            an access token.

    Raises
        ClientError (APIError)
            Thrown if there was an HTTP error.
    """
    url = build_url(auth.AUTH_HOST, auth.ACCESS_TOKEN_PATH)

    if isinstance(scopes, set):
        scopes = ' '.join(scopes)

    args = {
        'grant_type': grant_type,
        'client_id': client_id,
        'client_secret': client_secret,
        'scope': scopes,
        'code': code,
        'redirect_uri': redirect_url,
        'refresh_token': refresh_token,
    }

    response = post(url=url, data=args)

    if response.status_code == codes.ok:
        return response

    message = 'Failed to request access token: {}.'
    message = message.format(response.reason)
    raise ClientError(response, message)