Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

How do I form an SAS token for Microsoft Azure API Management's REST API in Node.js?

I am using Microsoft Azure API Management service and want to use the REST API service. In creating my SAS token, which is needed otherwise the API call doesn't authorize, I'm having difficulty forming a proper token. Microsoft's webpage about this SAS token for API Management only shows an example in C#. I want to know how to form an SAS token in Node.js, which is not shown. Below is my code that was working last week, but is not now for some unknown reason. The error I get is: 401 Authorization error, token invalid

If someone can help me formulate this token, I would appreciate it.

This is Microsoft's webpage regarding this authentication token: https://docs.microsoft.com/en-us/rest/api/apimanagement/apimanagementrest/azure-api-management-rest-api-authentication

Here's my code:

const crypto = require('crypto');
const util = require('util');

const sign = () => {
  const id = ${process.env.id}
  const key = `${process.env.SASKey}`;
  const date = new Date();
  const newDate = new Date(date.setTime(date.getTime() + 8 * 86400000));
  const expiry = `${newDate.getFullYear()}${
    newDate.getMonth() < 10
      ? '' + newDate.getMonth() + 1
      : newDate.getMonth() + 1
  }${newDate.getDate()}${newDate.getHours()}${
    newDate.getMinutes() < 10
      ? '0' + newDate.getMinutes()
      : newDate.getMinutes()
  }`;
  const dataToSignString = '%s
%s';
  const dataToSign = util.format(dataToSignString, ${id}, expiry);
  const hash = crypto
    .createHmac('sha512', key)
    .update(dataToSign)
    .digest('base64');

  const encodedToken = `SharedAccessSignature ${id}&${expiry}&${hash}`;
  console.log(encodedToken);
  return encodedToken;
};
question from:https://stackoverflow.com/questions/65862737/how-do-i-form-an-sas-token-for-microsoft-azure-api-managements-rest-api-in-node

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try the code:

protected getAPIManagementSAS(){

    let utf8 = require("utf8")
    let crypto= require("crypto")

    let identifier = process.env.API_IDENTIFIER;
    let key = process.env.API_KEY;

    var now = new Date;
    var utcDate = new Date(now.getUTCFullYear(),now.getUTCMonth(), now.getUTCDate() , now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds());

    let expiry = addMinutes(utcDate,1,"yyyy-MM-ddThh:mm:ss") + '.0000000Z'

    var dataToSign = identifier + "
" + expiry;
    var signatureUTF8 = utf8.encode(key); 
    var signature = crypto.createHmac('sha512', signatureUTF8).update(dataToSign).digest('base64'); 
    var encodedToken = `SharedAccessSignature uid=${identifier}&ex=${expiry}&sn=${signature}`;   

    return encodedToken

}

For more information, see here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...