@whook/oauth2 8.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (146) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +384 -0
  3. package/dist/handlers/getOAuth2Authorize.d.ts +26 -0
  4. package/dist/handlers/getOAuth2Authorize.js +192 -0
  5. package/dist/handlers/getOAuth2Authorize.js.map +1 -0
  6. package/dist/handlers/getOAuth2Authorize.mjs +169 -0
  7. package/dist/handlers/getOAuth2Authorize.mjs.map +1 -0
  8. package/dist/handlers/getOAuth2Authorize.test.d.ts +1 -0
  9. package/dist/handlers/getOAuth2Authorize.test.js +122 -0
  10. package/dist/handlers/getOAuth2Authorize.test.js.map +1 -0
  11. package/dist/handlers/getOAuth2Authorize.test.mjs +114 -0
  12. package/dist/handlers/getOAuth2Authorize.test.mjs.map +1 -0
  13. package/dist/handlers/postAuthLogin.d.ts +45 -0
  14. package/dist/handlers/postAuthLogin.js +125 -0
  15. package/dist/handlers/postAuthLogin.js.map +1 -0
  16. package/dist/handlers/postAuthLogin.mjs +108 -0
  17. package/dist/handlers/postAuthLogin.mjs.map +1 -0
  18. package/dist/handlers/postAuthLogin.test.d.ts +1 -0
  19. package/dist/handlers/postAuthLogin.test.js +90 -0
  20. package/dist/handlers/postAuthLogin.test.js.map +1 -0
  21. package/dist/handlers/postAuthLogin.test.mjs +85 -0
  22. package/dist/handlers/postAuthLogin.test.mjs.map +1 -0
  23. package/dist/handlers/postAuthLogout.d.ts +12 -0
  24. package/dist/handlers/postAuthLogout.js +46 -0
  25. package/dist/handlers/postAuthLogout.js.map +1 -0
  26. package/dist/handlers/postAuthLogout.mjs +33 -0
  27. package/dist/handlers/postAuthLogout.mjs.map +1 -0
  28. package/dist/handlers/postAuthLogout.test.d.ts +1 -0
  29. package/dist/handlers/postAuthLogout.test.js +35 -0
  30. package/dist/handlers/postAuthLogout.test.js.map +1 -0
  31. package/dist/handlers/postAuthLogout.test.mjs +30 -0
  32. package/dist/handlers/postAuthLogout.test.mjs.map +1 -0
  33. package/dist/handlers/postAuthRefresh.d.ts +45 -0
  34. package/dist/handlers/postAuthRefresh.js +140 -0
  35. package/dist/handlers/postAuthRefresh.js.map +1 -0
  36. package/dist/handlers/postAuthRefresh.mjs +122 -0
  37. package/dist/handlers/postAuthRefresh.mjs.map +1 -0
  38. package/dist/handlers/postAuthRefresh.test.d.ts +1 -0
  39. package/dist/handlers/postAuthRefresh.test.js +99 -0
  40. package/dist/handlers/postAuthRefresh.test.js.map +1 -0
  41. package/dist/handlers/postAuthRefresh.test.mjs +94 -0
  42. package/dist/handlers/postAuthRefresh.test.mjs.map +1 -0
  43. package/dist/handlers/postOAuth2Acknowledge.d.ts +28 -0
  44. package/dist/handlers/postOAuth2Acknowledge.js +165 -0
  45. package/dist/handlers/postOAuth2Acknowledge.js.map +1 -0
  46. package/dist/handlers/postOAuth2Acknowledge.mjs +150 -0
  47. package/dist/handlers/postOAuth2Acknowledge.mjs.map +1 -0
  48. package/dist/handlers/postOAuth2Acknowledge.test.d.ts +1 -0
  49. package/dist/handlers/postOAuth2Acknowledge.test.js +148 -0
  50. package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -0
  51. package/dist/handlers/postOAuth2Acknowledge.test.mjs +140 -0
  52. package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +1 -0
  53. package/dist/handlers/postOAuth2Token.d.ts +36 -0
  54. package/dist/handlers/postOAuth2Token.js +263 -0
  55. package/dist/handlers/postOAuth2Token.js.map +1 -0
  56. package/dist/handlers/postOAuth2Token.mjs +242 -0
  57. package/dist/handlers/postOAuth2Token.mjs.map +1 -0
  58. package/dist/handlers/postOAuth2Token.test.d.ts +1 -0
  59. package/dist/handlers/postOAuth2Token.test.js +166 -0
  60. package/dist/handlers/postOAuth2Token.test.js.map +1 -0
  61. package/dist/handlers/postOAuth2Token.test.mjs +160 -0
  62. package/dist/handlers/postOAuth2Token.test.mjs.map +1 -0
  63. package/dist/index.d.ts +17 -0
  64. package/dist/index.js +230 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/index.mjs +15 -0
  67. package/dist/index.mjs.map +1 -0
  68. package/dist/index.test.d.ts +1 -0
  69. package/dist/index.test.js +759 -0
  70. package/dist/index.test.js.map +1 -0
  71. package/dist/index.test.mjs +748 -0
  72. package/dist/index.test.mjs.map +1 -0
  73. package/dist/services/authCookies.d.ts +26 -0
  74. package/dist/services/authCookies.js +74 -0
  75. package/dist/services/authCookies.js.map +1 -0
  76. package/dist/services/authCookies.mjs +58 -0
  77. package/dist/services/authCookies.mjs.map +1 -0
  78. package/dist/services/authCookies.test.d.ts +1 -0
  79. package/dist/services/authCookies.test.js +82 -0
  80. package/dist/services/authCookies.test.js.map +1 -0
  81. package/dist/services/authCookies.test.mjs +77 -0
  82. package/dist/services/authCookies.test.mjs.map +1 -0
  83. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +17 -0
  84. package/dist/services/oAuth2ClientCredentialsGranter.js +56 -0
  85. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -0
  86. package/dist/services/oAuth2ClientCredentialsGranter.mjs +41 -0
  87. package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +1 -0
  88. package/dist/services/oAuth2CodeGranter.d.ts +17 -0
  89. package/dist/services/oAuth2CodeGranter.js +115 -0
  90. package/dist/services/oAuth2CodeGranter.js.map +1 -0
  91. package/dist/services/oAuth2CodeGranter.mjs +98 -0
  92. package/dist/services/oAuth2CodeGranter.mjs.map +1 -0
  93. package/dist/services/oAuth2CodeGranter.test.d.ts +1 -0
  94. package/dist/services/oAuth2CodeGranter.test.js +86 -0
  95. package/dist/services/oAuth2CodeGranter.test.js.map +1 -0
  96. package/dist/services/oAuth2CodeGranter.test.mjs +81 -0
  97. package/dist/services/oAuth2CodeGranter.test.mjs.map +1 -0
  98. package/dist/services/oAuth2Granters.d.ts +79 -0
  99. package/dist/services/oAuth2Granters.js +140 -0
  100. package/dist/services/oAuth2Granters.js.map +1 -0
  101. package/dist/services/oAuth2Granters.mjs +127 -0
  102. package/dist/services/oAuth2Granters.mjs.map +1 -0
  103. package/dist/services/oAuth2PasswordGranter.d.ts +16 -0
  104. package/dist/services/oAuth2PasswordGranter.js +55 -0
  105. package/dist/services/oAuth2PasswordGranter.js.map +1 -0
  106. package/dist/services/oAuth2PasswordGranter.mjs +40 -0
  107. package/dist/services/oAuth2PasswordGranter.mjs.map +1 -0
  108. package/dist/services/oAuth2RefreshTokenGranter.d.ts +15 -0
  109. package/dist/services/oAuth2RefreshTokenGranter.js +62 -0
  110. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -0
  111. package/dist/services/oAuth2RefreshTokenGranter.mjs +47 -0
  112. package/dist/services/oAuth2RefreshTokenGranter.mjs.map +1 -0
  113. package/dist/services/oAuth2TokenGranter.d.ts +15 -0
  114. package/dist/services/oAuth2TokenGranter.js +92 -0
  115. package/dist/services/oAuth2TokenGranter.js.map +1 -0
  116. package/dist/services/oAuth2TokenGranter.mjs +78 -0
  117. package/dist/services/oAuth2TokenGranter.mjs.map +1 -0
  118. package/package.json +205 -0
  119. package/src/__snapshots__/index.test.ts.snap +396 -0
  120. package/src/handlers/__snapshots__/getOAuth2Authorize.test.ts.snap +38 -0
  121. package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +61 -0
  122. package/src/handlers/__snapshots__/postOAuth2Token.test.ts.snap +52 -0
  123. package/src/handlers/getOAuth2Authorize.test.ts +142 -0
  124. package/src/handlers/getOAuth2Authorize.ts +222 -0
  125. package/src/handlers/postAuthLogin.test.ts +88 -0
  126. package/src/handlers/postAuthLogin.ts +123 -0
  127. package/src/handlers/postAuthLogout.test.ts +33 -0
  128. package/src/handlers/postAuthLogout.ts +38 -0
  129. package/src/handlers/postAuthRefresh.test.ts +97 -0
  130. package/src/handlers/postAuthRefresh.ts +146 -0
  131. package/src/handlers/postOAuth2Acknowledge.test.ts +175 -0
  132. package/src/handlers/postOAuth2Acknowledge.ts +209 -0
  133. package/src/handlers/postOAuth2Token.test.ts +153 -0
  134. package/src/handlers/postOAuth2Token.ts +302 -0
  135. package/src/index.test.ts +936 -0
  136. package/src/index.ts +105 -0
  137. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +49 -0
  138. package/src/services/authCookies.test.ts +93 -0
  139. package/src/services/authCookies.ts +82 -0
  140. package/src/services/oAuth2ClientCredentialsGranter.ts +76 -0
  141. package/src/services/oAuth2CodeGranter.test.ts +94 -0
  142. package/src/services/oAuth2CodeGranter.ts +133 -0
  143. package/src/services/oAuth2Granters.ts +274 -0
  144. package/src/services/oAuth2PasswordGranter.ts +75 -0
  145. package/src/services/oAuth2RefreshTokenGranter.ts +77 -0
  146. package/src/services/oAuth2TokenGranter.ts +105 -0
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = exports.definition = exports.authCookieHeaderParameter = void 0;
7
+
8
+ var _knifecycle = require("knifecycle");
9
+
10
+ var _yhttperror = _interopRequireDefault(require("yhttperror"));
11
+
12
+ var _authCookies = require("../services/authCookies");
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ const authCookieHeaderParameter = {
17
+ name: 'cookie',
18
+ example: 'access_token=an_access_token; refresh_token=a_refresh_token;',
19
+ parameter: {
20
+ name: 'cookie',
21
+ in: 'header',
22
+ required: false,
23
+ schema: {
24
+ type: 'string'
25
+ }
26
+ }
27
+ };
28
+ exports.authCookieHeaderParameter = authCookieHeaderParameter;
29
+ const definition = {
30
+ method: 'post',
31
+ path: `${_authCookies.AUTH_API_PREFIX}/refresh`,
32
+ operation: {
33
+ operationId: 'postAuthRefresh',
34
+ summary: 'Refreshs a user auth',
35
+ tags: ['auth'],
36
+ 'x-whook': {
37
+ disabled: true
38
+ },
39
+ parameters: [{
40
+ $ref: `#/components/parameters/${authCookieHeaderParameter.name}`
41
+ }],
42
+ requestBody: {
43
+ required: true,
44
+ content: {
45
+ 'application/json': {
46
+ schema: {
47
+ type: 'object',
48
+ required: [],
49
+ properties: {
50
+ scope: {
51
+ type: 'string'
52
+ },
53
+ remember: {
54
+ type: 'boolean'
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+ },
61
+ responses: {
62
+ 200: {
63
+ description: 'Successfuly refreshed.',
64
+ content: {
65
+ 'application/json': {
66
+ schema: {
67
+ type: 'object',
68
+ properties: {
69
+ access_token: {
70
+ type: 'string'
71
+ },
72
+ expiration_date: {
73
+ type: 'string'
74
+ },
75
+ expires_in: {
76
+ type: 'string'
77
+ },
78
+ token_type: {
79
+ type: 'string'
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
86
+ }
87
+ }
88
+ };
89
+ exports.definition = definition;
90
+
91
+ var _default = (0, _knifecycle.handler)(postAuthRefresh, "postAuthRefresh", ["ROOT_AUTHENTICATION_DATA", "authCookies", "postOAuth2Token"]);
92
+
93
+ exports.default = _default;
94
+
95
+ async function postAuthRefresh({
96
+ ROOT_AUTHENTICATION_DATA,
97
+ authCookies,
98
+ postOAuth2Token
99
+ }, {
100
+ body,
101
+ cookie = ''
102
+ }) {
103
+ const parsedCookies = authCookies.parse(cookie);
104
+
105
+ try {
106
+ if (!parsedCookies.refresh_token) {
107
+ throw new _yhttperror.default(401, 'E_REFRESH_COOKIE', cookie);
108
+ }
109
+
110
+ const response = await postOAuth2Token({
111
+ body: {
112
+ grant_type: 'refresh_token',
113
+ scope: body.scope,
114
+ refresh_token: parsedCookies.refresh_token
115
+ },
116
+ authenticationData: ROOT_AUTHENTICATION_DATA
117
+ });
118
+ return { ...response,
119
+ headers: { ...(response.headers || {}),
120
+ 'Set-Cookie': authCookies.build(response.status === 200 ? response.body : undefined, {
121
+ session: !body.remember
122
+ })
123
+ },
124
+ body: {
125
+ access_token: response.body.access_token,
126
+ expiration_date: response.body.expiration_date,
127
+ expires_in: response.body.expires_in,
128
+ token_type: response.body.token_type
129
+ }
130
+ };
131
+ } catch (err) {
132
+ const newErr = _yhttperror.default.wrap(err);
133
+
134
+ newErr.headers = { ...(err.headers || {}),
135
+ 'Set-Cookie': authCookies.build()
136
+ };
137
+ throw newErr;
138
+ }
139
+ }
140
+ //# sourceMappingURL=postAuthRefresh.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/handlers/postAuthRefresh.ts"],"names":["authCookieHeaderParameter","name","example","parameter","in","required","schema","type","definition","method","path","AUTH_API_PREFIX","operation","operationId","summary","tags","disabled","parameters","$ref","requestBody","content","properties","scope","remember","responses","description","access_token","expiration_date","expires_in","token_type","postAuthRefresh","ROOT_AUTHENTICATION_DATA","authCookies","postOAuth2Token","body","cookie","parsedCookies","parse","refresh_token","YHTTPError","response","grant_type","authenticationData","headers","build","status","undefined","session","err","newErr","wrap"],"mappings":";;;;;;;AAAA;;AACA;;AAEA;;;;AAYO,MAAMA,yBAA8D,GAAG;AAC5EC,EAAAA,IAAI,EAAE,QADsE;AAE5EC,EAAAA,OAAO,EAAE,8DAFmE;AAG5EC,EAAAA,SAAS,EAAE;AACTF,IAAAA,IAAI,EAAE,QADG;AAETG,IAAAA,EAAE,EAAE,QAFK;AAGTC,IAAAA,QAAQ,EAAE,KAHD;AAITC,IAAAA,MAAM,EAAE;AACNC,MAAAA,IAAI,EAAE;AADA;AAJC;AAHiE,CAAvE;;AAaA,MAAMC,UAAqC,GAAG;AACnDC,EAAAA,MAAM,EAAE,MAD2C;AAEnDC,EAAAA,IAAI,EAAG,GAAEC,4BAAgB,UAF0B;AAGnDC,EAAAA,SAAS,EAAE;AACTC,IAAAA,WAAW,EAAE,iBADJ;AAETC,IAAAA,OAAO,EAAE,sBAFA;AAGTC,IAAAA,IAAI,EAAE,CAAC,MAAD,CAHG;AAIT,eAAW;AACTC,MAAAA,QAAQ,EAAE;AADD,KAJF;AAOTC,IAAAA,UAAU,EAAE,CACV;AACEC,MAAAA,IAAI,EAAG,2BAA0BlB,yBAAyB,CAACC,IAAK;A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Error from 'yhttperror';\nimport initPostOauth2Token from './postOAuth2Token';\nimport { AUTH_API_PREFIX } from '../services/authCookies';\nimport type {\n WhookAPIHandlerDefinition,\n WhookAPIParameterDefinition,\n} from '@whook/whook';\nimport type {\n AuthCookiesService,\n AuthHandlersConfig,\n} from '../services/authCookies';\nimport type { AsyncReturnType } from 'type-fest';\nimport type { BaseAuthenticationData } from '@whook/authorization';\n\nexport const authCookieHeaderParameter: WhookAPIParameterDefinition<string> = {\n name: 'cookie',\n example: 'access_token=an_access_token; refresh_token=a_refresh_token;',\n parameter: {\n name: 'cookie',\n in: 'header',\n required: false,\n schema: {\n type: 'string',\n },\n },\n};\n\nexport const definition: WhookAPIHandlerDefinition = {\n method: 'post',\n path: `${AUTH_API_PREFIX}/refresh`,\n operation: {\n operationId: 'postAuthRefresh',\n summary: 'Refreshs a user auth',\n tags: ['auth'],\n 'x-whook': {\n disabled: true,\n },\n parameters: [\n {\n $ref: `#/components/parameters/${authCookieHeaderParameter.name}`,\n },\n ],\n requestBody: {\n required: true,\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n required: [],\n properties: {\n scope: { type: 'string' },\n remember: { type: 'boolean' },\n },\n },\n },\n },\n },\n responses: {\n 200: {\n description: 'Successfuly refreshed.',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n access_token: { type: 'string' },\n expiration_date: { type: 'string' },\n expires_in: { type: 'string' },\n token_type: { type: 'string' },\n },\n },\n },\n },\n },\n },\n },\n};\n\nexport default autoHandler(postAuthRefresh);\n\nasync function postAuthRefresh<\n AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,\n>(\n {\n ROOT_AUTHENTICATION_DATA,\n authCookies,\n postOAuth2Token,\n }: AuthHandlersConfig<AUTHENTICATION_DATA> & {\n authCookies: AuthCookiesService;\n postOAuth2Token: AsyncReturnType<typeof initPostOauth2Token>;\n },\n {\n body,\n cookie = '',\n }: {\n body: {\n scope: string;\n remember: boolean;\n };\n cookie?: string;\n },\n) {\n const parsedCookies = authCookies.parse(cookie);\n\n try {\n if (!parsedCookies.refresh_token) {\n throw new YHTTPError(401, 'E_REFRESH_COOKIE', cookie);\n }\n\n const response = await postOAuth2Token({\n body: {\n grant_type: 'refresh_token',\n scope: body.scope,\n refresh_token: parsedCookies.refresh_token,\n },\n authenticationData: ROOT_AUTHENTICATION_DATA,\n });\n\n return {\n ...response,\n headers: {\n ...(response.headers || {}),\n 'Set-Cookie': authCookies.build(\n response.status === 200 ? response.body : undefined,\n { session: !body.remember },\n ),\n },\n body: {\n access_token: response.body.access_token,\n expiration_date: response.body.expiration_date,\n expires_in: response.body.expires_in,\n token_type: response.body.token_type,\n },\n };\n } catch (err) {\n const newErr = YHTTPError.wrap(err);\n\n newErr.headers = {\n ...(err.headers || {}),\n 'Set-Cookie': authCookies.build(),\n };\n\n throw newErr;\n }\n}\n"],"file":"postAuthRefresh.js"}
@@ -0,0 +1,122 @@
1
+ import { handler as _handler } from 'knifecycle';
2
+ import YHTTPError from 'yhttperror';
3
+ import { AUTH_API_PREFIX } from '../services/authCookies';
4
+ export const authCookieHeaderParameter = {
5
+ name: 'cookie',
6
+ example: 'access_token=an_access_token; refresh_token=a_refresh_token;',
7
+ parameter: {
8
+ name: 'cookie',
9
+ in: 'header',
10
+ required: false,
11
+ schema: {
12
+ type: 'string'
13
+ }
14
+ }
15
+ };
16
+ export const definition = {
17
+ method: 'post',
18
+ path: `${AUTH_API_PREFIX}/refresh`,
19
+ operation: {
20
+ operationId: 'postAuthRefresh',
21
+ summary: 'Refreshs a user auth',
22
+ tags: ['auth'],
23
+ 'x-whook': {
24
+ disabled: true
25
+ },
26
+ parameters: [{
27
+ $ref: `#/components/parameters/${authCookieHeaderParameter.name}`
28
+ }],
29
+ requestBody: {
30
+ required: true,
31
+ content: {
32
+ 'application/json': {
33
+ schema: {
34
+ type: 'object',
35
+ required: [],
36
+ properties: {
37
+ scope: {
38
+ type: 'string'
39
+ },
40
+ remember: {
41
+ type: 'boolean'
42
+ }
43
+ }
44
+ }
45
+ }
46
+ }
47
+ },
48
+ responses: {
49
+ 200: {
50
+ description: 'Successfuly refreshed.',
51
+ content: {
52
+ 'application/json': {
53
+ schema: {
54
+ type: 'object',
55
+ properties: {
56
+ access_token: {
57
+ type: 'string'
58
+ },
59
+ expiration_date: {
60
+ type: 'string'
61
+ },
62
+ expires_in: {
63
+ type: 'string'
64
+ },
65
+ token_type: {
66
+ type: 'string'
67
+ }
68
+ }
69
+ }
70
+ }
71
+ }
72
+ }
73
+ }
74
+ }
75
+ };
76
+ export default _handler(postAuthRefresh, "postAuthRefresh", ["ROOT_AUTHENTICATION_DATA", "authCookies", "postOAuth2Token"]);
77
+
78
+ async function postAuthRefresh({
79
+ ROOT_AUTHENTICATION_DATA,
80
+ authCookies,
81
+ postOAuth2Token
82
+ }, {
83
+ body,
84
+ cookie = ''
85
+ }) {
86
+ const parsedCookies = authCookies.parse(cookie);
87
+
88
+ try {
89
+ if (!parsedCookies.refresh_token) {
90
+ throw new YHTTPError(401, 'E_REFRESH_COOKIE', cookie);
91
+ }
92
+
93
+ const response = await postOAuth2Token({
94
+ body: {
95
+ grant_type: 'refresh_token',
96
+ scope: body.scope,
97
+ refresh_token: parsedCookies.refresh_token
98
+ },
99
+ authenticationData: ROOT_AUTHENTICATION_DATA
100
+ });
101
+ return { ...response,
102
+ headers: { ...(response.headers || {}),
103
+ 'Set-Cookie': authCookies.build(response.status === 200 ? response.body : undefined, {
104
+ session: !body.remember
105
+ })
106
+ },
107
+ body: {
108
+ access_token: response.body.access_token,
109
+ expiration_date: response.body.expiration_date,
110
+ expires_in: response.body.expires_in,
111
+ token_type: response.body.token_type
112
+ }
113
+ };
114
+ } catch (err) {
115
+ const newErr = YHTTPError.wrap(err);
116
+ newErr.headers = { ...(err.headers || {}),
117
+ 'Set-Cookie': authCookies.build()
118
+ };
119
+ throw newErr;
120
+ }
121
+ }
122
+ //# sourceMappingURL=postAuthRefresh.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/handlers/postAuthRefresh.ts"],"names":["autoHandler","YHTTPError","AUTH_API_PREFIX","authCookieHeaderParameter","name","example","parameter","in","required","schema","type","definition","method","path","operation","operationId","summary","tags","disabled","parameters","$ref","requestBody","content","properties","scope","remember","responses","description","access_token","expiration_date","expires_in","token_type","postAuthRefresh","ROOT_AUTHENTICATION_DATA","authCookies","postOAuth2Token","body","cookie","parsedCookies","parse","refresh_token","response","grant_type","authenticationData","headers","build","status","undefined","session","err","newErr","wrap"],"mappings":"AAAA,SAASA,OAAW,IAAXA,QAAT,QAA4B,YAA5B;AACA,OAAOC,UAAP,MAAuB,YAAvB;AAEA,SAASC,eAAT,QAAgC,yBAAhC;AAYA,OAAO,MAAMC,yBAA8D,GAAG;AAC5EC,EAAAA,IAAI,EAAE,QADsE;AAE5EC,EAAAA,OAAO,EAAE,8DAFmE;AAG5EC,EAAAA,SAAS,EAAE;AACTF,IAAAA,IAAI,EAAE,QADG;AAETG,IAAAA,EAAE,EAAE,QAFK;AAGTC,IAAAA,QAAQ,EAAE,KAHD;AAITC,IAAAA,MAAM,EAAE;AACNC,MAAAA,IAAI,EAAE;AADA;AAJC;AAHiE,CAAvE;AAaP,OAAO,MAAMC,UAAqC,GAAG;AACnDC,EAAAA,MAAM,EAAE,MAD2C;AAEnDC,EAAAA,IAAI,EAAG,GAAEX,eAAgB,UAF0B;AAGnDY,EAAAA,SAAS,EAAE;AACTC,IAAAA,WAAW,EAAE,iBADJ;AAETC,IAAAA,OAAO,EAAE,sBAFA;AAGTC,IAAAA,IAAI,EAAE,CAAC,MAAD,CAHG;AAIT,eAAW;AACTC,MAAAA,QAAQ,EAAE;AADD,KAJF;AAOTC,IAAAA,UAAU,EAAE,CACV;AACEC,MAAAA,IAAI,EAAG,2BAA0BjB,yBAAyB,CAACC,IAAK;AADlE,KADU,CAPH;AAYTiB,IAAAA,WAAW,EAAE;AACXb,MAAAA,QAAQ,EAAE,IADC;AAEXc,MAAAA,OAAO,EAAE;AACP,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Q,GAAG,MAAMN,eAAe,CAAC;AACrCC,MAAAA,IAAI,EAAE;AACJM,QAAAA,UAAU,EAAE,eADR;AAEJlB,QAAAA,KAAK,EAAEY,IAAI,CAACZ,KAFR;AAGJgB,QAAAA,aAAa,EAAEF,aAAa,CAACE;AAHzB,OAD+B;AAMrCG,MAAAA,kBAAkB,EAAEV;AANiB,KAAD,CAAtC;AASA,WAAO,EACL,GAAGQ,QADE;AAELG,MAAAA,OAAO,EAAE,EACP,IAAIH,QAAQ,CAACG,OAAT,IAAoB,EAAxB,CADO;AAEP,sBAAcV,WAAW,CAACW,KAAZ,CACZJ,QAAQ,CAACK,MAAT,KAAoB,GAApB,GAA0BL,QAAQ,CAACL,IAAnC,GAA0CW,SAD9B,EAEZ;AAAEC,UAAAA,OAAO,EAAE,CAACZ,IAAI,CAACX;AAAjB,SAFY;AAFP,OAFJ;AASLW,MAAAA,IAAI,EAAE;AACJR,QAAAA,YAAY,EAAEa,QAAQ,CAACL,IAAT,CAAcR,YADxB;AAEJC,QAAAA,eAAe,EAAEY,QAAQ,CAACL,IAAT,CAAcP,eAF3B;AAGJC,QAAAA,UAAU,EAAEW,QAAQ,CAACL,IAAT,CAAcN,UAHtB;AAIJC,QAAAA,UAAU,EAAEU,QAAQ,CAACL,IAAT,CAAcL;AAJtB;AATD,KAAP;AAgBD,GA9BD,CA8BE,OAAOkB,GAAP,EAAY;AACZ,UAAMC,MAAM,GAAGjD,UAAU,CAACkD,IAAX,CAAgBF,GAAhB,CAAf;AAEAC,IAAAA,MAAM,CAACN,OAAP,GAAiB,EACf,IAAIK,GAAG,CAACL,OAAJ,IAAe,EAAnB,CADe;AAEf,oBAAcV,WAAW,CAACW,KAAZ;AAFC,KAAjB;AAKA,UAAMK,MAAN;AACD;AACF","sourcesContent":["import { autoHandler } from 'knifecycle';\nimport YHTTPError from 'yhttperror';\nimport initPostOauth2Token from './postOAuth2Token';\nimport { AUTH_API_PREFIX } from '../services/authCookies';\nimport type {\n WhookAPIHandlerDefinition,\n WhookAPIParameterDefinition,\n} from '@whook/whook';\nimport type {\n AuthCookiesService,\n AuthHandlersConfig,\n} from '../services/authCookies';\nimport type { AsyncReturnType } from 'type-fest';\nimport type { BaseAuthenticationData } from '@whook/authorization';\n\nexport const authCookieHeaderParameter: WhookAPIParameterDefinition<string> = {\n name: 'cookie',\n example: 'access_token=an_access_token; refresh_token=a_refresh_token;',\n parameter: {\n name: 'cookie',\n in: 'header',\n required: false,\n schema: {\n type: 'string',\n },\n },\n};\n\nexport const definition: WhookAPIHandlerDefinition = {\n method: 'post',\n path: `${AUTH_API_PREFIX}/refresh`,\n operation: {\n operationId: 'postAuthRefresh',\n summary: 'Refreshs a user auth',\n tags: ['auth'],\n 'x-whook': {\n disabled: true,\n },\n parameters: [\n {\n $ref: `#/components/parameters/${authCookieHeaderParameter.name}`,\n },\n ],\n requestBody: {\n required: true,\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n required: [],\n properties: {\n scope: { type: 'string' },\n remember: { type: 'boolean' },\n },\n },\n },\n },\n },\n responses: {\n 200: {\n description: 'Successfuly refreshed.',\n content: {\n 'application/json': {\n schema: {\n type: 'object',\n properties: {\n access_token: { type: 'string' },\n expiration_date: { type: 'string' },\n expires_in: { type: 'string' },\n token_type: { type: 'string' },\n },\n },\n },\n },\n },\n },\n },\n};\n\nexport default autoHandler(postAuthRefresh);\n\nasync function postAuthRefresh<\n AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,\n>(\n {\n ROOT_AUTHENTICATION_DATA,\n authCookies,\n postOAuth2Token,\n }: AuthHandlersConfig<AUTHENTICATION_DATA> & {\n authCookies: AuthCookiesService;\n postOAuth2Token: AsyncReturnType<typeof initPostOauth2Token>;\n },\n {\n body,\n cookie = '',\n }: {\n body: {\n scope: string;\n remember: boolean;\n };\n cookie?: string;\n },\n) {\n const parsedCookies = authCookies.parse(cookie);\n\n try {\n if (!parsedCookies.refresh_token) {\n throw new YHTTPError(401, 'E_REFRESH_COOKIE', cookie);\n }\n\n const response = await postOAuth2Token({\n body: {\n grant_type: 'refresh_token',\n scope: body.scope,\n refresh_token: parsedCookies.refresh_token,\n },\n authenticationData: ROOT_AUTHENTICATION_DATA,\n });\n\n return {\n ...response,\n headers: {\n ...(response.headers || {}),\n 'Set-Cookie': authCookies.build(\n response.status === 200 ? response.body : undefined,\n { session: !body.remember },\n ),\n },\n body: {\n access_token: response.body.access_token,\n expiration_date: response.body.expiration_date,\n expires_in: response.body.expires_in,\n token_type: response.body.token_type,\n },\n };\n } catch (err) {\n const newErr = YHTTPError.wrap(err);\n\n newErr.headers = {\n ...(err.headers || {}),\n 'Set-Cookie': authCookies.build(),\n };\n\n throw newErr;\n }\n}\n"],"file":"postAuthRefresh.mjs"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+
3
+ var _postAuthRefresh = _interopRequireDefault(require("./postAuthRefresh"));
4
+
5
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
6
+
7
+ describe('postAuthRefresh', () => {
8
+ test('should work', async () => {
9
+ const authCookies = {
10
+ parse: jest.fn(),
11
+ build: jest.fn()
12
+ };
13
+ const postOAuth2Token = jest.fn();
14
+ const postAuthRefresh = await (0, _postAuthRefresh.default)({
15
+ ROOT_AUTHENTICATION_DATA: {
16
+ scope: 'user',
17
+ applicationId: 'root_app_id'
18
+ },
19
+ authCookies,
20
+ postOAuth2Token
21
+ });
22
+ authCookies.parse.mockReturnValueOnce({
23
+ access_token: 'an_access_token',
24
+ refresh_token: 'a_refresh_token'
25
+ });
26
+ authCookies.build.mockReturnValueOnce('the_build_cookies');
27
+ postOAuth2Token.mockResolvedValueOnce({
28
+ status: 200,
29
+ body: {
30
+ access_token: 'an_access_token',
31
+ expiration_date: '2020-02-02T20:22:02Z',
32
+ expires_in: 3600,
33
+ token_type: 'bearer'
34
+ }
35
+ });
36
+ const response = await postAuthRefresh({
37
+ cookie: 'a_given_cookie',
38
+ body: {
39
+ remember: false,
40
+ scope: 'user'
41
+ }
42
+ });
43
+ expect({
44
+ response,
45
+ authCookiesParseCalls: authCookies.parse.mock.calls,
46
+ authCookiesBuildCalls: authCookies.build.mock.calls,
47
+ postOAuth2TokenCalls: postOAuth2Token.mock.calls
48
+ }).toMatchInlineSnapshot(`
49
+ Object {
50
+ "authCookiesBuildCalls": Array [
51
+ Array [
52
+ Object {
53
+ "access_token": "an_access_token",
54
+ "expiration_date": "2020-02-02T20:22:02Z",
55
+ "expires_in": 3600,
56
+ "token_type": "bearer",
57
+ },
58
+ Object {
59
+ "session": true,
60
+ },
61
+ ],
62
+ ],
63
+ "authCookiesParseCalls": Array [
64
+ Array [
65
+ "a_given_cookie",
66
+ ],
67
+ ],
68
+ "postOAuth2TokenCalls": Array [
69
+ Array [
70
+ Object {
71
+ "authenticationData": Object {
72
+ "applicationId": "root_app_id",
73
+ "scope": "user",
74
+ },
75
+ "body": Object {
76
+ "grant_type": "refresh_token",
77
+ "refresh_token": "a_refresh_token",
78
+ "scope": "user",
79
+ },
80
+ },
81
+ ],
82
+ ],
83
+ "response": Object {
84
+ "body": Object {
85
+ "access_token": "an_access_token",
86
+ "expiration_date": "2020-02-02T20:22:02Z",
87
+ "expires_in": 3600,
88
+ "token_type": "bearer",
89
+ },
90
+ "headers": Object {
91
+ "Set-Cookie": "the_build_cookies",
92
+ },
93
+ "status": 200,
94
+ },
95
+ }
96
+ `);
97
+ });
98
+ });
99
+ //# sourceMappingURL=postAuthRefresh.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/handlers/postAuthRefresh.test.ts"],"names":["describe","test","authCookies","parse","jest","fn","build","postOAuth2Token","postAuthRefresh","ROOT_AUTHENTICATION_DATA","scope","applicationId","mockReturnValueOnce","access_token","refresh_token","mockResolvedValueOnce","status","body","expiration_date","expires_in","token_type","response","cookie","remember","expect","authCookiesParseCalls","mock","calls","authCookiesBuildCalls","postOAuth2TokenCalls","toMatchInlineSnapshot"],"mappings":";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uBC,KAFzC;AAGLC,MAAAA,qBAAqB,EAAE1B,WAAW,CAACI,KAAZ,CAAkBoB,IAAlB,CAAuBC,KAHzC;AAILE,MAAAA,oBAAoB,EAAEtB,eAAe,CAACmB,IAAhB,CAAqBC;AAJtC,KAAD,CAAN,CAKGG,qBALH,CAK0B;AAC9B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,KArDI;AAsDD,GA5FG,CAAJ;AA6FD,CA9FO,CAAR","sourcesContent":["import initPostAuthRefresh from './postAuthRefresh';\n\ndescribe('postAuthRefresh', () => {\n test('should work', async () => {\n const authCookies = {\n parse: jest.fn(),\n build: jest.fn(),\n };\n const postOAuth2Token = jest.fn();\n const postAuthRefresh = await initPostAuthRefresh({\n ROOT_AUTHENTICATION_DATA: {\n scope: 'user',\n applicationId: 'root_app_id',\n },\n authCookies,\n postOAuth2Token,\n });\n\n authCookies.parse.mockReturnValueOnce({\n access_token: 'an_access_token',\n refresh_token: 'a_refresh_token',\n });\n authCookies.build.mockReturnValueOnce('the_build_cookies');\n postOAuth2Token.mockResolvedValueOnce({\n status: 200,\n body: {\n access_token: 'an_access_token',\n expiration_date: '2020-02-02T20:22:02Z',\n expires_in: 3600,\n token_type: 'bearer',\n },\n });\n\n const response = await postAuthRefresh({\n cookie: 'a_given_cookie',\n body: {\n remember: false,\n scope: 'user',\n },\n });\n\n expect({\n response,\n authCookiesParseCalls: authCookies.parse.mock.calls,\n authCookiesBuildCalls: authCookies.build.mock.calls,\n postOAuth2TokenCalls: postOAuth2Token.mock.calls,\n }).toMatchInlineSnapshot(`\n Object {\n \"authCookiesBuildCalls\": Array [\n Array [\n Object {\n \"access_token\": \"an_access_token\",\n \"expiration_date\": \"2020-02-02T20:22:02Z\",\n \"expires_in\": 3600,\n \"token_type\": \"bearer\",\n },\n Object {\n \"session\": true,\n },\n ],\n ],\n \"authCookiesParseCalls\": Array [\n Array [\n \"a_given_cookie\",\n ],\n ],\n \"postOAuth2TokenCalls\": Array [\n Array [\n Object {\n \"authenticationData\": Object {\n \"applicationId\": \"root_app_id\",\n \"scope\": \"user\",\n },\n \"body\": Object {\n \"grant_type\": \"refresh_token\",\n \"refresh_token\": \"a_refresh_token\",\n \"scope\": \"user\",\n },\n },\n ],\n ],\n \"response\": Object {\n \"body\": Object {\n \"access_token\": \"an_access_token\",\n \"expiration_date\": \"2020-02-02T20:22:02Z\",\n \"expires_in\": 3600,\n \"token_type\": \"bearer\",\n },\n \"headers\": Object {\n \"Set-Cookie\": \"the_build_cookies\",\n },\n \"status\": 200,\n },\n }\n `);\n });\n});\n"],"file":"postAuthRefresh.test.js"}
@@ -0,0 +1,94 @@
1
+ import initPostAuthRefresh from './postAuthRefresh';
2
+ describe('postAuthRefresh', () => {
3
+ test('should work', async () => {
4
+ const authCookies = {
5
+ parse: jest.fn(),
6
+ build: jest.fn()
7
+ };
8
+ const postOAuth2Token = jest.fn();
9
+ const postAuthRefresh = await initPostAuthRefresh({
10
+ ROOT_AUTHENTICATION_DATA: {
11
+ scope: 'user',
12
+ applicationId: 'root_app_id'
13
+ },
14
+ authCookies,
15
+ postOAuth2Token
16
+ });
17
+ authCookies.parse.mockReturnValueOnce({
18
+ access_token: 'an_access_token',
19
+ refresh_token: 'a_refresh_token'
20
+ });
21
+ authCookies.build.mockReturnValueOnce('the_build_cookies');
22
+ postOAuth2Token.mockResolvedValueOnce({
23
+ status: 200,
24
+ body: {
25
+ access_token: 'an_access_token',
26
+ expiration_date: '2020-02-02T20:22:02Z',
27
+ expires_in: 3600,
28
+ token_type: 'bearer'
29
+ }
30
+ });
31
+ const response = await postAuthRefresh({
32
+ cookie: 'a_given_cookie',
33
+ body: {
34
+ remember: false,
35
+ scope: 'user'
36
+ }
37
+ });
38
+ expect({
39
+ response,
40
+ authCookiesParseCalls: authCookies.parse.mock.calls,
41
+ authCookiesBuildCalls: authCookies.build.mock.calls,
42
+ postOAuth2TokenCalls: postOAuth2Token.mock.calls
43
+ }).toMatchInlineSnapshot(`
44
+ Object {
45
+ "authCookiesBuildCalls": Array [
46
+ Array [
47
+ Object {
48
+ "access_token": "an_access_token",
49
+ "expiration_date": "2020-02-02T20:22:02Z",
50
+ "expires_in": 3600,
51
+ "token_type": "bearer",
52
+ },
53
+ Object {
54
+ "session": true,
55
+ },
56
+ ],
57
+ ],
58
+ "authCookiesParseCalls": Array [
59
+ Array [
60
+ "a_given_cookie",
61
+ ],
62
+ ],
63
+ "postOAuth2TokenCalls": Array [
64
+ Array [
65
+ Object {
66
+ "authenticationData": Object {
67
+ "applicationId": "root_app_id",
68
+ "scope": "user",
69
+ },
70
+ "body": Object {
71
+ "grant_type": "refresh_token",
72
+ "refresh_token": "a_refresh_token",
73
+ "scope": "user",
74
+ },
75
+ },
76
+ ],
77
+ ],
78
+ "response": Object {
79
+ "body": Object {
80
+ "access_token": "an_access_token",
81
+ "expiration_date": "2020-02-02T20:22:02Z",
82
+ "expires_in": 3600,
83
+ "token_type": "bearer",
84
+ },
85
+ "headers": Object {
86
+ "Set-Cookie": "the_build_cookies",
87
+ },
88
+ "status": 200,
89
+ },
90
+ }
91
+ `);
92
+ });
93
+ });
94
+ //# sourceMappingURL=postAuthRefresh.test.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/handlers/postAuthRefresh.test.ts"],"names":["initPostAuthRefresh","describe","test","authCookies","parse","jest","fn","build","postOAuth2Token","postAuthRefresh","ROOT_AUTHENTICATION_DATA","scope","applicationId","mockReturnValueOnce","access_token","refresh_token","mockResolvedValueOnce","status","body","expiration_date","expires_in","token_type","response","cookie","remember","expect","authCookiesParseCalls","mock","calls","authCookiesBuildCalls","postOAuth2TokenCalls","toMatchInlineSnapshot"],"mappings":"AAAA,OA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Z,CAAkBuB,IAAlB,CAAuBC,KAFzC;AAGLC,MAAAA,qBAAqB,EAAE1B,WAAW,CAACI,KAAZ,CAAkBoB,IAAlB,CAAuBC,KAHzC;AAILE,MAAAA,oBAAoB,EAAEtB,eAAe,CAACmB,IAAhB,CAAqBC;AAJtC,KAAD,CAAN,CAKGG,qBALH,CAK0B;AAC9B;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,KArDI;AAsDD,GA5FG,CAAJ;AA6FD,CA9FO,CAAR","sourcesContent":["import initPostAuthRefresh from './postAuthRefresh';\n\ndescribe('postAuthRefresh', () => {\n test('should work', async () => {\n const authCookies = {\n parse: jest.fn(),\n build: jest.fn(),\n };\n const postOAuth2Token = jest.fn();\n const postAuthRefresh = await initPostAuthRefresh({\n ROOT_AUTHENTICATION_DATA: {\n scope: 'user',\n applicationId: 'root_app_id',\n },\n authCookies,\n postOAuth2Token,\n });\n\n authCookies.parse.mockReturnValueOnce({\n access_token: 'an_access_token',\n refresh_token: 'a_refresh_token',\n });\n authCookies.build.mockReturnValueOnce('the_build_cookies');\n postOAuth2Token.mockResolvedValueOnce({\n status: 200,\n body: {\n access_token: 'an_access_token',\n expiration_date: '2020-02-02T20:22:02Z',\n expires_in: 3600,\n token_type: 'bearer',\n },\n });\n\n const response = await postAuthRefresh({\n cookie: 'a_given_cookie',\n body: {\n remember: false,\n scope: 'user',\n },\n });\n\n expect({\n response,\n authCookiesParseCalls: authCookies.parse.mock.calls,\n authCookiesBuildCalls: authCookies.build.mock.calls,\n postOAuth2TokenCalls: postOAuth2Token.mock.calls,\n }).toMatchInlineSnapshot(`\n Object {\n \"authCookiesBuildCalls\": Array [\n Array [\n Object {\n \"access_token\": \"an_access_token\",\n \"expiration_date\": \"2020-02-02T20:22:02Z\",\n \"expires_in\": 3600,\n \"token_type\": \"bearer\",\n },\n Object {\n \"session\": true,\n },\n ],\n ],\n \"authCookiesParseCalls\": Array [\n Array [\n \"a_given_cookie\",\n ],\n ],\n \"postOAuth2TokenCalls\": Array [\n Array [\n Object {\n \"authenticationData\": Object {\n \"applicationId\": \"root_app_id\",\n \"scope\": \"user\",\n },\n \"body\": Object {\n \"grant_type\": \"refresh_token\",\n \"refresh_token\": \"a_refresh_token\",\n \"scope\": \"user\",\n },\n },\n ],\n ],\n \"response\": Object {\n \"body\": Object {\n \"access_token\": \"an_access_token\",\n \"expiration_date\": \"2020-02-02T20:22:02Z\",\n \"expires_in\": 3600,\n \"token_type\": \"bearer\",\n },\n \"headers\": Object {\n \"Set-Cookie\": \"the_build_cookies\",\n },\n \"status\": 200,\n },\n }\n `);\n });\n});\n"],"file":"postAuthRefresh.test.mjs"}
@@ -0,0 +1,28 @@
1
+ import type { WhookAPIHandlerDefinition, WhookErrorsDescriptors } from '@whook/whook';
2
+ import type { CheckApplicationService, OAuth2GranterService } from '../services/oAuth2Granters';
3
+ import type { LogService } from 'common-services';
4
+ import type { BaseAuthenticationData } from '@whook/authorization';
5
+ export declare const definition: WhookAPIHandlerDefinition;
6
+ declare const _default: <AUTHENTICATION_DATA extends BaseAuthenticationData<string, string> = BaseAuthenticationData<string, string>>(dependencies?: {
7
+ ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
8
+ oAuth2Granters: OAuth2GranterService<Record<string, unknown>, Record<string, unknown>, Record<string, unknown>, BaseAuthenticationData<string, string>>[];
9
+ checkApplication: CheckApplicationService;
10
+ log: LogService;
11
+ }) => Promise<(parameters: {
12
+ authenticationData: AUTHENTICATION_DATA;
13
+ body: {
14
+ [name: string]: unknown;
15
+ responseType: string;
16
+ clientId: string;
17
+ redirectURI: string;
18
+ scope: string;
19
+ state: string;
20
+ acknowledged: boolean;
21
+ };
22
+ }) => Promise<{
23
+ status: number;
24
+ headers: {
25
+ location: string;
26
+ };
27
+ }>>;
28
+ export default _default;