{"id":15297504,"url":"https://github.com/antechrestos/oauth2client","last_synced_at":"2025-10-28T01:19:19.790Z","repository":{"id":57447528,"uuid":"53609148","full_name":"antechrestos/OAuth2Client","owner":"antechrestos","description":"A library based on requests that handle client oauth2 process. ","archived":false,"fork":false,"pushed_at":"2025-01-05T13:29:11.000Z","size":78,"stargazers_count":16,"open_issues_count":0,"forks_count":11,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-05T20:07:41.582Z","etag":null,"topics":["oauth2","oauth2-client","python","python-2","python3","requests"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antechrestos.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-10T18:55:42.000Z","updated_at":"2025-01-05T13:28:55.000Z","dependencies_parsed_at":"2024-06-18T21:27:16.527Z","dependency_job_id":"2f901a18-966b-4b3b-949e-328f2440f439","html_url":"https://github.com/antechrestos/OAuth2Client","commit_stats":{"total_commits":60,"total_committers":4,"mean_commits":15.0,"dds":"0.19999999999999996","last_synced_commit":"a64ec6dcefc14a49c160059b6bdddc3779220019"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antechrestos%2FOAuth2Client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antechrestos%2FOAuth2Client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antechrestos%2FOAuth2Client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antechrestos%2FOAuth2Client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antechrestos","download_url":"https://codeload.github.com/antechrestos/OAuth2Client/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393570,"owners_count":20931812,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["oauth2","oauth2-client","python","python-2","python3","requests"],"created_at":"2024-09-30T19:17:57.463Z","updated_at":"2025-10-28T01:19:14.758Z","avatar_url":"https://github.com/antechrestos.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"OAuth2Client\n============\n.. image:: https://img.shields.io/pypi/v/oauth2-client.svg\n    :target: https://pypi.python.org/pypi/oauth2-client\n\n.. image:: https://img.shields.io/github/license/antechrestos/Oauth2Client.svg\n    :target: https://raw.githubusercontent.com/antechrestos/OAuth2Client/master/LICENSE\n\n\nPresentation\n------------\n\nOAuth2Client is a simple python client library for OAuth2. It is based on the requests_\n    .. _requests: https://pypi.python.org/pypi/requests/\n\n\n:warning: Starting version `1.2.0`, versions older that python `3.6.0` will not be supported anymore. This late version was released by the end 2016.\n\nFor those that are still using python 2.7, it won't be supported by the end of 2020 and all library shall stop supporting it.\n\nLogin process\n-------------\nFor now it can handle two token process:\n\n* Authorization code\n* User Credentials\n* Client Credentials\n\nAuthorization code\n~~~~~~~~~~~~~~~~~~\nSince authorization code process needs the user to accept the access to its data by the application, the library\nstarts locally a http server. You may put the host part of the ``redirect_uri`` parameter in your *hosts* file\npointing to your loop-back address. The server waits a ``GET`` requests with the  ``code`` as a query parameter.\n\nGetting a couple of access token may be done like this:\n\n.. code-block:: python\n\n    scopes = ['scope_1', 'scope_2']\n\n    service_information = ServiceInformation('https://authorization-server/oauth/authorize',\n                                             'https://token-server/oauth/token',\n                                             'client_id',\n                                             'client_secret',\n                                              scopes)\n    manager = CredentialManager(service_information,\n                                proxies=dict(http='http://localhost:3128', https='http://localhost:3128'))\n    redirect_uri = 'http://somewhere.io:8080/oauth/code'\n\n    # Builds the authorization url and starts the local server according to the redirect_uri parameter\n    url = manager.init_authorize_code_process(redirect_uri, 'state_test')\n    _logger.info('Open this url in your browser\\n%s', url)\n\n    code = manager.wait_and_terminate_authorize_code_process()\n    # From this point the http server is opened on 8080 port and wait to receive a single GET request\n    # All you need to do is open the url and the process will go on\n    # (as long you put the host part of your redirect uri in your host file)\n    # when the server gets the request with the code (or error) in its query parameters\n    _logger.debug('Code got = %s', code)\n    manager.init_with_authorize_code(redirect_uri, code)\n    _logger.debug('Access got = %s', manager._access_token)\n    # Here access and refresh token may be used with self.refresh_token\n\nAuthorization code with Proof Key for Code Exchange (PKCE)\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nIn case you can generate a couple of code verifier and code challenge as follows:\n\n.. code-block:: python\n\n    import base64\n    import hashlib\n    import logging\n    import secrets\n    from typing import Tuple\n\n    def generate_sha256_pkce(length: int) -\u003e Tuple[str, str]:\n        if not (43 \u003c= length \u003c= 128):\n            raise Exception(\"Invalid length: \" % str(length))\n        verifier = secrets.token_urlsafe(length)\n        encoded = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode('ascii')).digest())\n        challenge = encoded.decode('ascii')[:-1]\n        return verifier, challenge\n\n\nThen you can init authorization code workflow as follows\n\n.. code-block:: python\n\n    code_verifier, code_challenge = generate_sha256_pkce(64)\n    url = manager.init_authorize_code_process(redirect_uri, 'state_test',\n                                              code_challenge=code_challenge,\n                                              code_challenge_method=\"S256\")\n\n\nor either generate the url\n\n.. code-block:: python\n\n    url = manager.generate_authorize_url(redirect_uri, 'state_test',\n                                         code_challenge=code_challenge,\n                                         code_challenge_method=\"S256\")\n\n\nAnd once you obtains the ``code`` exchange it as follows\n\n.. code-block:: python\n\n    manager.init_with_authorize_code(redirect_uri, code, code_verifier=code_verifier)\n\n\nUser credentials\n~~~~~~~~~~~~~~~~\nGetting a couple of access and refresh token is much easier:\n\n.. code-block:: python\n\n    scopes = ['scope_1', 'scope_2']\n\n    service_information = ServiceInformation('https://authorization-server/oauth/authorize',\n                                             'https://token-server/oauth/token',\n                                             'client_id',\n                                             'client_secret',\n                                              scopes)\n    manager = CredentialManager(service_information,\n                                proxies=dict(http='http://localhost:3128', https='http://localhost:3128'))\n    manager.init_with_user_credentials('login', 'password')\n    _logger.debug('Access got = %s', manager._access_token)\n    # Here access and refresh token may be used\n\nClient credentials\n~~~~~~~~~~~~~~~~~~\nYou can also get a token with client credentials process\n\n.. code-block:: python\n\n    manager = CredentialManager(service_information,\n                                proxies=dict(http='http://localhost:3128', https='http://localhost:3128'))\n    manager.init_with_client_credentials()\n    # here application admin operation may be called\n\nRefresh token\n~~~~~~~~~~~~~\nProvided that you kept a previous ``refresh_token``, you can initiate your credential manager with it:\n\n.. code-block:: python\n\n    manager = CredentialManager(service_information,\n                                proxies=dict(http='http://localhost:3128', https='http://localhost:3128'))\n    manager.init_with_token('my saved refreshed token')\n\nToken expiration\n~~~~~~~~~~~~~~~~\n``CredentialManager`` class handle token expiration by calling the ``CredentialManager._is_token_expired`` static method.\nThis implementation is not accurate for all OAuth server implementation. You'd better extend  ``CredentialManager`` class\nand override ``_is_token_expired`` method.\n\nRead other fields from token response\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n``CredentialManager`` can be subclassed to handle other token response fields such as ``id_token`` in OpenId protocol.\n\n.. code-block:: python\n\n    class OpenIdCredentialManager(CredentialManager):\n        def __init__(self, service_information, proxies=None):\n            super(OpenIdCredentialManager, self).__init__(service_information, proxies)\n            self.id_token = None\n\n        def _process_token_response(self,  token_response, refresh_token_mandatory):\n            id_token = token_response.get('id_token')\n            OpenIdCredentialManager._check_id(id_token)\n            super(OpenIdCredentialManager, self)._process_token_response(token_response, refresh_token_mandatory)\n            self.id_token = id_token\n\n        @staticmethod\n        def _check_id(id_token):\n            # check that open id token is valid\n            pass\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantechrestos%2Foauth2client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantechrestos%2Foauth2client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantechrestos%2Foauth2client/lists"}