forked from fioprotocol/fio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebmail with terraform and google id
769 lines (423 loc) · 14.7 KB
/
webmail with terraform and google id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebmailServer.Models;
using WebmailServer.ViewModels;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
namespace webmail_server.Controllers
{
[Produces("application/json")]
[Route("api/Mailbox")]
public class MailboxController : Controller
{
private readonly webmailContext _context;
public MailboxController(webmailContext context)
{
_context = context;
}
[HttpGet("user/{userId}/folders", Name = "folders")]
public IEnumerable<MailboxFolder> Folders(int userId)
{
List<MailboxFolder> mailboxFolders = new List<MailboxFolder>();
var userEmails = _context.UserEmail
.Where(e => e.UserId == userId)
.GroupBy(e => e.CategoryId)
.ToList();
foreach (var group in userEmails)
{
MailboxFolder folder = new MailboxFolder()
{
Category = group.Key,
TotalEmails = group.Count(),
UnreadEmails = group.Where(e => e.IsRead == false).Count()
};
mailboxFolders.Add(folder);
}
return mailboxFolders;
}
[HttpGet("user/{userId}/folder/{category}/emails", Name = "folderEmails")]
public FolderEmailsVM FolderEmails(int userId, int category)
{
List<int> userIds = new List<int>();
List<UserEmail> userEmails = _context.UserEmail.Include(ue => ue.Email)
.Where(e => e.UserId == userId && e.CategoryId == category).ToList();
List<Email> emails = _context.Email
.Include(e => e.UserEmail)
.Where(e => (userEmails.Select(ue => ue.EmailId).Contains(e.Id)))
.ToList();
foreach (Email e in emails)
{
userIds.AddRange(e.UserEmail.Select(ue => ue.UserId).Distinct());
}
userIds = userIds.Distinct().ToList();
List<User> users = _context.User.Where(u => userIds.Contains(u.Id)).ToList();
return new FolderEmailsVM()
{
emails = Mapper.Map<List<EmailVM>>(emails),
userEmails = Mapper.Map<List<UserEmailVM>>(userEmails),
users = Mapper.Map<List<UserVM>>(users)
};
}
[HttpGet("email/{id}/history", Name = "emailHistory")]
public EmailHistoryVM EmailHistory(int id)
{
List<int> userIds = new List<int>();
List<Email> emails = new List<Email>();
List<User> users = new List<WebmailServer.Models.User>();
Email email = _context.Email
.Include(e => e.Parent)
.ThenInclude(e => e.UserEmail).Where(e => e.Id == id).First();
if(email.Parent != null)
{
emails.Add(email.Parent);
userIds.AddRange(email.Parent.UserEmail.Select(ue => ue.UserId).Distinct());
}
userIds = userIds.Distinct().ToList();
users = _context.User.Where(u => userIds.Contains(u.Id)).ToList();
return new EmailHistoryVM()
{
emails = Mapper.Map<List<EmailVM>>(emails),
users = Mapper.Map<List<UserVM>>(users)
};
}
[HttpPost("send")]
public void PostEmail([FromBody] EmailVM emailVm)
{
Email email = new Email()
{
AuthorId = emailVm.AuthorId,
Subject = emailVm.Subject,
Body = emailVm.Body,
DateCreated = DateTime.Now,
ParentId = emailVm.ParentId
};
foreach(var receiver in emailVm.Receivers)
{
email.UserEmail.Add(new UserEmail()
{
CategoryId = 1,
UserId = receiver
});
}
email.UserEmail.Add(new UserEmail()
{
CategoryId = 4,
UserId = emailVm.AuthorId
});
_context.Email.Add(email);
_context.SaveChanges();
}
}
}
-----BEGIN CERTIFICATE-----
MIIB8TCCAVoCCQCg2ZYlANUEvjANBgkqhkiG9w0BAQsFADA9MQswCQYDVQQGEwJV
UzELMAkGA1UECAwCQ0ExITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0
ZDAeFw0xNDA4MTgyMzE5NDJaFw0xNTA4MTgyMzE5NDJaMD0xCzAJBgNVBAYTAlVT
MQswCQYDVQQIDAJDQTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRk
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDV4suKtPRyipQJg35O/wIndwm+
5RV+s+jqo8VS7tJ1E4OIsSMo7eVuNU4pLTIqehNN+Skyk/i17y6cPwo2Mff+E6VB
lJrjNLO+rI+B7Ttx7Cs9imoE38Pmv0LKzQbAz8Uz3T6zxXHJpjIWA4PKiw+mO6qw
niEDDutypPa2mB+KjQIDAQABMA0GCSqGSIb3DQEBCwUAA4GBAHUfkcY4wNZZGT3f
oCoB0cNy+gtS86Iu2XU+WzKWxQxvgSiloQ2l0NDsRlw9wBQQZNQOJtPNfTIXkpfU
NoD7qU0Dd0TawoIRAetWzweW0PIJt+Dh7/z7FUTXg5p2IRhOPVNA9+K1wBGfOkEF
6cYkdpr0FmQ52L+Vc1QcNCxwYtWm
-----END CERTIFICATE-----
resource "google_compute_network" "mesos-global-net" {
name = "${var.name}-global-net"
auto_create_subnetworks = false # custom subnetted network will be created that can support google_compute_subnetwork resources
}
resource "google_compute_subnetwork" "mesos-net" {
name = "${var.name}-${var.region}-net"
ip_cidr_range = "${var.subnetwork}"
network = "${google_compute_network.mesos-global-net.self_link}" # parent network
}
var Buffer = require('safe-buffer').Buffer
module.exports = function base (ALPHABET) {
var ALPHABET_MAP = {}
var BASE = ALPHABET.length
var LEADER = ALPHABET.charAt(0)
// pre-compute lookup table
for (var z = 0; z < ALPHABET.length; z++) {
var x = ALPHABET.charAt(z)
if (ALPHABET_MAP[x] !== undefined) throw new TypeError(x + ' is ambiguous')
ALPHABET_MAP[x] = z
}
function encode (source) {
if (source.length === 0) return ''
var digits = [0]
for (var i = 0; i < source.length; ++i) {
for (var j = 0, carry = source[i]; j < digits.length; ++j) {
carry += digits[j] << 8
digits[j] = carry % BASE
carry = (carry / BASE) | 0
}
while (carry > 0) {
digits.push(carry % BASE)
carry = (carry / BASE) | 0
}
}
var string = ''
// deal with leading zeros
for (var k = 0; source[k] === 0 && k < source.length - 1; ++k) string += ALPHABET[0]
// convert digits to a string
for (var q = digits.length - 1; q >= 0; --q) string += ALPHABET[digits[q]]
return string
}
function decodeUnsafe (string) {
if (string.length === 0) return Buffer.allocUnsafe(0)
var bytes = [0]
for (var i = 0; i < string.length; i++) {
var value = ALPHABET_MAP[string[i]]
if (value === undefined) return
for (var j = 0, carry = value; j < bytes.length; ++j) {
carry += bytes[j] * BASE
bytes[j] = carry & 0xff
carry >>= 8
}
while (carry > 0) {
bytes.push(carry & 0xff)
carry >>= 8
}
}
// deal with leading zeros
for (var k = 0; string[k] === LEADER && k < string.length - 1; ++k) {
bytes.push(0)
}
return Buffer.from(bytes.reverse())
}
function decode (string) {
var buffer = decodeUnsafe(string)
if (buffer) return buffer
throw new Error('Non-base' + BASE + ' character')
}
return {
encode: encode,
decodeUnsafe: decodeUnsafe,
decode: decode
}
}
},{"safe-buffer":65}],4:[function(require,module,exports){
'use strict'
exports.byteLength = byteLength
exports.toByteArray = toByteArray
exports.fromByteArray = fromByteArray
var lookup = []
var revLookup = []
var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
for (var i = 0, len = code.length; i < len; ++i) {
lookup[i] = code[i]
revLookup[code.charCodeAt(i)] = i
}
revLookup['-'.charCodeAt(0)] = 62
revLookup['_'.charCodeAt(0)] = 63
function placeHoldersCount (b64) {
var len = b64.length
if (len % 4 > 0) {
throw new Error('Invalid string. Length must be a multiple of 4')
}
// the number of equal signs (place holders)
// if there are two placeholders, than the two characters before it
// represent one byte
// if there is only one, then the three characters before it represent 2 bytes
// this is just a cheap hack to not do indexOf twice
return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0
}
function byteLength (b64) {
// base64 is 4/3 + up to two characters of the original data
return b64.length * 3 / 4 - placeHoldersCount(b64)
}
function toByteArray (b64) {
var i, j, l, tmp, placeHolders, arr
var len = b64.length
placeHolders = placeHoldersCount(b64)
arr = new Arr(len * 3 / 4 - placeHolders)
// if there are placeholders, only get up to the last complete 4 chars
l = placeHolders > 0 ? len - 4 : len
var L = 0
for (i = 0, j = 0; i < l; i += 4, j += 3) {
tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]
arr[L++] = (tmp >> 16) & 0xFF
arr[L++] = (tmp >> 8) & 0xFF
arr[L++] = tmp & 0xFF
}
if (placeHolders === 2) {
tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)
arr[L++] = tmp & 0xFF
} else if (placeHolders === 1) {
tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2)
arr[L++] = (tmp >> 8) & 0xFF
arr[L++] = tmp & 0xFF
}
return arr
}
function tripletToBase64 (num) {
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
}
function encodeChunk (uint8, start, end) {
var tmp
var output = []
for (var i = start; i < end; i += 3) {
tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
output.push(tripletToBase64(tmp))
}
return output.join('')
}
function fromByteArray (uint8) {
var tmp
var len = uint8.length
var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
var output = ''
var parts = []
var maxChunkLength = 16383 // must be multiple of 3
// go through the array every three bytes, we'll deal with trailing stuff later
for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
}
// pad the end with zeros, but make sure to not forget the extra bytes
if (extraBytes === 1) {
tmp = uint8[len - 1]
output += lookup[tmp >> 2]
output += lookup[(tmp << 4) & 0x3F]
output += '=='
} else if (extraBytes === 2) {
tmp = (uint8[len - 2] << 8) + (uint8[len - 1])
output += lookup[tmp >> 10]
output += lookup[(tmp >> 4) & 0x3F]
output += lookup[(tmp << 2) & 0x3F]
output += '='
}
parts.push(output)
return parts.join('')
}
},{}],5:[function(require,module,exports){
// (public) Constructor
function BigInteger(a, b, c) {
if (!(this instanceof BigInteger))
return new BigInteger(a, b, c)
if (a != null) {
if ("number" == typeof a) this.fromNumber(a, b, c)
else if (b == null && "string" != typeof a) this.fromString(a, 256)
else this.fromString(a, b)
}
}
var proto = BigInteger.prototype
// duck-typed isBigInteger
proto.__bigi = require('../package.json').version
BigInteger.isBigInteger = function (obj, check_ver) {
return obj && obj.__bigi && (!check_ver || obj.__bigi === proto.__bigi)
}
// Bits per digit
var dbits
// am: Compute w_j += (x*this_i), propagate carries,
// c is initial carry, returns final carry.
// c < 3*dvalue, x < 2*dvalue, this_i < dvalue
// We need to select the fastest one that works in this environment.
// am1: use a single mult and divide to get the high bits,
// max digit bits should be 26 because
// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
function am1(i, x, w, j, c, n) {
while (--n >= 0) {
var v = x * this[i++] + w[j] + c
c = Math.floor(v / 0x4000000)
w[j++] = v & 0x3ffffff
}
return c
}
}
#' Get the logged in user's email and other info
#'
#' @param id ID of the person to get the profile data for. 'me' to get current user.
#'
#' @return A People resource
#'
#' https://developers.google.com/+/web/api/rest/latest/people#resource-representations
#'
#' @seealso https://developers.google.com/+/web/api/rest/latest/people
#'
#' @export
#'
#' @examples
#'
#' \dontrun{
#' library(googleAuthR)
#' library(googleID)
#' options(googleAuthR.scopes.selected =
#' c("https://www.googleapis.com/auth/userinfo.email",
#' "https://www.googleapis.com/auth/userinfo.profile"))
#'
#' googleAuthR::gar_auth()
#'
#' ## default is user logged in
#' user <- get_user_info()
#' }
#'
get_user_info <- function(id = "me"){
url <- sprintf("https://www.googleapis.com/plus/v1/people/%s", id)
g <- googleAuthR::gar_api_generator(url, "GET")
req <- g()
req$content
}
#' Whitelist check
#'
#' After a user logs in, check to see if they are on a whitelist
#'
#' @param user_info the object returned by \link{get_user_info}
#' @param whitelist A character vector of emails on whitelist
#'
#' @return TRUE if on whitelist or no whitelist, FALSE if not
#' @export
#'
#' @examples
#'
#' \dontrun{
#' library(googleAuthR)
#' library(googleID)
#' options(googleAuthR.scopes.selected =
#' c("https://www.googleapis.com/auth/userinfo.email",
#' "https://www.googleapis.com/auth/userinfo.profile"))
#'
#' googleAuthR::gar_auth()
#'
#' ## default is user logged in
#' user <- get_user_info()
#'
#' the_list <- whitelist(user, c("[email protected]",
#' "[email protected]",
#' "[email protected]"))
#'
#' if(the_list){
#' message("You are on the list.")
#' } else {
#' message("If you're not on the list, you're not getting in.")
#'}
#'
#'
#'
#' }
whitelist <- function(user_info, whitelist = NULL){
if(user_info$kind != "plus#person"){
stop("Invalid user object used for user_info")
}
out <- FALSE
if(is.null(whitelist)){
message("No whitelist found")
out <- TRUE
}
check <- user_info$emails$value
if(is.null(check)){
stop("No user email found")
}
if(any(check %in% whitelist)){
message(check, " is in whitelist ")
out <- TRUE
} else {
message(check, " is NOT on whitelist")
}
out
}
© 2020 GitHub, Inc.