Skip to content

AuthClient no longer unconditionally makes http request on initialization #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions intuitlib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,18 @@
get_auth_header,
send_request,
)
from intuitlib.config import (
OAUTH2_ISSUER,
OAUTH2_AUTH_ENDPOINT,
OAUTH2_TOKEN_ENDPOINT,
OAUTH2_REV_ENDPOINT,
OAUTH2_JWKS_URI,
OAUTH2_SANDBOX,
OAUTH2_PROD,
)

class AuthClient(requests.Session):
"""Handles OAuth 2.0 and OpenID Connect flows to get access to User Info API, Accounting APIs and Payments APIs
"""
"""Handles OAuth 2.0 and OpenID Connect flows to get access to User Info API, Accounting APIs and Payments APIs"""

def __init__(self, client_id, client_secret, redirect_uri, environment, state_token=None, access_token=None, refresh_token=None, id_token=None, realm_id=None):
"""Constructor for AuthClient
Expand All @@ -52,14 +60,16 @@ def __init__(self, client_id, client_secret, redirect_uri, environment, state_to
self.environment = environment
self.state_token = state_token

# Discovery doc contains endpoints based on environment specified
discovery_doc = get_discovery_doc(self.environment, session=self)
self.auth_endpoint = discovery_doc['authorization_endpoint']
self.token_endpoint = discovery_doc['token_endpoint']
self.revoke_endpoint = discovery_doc['revocation_endpoint']
self.issuer_uri = discovery_doc['issuer']
self.jwks_uri = discovery_doc['jwks_uri']
self.user_info_url = discovery_doc['userinfo_endpoint']
# OAUTH2 constant endpoints
self.auth_endpoint = OAUTH2_AUTH_ENDPOINT
self.token_endpoint = OAUTH2_TOKEN_ENDPOINT
self.revoke_endpoint = OAUTH2_REV_ENDPOINT
self.issuer_uri = OAUTH2_ISSUER
self.jwks_uri = OAUTH2_JWKS_URI
if self.environment.lower() in {'production', 'prod'}:
self.user_info_url = OAUTH2_PROD
else:
self.user_info_url = OAUTH2_SANDBOX

# response values
self.realm_id = realm_id
Expand All @@ -68,17 +78,27 @@ def __init__(self, client_id, client_secret, redirect_uri, environment, state_to
self.refresh_token = refresh_token
self.x_refresh_token_expires_in = None
self.id_token = id_token

def setAuthorizeURLs(self, urlObject):

def discover_authorize_urls(self):
"""Set authorization url using discovery doc based on specified environment"""
discovery_doc = get_discovery_doc(self.environment, session=self)
self.auth_endpoint = discovery_doc['authorization_endpoint']
self.token_endpoint = discovery_doc['token_endpoint']
self.revoke_endpoint = discovery_doc['revocation_endpoint']
self.issuer_uri = discovery_doc['issuer']
self.jwks_uri = discovery_doc['jwks_uri']
self.user_info_url = discovery_doc['userinfo_endpoint']

def set_authorize_urls(self, url_obj):
"""Set authorization url using custom values passed in the data dict
:param **data: data dict for custom authorizationURLS
:return: self
:return: None
"""
if urlObject is not None:
self.auth_endpoint = urlObject['auth_endpoint']
self.token_endpoint = urlObject['token_endpoint']
self.revoke_endpoint = urlObject['revoke_endpoint']
self.user_info_url = urlObject['user_info_url']
if url_obj is not None:
self.auth_endpoint = url_obj['auth_endpoint']
self.token_endpoint = url_obj['token_endpoint']
self.revoke_endpoint = url_obj['revoke_endpoint']
self.user_info_url = url_obj['user_info_url']
return None

def get_authorization_url(self, scopes, state_token=None):
Expand Down
10 changes: 9 additions & 1 deletion intuitlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
'production': 'https://developer.intuit.com/.well-known/openid_configuration/',
}

OAUTH2_ISSUER = "https://oauth.platform.intuit.com/op/v1"
OAUTH2_AUTH_ENDPOINT = "https://appcenter.intuit.com/connect/oauth2"
OAUTH2_TOKEN_ENDPOINT = "https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer"
OAUTH2_REV_ENDPOINT = "https://developer.api.intuit.com/v2/oauth2/tokens/revoke"
OAUTH2_JWKS_URI = "https://oauth.platform.intuit.com/op/v1/jwks"
OAUTH2_SANDBOX = "https://sandbox-accounts.platform.intuit.com/v1/openid_connect/userinfo"
OAUTH2_PROD = "https://accounts.platform.intuit.com/v1/openid_connect/userinfo"

# info for user-agent
PYTHON_VERSION = platform.python_version()
OS_SYSTEM = platform.uname()[0]
Expand All @@ -36,4 +44,4 @@
ACCEPT_HEADER = {
'Accept': 'application/json',
'User-Agent': '{0}-{1}-{2}-{3} {4} {5} {6}'.format('Intuit-OAuthClient', version.__version__,'Python', PYTHON_VERSION, OS_SYSTEM, OS_RELEASE_VER, OS_MACHINE)
}
}
6 changes: 3 additions & 3 deletions intuitlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
from datetime import datetime
import random
import string

from jose import jwk
import requests
from requests.sessions import Session
import six
from requests_oauthlib import OAuth1


from intuitlib.enums import Scopes
from intuitlib.exceptions import AuthClientError
from intuitlib.config import DISCOVERY_URL, ACCEPT_HEADER
Expand All @@ -39,9 +39,9 @@ def get_discovery_doc(environment, session=None):
:return: Discovery doc response
:raises HTTPError: if response status != 200
"""
if environment.lower() in ['production', 'prod']:
if environment.lower() in {'production', 'prod'}:
discovery_url = DISCOVERY_URL['production']
elif environment.lower() in ['sandbox', 'sand']:
elif environment.lower() in {'sandbox', 'sand'}:
discovery_url = DISCOVERY_URL['sandbox']
else:
discovery_url = environment
Expand Down