Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. c.ApiKey("auth")
  2. .Description("API Key Authentication")
  3. .Name("auth")
  4. .In("query");
  5.  
  6. c.InjectJavaScript(thisAssembly, "SwashbuckleCustomAuth.CustomContent.basic-auth.js");
  7.  
  8. $('#explore').off();
  9.  
  10. $('#explore').click(function () {
  11. var key = $('#input_apiKey')[0].value;
  12. var credentials = key.split(':'); //username:password expected
  13.  
  14. $.ajax({
  15. url: "yourAuthEndpoint",
  16. type: "post",
  17. contenttype: 'x-www-form-urlencoded',
  18. data: "grant_type=password&username=" + credentials[0] + "&password=" + credentials[1],
  19. success: function (response) {
  20. var bearerToken = 'Bearer ' + response.access_token;
  21.  
  22. window.swaggerUi.api.clientAuthorizations.add('Authorization', new SwaggerClient.ApiKeyAuthorization('Authorization', bearerToken, 'header'));
  23. window.swaggerUi.api.clientAuthorizations.remove("api_key");
  24. alert("Login successfull");
  25. },
  26. error: function (xhr, ajaxoptions, thrownerror) {
  27. alert("Login failed!");
  28. }
  29. });
  30. });
  31.  
  32. function addApiKeyAuthorization() {
  33. var key = encodeURIComponent($('#input_apiKey')[0].value);
  34. if (key && key.trim() != "") {
  35. var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("NAME OF PARAMETER", key, "query");
  36. window.swaggerUi.api.clientAuthorizations.add("api_key", apiKeyAuth);
  37. log("added key " + key);
  38. }
  39. }
  40.  
  41. {
  42. "swagger": "2.0",
  43. "info": {
  44. "description": "API Documentation",
  45. "version": "1.0.2",
  46. "title": "my super service"
  47. },
  48. "basePath": "/my-super-services/api",
  49. "host": "example.com",
  50. "schemes": [
  51. "https",
  52. "http"
  53. ],
  54. "securityDefinitions": {
  55. "UserSecurity": {
  56. "type": "apiKey",
  57. "in": "header", <----- set this to header or cookie or query
  58. "name": "X-Api-Key" <----- you custom key goes here
  59. }
  60. },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement