Forum Discussion
Thanks for the reply back.
Glad to see I am not the only one who has issues trying to decipher Microsoft's Azure REST API reference information.
I still have to play with this more from what you sent me.
I am on the right path per what you mentioned.
I re-verify the Authorization format as well as the Version used for x-ms-version.
The x-ms-date part is where I am confused because I cannot determine by what timezone location
on Earth I should be using here. I live in Texas, so for x-ms-date, if I sent a GET request right now,
is this per my timezone or per Greenwich England ? And what is the fudge factor here for it to be accurate
when being set and sent?
Following back up on this, since I got to play with some more:
The format for Shared Key Authorization header is as follows:
Authorization="[SharedKey|SharedKeyLite] <AccountName>:<Signature>"
so it should read something like
Authorization="SharedKey accountname:ctzMq410TV3wS7upTBcunJTDLEJwMAZuFPfr0mrrA08=
SharedKey is the name of the authorization scheme, AccountName is the name of the account requesting the resource, and Signature is a Hash-based Message Authentication Code (HMAC) constructed from the request and computed by using the SHA256 algorithm, and then encoded by using Base64 encoding
I put together some Groovy script code, to try to do the above encoding:
The code is not correct yet but a start to which I am turning to debug and
fine tune to encode correctly per the above mentioned paragraph.
import java.text.SimpleDateFormat;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.math.BigInteger
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = sdf.format(new Date()) + " GMT";
//signature = signature + "\nx-ms-date:" + date;
log.info "\nx-ms-date:" + date;
/**
* Param secretKey
* Param data
* @return HMAC/SHA256 representation of the given string
*/
//def hmac_sha256(String secretKey, String data)
def key = "<Shared Key from Azure account>=="; // Does not have signature part added to it yet
log.info key;
//def strTime = (new Date()).toUTCString();
def strTime = date;
log.info strTime;
def strToSign = 'GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:' + strTime + '\nx-ms-version:2017-11-09\n/nprodpayconm3repo/\ncomp:list';
log.info strToSign;
//def secret = CryptoJS.enc.Base64.parse(key);
def secret = key.bytes.encodeBase64().toString();
log.info secret;
def secretKey = "<Shared Key from Azure account>==";
log.info secretKey;
def data = key.bytes.encodeBase64().toString();
log.info data;
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes("UTF-8"), "HmacSHA256")
Mac mac = Mac.getInstance("HmacSHA256")
mac.init(secretKeySpec)
byte[] digest = mac.doFinal(data.getBytes("UTF-8"))
BigInteger bigInteger = new BigInteger(1, digest)
String hash = bigInteger.toString(16)
//Zero pad it
while (hash.length() < 64)
{
hash = "0" + hash
}
log.info "-----------"
log.info "HASH:"
log.info hash
Any more suggestion from the community for my issue would be much appreciated.
There has to be other folks out there accessing Azure using it's REST APIs.