44from contentstack_management .stack import stack
55from contentstack_management .user_session import user_session
66from contentstack_management .users import user
7+ from contentstack_management .oauth .oauth_handler import OAuthHandler
78
89version = '0.0.1'
910
@@ -33,7 +34,7 @@ class Client:
3334 def __init__ (self , host : str = 'api.contentstack.io' , scheme : str = 'https://' ,
3435 authtoken : str = None , management_token = None , headers : dict = None ,
3536 region : Region = Region .US .value , version = 'v3' , timeout = 2 , max_retries : int = 18 , early_access : list = None ,
36- ** kwargs ):
37+ oauth_config : dict = None , ** kwargs ):
3738 self .endpoint = 'https://api.contentstack.io/v3/'
3839 if region is not None and host is not None and region is not Region .US .value :
3940 self .endpoint = f'{ scheme } { region } -{ host } /{ version } /'
@@ -55,6 +56,19 @@ def __init__(self, host: str = 'api.contentstack.io', scheme: str = 'https://',
5556 headers ['authorization' ] = management_token
5657 headers = user_agents (headers )
5758 self .client = _APIClient (endpoint = self .endpoint , headers = headers , timeout = timeout , max_retries = max_retries )
59+
60+ # Initialize OAuth if configuration is provided
61+ self .oauth_handler = None
62+ if oauth_config :
63+ self .oauth_handler = OAuthHandler (
64+ app_id = oauth_config .get ('app_id' ),
65+ client_id = oauth_config .get ('client_id' ),
66+ redirect_uri = oauth_config .get ('redirect_uri' ),
67+ response_type = oauth_config .get ('response_type' , 'code' ),
68+ client_secret = oauth_config .get ('client_secret' ),
69+ scope = oauth_config .get ('scope' ),
70+ api_client = self .client
71+ )
5872
5973 """
6074 :param host: Optional hostname for the API endpoint.
@@ -96,3 +110,41 @@ def organizations(self, organization_uid: str = None):
96110
97111 def stack (self , api_key : str = None ):
98112 return stack .Stack (self .client , api_key )
113+
114+ def oauth (self , app_id : str , client_id : str , redirect_uri : str ,
115+ response_type : str = "code" , client_secret : str = None ,
116+ scope : list = None ):
117+ """
118+ Create an OAuth handler for OAuth 2.0 authentication.
119+
120+ Args:
121+ app_id: Your registered App ID
122+ client_id: Your OAuth Client ID
123+ redirect_uri: The URL where the user is redirected after login and consent
124+ response_type: OAuth response type (default: "code")
125+ client_secret: Client secret for standard OAuth flows (optional for PKCE)
126+ scope: Permissions requested (optional)
127+
128+ Returns:
129+ OAuthHandler instance
130+
131+ Example:
132+ >>> import contentstack_management
133+ >>> client = contentstack_management.Client()
134+ >>> oauth_handler = client.oauth(
135+ ... app_id='your-app-id',
136+ ... client_id='your-client-id',
137+ ... redirect_uri='http://localhost:3000/callback'
138+ ... )
139+ >>> auth_url = oauth_handler.authorize()
140+ >>> print(f"Visit this URL to authorize: {auth_url}")
141+ """
142+ return OAuthHandler (
143+ app_id = app_id ,
144+ client_id = client_id ,
145+ redirect_uri = redirect_uri ,
146+ response_type = response_type ,
147+ client_secret = client_secret ,
148+ scope = scope ,
149+ api_client = self .client
150+ )
0 commit comments