Commit d2cd15c7 authored by Sam McCollum's avatar Sam McCollum

Add token renewal and improve documentation

parent 6190fd24
Pipeline #223 passed with stage
in 26 seconds
......@@ -11,4 +11,68 @@ Requirements:
socket and json are probably already installed, but the remaining can be installed with pip.
**NOTE: Currently does not include token renewal**
```{.python}
"""
" Perform logout of the token given by delToken
"
" @param delToken(Dict): A dict generated from the json of the token you want to log out
" @param token(String): The token to use for authentication (can be gotten with wmappauth.getTokenString() )
" @param sigb64(String): Base64-encoded signature to use for authentication (can be gotten with wmappauth.getSignature() )
"""
def invalidateToken(delToken, token, sigb64)
"""
" Get a list of tokens from the server (returns the raw result of requests.get)
"""
def listTokens(token, sigb64)
"""
" Renew the token, persisting the result to the disk if successful
"
" @return: True or False, indicating whether the operation succeeded
"""
def renewToken(token, sigb64)
"""
" Pass a dict invalidationSubScription
"""
def subscribeToInvalidation(invalidationSubscription, token, sigb64)
"""
" Check if the given token and signature are valid (performs a network call)
"""
def isValidToken(token, sigb64)
"""
" Retrieve the token if credentials are present
"""
def getTokenString()
"""
" Retrieve the token signature if credentials are present
"""
def getSignature()
"""
" Save the token/signature to the disk (shouldn't ever need to call this)
"""
def persistTokens()
"""
" Login with the supplied credentials, saving them to the disk if successful
"
" @return True or False, indicating whether the operation succeeded
"""
def wmLogin(username, password, deviceName=socket.gethostname())
"""
/**
* Prompt the user to login (handles obtaining the username and password on a CLI for you), saving the result to disk if successful
*
* @return: True or False, indicating whether the operation succeeded
*/
"""
def promptLogin()
"""
/**
* Load credentials from disk, prompting for a login in the terminal be default if the operation fails
*
* @return: True or False, indicating whether they were loaded
*/
"""
def loadCredentials(prompt=True)
```
......@@ -24,6 +24,9 @@ tokenValidUrl = tokenBaseUrl+"/tokenValid"
tokenSignature=""
tokenString=""
TOKEN_HEADER='Token'
TOKEN_SIGNATURE_HEADER='TokenSignature'
def readTokens():
try:
with open('token.json', 'r') as tf:
......@@ -54,10 +57,20 @@ def listTokens(token, sigb64):
return requests.get(url=listUrl, headers=hrs)
def renewToken(token, sigb64):
global TOKEN_HEADER
global TOKEN_SIGNATURE_HEADER
hrs = {'Content-Type': 'application/json'}
hrs['Token'] = token
hrs['TokenSignature'] = sigb64
return requests.get(url=renewUrl, headers=hrs)
hrs[TOKEN_HEADER] = token
hrs[TOKEN_SIGNATURE_HEADER] = sigb64
newCreds = requests.get(url=renewUrl, headers=hrs)
if not checkCode(newCreds, 200, "renew"):
return False
global tokenString
tokenString = str(newCreds.content, 'utf-8')
global tokenSignature
tokenSignature = newCreds.headers[TOKEN_SIGNATURE_HEADER]
persistTokens()
return True
def subscribeToInvalidation(invalidationSubscription, token, sigb64):
hrs = {'Content-Type': 'application/json'}
......@@ -130,7 +143,7 @@ def loadCredentials(prompt=True):
"""
/*
* If this is being executed as a program
* If this is being executed as a program, run sample code
*/
"""
if __name__ == "__main__":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment