m06_task3.js 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. package/.idea/.name +1 -0
  2. package/.idea/inspectionProfiles/Project_Default.xml +10 -0
  3. package/.idea/jsLibraryMappings.xml +6 -0
  4. package/.idea/modules.xml +8 -0
  5. package/.idea/node_lab_6.iml +12 -0
  6. package/06_task2.html +73 -0
  7. package/06_task2.js +52 -0
  8. package/06_task3.js +3 -0
  9. package/06_task4.js +3 -0
  10. package/m06_task3.js +28 -0
  11. package/package/index.js +28 -0
  12. package/package/node_modules/.package-lock.json +31 -0
  13. package/package/node_modules/@types/node/LICENSE +21 -0
  14. package/package/node_modules/@types/node/README.md +16 -0
  15. package/package/node_modules/@types/node/assert/strict.d.ts +8 -0
  16. package/package/node_modules/@types/node/assert.d.ts +961 -0
  17. package/package/node_modules/@types/node/async_hooks.d.ts +501 -0
  18. package/package/node_modules/@types/node/buffer.d.ts +2258 -0
  19. package/package/node_modules/@types/node/child_process.d.ts +1369 -0
  20. package/package/node_modules/@types/node/cluster.d.ts +410 -0
  21. package/package/node_modules/@types/node/console.d.ts +412 -0
  22. package/package/node_modules/@types/node/constants.d.ts +18 -0
  23. package/package/node_modules/@types/node/crypto.d.ts +3964 -0
  24. package/package/node_modules/@types/node/dgram.d.ts +545 -0
  25. package/package/node_modules/@types/node/diagnostics_channel.d.ts +153 -0
  26. package/package/node_modules/@types/node/dns/promises.d.ts +370 -0
  27. package/package/node_modules/@types/node/dns.d.ts +659 -0
  28. package/package/node_modules/@types/node/dom-events.d.ts +126 -0
  29. package/package/node_modules/@types/node/domain.d.ts +170 -0
  30. package/package/node_modules/@types/node/events.d.ts +678 -0
  31. package/package/node_modules/@types/node/fs/promises.d.ts +1138 -0
  32. package/package/node_modules/@types/node/fs.d.ts +3872 -0
  33. package/package/node_modules/@types/node/globals.d.ts +300 -0
  34. package/package/node_modules/@types/node/globals.global.d.ts +1 -0
  35. package/package/node_modules/@types/node/http.d.ts +1614 -0
  36. package/package/node_modules/@types/node/http2.d.ts +2134 -0
  37. package/package/node_modules/@types/node/https.d.ts +541 -0
  38. package/package/node_modules/@types/node/index.d.ts +133 -0
  39. package/package/node_modules/@types/node/inspector.d.ts +2741 -0
  40. package/package/node_modules/@types/node/module.d.ts +114 -0
  41. package/package/node_modules/@types/node/net.d.ts +869 -0
  42. package/package/node_modules/@types/node/os.d.ts +466 -0
  43. package/package/node_modules/@types/node/package.json +232 -0
  44. package/package/node_modules/@types/node/path.d.ts +191 -0
  45. package/package/node_modules/@types/node/perf_hooks.d.ts +625 -0
  46. package/package/node_modules/@types/node/process.d.ts +1482 -0
  47. package/package/node_modules/@types/node/punycode.d.ts +117 -0
  48. package/package/node_modules/@types/node/querystring.d.ts +131 -0
  49. package/package/node_modules/@types/node/readline/promises.d.ts +143 -0
  50. package/package/node_modules/@types/node/readline.d.ts +653 -0
  51. package/package/node_modules/@types/node/repl.d.ts +424 -0
  52. package/package/node_modules/@types/node/stream/consumers.d.ts +12 -0
  53. package/package/node_modules/@types/node/stream/promises.d.ts +42 -0
  54. package/package/node_modules/@types/node/stream/web.d.ts +330 -0
  55. package/package/node_modules/@types/node/stream.d.ts +1340 -0
  56. package/package/node_modules/@types/node/string_decoder.d.ts +67 -0
  57. package/package/node_modules/@types/node/test.d.ts +314 -0
  58. package/package/node_modules/@types/node/timers/promises.d.ts +68 -0
  59. package/package/node_modules/@types/node/timers.d.ts +94 -0
  60. package/package/node_modules/@types/node/tls.d.ts +1028 -0
  61. package/package/node_modules/@types/node/trace_events.d.ts +171 -0
  62. package/package/node_modules/@types/node/ts4.8/assert/strict.d.ts +8 -0
  63. package/package/node_modules/@types/node/ts4.8/assert.d.ts +961 -0
  64. package/package/node_modules/@types/node/ts4.8/async_hooks.d.ts +501 -0
  65. package/package/node_modules/@types/node/ts4.8/buffer.d.ts +2259 -0
  66. package/package/node_modules/@types/node/ts4.8/child_process.d.ts +1369 -0
  67. package/package/node_modules/@types/node/ts4.8/cluster.d.ts +410 -0
  68. package/package/node_modules/@types/node/ts4.8/console.d.ts +412 -0
  69. package/package/node_modules/@types/node/ts4.8/constants.d.ts +18 -0
  70. package/package/node_modules/@types/node/ts4.8/crypto.d.ts +3964 -0
  71. package/package/node_modules/@types/node/ts4.8/dgram.d.ts +545 -0
  72. package/package/node_modules/@types/node/ts4.8/diagnostics_channel.d.ts +153 -0
  73. package/package/node_modules/@types/node/ts4.8/dns/promises.d.ts +370 -0
  74. package/package/node_modules/@types/node/ts4.8/dns.d.ts +659 -0
  75. package/package/node_modules/@types/node/ts4.8/dom-events.d.ts +126 -0
  76. package/package/node_modules/@types/node/ts4.8/domain.d.ts +170 -0
  77. package/package/node_modules/@types/node/ts4.8/events.d.ts +678 -0
  78. package/package/node_modules/@types/node/ts4.8/fs/promises.d.ts +1138 -0
  79. package/package/node_modules/@types/node/ts4.8/fs.d.ts +3872 -0
  80. package/package/node_modules/@types/node/ts4.8/globals.d.ts +294 -0
  81. package/package/node_modules/@types/node/ts4.8/globals.global.d.ts +1 -0
  82. package/package/node_modules/@types/node/ts4.8/http.d.ts +1614 -0
  83. package/package/node_modules/@types/node/ts4.8/http2.d.ts +2134 -0
  84. package/package/node_modules/@types/node/ts4.8/https.d.ts +541 -0
  85. package/package/node_modules/@types/node/ts4.8/index.d.ts +88 -0
  86. package/package/node_modules/@types/node/ts4.8/inspector.d.ts +2741 -0
  87. package/package/node_modules/@types/node/ts4.8/module.d.ts +114 -0
  88. package/package/node_modules/@types/node/ts4.8/net.d.ts +869 -0
  89. package/package/node_modules/@types/node/ts4.8/os.d.ts +466 -0
  90. package/package/node_modules/@types/node/ts4.8/path.d.ts +191 -0
  91. package/package/node_modules/@types/node/ts4.8/perf_hooks.d.ts +625 -0
  92. package/package/node_modules/@types/node/ts4.8/process.d.ts +1482 -0
  93. package/package/node_modules/@types/node/ts4.8/punycode.d.ts +117 -0
  94. package/package/node_modules/@types/node/ts4.8/querystring.d.ts +131 -0
  95. package/package/node_modules/@types/node/ts4.8/readline/promises.d.ts +143 -0
  96. package/package/node_modules/@types/node/ts4.8/readline.d.ts +653 -0
  97. package/package/node_modules/@types/node/ts4.8/repl.d.ts +424 -0
  98. package/package/node_modules/@types/node/ts4.8/stream/consumers.d.ts +12 -0
  99. package/package/node_modules/@types/node/ts4.8/stream/promises.d.ts +42 -0
  100. package/package/node_modules/@types/node/ts4.8/stream/web.d.ts +330 -0
  101. package/package/node_modules/@types/node/ts4.8/stream.d.ts +1340 -0
  102. package/package/node_modules/@types/node/ts4.8/string_decoder.d.ts +67 -0
  103. package/package/node_modules/@types/node/ts4.8/test.d.ts +314 -0
  104. package/package/node_modules/@types/node/ts4.8/timers/promises.d.ts +68 -0
  105. package/package/node_modules/@types/node/ts4.8/timers.d.ts +94 -0
  106. package/package/node_modules/@types/node/ts4.8/tls.d.ts +1028 -0
  107. package/package/node_modules/@types/node/ts4.8/trace_events.d.ts +171 -0
  108. package/package/node_modules/@types/node/ts4.8/tty.d.ts +206 -0
  109. package/package/node_modules/@types/node/ts4.8/url.d.ts +897 -0
  110. package/package/node_modules/@types/node/ts4.8/util.d.ts +1850 -0
  111. package/package/node_modules/@types/node/ts4.8/v8.d.ts +396 -0
  112. package/package/node_modules/@types/node/ts4.8/vm.d.ts +509 -0
  113. package/package/node_modules/@types/node/ts4.8/wasi.d.ts +158 -0
  114. package/package/node_modules/@types/node/ts4.8/worker_threads.d.ts +689 -0
  115. package/package/node_modules/@types/node/ts4.8/zlib.d.ts +517 -0
  116. package/package/node_modules/@types/node/tty.d.ts +206 -0
  117. package/package/node_modules/@types/node/url.d.ts +897 -0
  118. package/package/node_modules/@types/node/util.d.ts +1850 -0
  119. package/package/node_modules/@types/node/v8.d.ts +396 -0
  120. package/package/node_modules/@types/node/vm.d.ts +509 -0
  121. package/package/node_modules/@types/node/wasi.d.ts +158 -0
  122. package/package/node_modules/@types/node/worker_threads.d.ts +689 -0
  123. package/package/node_modules/@types/node/zlib.d.ts +517 -0
  124. package/package/node_modules/@types/nodemailer/LICENSE +21 -0
  125. package/package/node_modules/@types/nodemailer/README.md +16 -0
  126. package/package/node_modules/@types/nodemailer/index.d.ts +83 -0
  127. package/package/node_modules/@types/nodemailer/lib/addressparser/index.d.ts +31 -0
  128. package/package/node_modules/@types/nodemailer/lib/base64/index.d.ts +22 -0
  129. package/package/node_modules/@types/nodemailer/lib/dkim/index.d.ts +45 -0
  130. package/package/node_modules/@types/nodemailer/lib/dkim/message-parser.d.ts +75 -0
  131. package/package/node_modules/@types/nodemailer/lib/dkim/relaxed-body.d.ts +75 -0
  132. package/package/node_modules/@types/nodemailer/lib/dkim/sign.d.ts +11 -0
  133. package/package/node_modules/@types/nodemailer/lib/fetch/cookies.d.ts +54 -0
  134. package/package/node_modules/@types/nodemailer/lib/fetch/index.d.ts +38 -0
  135. package/package/node_modules/@types/nodemailer/lib/json-transport/index.d.ts +50 -0
  136. package/package/node_modules/@types/nodemailer/lib/mail-composer/index.d.ts +25 -0
  137. package/package/node_modules/@types/nodemailer/lib/mailer/index.d.ts +230 -0
  138. package/package/node_modules/@types/nodemailer/lib/mailer/mail-message.d.ts +28 -0
  139. package/package/node_modules/@types/nodemailer/lib/mime-funcs/index.d.ts +87 -0
  140. package/package/node_modules/@types/nodemailer/lib/mime-funcs/mime-types.d.ts +2 -0
  141. package/package/node_modules/@types/nodemailer/lib/mime-node/index.d.ts +137 -0
  142. package/package/node_modules/@types/nodemailer/lib/mime-node/last-newline.d.ts +9 -0
  143. package/package/node_modules/@types/nodemailer/lib/qp/index.d.ts +23 -0
  144. package/package/node_modules/@types/nodemailer/lib/sendmail-transport/index.d.ts +53 -0
  145. package/package/node_modules/@types/nodemailer/lib/sendmail-transport/le-unix.d.ts +7 -0
  146. package/package/node_modules/@types/nodemailer/lib/sendmail-transport/le-windows.d.ts +7 -0
  147. package/package/node_modules/@types/nodemailer/lib/ses-transport/index.d.ts +136 -0
  148. package/package/node_modules/@types/nodemailer/lib/shared/index.d.ts +51 -0
  149. package/package/node_modules/@types/nodemailer/lib/smtp-connection/data-stream.d.ts +11 -0
  150. package/package/node_modules/@types/nodemailer/lib/smtp-connection/http-proxy-client.d.ts +11 -0
  151. package/package/node_modules/@types/nodemailer/lib/smtp-connection/index.d.ts +254 -0
  152. package/package/node_modules/@types/nodemailer/lib/smtp-pool/index.d.ts +90 -0
  153. package/package/node_modules/@types/nodemailer/lib/smtp-pool/pool-resource.d.ts +66 -0
  154. package/package/node_modules/@types/nodemailer/lib/smtp-transport/index.d.ts +115 -0
  155. package/package/node_modules/@types/nodemailer/lib/stream-transport/index.d.ts +56 -0
  156. package/package/node_modules/@types/nodemailer/lib/well-known/index.d.ts +6 -0
  157. package/package/node_modules/@types/nodemailer/lib/xoauth2/index.d.ts +110 -0
  158. package/package/node_modules/@types/nodemailer/package.json +37 -0
  159. package/package/node_modules/nodemailer/.gitattributes +6 -0
  160. package/package/node_modules/nodemailer/.prettierrc.js +8 -0
  161. package/package/node_modules/nodemailer/CHANGELOG.md +715 -0
  162. package/package/node_modules/nodemailer/CODE_OF_CONDUCT.md +76 -0
  163. package/package/node_modules/nodemailer/CONTRIBUTING.md +67 -0
  164. package/package/node_modules/nodemailer/LICENSE +16 -0
  165. package/package/node_modules/nodemailer/README.md +93 -0
  166. package/package/node_modules/nodemailer/SECURITY.txt +22 -0
  167. package/package/node_modules/nodemailer/lib/addressparser/index.js +313 -0
  168. package/package/node_modules/nodemailer/lib/base64/index.js +142 -0
  169. package/package/node_modules/nodemailer/lib/dkim/index.js +251 -0
  170. package/package/node_modules/nodemailer/lib/dkim/message-parser.js +155 -0
  171. package/package/node_modules/nodemailer/lib/dkim/relaxed-body.js +154 -0
  172. package/package/node_modules/nodemailer/lib/dkim/sign.js +117 -0
  173. package/package/node_modules/nodemailer/lib/fetch/cookies.js +281 -0
  174. package/package/node_modules/nodemailer/lib/fetch/index.js +269 -0
  175. package/package/node_modules/nodemailer/lib/json-transport/index.js +82 -0
  176. package/package/node_modules/nodemailer/lib/mail-composer/index.js +558 -0
  177. package/package/node_modules/nodemailer/lib/mailer/index.js +427 -0
  178. package/package/node_modules/nodemailer/lib/mailer/mail-message.js +315 -0
  179. package/package/node_modules/nodemailer/lib/mime-funcs/index.js +619 -0
  180. package/package/node_modules/nodemailer/lib/mime-funcs/mime-types.js +2102 -0
  181. package/package/node_modules/nodemailer/lib/mime-node/index.js +1290 -0
  182. package/package/node_modules/nodemailer/lib/mime-node/last-newline.js +33 -0
  183. package/package/node_modules/nodemailer/lib/mime-node/le-unix.js +43 -0
  184. package/package/node_modules/nodemailer/lib/mime-node/le-windows.js +52 -0
  185. package/package/node_modules/nodemailer/lib/nodemailer.js +143 -0
  186. package/package/node_modules/nodemailer/lib/qp/index.js +219 -0
  187. package/package/node_modules/nodemailer/lib/sendmail-transport/index.js +210 -0
  188. package/package/node_modules/nodemailer/lib/ses-transport/index.js +349 -0
  189. package/package/node_modules/nodemailer/lib/shared/index.js +639 -0
  190. package/package/node_modules/nodemailer/lib/smtp-connection/data-stream.js +108 -0
  191. package/package/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +143 -0
  192. package/package/node_modules/nodemailer/lib/smtp-connection/index.js +1786 -0
  193. package/package/node_modules/nodemailer/lib/smtp-pool/index.js +648 -0
  194. package/package/node_modules/nodemailer/lib/smtp-pool/pool-resource.js +253 -0
  195. package/package/node_modules/nodemailer/lib/smtp-transport/index.js +416 -0
  196. package/package/node_modules/nodemailer/lib/stream-transport/index.js +135 -0
  197. package/package/node_modules/nodemailer/lib/well-known/index.js +47 -0
  198. package/package/node_modules/nodemailer/lib/well-known/services.json +286 -0
  199. package/package/node_modules/nodemailer/lib/xoauth2/index.js +376 -0
  200. package/package/node_modules/nodemailer/package.json +46 -0
  201. package/package/node_modules/nodemailer/postinstall.js +101 -0
  202. package/package/package-lock.json +60 -0
  203. package/package/package.json +6 -0
  204. package/package.json +19 -0
