Skip to content

Commit fe1f34e

Browse files
authored
API-2197 [BE] Add client credentials auth support for Python SDK (#47)
* API-2197 check for client credentials, use corresponding client in oauth library * API-2197 check for client credentials key * API-2197 check for client credentials key and boolean value * API-2197 update README for client credentials usage
1 parent cba49ff commit fe1f34e

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,36 @@ bynder_client = BynderClient(
8484
permanent_token=''
8585
)
8686
```
87+
To use client credentials grant type, add the field `client_credentials` to `secret.json` and set the field to true.
88+
89+
```json
90+
{
91+
"domain": "*****",
92+
"client_id": "*****",
93+
"client_secret": "******",
94+
"client_credentials": true,
95+
"scopes": ["asset.usage:write", "collection:write", "meta.workflow:read", "asset:write", "asset:read", "meta.assetbank:write", "collection:read", "admin.user:read", "meta.assetbank:read", "current.user:read", "current.profile:read", "offline", "admin.profile:read", "asset.usage:read", "admin.user:write"]
96+
}
97+
```
98+
``` python
99+
# client credentials grant type
100+
elif self.config_data.get('token', None) is None and self.config_data.get('client_credentials', None) == True:
101+
bynder_client.fetch_token(code=None)
102+
```
103+
104+
```python
105+
bynder_client = BynderClient(
106+
**self.config_data,
107+
token_saver=self.token_saver, # optional, defaults to empty lambda
108+
)
109+
```
110+
Client credentials provided as keyword argument:
111+
```python
112+
bynder_client = BynderClient(
113+
client_credentials=True,
114+
token_saver=self.token_saver, # optional, defaults to empty lambda
115+
)
116+
```
87117

88118
Finally call one of the API's endpoints through one of the clients:
89119

bynder_sdk/client/bynder_client.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from bynder_sdk.client.workflow_client import WorkflowClient
55
from bynder_sdk.oauth2 import BynderOAuth2Session
66
from bynder_sdk.permanent_token import PermanentTokenSession
7+
from oauthlib.oauth2 import BackendApplicationClient
78

89
REQUIRED_OAUTH_KWARGS = (
910
'client_id', 'client_secret', 'redirect_uri', 'scopes')
@@ -29,6 +30,8 @@ def __init__(self, domain, **kwargs):
2930
f'Missing required arguments: {missing}'
3031
)
3132

33+
# if client credentials use BackendApplicationClient from oauthlib, client suited for client credentials
34+
client_credentials = BackendApplicationClient(kwargs['client_id']) if kwargs.get('client_credentials', None) == True else None
3235
self.session = BynderOAuth2Session(
3336
domain,
3437
kwargs['client_id'],
@@ -38,7 +41,9 @@ def __init__(self, domain, **kwargs):
3841
'client_id': kwargs['client_id'],
3942
'client_secret': kwargs['client_secret']
4043
},
41-
token_updater=kwargs.get('token_saver', (lambda _: None))
44+
token_updater=kwargs.get('token_saver', (lambda _: None)),
45+
# if client is None, default to WebApplicationClient which uses authorization_code grant type
46+
client=client_credentials
4247
)
4348

4449
if kwargs.get('token') is not None:

samples/client.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,16 @@ def get_auth_client(self) -> BynderClient:
3939
# "scope": ["offline"],
4040
# "token_type": "bearer"
4141
# }
42-
if self.config_data.get('token', None) is None:
42+
43+
# auth code grant type
44+
if self.config_data.get('token', None) is None and not self.config_data.get('client_credentials', None):
4345
print(bynder_client.get_authorization_url())
4446

4547
code = input('Code: ')
4648
print(bynder_client.fetch_token(code))
4749

48-
return bynder_client
50+
# client credentials grant type
51+
elif self.config_data.get('token', None) is None and self.config_data.get('client_credentials', None) == True:
52+
bynder_client.fetch_token(code=None)
53+
54+
return bynder_client

0 commit comments

Comments
 (0)