1. Copy the below script to a .py extension file (in ours e.g. it is set as access_expiry.py). Please check the comments added in the code to understand more. Before executing the script please remember to change the following based on your environment and needs:
2. Once you are done with the previous step, please go to the location of the file in a command or terminal window and use the following command:
Output for the above is as follows:
(NOTE: We can do changes in the code to get non-expiring, expired, and expiring tokens over N days too)
- customerarturl( Artifactory url)
- customertoken(access token)
- tokendaysexpiry(Number of days you need token information for). In this sample, it is set as 300.
2. Once you are done with the previous step, please go to the location of the file in a command or terminal window and use the following command:
“python3 access_expiry.py”.
import requests import json from datetime import datetime from time import time headers = {'Authorization': 'Bearer customertoken'} url = "https://customerarturl/access/api/v1/tokens" x = requests.get(url, headers=headers) data = json.loads(x.text) # number of days to check for expiring tokens. tested for positive only. so keep this as a positive number tokendaysexpiry = 300 expiringtokenswithinNdate = [] expiredtokenswithdate = [] expiringtokensbeyondNdate = [] nonexpiringtokens = [] index_nonexpiring = 0 indexgreaterN_expiring = 0 indexwithinNdays_expiring = 0 index_expired = 0 keyexpiry = 'expiry' def change_token(token): # for tokens expiring within N days epochtimefor1day = 24*60*60 epochtimeforNdays = epochtimefor1day*tokendaysexpiry Ndaysinepochtime = epochtimeforNdays+time() currentepochtime = time() # print(epochtimeforNdays) # print("time now: " + str(time())) # print("total time :" + str(epochtimeforNdays+time())) epochinInt = int(token["expiry"]) retval = dict() token["expiry_date"] = datetime.utcfromtimestamp( epochinInt).strftime("%d-%m-%Y %H:%M:%S") if epochinInt < Ndaysinepochtime: # less than N days of epoch time and greater than current time if epochinInt > currentepochtime: retval["tokenval"] = token retval["tokenmessage"] = "validtoken" return retval elif epochinInt < currentepochtime: # less than current epoch time retval["tokenval"] = token retval["tokenmessage"] = "expiredtoken" return retval elif epochinInt > Ndaysinepochtime: retval["tokenval"] = token retval["tokenmessage"] = "tokensbeyondNdays" return retval for key, tokens in data.items(): # get the key and value for tokens. The value is of list type. # print(key, '->', tokens) print("total number of tokens :" + str(len(tokens))) for tok in tokens: if keyexpiry in tok: # this case is for a condition when the json doesn't have expiry property as it is non-expiring token tokobject = change_token(tok) if tokobject is not None: if tokobject["tokenmessage"] == "validtoken": expiringtokenswithinNdate.insert( indexwithinNdays_expiring, tokobject["tokenval"]) # for expiring tokens within last N days as per the argument value you will set for tokendaysexpiry indexwithinNdays_expiring += 1 # print("expiring tokens within N days") # print(expiringtokensbeyondNdate) elif tokobject["tokenmessage"] == "tokensbeyondNdays": expiringtokensbeyondNdate.insert( indexgreaterN_expiring, tokobject["tokenval"]) # for expiring tokens after N days as per the argument value you will set for tokendaysexpiry indexgreaterN_expiring += 1 # print("expiring tokens more than N days") else: # print(tok) expiredtokenswithdate.insert( index_expired, tokobject["tokenval"]) # for expired tokens less than today index_expired += 1 # print("expired tokens") else: # for non expiring tokens nonexpiringtokens.insert(index_nonexpiring, tok) # print("non expiring tokens") # print(tok) index_nonexpiring += 1 # print("expiring tokens within N days : " + str(indexwithinNdays_expiring)) if len(expiringtokenswithinNdate) > 0: print("tokens expiring within "+str(tokendaysexpiry) + " days are " + ' '.join([str(elem) for elem in expiringtokenswithinNdate])) else: print("No tokens expiring within "+str(tokendaysexpiry)) print("expiring tokens more than "+str(tokendaysexpiry) + " days count: " + str(indexgreaterN_expiring)) print("expired token count: " + str(index_expired)) print("non expiring token count: " + str(index_nonexpiring))
Output for the above is as follows:
(NOTE: We can do changes in the code to get non-expiring, expired, and expiring tokens over N days too)