@@ -0,0 +1,376 @@
1
+ 'use strict';
2
+
3
+ const Stream = require('stream').Stream;
4
+ const nmfetch = require('../fetch');
5
+ const crypto = require('crypto');
6
+ const shared = require('../shared');
7
+
8
+ /**
9
+ * XOAUTH2 access_token generator for Gmail.
10
+ * Create client ID for web applications in Google API console to use it.
11
+ * See Offline Access for receiving the needed refreshToken for an user
12
+ * https://developers.google.com/accounts/docs/OAuth2WebServer#offline
13
+ *
14
+ * Usage for generating access tokens with a custom method using provisionCallback:
15
+ * provisionCallback(user, renew, callback)
16
+ * * user is the username to get the token for
17
+ * * renew is a boolean that if true indicates that existing token failed and needs to be renewed
18
+ * * callback is the callback to run with (error, accessToken [, expires])
19
+ * * accessToken is a string
20
+ * * expires is an optional expire time in milliseconds
21
+ * If provisionCallback is used, then Nodemailer does not try to attempt generating the token by itself
22
+ *
23
+ * @constructor
24
+ * @param {Object} options Client information for token generation
25
+ * @param {String} options.user User e-mail address
26
+ * @param {String} options.clientId Client ID value
27
+ * @param {String} options.clientSecret Client secret value
28
+ * @param {String} options.refreshToken Refresh token for an user
29
+ * @param {String} options.accessUrl Endpoint for token generation, defaults to 'https://accounts.google.com/o/oauth2/token'
30
+ * @param {String} options.accessToken An existing valid accessToken
31
+ * @param {String} options.privateKey Private key for JSW
32
+ * @param {Number} options.expires Optional Access Token expire time in ms
33
+ * @param {Number} options.timeout Optional TTL for Access Token in seconds
34
+ * @param {Function} options.provisionCallback Function to run when a new access token is required
35
+ */
36
+ class XOAuth2 extends Stream {
37
+ constructor(options, logger) {
38
+ super();
39
+
40
+ this.options = options || {};
41
+
42
+ if (options && options.serviceClient) {
43
+ if (!options.privateKey || !options.user) {
44
+ setImmediate(() => this.emit('error', new Error('Options "privateKey" and "user" are required for service account!')));
45
+ return;
46
+ }
47
+
48
+ let serviceRequestTimeout = Math.min(Math.max(Number(this.options.serviceRequestTimeout) || 0, 0), 3600);
49
+ this.options.serviceRequestTimeout = serviceRequestTimeout || 5 * 60;
50
+ }
51
+
52
+ this.logger = shared.getLogger(
53
+ {
54
+ logger
55
+ },
56
+ {
57
+ component: this.options.component || 'OAuth2'
58
+ }
59
+ );
60
+
61
+ this.provisionCallback = typeof this.options.provisionCallback === 'function' ? this.options.provisionCallback : false;
62
+
63
+ this.options.accessUrl = this.options.accessUrl || 'https://accounts.google.com/o/oauth2/token';
64
+ this.options.customHeaders = this.options.customHeaders || {};
65
+ this.options.customParams = this.options.customParams || {};
66
+
67
+ this.accessToken = this.options.accessToken || false;
68
+
69
+ if (this.options.expires && Number(this.options.expires)) {
70
+ this.expires = this.options.expires;
71
+ } else {
72
+ let timeout = Math.max(Number(this.options.timeout) || 0, 0);
73
+ this.expires = (timeout && Date.now() + timeout * 1000) || 0;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Returns or generates (if previous has expired) a XOAuth2 token
79
+ *
80
+ * @param {Boolean} renew If false then use cached access token (if available)
81
+ * @param {Function} callback Callback function with error object and token string
82
+ */
83
+ getToken(renew, callback) {
84
+ if (!renew && this.accessToken && (!this.expires || this.expires > Date.now())) {
85
+ return callback(null, this.accessToken);
86
+ }
87
+
88
+ let generateCallback = (...args) => {
89
+ if (args[0]) {
90
+ this.logger.error(
91
+ {
92
+ err: args[0],
93
+ tnx: 'OAUTH2',
94
+ user: this.options.user,
95
+ action: 'renew'
96
+ },
97
+ 'Failed generating new Access Token for %s',
98
+ this.options.user
99
+ );
100
+ } else {
101
+ this.logger.info(
102
+ {
103
+ tnx: 'OAUTH2',
104
+ user: this.options.user,
105
+ action: 'renew'
106
+ },
107
+ 'Generated new Access Token for %s',
108
+ this.options.user
109
+ );
110
+ }
111
+ callback(...args);
112
+ };
113
+
114
+ if (this.provisionCallback) {
115
+ this.provisionCallback(this.options.user, !!renew, (err, accessToken, expires) => {
116
+ if (!err && accessToken) {
117
+ this.accessToken = accessToken;
118
+ this.expires = expires || 0;
119
+ }
120
+ generateCallback(err, accessToken);
121
+ });
122
+ } else {
123
+ this.generateToken(generateCallback);
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Updates token values
129
+ *
130
+ * @param {String} accessToken New access token
131
+ * @param {Number} timeout Access token lifetime in seconds
132
+ *
133
+ * Emits 'token': { user: User email-address, accessToken: the new accessToken, timeout: TTL in seconds}
134
+ */
135
+ updateToken(accessToken, timeout) {
136
+ this.accessToken = accessToken;
137
+ timeout = Math.max(Number(timeout) || 0, 0);
138
+ this.expires = (timeout && Date.now() + timeout * 1000) || 0;
139
+
140
+ this.emit('token', {
141
+ user: this.options.user,
142
+ accessToken: accessToken || '',
143
+ expires: this.expires
144
+ });
145
+ }
146
+
147
+ /**
148
+ * Generates a new XOAuth2 token with the credentials provided at initialization
149
+ *
150
+ * @param {Function} callback Callback function with error object and token string
151
+ */
152
+ generateToken(callback) {
153
+ let urlOptions;
154
+ let loggedUrlOptions;
155
+ if (this.options.serviceClient) {
156
+ // service account - https://developers.google.com/identity/protocols/OAuth2ServiceAccount
157
+ let iat = Math.floor(Date.now() / 1000); // unix time
158
+ let tokenData = {
159
+ iss: this.options.serviceClient,
160
+ scope: this.options.scope || 'https://mail.google.com/',
161
+ sub: this.options.user,
162
+ aud: this.options.accessUrl,
163
+ iat,
164
+ exp: iat + this.options.serviceRequestTimeout
165
+ };
166
+ let token;
167
+ try {
168
+ token = this.jwtSignRS256(tokenData);
169
+ } catch (err) {
170
+ return callback(new Error('Can\x27t generate token. Check your auth options'));
171
+ }
172
+
173
+ urlOptions = {
174
+ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
175
+ assertion: token
176
+ };
177
+
178
+ loggedUrlOptions = {
179
+ grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
180
+ assertion: tokenData
181
+ };
182
+ } else {
183
+ if (!this.options.refreshToken) {
184
+ return callback(new Error('Can\x27t create new access token for user'));
185
+ }
186
+
187
+ // web app - https://developers.google.com/identity/protocols/OAuth2WebServer
188
+ urlOptions = {
189
+ client_id: this.options.clientId || '',
190
+ client_secret: this.options.clientSecret || '',
191
+ refresh_token: this.options.refreshToken,
192
+ grant_type: 'refresh_token'
193
+ };
194
+
195
+ loggedUrlOptions = {
196
+ client_id: this.options.clientId || '',
197
+ client_secret: (this.options.clientSecret || '').substr(0, 6) + '...',
198
+ refresh_token: (this.options.refreshToken || '').substr(0, 6) + '...',
199
+ grant_type: 'refresh_token'
200
+ };
201
+ }
202
+
203
+ Object.keys(this.options.customParams).forEach(key => {
204
+ urlOptions[key] = this.options.customParams[key];
205
+ loggedUrlOptions[key] = this.options.customParams[key];
206
+ });
207
+
208
+ this.logger.debug(
209
+ {
210
+ tnx: 'OAUTH2',
211
+ user: this.options.user,
212
+ action: 'generate'
213
+ },
214
+ 'Requesting token using: %s',
215
+ JSON.stringify(loggedUrlOptions)
216
+ );
217
+
218
+ this.postRequest(this.options.accessUrl, urlOptions, this.options, (error, body) => {
219
+ let data;
220
+
221
+ if (error) {
222
+ return callback(error);
223
+ }
224
+
225
+ try {
226
+ data = JSON.parse(body.toString());
227
+ } catch (E) {
228
+ return callback(E);
229
+ }
230
+
231
+ if (!data || typeof data !== 'object') {
232
+ this.logger.debug(
233
+ {
234
+ tnx: 'OAUTH2',
235
+ user: this.options.user,
236
+ action: 'post'
237
+ },
238
+ 'Response: %s',
239
+ (body || '').toString()
240
+ );
241
+ return callback(new Error('Invalid authentication response'));
242
+ }
243
+
244
+ let logData = {};
245
+ Object.keys(data).forEach(key => {
246
+ if (key !== 'access_token') {
247
+ logData[key] = data[key];
248
+ } else {
249
+ logData[key] = (data[key] || '').toString().substr(0, 6) + '...';
250
+ }
251
+ });
252
+
253
+ this.logger.debug(
254
+ {
255
+ tnx: 'OAUTH2',
256
+ user: this.options.user,
257
+ action: 'post'
258
+ },
259
+ 'Response: %s',
260
+ JSON.stringify(logData)
261
+ );
262
+
263
+ if (data.error) {
264
+ // Error Response : https://tools.ietf.org/html/rfc6749#section-5.2
265
+ let errorMessage = data.error;
266
+ if (data.error_description) {
267
+ errorMessage += ': ' + data.error_description;
268
+ }
269
+ if (data.error_uri) {
270
+ errorMessage += ' (' + data.error_uri + ')';
271
+ }
272
+ return callback(new Error(errorMessage));
273
+ }
274
+
275
+ if (data.access_token) {
276
+ this.updateToken(data.access_token, data.expires_in);
277
+ return callback(null, this.accessToken);
278
+ }
279
+
280
+ return callback(new Error('No access token'));
281
+ });
282
+ }
283
+
284
+ /**
285
+ * Converts an access_token and user id into a base64 encoded XOAuth2 token
286
+ *
287
+ * @param {String} [accessToken] Access token string
288
+ * @return {String} Base64 encoded token for IMAP or SMTP login
289
+ */
290
+ buildXOAuth2Token(accessToken) {
291
+ let authData = ['user=' + (this.options.user || ''), 'auth=Bearer ' + (accessToken || this.accessToken), '', ''];
292
+ return Buffer.from(authData.join('\x01'), 'utf-8').toString('base64');
293
+ }
294
+
295
+ /**
296
+ * Custom POST request handler.
297
+ * This is only needed to keep paths short in Windows – usually this module
298
+ * is a dependency of a dependency and if it tries to require something
299
+ * like the request module the paths get way too long to handle for Windows.
300
+ * As we do only a simple POST request we do not actually require complicated
301
+ * logic support (no redirects, no nothing) anyway.
302
+ *
303
+ * @param {String} url Url to POST to
304
+ * @param {String|Buffer} payload Payload to POST
305
+ * @param {Function} callback Callback function with (err, buff)
306
+ */
307
+ postRequest(url, payload, params, callback) {
308
+ let returned = false;
309
+
310
+ let chunks = [];
311
+ let chunklen = 0;
312
+
313
+ let req = nmfetch(url, {
314
+ method: 'post',
315
+ headers: params.customHeaders,
316
+ body: payload,
317
+ allowErrorResponse: true
318
+ });
319
+
320
+ req.on('readable', () => {
321
+ let chunk;
322
+ while ((chunk = req.read()) !== null) {
323
+ chunks.push(chunk);
324
+ chunklen += chunk.length;
325
+ }
326
+ });
327
+
328
+ req.once('error', err => {
329
+ if (returned) {
330
+ return;
331
+ }
332
+ returned = true;
333
+ return callback(err);
334
+ });
335
+
336
+ req.once('end', () => {
337
+ if (returned) {
338
+ return;
339
+ }
340
+ returned = true;
341
+ return callback(null, Buffer.concat(chunks, chunklen));
342
+ });
343
+ }
344
+
345
+ /**
346
+ * Encodes a buffer or a string into Base64url format
347
+ *
348
+ * @param {Buffer|String} data The data to convert
349
+ * @return {String} The encoded string
350
+ */
351
+ toBase64URL(data) {
352
+ if (typeof data === 'string') {
353
+ data = Buffer.from(data);
354
+ }
355
+
356
+ return data
357
+ .toString('base64')
358
+ .replace(/[=]+/g, '') // remove '='s
359
+ .replace(/\+/g, '-') // '+' → '-'
360
+ .replace(/\//g, '_'); // '/' → '_'
361
+ }
362
+
363
+ /**
364
+ * Creates a JSON Web Token signed with RS256 (SHA256 + RSA)
365
+ *
366
+ * @param {Object} payload The payload to include in the generated token
367
+ * @return {String} The generated and signed token
368
+ */
369
+ jwtSignRS256(payload) {
370
+ payload = ['{"alg":"RS256","typ":"JWT"}', JSON.stringify(payload)].map(val => this.toBase64URL(val)).join('.');
371
+ let signature = crypto.createSign('RSA-SHA256').update(payload).sign(this.options.privateKey);
372
+ return payload + '.' + this.toBase64URL(signature);
373
+ }
374
+ }
375
+
376
+ module.exports = XOAuth2;
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "nodemailer",
3
+ "version": "6.8.0",
4
+ "description": "Easy as cake e-mail sending from your Node.js applications",
5
+ "main": "lib/nodemailer.js",
6
+ "scripts": {
7
+ "test": "grunt --trace-warnings"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/nodemailer/nodemailer.git"
12
+ },
13
+ "keywords": [
14
+ "Nodemailer"
15
+ ],
16
+ "author": "Andris Reinman",
17
+ "license": "MIT",
18
+ "bugs": {
19
+ "url": "https://github.com/nodemailer/nodemailer/issues"
20
+ },
21
+ "homepage": "https://nodemailer.com/",
22
+ "devDependencies": {
23
+ "@aws-sdk/client-ses": "3.180.0",
24
+ "aws-sdk": "2.1225.0",
25
+ "bunyan": "1.8.15",
26
+ "chai": "4.3.6",
27
+ "eslint-config-nodemailer": "1.2.0",
28
+ "eslint-config-prettier": "8.5.0",
29
+ "grunt": "1.5.3",
30
+ "grunt-cli": "1.4.3",
31
+ "grunt-eslint": "24.0.0",
32
+ "grunt-mocha-test": "0.13.3",
33
+ "libbase64": "1.2.1",
34
+ "libmime": "5.1.0",
35
+ "libqp": "1.1.0",
36
+ "mocha": "10.0.0",
37
+ "nodemailer-ntlm-auth": "1.0.3",
38
+ "proxy": "1.0.2",
39
+ "proxy-test-server": "1.0.0",
40
+ "sinon": "14.0.0",
41
+ "smtp-server": "3.11.0"
42
+ },
43
+ "engines": {
44
+ "node": ">=6.0.0"
45
+ }
46
+ }
@@ -0,0 +1,101 @@
1
+ /* eslint no-control-regex:0 */
2
+ 'use strict';
3
+
4
+ const packageData = require('./package.json');
5
+ const isEnabled = value => !!value && value !== '0' && value !== 'false';
6
+ const canUseColor = isEnabled(process.env.npm_config_color);
7
+
8
+ const title = `=== Nodemailer ${packageData.version} ===`;
9
+ const text = `
10
+ Thank you for using Nodemailer for your email sending needs! While Nodemailer itself is mostly meant to be a SMTP client there are other related projects in the Nodemailer project as well.
11
+
12
+ > IMAP API ( https://imapapi.com ) is a server application to easily access IMAP accounts via REST API
13
+ > ImapFlow ( https://imapflow.com/ ) is an async IMAP client library for Node.js
14
+ > NodemailerApp ( https://nodemailer.com/app/ ) is a cross platform GUI app to debug emails
15
+ > Project Pending ( https://projectpending.com/ ) allows you to host DNS of your project domains
16
+ > Pending DNS ( https://pendingdns.com/ ) is the DNS server used that powers Project Pending
17
+ > Ethereal Email ( https://ethereal.email/ ) is an email testing service that accepts all your test emails
18
+ `;
19
+
20
+ const footer = `Don't like this message?
21
+ There's a Github Sponsors goal to remove it
22
+ https://github.com/sponsors/andris9
23
+ `;
24
+
25
+ const secs = 4;
26
+
27
+ const formatCentered = (row, columns) => {
28
+ return row
29
+ .split(/\r?\n/)
30
+ .map(row => {
31
+ if (columns <= row.length) {
32
+ return row;
33
+ }
34
+
35
+ return ' '.repeat(Math.round(columns / 2 - row.length / 2)) + row;
36
+ })
37
+ .join('\n');
38
+ };
39
+
40
+ const formatRow = (row, columns) => {
41
+ if (row.length <= columns) {
42
+ return [row];
43
+ }
44
+ // wrap!
45
+ let lines = [];
46
+ while (row.length) {
47
+ if (row.length <= columns) {
48
+ lines.push(row);
49
+ break;
50
+ }
51
+ let slice = row.substr(0, columns);
52
+
53
+ let prefix = slice.charAt(0) === '>' ? ' ' : '';
54
+
55
+ let match = slice.match(/(\s+)[^\s]*$/);
56
+ if (match && match.index) {
57
+ let line = row.substr(0, match.index);
58
+ row = prefix + row.substr(line.length + match[1].length);
59
+ lines.push(line);
60
+ } else {
61
+ lines.push(row);
62
+ break;
63
+ }
64
+ }
65
+ return lines;
66
+ };
67
+
68
+ const wrapText = text => {
69
+ let columns = Number(process.stdout.columns) || 80;
70
+ columns = Math.min(columns, 80) - 1;
71
+
72
+ return (
73
+ (formatCentered(title, columns) + '\n' + text)
74
+ .split('\n')
75
+ .flatMap(row => formatRow(row, columns))
76
+ .join('\n') +
77
+ '\n' +
78
+ formatCentered(footer, columns)
79
+ );
80
+ };
81
+
82
+ const banner = wrapText(text)
83
+ .replace(/^/gm, '\u001B[96m')
84
+ .replace(/$/gm, '\u001B[0m')
85
+ .replace(/(https:[^\s)]+)/g, '\u001B[94m $1 \u001B[96m');
86
+
87
+ console.log(canUseColor ? banner : banner.replace(/\u001B\[\d+m/g, ''));
88
+ if (canUseColor) {
89
+ process.stdout.write('\u001B[96m');
90
+ }
91
+
92
+ setInterval(() => {
93
+ process.stdout.write('.');
94
+ }, 500);
95
+
96
+ setTimeout(() => {
97
+ if (canUseColor) {
98
+ process.stdout.write('\u001B[0m\n');
99
+ }
100
+ process.exit(0);
101
+ }, secs * 1000 + 100);
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "package",
3
+ "lockfileVersion": 2,
4
+ "requires": true,
5
+ "packages": {
6
+ "": {
7
+ "devDependencies": {
8
+ "@types/nodemailer": "^6.4.6",
9
+ "nodemailer": "^6.8.0"
10
+ }
11
+ },
12
+ "node_modules/@types/node": {
13
+ "version": "18.11.7",
14
+ "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.7.tgz",
15
+ "integrity": "sha512-LhFTglglr63mNXUSRYD8A+ZAIu5sFqNJ4Y2fPuY7UlrySJH87rRRlhtVmMHplmfk5WkoJGmDjE9oiTfyX94CpQ==",
16
+ "dev": true
17
+ },
18
+ "node_modules/@types/nodemailer": {
19
+ "version": "6.4.6",
20
+ "resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-6.4.6.tgz",
21
+ "integrity": "sha512-pD6fL5GQtUKvD2WnPmg5bC2e8kWCAPDwMPmHe/ohQbW+Dy0EcHgZ2oCSuPlWNqk74LS5BVMig1SymQbFMPPK3w==",
22
+ "dev": true,
23
+ "dependencies": {
24
+ "@types/node": "*"
25
+ }
26
+ },
27
+ "node_modules/nodemailer": {
28
+ "version": "6.8.0",
29
+ "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.8.0.tgz",
30
+ "integrity": "sha512-EjYvSmHzekz6VNkNd12aUqAco+bOkRe3Of5jVhltqKhEsjw/y0PYPJfp83+s9Wzh1dspYAkUW/YNQ350NATbSQ==",
31
+ "dev": true,
32
+ "engines": {
33
+ "node": ">=6.0.0"
34
+ }
35
+ }
36
+ },
37
+ "dependencies": {
38
+ "@types/node": {
39
+ "version": "18.11.7",
40
+ "resolved": "https://registry.npmjs.org/@types/node/-/node-18.11.7.tgz",
41
+ "integrity": "sha512-LhFTglglr63mNXUSRYD8A+ZAIu5sFqNJ4Y2fPuY7UlrySJH87rRRlhtVmMHplmfk5WkoJGmDjE9oiTfyX94CpQ==",
42
+ "dev": true
43
+ },
44
+ "@types/nodemailer": {
45
+ "version": "6.4.6",
46
+ "resolved": "https://registry.npmjs.org/@types/nodemailer/-/nodemailer-6.4.6.tgz",
47
+ "integrity": "sha512-pD6fL5GQtUKvD2WnPmg5bC2e8kWCAPDwMPmHe/ohQbW+Dy0EcHgZ2oCSuPlWNqk74LS5BVMig1SymQbFMPPK3w==",
48
+ "dev": true,
49
+ "requires": {
50
+ "@types/node": "*"
51
+ }
52
+ },
53
+ "nodemailer": {
54
+ "version": "6.8.0",
55
+ "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.8.0.tgz",
56
+ "integrity": "sha512-EjYvSmHzekz6VNkNd12aUqAco+bOkRe3Of5jVhltqKhEsjw/y0PYPJfp83+s9Wzh1dspYAkUW/YNQ350NATbSQ==",
57
+ "dev": true
58
+ }
59
+ }
60
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "devDependencies": {
3
+ "@types/nodemailer": "^6.4.6",
4
+ "nodemailer": "^6.8.0"
5
+ }
6
+ }
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "m06_task3.js",
3
+ "version": "1.0.0",
4
+ "description": "Node_Js Laboratory Practice №6",
5
+ "main": "m06_task3.js",
6
+ "dependencies": {
7
+ "nodemailer": "^6.8.0"
8
+ },
9
+ "devDependencies": {},
10
+ "scripts": {
11
+ "test": "test"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "-"
16
+ },
17
+ "author": "LexRicord",
18
+ "license": "ISC"
19
+ }