Created
April 5, 2019 06:13
-
-
Save StefH/0f92c44ab1d8fadf0d366067eb54e87c to your computer and use it in GitHub Desktop.
SoapUI hmac_sha256 code to create a SAS SharedAccessSignature for Azure Service Bus Queue
This file contains hidden or 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
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.security.InvalidKeyException; | |
def hmac_sha256(String secretKey, String data) { | |
def mac = Mac.getInstance("HmacSHA256"); | |
def secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256"); | |
mac.init(secretKeySpec); | |
return mac.doFinal(data.getBytes()); | |
} | |
def createSharedAccessToken(String uri, String saName, String base64EncodedSharedAccessKey) { | |
def encodedUri = URLEncoder.encode(uri); | |
def now = new Date(); | |
def onedayInSeconds = 60 * 60 * 24; | |
def ttl = Math.round(now.getTime() / 1000) + onedayInSeconds; | |
def urlWithTTL = encodedUri + '\n' + ttl; | |
def encoded = hmac_sha256(base64EncodedSharedAccessKey, urlWithTTL).encodeBase64().toString(); | |
def signature = URLEncoder.encode(encoded); | |
return 'SharedAccessSignature sig=' + signature + '&se=' + ttl + '&skn=' + saName + '&sr=' + encodedUri; | |
} | |
def testSuite = testRunner.getTestCase().getTestSuite(); | |
def uri = testSuite.getPropertyValue("endpoint"); | |
def keyName = testSuite.getPropertyValue("sharedAccessKeyName"); | |
def key = testSuite.getPropertyValue("sharedAccessKey"); | |
log.info("uri=" + uri); | |
log.info("keyName=" + keyName); | |
log.info("key=" + key); | |
def sas = createSharedAccessToken(uri, keyName, key); | |
log.info("SAS=" + sas); | |
context.setProperty("SAS", sas); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment