Skip to content
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

feat: add retrieveAuthorizationHeader method to authentication classes (box/box-codegen#442) Closes: SDK-3600 #64

Closed
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
1 change: 1 addition & 0 deletions .codegen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "engineHash": "905c6a0", "specHash": "b2f7568", "version": "0.1.0" }
10 changes: 10 additions & 0 deletions box_sdk_gen/box/ccg_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

from box_sdk_gen.box.errors import BoxSDKError

from box_sdk_gen.schemas import PostOAuth2Token

from box_sdk_gen.schemas import PostOAuth2Revoke


class CCGConfig:
def __init__(
Expand Down Expand Up @@ -114,6 +118,12 @@ def retrieve_token(
return new_token
return old_token

def retrieve_authorization_header(
self, network_session: Optional[NetworkSession] = None
) -> str:
token: AccessToken = self.retrieve_token(network_session)
return ''.join(['Bearer ', token.access_token])

def as_user(self, user_id: str, token_storage: TokenStorage = None) -> 'BoxCCGAuth':
"""
Create a new BoxCCGAuth instance that uses the provided user ID as the subject ID.
Expand Down
6 changes: 6 additions & 0 deletions box_sdk_gen/box/developer_token_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ def refresh_token(
raise BoxSDKError(
message='Developer token has expired. Please provide a new one.'
)

def retrieve_authorization_header(
self, network_session: Optional[NetworkSession] = None
) -> str:
token: AccessToken = self.retrieve_token(network_session)
return ''.join(['Bearer ', token.access_token])
6 changes: 6 additions & 0 deletions box_sdk_gen/box/jwt_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,12 @@ def retrieve_token(
return new_token
return old_token

def retrieve_authorization_header(
self, network_session: Optional[NetworkSession] = None
) -> str:
token: AccessToken = self.retrieve_token(network_session)
return ''.join(['Bearer ', token.access_token])

def as_user(self, user_id: str, token_storage: TokenStorage = None) -> 'BoxJWTAuth':
"""
Create a new BoxJWTAuth instance that uses the provided user ID as the subject of the JWT assertion.
Expand Down
7 changes: 6 additions & 1 deletion box_sdk_gen/box/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ def refresh_token(
self.token_storage.store(token)
return token

def retrieve_authorization_header(
self, network_session: Optional[NetworkSession] = None
) -> str:
token: AccessToken = self.retrieve_token(network_session)
return ''.join(['Bearer ', token.access_token])

def revoke_token(self, network_session: Optional[NetworkSession] = None) -> None:
"""
Revoke an active Access Token, effectively logging a user out that has been previously authenticated.
Expand All @@ -200,7 +206,6 @@ def revoke_token(self, network_session: Optional[NetworkSession] = None) -> None
auth_manager.revoke_access_token(
self.config.client_id, self.config.client_secret, token.access_token
)
self.token_storage.clear()
return None

def downscope_token(
Expand Down
10 changes: 9 additions & 1 deletion box_sdk_gen/networking/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,13 @@ def retrieve_token(
pass

@abstractmethod
def refresh_token(self, network_session: NetworkSession) -> AccessToken:
def refresh_token(
self, network_session: Optional[NetworkSession] = None
) -> AccessToken:
pass

@abstractmethod
def retrieve_authorization_header(
self, network_session: Optional[NetworkSession] = None
) -> str:
pass
11 changes: 4 additions & 7 deletions box_sdk_gen/networking/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,10 @@ def __prepare_headers(
headers.update(options.headers)
if options.auth:
if reauthenticate:
headers['Authorization'] = (
f'Bearer {options.auth.refresh_token(options.network_session).access_token}'
)
else:
headers['Authorization'] = (
f'Bearer {options.auth.retrieve_token(options.network_session).access_token}'
)
options.auth.refresh_token(options.network_session)
headers['Authorization'] = options.auth.retrieve_authorization_header(
options.network_session
)

headers['User-Agent'] = USER_AGENT_HEADER
headers['X-Box-UA'] = X_BOX_UA_HEADER
Expand Down
8 changes: 4 additions & 4 deletions test/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ def test_oauth_auth_revoke():
client_id=get_env_var('CLIENT_ID'), client_secret=get_env_var('CLIENT_SECRET')
)
auth: BoxOAuth = BoxOAuth(config=config)
client: BoxClient = BoxClient(auth=auth)
token: AccessToken = get_access_token()
auth.token_storage.store(token)
token_before_revoke: Optional[AccessToken] = auth.token_storage.get()
client.users.get_user_me()
auth.revoke_token()
token_after_revoke: Optional[AccessToken] = auth.token_storage.get()
assert not token_before_revoke == None
assert token_after_revoke == None
with pytest.raises(Exception):
client.users.get_user_me()


def test_oauth_auth_downscope():
Expand Down
Loading