20
20
# Constants
21
21
API_BASE_URL = 'https://api.paloaltonetworks.com'
22
22
AUTH_BASE_URL = 'https://identity.paloaltonetworks.com/as/authorization.oauth2'
23
+ DEVELOPER_TOKEN_URL = 'https://app.apiexplorer.rocks'
23
24
24
25
ReadOnlyCredentials = namedtuple (
25
26
'ReadOnlyCredentials' ,
@@ -31,11 +32,11 @@ class Credentials(object):
31
32
"""An Application Framework credentials object."""
32
33
33
34
def __init__ (self , access_token = None , auth_base_url = None , cache_token = True ,
34
- client_id = None , client_secret = None , instance_id = None ,
35
- profile = None , redirect_uri = None , region = None ,
36
- refresh_token = None , scope = None , storage_adapter = None ,
37
- storage_params = None , token_url = None , token_revoke_url = None ,
38
- ** kwargs ):
35
+ client_id = None , client_secret = None , developer_token = None ,
36
+ developer_token_url = None , instance_id = None , profile = None ,
37
+ redirect_uri = None , region = None , refresh_token = None ,
38
+ scope = None , storage_adapter = None , storage_params = None ,
39
+ token_url = None , ** kwargs ):
39
40
"""Persist Session() and credentials attributes.
40
41
41
42
The ``Credentials`` class is an abstraction layer for accessing,
@@ -55,6 +56,8 @@ def __init__(self, access_token=None, auth_base_url=None, cache_token=True,
55
56
cache_token (bool): If ``True``, stores ``access_token`` in token store. Defaults to ``True``.
56
57
client_id (str): OAuth2 client ID. Defaults to ``None``.
57
58
client_secret (str): OAuth2 client secret. Defaults to ``None``.
59
+ developer_token (str): Developer Token. Defaults to ``None``.
60
+ developer_token_url (str): Developer Token URL. Defaults to ``None``.
58
61
instance_id (str): Instance ID. Defaults to ``None``.
59
62
profile (str): Credentials profile. Defaults to ``'default'``.
60
63
redirect_uri (str): Redirect URI. Defaults to ``None``.
@@ -72,6 +75,8 @@ def __init__(self, access_token=None, auth_base_url=None, cache_token=True,
72
75
self .cache_token_ = cache_token
73
76
self .client_id_ = client_id
74
77
self .client_secret_ = client_secret
78
+ self .developer_token_ = developer_token
79
+ self .developer_token_url = developer_token_url or DEVELOPER_TOKEN_URL
75
80
self .instance_id = instance_id
76
81
self .jwt_exp_ = None
77
82
self .profile = profile or 'default'
@@ -93,7 +98,8 @@ def __init__(self, access_token=None, auth_base_url=None, cache_token=True,
93
98
94
99
def __repr__ (self ):
95
100
args = self .__dict__ .copy ()
96
- for k in ['access_token_' , 'refresh_token_' , 'client_secret_' ]:
101
+ for k in ['access_token_' , 'refresh_token_' , 'client_secret_' ,
102
+ 'developer_token_' ]:
97
103
if args [k ] is not None :
98
104
args [k ] = '*' * 6
99
105
return '{}({})' .format (
@@ -142,6 +148,16 @@ def client_secret(self, client_secret):
142
148
"""Set client_secret."""
143
149
self .client_secret_ = client_secret
144
150
151
+ @property
152
+ def developer_token (self ):
153
+ """Get developer token."""
154
+ return self .developer_token_ or os .getenv ('PAN_DEVELOPER_TOKEN' )
155
+
156
+ @developer_token .setter
157
+ def developer_token (self , developer_token ):
158
+ """Set developer token."""
159
+ self .developer_token_ = developer_token
160
+
145
161
@property
146
162
def jwt_exp (self ):
147
163
"""Get JWT exp."""
@@ -416,12 +432,31 @@ def refresh(self, access_token=None, **kwargs):
416
432
if not self .token_lock .locked ():
417
433
with self .token_lock :
418
434
if access_token == self .access_token or access_token is None :
419
- c = self .get_credentials ()
420
- if c .client_id and c .client_secret and c .refresh_token :
435
+ if self .developer_token is not None :
436
+ r = self ._httpclient .request (
437
+ method = 'POST' ,
438
+ url = self .developer_token_url ,
439
+ path = '/request_token' ,
440
+ headers = {
441
+ 'Authorization' : 'Bearer {}' .format (
442
+ self .developer_token
443
+ )
444
+ },
445
+ timeout = 30 ,
446
+ raise_for_status = True
447
+ )
448
+
449
+ elif all (
450
+ [
451
+ self .client_id ,
452
+ self .client_secret ,
453
+ self .refresh_token
454
+ ]
455
+ ):
421
456
data = {
422
- 'client_id' : c .client_id ,
423
- 'client_secret' : c .client_secret ,
424
- 'refresh_token' : c .refresh_token ,
457
+ 'client_id' : self .client_id ,
458
+ 'client_secret' : self .client_secret ,
459
+ 'refresh_token' : self .refresh_token ,
425
460
'grant_type' : 'refresh_token'
426
461
}
427
462
r = self ._httpclient .request (
@@ -431,9 +466,16 @@ def refresh(self, access_token=None, **kwargs):
431
466
path = '/api/oauth2/RequestToken' ,
432
467
** kwargs
433
468
)
469
+ else :
470
+ raise PartialCredentialsError (
471
+ "Missing one or more required credentials"
472
+ )
473
+
474
+ if r :
434
475
if not r .ok :
435
476
raise PanCloudError (
436
- '%s %s: %s' % (r .status_code , r .reason , r .text )
477
+ '%s %s: %s' % (
478
+ r .status_code , r .reason , r .text )
437
479
)
438
480
try :
439
481
r_json = r .json ()
@@ -456,10 +498,6 @@ def refresh(self, access_token=None, **kwargs):
456
498
r_json .get ('refresh_token' )
457
499
self .write_credentials ()
458
500
return self .access_token_
459
- else :
460
- raise PartialCredentialsError (
461
- "Missing one or more required credentials"
462
- )
463
501
464
502
def revoke_access_token (self , ** kwargs ):
465
503
"""Revoke access token."""
0 commit comments