-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from CMSTrackerDPG/new_sso
- Loading branch information
Showing
13 changed files
with
189 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SSO_CLIENT_ID= | ||
SSO_CLIENT_SECRET= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
API Token getter, based on | ||
https://gitlab.cern.ch/authzsvc/docs/api-access-examples/-/blob/master/python/api_token.py | ||
""" | ||
|
||
import logging | ||
import datetime | ||
import requests | ||
import jwt | ||
|
||
DEFAULT_SERVER = "auth.cern.ch" | ||
DEFAULT_REALM = "cern" | ||
DEFAULT_REALM_PREFIX = "auth/realms/{}" | ||
DEFAULT_TOKEN_ENDPOINT = "api-access/token" | ||
|
||
|
||
def get_token_endpoint(server=DEFAULT_SERVER, realm=DEFAULT_REALM): | ||
""" | ||
Gets the token enpdoint path from the args | ||
""" | ||
return "https://{}/{}/{}".format( | ||
server, DEFAULT_REALM_PREFIX.format(realm), DEFAULT_TOKEN_ENDPOINT | ||
) | ||
|
||
|
||
def get_api_token( | ||
client_id, client_secret, target_application, token_endpoint=get_token_endpoint() | ||
): | ||
logging.debug( | ||
"[x] Getting API token as {} for {}".format(client_id, target_application) | ||
) | ||
|
||
r = requests.post( | ||
token_endpoint, | ||
auth=(client_id, client_secret), | ||
data={"grant_type": "client_credentials", "audience": target_application}, | ||
) | ||
|
||
if not r.ok: | ||
msg = "ERROR getting token: {}".format(r.json()) | ||
logging.error(msg) | ||
raise Exception(msg) | ||
|
||
response_json = r.json() | ||
token = response_json["access_token"] | ||
expires_in_seconds = response_json["expires_in"] | ||
expiration_datetime = datetime.datetime.now() + datetime.timedelta( | ||
seconds=expires_in_seconds | ||
) | ||
|
||
# logging.debug(jwt.decode(token, verify=False)) | ||
logging.debug("[x] Token obtained") | ||
|
||
return token, expiration_datetime |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
|
||
setup( | ||
name="cernrequests", | ||
version="0.3.2", | ||
version="0.4.0", | ||
desription="CERN wrapper around the requests package", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
|
@@ -29,8 +29,8 @@ | |
author_email="[email protected]", | ||
packages=["cernrequests"], | ||
package_dir={"cernrequests": "cernrequests"}, | ||
package_data={"cernrequests": ["*.pem"]}, | ||
install_requires=["requests", "future"], | ||
# package_data={"cernrequests": ["*.pem"]}, | ||
install_requires=["requests", "future", "python-dotenv", "pyjwt"], | ||
classifiers=[ | ||
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)", | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters