How to Add custom authorization in swagger UI?

user931174 Source

In one of my Rest API call I need to add header "Authorization: partner_id : timestamp signature"

where company name is static string it can be hardcoded where as partner_id is part of query parameter user inputs it and signature is calculated as sha256(secret,password).digest.encode('base64')

How and where can I implement this authorization as right now as I see there is only api_key, and basic authorization allowed in swagger-ui.

javascriptauthorizationswaggerswagger-uiswagger-2.0

Answers

answered 2 years ago fehguy #1

Yes, you can easily create your own signing techniques. From the swagger-js README:

var CustomRequestSigner = function(name) {
  this.name = name;
};

CustomRequestSigner.prototype.apply = function(obj, authorizations) {
  // do something real instead of this...
  var hashFunction = this._btoa;
  var hash = hashFunction(obj.url);

  obj.headers["signature"] = hash;
  return true;
};

and add it to swagger like such:

client.clientAuthorizations
    .add("custom", new CustomRequestSigner("custom","special-key","query"));

Then any operation tagged with the security requirement custom will have this applied.

comments powered by Disqus