Guest User

Untitled

a guest
Dec 20th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. import { ZObject, Bundle } from "zapier-platform-core";
  2. import IAuthenticationScheme from "../interfaces/authentication/IAuthenticationScheme";
  3.  
  4. const getSessionKey = (z: ZObject, bundle: Bundle) => {
  5. console.log('GET SESSION called');
  6. const { username: auth_login, password: auth_password } = bundle.authData;
  7. return z.request({
  8. method: 'POST',
  9. url: 'http://******/perl/auth/login',
  10. body: { auth_login, auth_password }
  11. }).then(response => {
  12. z.console.log(response);
  13. console.log(response);
  14.  
  15. if (response.status === 401) {
  16. throw new Error('The username/password you supplied is invalid');
  17. } else {
  18. return {
  19. sessionKey: z.JSON.parse(response.content).session_id
  20. };
  21. }
  22. });
  23. };
  24.  
  25. const includeSessionKeyHeader = (request: any, z: ZObject, bundle: Bundle) => {
  26. console.log('includeSessionKeyHeader called');
  27.  
  28. if (bundle.authData.sessionKey) {
  29. request.headers = Object.assign({}, request.headers);
  30. let { Cookie: cookie = '' } = request.headers;
  31. cookie = `${bundle.authData.sessionKey};${cookie}`;
  32. request.headers['Cookie'] = cookie;
  33. }
  34. return request;
  35. };
  36.  
  37. const sessionRefreshIf401 = (response: any, z: ZObject, bundle: Bundle) => {
  38. console.warn('sessionRefreshIf401 called');
  39. if (bundle.authData.sessionKey) {
  40. if (response.status === 401) {
  41. throw new z.errors.RefreshAuthError(); // ask for a refresh & retry
  42. }
  43. }
  44. return response;
  45. };
  46.  
  47. const test = (z: ZObject, bundle: Bundle) => {
  48. console.log('test called');
  49. return z.request({
  50. url: 'http://******/ruby/features'
  51. }).then((response) => {
  52. z.console.log(response);
  53. if (response.status === 401) {
  54. throw new Error('The API Key you supplied is invalid');
  55. }
  56. return response
  57. });
  58. };
  59.  
  60. const authentication: IAuthenticationScheme<any> = {
  61. type: 'session',
  62. test,
  63. fields: [
  64. {
  65. key: 'username',
  66. type: 'string',
  67. required: true,
  68. helpText: 'Your login username.'
  69. },
  70. {
  71. key: 'password',
  72. type: 'string',
  73. required: true,
  74. helpText: 'Your login password.'
  75. }
  76. ],
  77. connectionLabel: (z, bundle) => {
  78. return bundle.inputData.username;
  79. },
  80. sessionConfig: {
  81. perform: getSessionKey
  82. }
  83. };
  84.  
  85. export default {
  86. authentication,
  87. beforeRequest: { includeSessionKeyHeader },
  88. afterRequest: { sessionRefreshIf401 }
  89. };
  90.  
  91. import { should } from "should";
  92. import { describe } from "mocha";
  93. const { version } = require("../../package.json");
  94. import { version as platformVersion } from "zapier-platform-core";
  95. import { createAppTester } from "zapier-platform-core";
  96. import PlackSession from "../authentication/PlackSession";
  97.  
  98. const App = {
  99. version,
  100. platformVersion,
  101. authentication: PlackSession.authentication,
  102. beforeRequest: [PlackSession.beforeRequest.includeSessionKeyHeader],
  103. afterResponse: [PlackSession.afterRequest.sessionRefreshIf401],
  104. };
  105.  
  106. const appTester = createAppTester(App);
  107.  
  108. export default () => {
  109. describe('PlackSession authentication', () => {
  110. it('should authenticate', done => {
  111. console.log(`AUTHENTICATE!!`)
  112. const bundle = {
  113. authData: {
  114. username: 'dev@******.com',
  115. password: 'abc123'
  116. }
  117. };
  118.  
  119. appTester(App.authentication.test, bundle)
  120. .then(response => {
  121. console.log('BBBBBBBB')
  122. done();
  123. })
  124. .catch(a => {
  125. console.log('CCCCCC');
  126. done(a)
  127. });
  128.  
  129. });
  130. });
  131. };
  132.  
  133. authentication
  134. PlackSession authentication
  135. AUTHENTICATE!!
  136. test called
  137. includeSessionKeyHeader called
  138. CCCCCC
  139. 1) should authenticate
Add Comment
Please, Sign In to add comment