Skip to content
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
2 changes: 1 addition & 1 deletion .codegen.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "engineHash": "0eab570", "specHash": "7f8494a", "version": "4.20.0" }
{ "engineHash": "6772894", "specHash": "7cec401", "version": "4.20.0" }
113 changes: 84 additions & 29 deletions box_sdk_gen/box/jwt_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from box_sdk_gen.serialization.json import deserialize

from box_sdk_gen.serialization.json import serialize

from typing import List

from box_sdk_gen.schemas.post_o_auth_2_token import PostOAuth2TokenGrantTypeField
Expand All @@ -30,6 +32,14 @@

from box_sdk_gen.serialization.json import SerializedData

from box_sdk_gen.networking.fetch_options import FetchOptions

from box_sdk_gen.networking.fetch_options import RequestDataFactory

from box_sdk_gen.networking.fetch_options import ResponseFormat

from box_sdk_gen.networking.fetch_response import FetchResponse

from box_sdk_gen.internal.utils import get_uuid

from box_sdk_gen.internal.utils import read_text_from_file
Expand Down Expand Up @@ -269,36 +279,32 @@ def from_config_file(
)


class BoxJWTAuth(Authentication):
def __init__(self, config: JWTConfig, **kwargs):
class JwtAccessTokenDataFactory(RequestDataFactory):
def __init__(
self,
config: JWTConfig,
*,
subject_id: Optional[str] = None,
subject_type: Optional[str] = None,
**kwargs
):
"""
:param config: An object containing all JWT configuration to use for authentication
:param config: JWT configuration used to sign the assertion
:type config: JWTConfig
:param subject_id: The ID of the user or enterprise to authenticate as, defaults to None
:type subject_id: Optional[str], optional
:param subject_type: The type of the subject ID, either 'user' or 'enterprise', defaults to None
:type subject_type: Optional[str], optional
"""
super().__init__(**kwargs)
self.config = config
self.token_storage = self.config.token_storage
self.subject_id = (
self.config.enterprise_id
if not self.config.enterprise_id == None
else self.config.user_id
)
self.subject_type = (
'enterprise' if not self.config.enterprise_id == None else 'user'
)
self.subject_id = subject_id
self.subject_type = subject_type

def refresh_token(
self, *, network_session: Optional[NetworkSession] = None
) -> AccessToken:
def create(self) -> SerializedData:
"""
Get new access token using JWT auth.
:param network_session: An object to keep network session state, defaults to None
:type network_session: Optional[NetworkSession], optional
Creates the access token request body with a newly signed JWT assertion.
"""
if is_browser():
raise BoxSDKError(
message='JWT auth is not supported in browser environment.'
)
alg: JwtAlgorithm = (
self.config.algorithm
if not self.config.algorithm == None
Expand All @@ -321,17 +327,66 @@ def refresh_token(
key=self.config.private_key, passphrase=self.config.private_key_passphrase
)
assertion: str = create_jwt_assertion(claims, jwt_key, jwt_options)
auth_manager: AuthorizationManager = AuthorizationManager(
network_session=(
network_session if not network_session == None else NetworkSession()
)
)
token: AccessToken = auth_manager.request_access_token(
PostOAuth2TokenGrantTypeField.URN_IETF_PARAMS_OAUTH_GRANT_TYPE_JWT_BEARER,
request_body: PostOAuth2Token = PostOAuth2Token(
grant_type=PostOAuth2TokenGrantTypeField.URN_IETF_PARAMS_OAUTH_GRANT_TYPE_JWT_BEARER,
assertion=assertion,
client_id=self.config.client_id,
client_secret=self.config.client_secret,
)
return serialize(request_body)


class BoxJWTAuth(Authentication):
def __init__(self, config: JWTConfig, **kwargs):
"""
:param config: An object containing all JWT configuration to use for authentication
:type config: JWTConfig
"""
super().__init__(**kwargs)
self.config = config
self.token_storage = self.config.token_storage
self.subject_id = (
self.config.enterprise_id
if not self.config.enterprise_id == None
else self.config.user_id
)
self.subject_type = (
'enterprise' if not self.config.enterprise_id == None else 'user'
)

def refresh_token(
self, *, network_session: Optional[NetworkSession] = None
) -> AccessToken:
"""
Get new access token using JWT auth.
:param network_session: An object to keep network session state, defaults to None
:type network_session: Optional[NetworkSession], optional
"""
if is_browser():
raise BoxSDKError(
message='JWT auth is not supported in browser environment.'
)
network_session_to_use: NetworkSession = (
network_session if not network_session == None else NetworkSession()
)
data_factory: JwtAccessTokenDataFactory = JwtAccessTokenDataFactory(
config=self.config,
subject_id=self.subject_id,
subject_type=self.subject_type,
)
response: FetchResponse = network_session_to_use.network_client.fetch(
FetchOptions(
url=''.join(
[network_session_to_use.base_urls.base_url, '/oauth2/token']
),
method='POST',
data_factory=data_factory,
content_type='application/x-www-form-urlencoded',
response_format=ResponseFormat.JSON,
network_session=network_session_to_use,
)
)
token: AccessToken = deserialize(response.data, AccessToken)
self.token_storage.store(token)
return token

Expand Down
5 changes: 5 additions & 0 deletions box_sdk_gen/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@

from box_sdk_gen.managers.shuttle import ShuttleManager

from box_sdk_gen.managers.ai_sessions import AiSessionsManager

from box_sdk_gen.networking.auth import Authentication

from box_sdk_gen.networking.network import NetworkSession
Expand Down Expand Up @@ -489,6 +491,9 @@ def __init__(
self.shuttle = ShuttleManager(
auth=self.auth, network_session=self.network_session
)
self.ai_sessions = AiSessionsManager(
auth=self.auth, network_session=self.network_session
)

def make_request(self, fetch_options: FetchOptions) -> FetchResponse:
"""
Expand Down
2 changes: 2 additions & 0 deletions box_sdk_gen/managers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,5 @@
from box_sdk_gen.managers.query import *

from box_sdk_gen.managers.shuttle import *

from box_sdk_gen.managers.ai_sessions import *
Loading
Loading