Share via


Requesting a Token from Access Control Service in Python

[UPDATE 2/11: Updated to use new STS V0.9 instead of V0.8] 

In the previous posts I demonstrated requesting tokens from the Access Control Service using both Simple Web Token and Shared Secret requests in Java and in PHP. In this little example I am only showing the Shared Secret request in Python.

import sys, httplib, urllib

def MakeSTSRequest(claimSet,stsUrl):
    headers = {"Content-type":"application/x-www-form-urlencoded"}
   
    conn = httplib.HTTPSConnection(stsUri)
    conn.request("POST","/WRAPv0.9/",claimSet,headers)
    response = conn.getresponse()
    data = response.read()
    conn.close()   
    return data

def GetTokenBySharedSecret(stsUrl,claimSet,issuerName,issuerKey,rpURL):
    claimSet = urllib.urlencode({"wrap_scope":rpUri,
                           "wrap_name":issuerName,
                           "wrap_password":issuerKey})
    responseString=MakeSTSRequest(claimSet,stsUrl)
    return ExtractTokenFromResponse(responseString)
   

def ExtractTokenFromResponse(stringResponse):
    claims=stringResponse.split("&")
    for claim in claims:
        keyValue=claim.split("=")
        if(keyValue[0]=="wrap_token"):
            return keyValue[1]
    return stringResponse

stsUri="[service namespace].accesscontrol.windows.net"
rpUri="[scope applies_to]"
issuerName="[issuer name]"
issuerKey="[issuer key]"

claimSet={"sample_claim_type":"sample_claim_value"}
print GetTokenBySharedSecret(stsUri,claimSet,issuerName,issuerKey,rpUri)