-
Notifications
You must be signed in to change notification settings - Fork 461
/
cb_whitelabel.erl
927 lines (836 loc) · 39.5 KB
/
cb_whitelabel.erl
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
%%%-------------------------------------------------------------------
%%% @copyright (C) 2011-2017, 2600Hz INC
%%% @doc
%%% Account module
%%%
%%% @end
%%% @contributors
%%% Jon Blanton <jon@2600hz.com>
%%%-------------------------------------------------------------------
-module(cb_whitelabel).
-export([init/0
,allowed_methods/0, allowed_methods/1, allowed_methods/2
,resource_exists/0, resource_exists/1, resource_exists/2
,authorize/1, authenticate/1
,validate/1, validate/2, validate/3
,content_types_provided/2, content_types_provided/3
,content_types_accepted/2
,put/1
,post/1, post/2
,delete/1
,acceptable_content_types/0
]).
-include("crossbar.hrl").
-define(WHITELABEL_ID, <<"whitelabel">>).
-define(LOGO_REQ, <<"logo">>).
-define(ICON_REQ, <<"icon">>).
-define(DOMAINS_REQ, <<"domains">>).
-define(WELCOME_REQ, <<"welcome">>).
-define(WHITELABEL_MIME_TYPES, ?IMAGE_CONTENT_TYPES ++ ?BASE64_CONTENT_TYPES).
%% Commonly found ico mime types
-define(WHITELABEL_ICON_MIME_TYPES, [{<<"image">>, <<"ico">>}
,{<<"image">>, <<"vnd.microsoft.icon">>}
,{<<"image">>, <<"x-icon">>}
,{<<"image">>, <<"icon">>}
| ?WHITELABEL_MIME_TYPES
]).
-define(WHITELABEL_WELCOME_MIME_TYPES, [{<<"text">>, <<"html">>}]).
-define(AGG_VIEW_WHITELABEL_DOMAIN, <<"accounts/list_by_whitelabel_domain">>).
%%%===================================================================
%%% API
%%%===================================================================
-spec init() -> ok.
init() ->
Bindings = [{<<"*.authenticate">>, 'authenticate'}
,{<<"*.authorize">>, 'authorize'}
,{<<"*.content_types_provided.whitelabel">>, 'content_types_provided'}
,{<<"*.content_types_accepted.whitelabel">>, 'content_types_accepted'}
,{<<"*.allowed_methods.whitelabel">>, 'allowed_methods'}
,{<<"*.resource_exists.whitelabel">>, 'resource_exists'}
,{<<"*.validate.whitelabel">>, 'validate'}
,{<<"*.execute.put.whitelabel">>, 'put'}
,{<<"*.execute.post.whitelabel">>, 'post'}
,{<<"*.execute.delete.whitelabel">>, 'delete'}
],
_ = cb_modules_util:bind(?MODULE, Bindings),
ok.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% This function determines the verbs that are appropriate for the
%% given Nouns. IE: '/accounts/' can only accept GET and PUT
%%
%% Failure here returns 405
%% @end
%%--------------------------------------------------------------------
-spec allowed_methods() -> http_methods().
-spec allowed_methods(path_token()) -> http_methods().
-spec allowed_methods(path_token(), path_token()) -> http_methods().
allowed_methods() ->
[?HTTP_GET, ?HTTP_PUT, ?HTTP_POST, ?HTTP_DELETE].
allowed_methods(?LOGO_REQ) ->
[?HTTP_GET, ?HTTP_POST];
allowed_methods(?ICON_REQ) ->
[?HTTP_GET, ?HTTP_POST];
allowed_methods(?WELCOME_REQ) ->
[?HTTP_GET, ?HTTP_POST];
allowed_methods(?DOMAINS_REQ) ->
[?HTTP_GET, ?HTTP_POST];
allowed_methods(_WhitelabelDomain) ->
[?HTTP_GET].
allowed_methods(_WhitelabelDomain, ?LOGO_REQ) ->
[?HTTP_GET];
allowed_methods(_WhitelabelDomain, ?ICON_REQ) ->
[?HTTP_GET];
allowed_methods(_WhitelabelDomain, ?WELCOME_REQ) ->
[?HTTP_GET].
%%--------------------------------------------------------------------
%% @public
%% @doc
%% This function determines if the provided list of Nouns are valid.
%%
%% Failure here returns 404
%% @end
%%--------------------------------------------------------------------
-spec resource_exists() -> 'true'.
-spec resource_exists(path_token()) -> 'true'.
-spec resource_exists(path_token(), path_token()) -> 'true'.
resource_exists() -> 'true'.
resource_exists(?LOGO_REQ) -> 'true';
resource_exists(?ICON_REQ) -> 'true';
resource_exists(?WELCOME_REQ) -> 'true';
resource_exists(?DOMAINS_REQ) -> 'true';
resource_exists(_) -> 'true'.
resource_exists(_, ?LOGO_REQ) -> 'true';
resource_exists(_, ?WELCOME_REQ) -> 'true';
resource_exists(_, ?ICON_REQ) -> 'true'.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec authorize(cb_context:context()) -> boolean().
authorize(Context) ->
authorize(Context
,cb_context:req_nouns(Context)
,cb_context:req_verb(Context)
).
-spec authorize(cb_context:context(), req_nouns(), http_method()) -> boolean().
authorize(Context, [{<<"whitelabel">>, [?DOMAINS_REQ]}], ?HTTP_POST) ->
%% /{VERSION}/whitelabel/domains retricted to sys-admin account
cb_context:is_superduper_admin(Context);
authorize(_Context, [{<<"whitelabel">>, [_]}], ?HTTP_GET) ->
'true';
authorize(_Context, [{<<"whitelabel">>, [_ | [?LOGO_REQ]]}], ?HTTP_GET) ->
'true';
authorize(_Context, [{<<"whitelabel">>, [_ | [?ICON_REQ]]}], ?HTTP_GET) ->
'true';
authorize(_Context, [{<<"whitelabel">>, [_ | [?WELCOME_REQ]]}], ?HTTP_GET) ->
'true';
authorize(_Context, _Nouns, _Verb) ->
'false'.
%%--------------------------------------------------------------------
%% @public
%% @doc
%% @end
%%--------------------------------------------------------------------
-spec authenticate(cb_context:context()) -> boolean().
authenticate(Context) ->
authenticate(cb_context:req_nouns(Context), cb_context:req_verb(Context)).
-spec authenticate(req_nouns(), http_method()) -> boolean().
authenticate([{<<"whitelabel">>, [_]}], ?HTTP_GET) ->
'true';
authenticate([{<<"whitelabel">>, [_ | [?LOGO_REQ]]}], ?HTTP_GET) ->
'true';
authenticate([{<<"whitelabel">>, [_ | [?ICON_REQ]]}], ?HTTP_GET) ->
'true';
authenticate([{<<"whitelabel">>, [_ | [?WELCOME_REQ]]}], ?HTTP_GET) ->
'true';
authenticate(_Nouns, _Verb) ->
'false'.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Add content types accepted and provided by this module
%%
%% @end
%%--------------------------------------------------------------------
-spec acceptable_content_types() -> kz_proplist().
acceptable_content_types() ->
?WHITELABEL_WELCOME_MIME_TYPES ++ ?WHITELABEL_ICON_MIME_TYPES.
-spec content_types_provided(cb_context:context(), path_token()) ->
cb_context:context().
content_types_provided(Context, AttachType) ->
content_types_provided_for_attachments(Context, AttachType, cb_context:req_verb(Context)).
-spec content_types_provided_for_attachments(cb_context:context(), path_token(), http_method()) ->
cb_context:context().
content_types_provided_for_attachments(Context, ?LOGO_REQ, ?HTTP_GET) ->
content_types_provided_for_attachments(Context, ?LOGO_REQ);
content_types_provided_for_attachments(Context, ?ICON_REQ, ?HTTP_GET) ->
content_types_provided_for_attachments(Context, ?ICON_REQ);
content_types_provided_for_attachments(Context, ?WELCOME_REQ, ?HTTP_GET) ->
content_types_provided_for_attachments(Context, ?WELCOME_REQ);
content_types_provided_for_attachments(Context, _Type, _Verb) ->
Context.
-spec content_types_provided_for_attachments(cb_context:context(), ne_binary()) ->
cb_context:context().
content_types_provided_for_attachments(Context, AttachType) ->
Context1 = load_whitelabel_meta(Context, ?WHITELABEL_ID),
case whitelabel_binary_meta(Context1, AttachType) of
'undefined' -> Context1;
{_, JObj} -> set_content_type_provided(Context1, JObj)
end.
-spec content_types_provided(cb_context:context(), path_token(), path_token()) ->
cb_context:context().
content_types_provided(Context, Domain, AttachType) ->
content_types_provided_for_domain_attachments(Context, Domain, AttachType, cb_context:req_verb(Context)).
-spec content_types_provided_for_domain_attachments(cb_context:context(), path_token(), path_token(), http_method()) ->
cb_context:context().
content_types_provided_for_domain_attachments(Context, Domain, ?LOGO_REQ, ?HTTP_GET) ->
content_types_provided_for_domain_attachments(Context, Domain, ?LOGO_REQ);
content_types_provided_for_domain_attachments(Context, Domain, ?ICON_REQ, ?HTTP_GET) ->
content_types_provided_for_domain_attachments(Context, Domain, ?ICON_REQ);
content_types_provided_for_domain_attachments(Context, Domain, ?WELCOME_REQ, ?HTTP_GET) ->
content_types_provided_for_domain_attachments(Context, Domain, ?WELCOME_REQ);
content_types_provided_for_domain_attachments(Context, _Domain, _AttachType, _Verb) ->
Context.
-spec content_types_provided_for_domain_attachments(cb_context:context(), path_token(), path_token()) ->
cb_context:context().
content_types_provided_for_domain_attachments(Context, Domain, AttachType) ->
case find_whitelabel_binary_meta(Context, Domain, AttachType) of
'undefined' -> Context;
{_, JObj} -> set_content_type_provided(Context, JObj)
end.
-spec set_content_type_provided(cb_context:context(), kz_json:object()) -> cb_context:context().
set_content_type_provided(Context, JObj) ->
CT = kz_json:get_value(<<"content_type">>, JObj),
[Type, SubType] = binary:split(CT, <<"/">>),
cb_context:set_content_types_provided(Context, [{'to_binary', [{Type, SubType}]}]).
-spec content_types_accepted(cb_context:context(), path_token()) -> cb_context:context().
content_types_accepted(Context, AttachType) ->
content_types_accepted(Context, AttachType, cb_context:req_verb(Context)).
-spec content_types_accepted(cb_context:context(), path_token(), http_method()) ->
cb_context:context().
content_types_accepted(Context, ?LOGO_REQ, ?HTTP_POST) ->
CTA = [{'from_binary', ?WHITELABEL_MIME_TYPES}],
cb_context:set_content_types_accepted(Context, CTA);
content_types_accepted(Context, ?ICON_REQ, ?HTTP_POST) ->
CTA = [{'from_binary', ?WHITELABEL_ICON_MIME_TYPES}],
cb_context:set_content_types_accepted(Context, CTA);
content_types_accepted(Context, ?WELCOME_REQ, ?HTTP_POST) ->
CTA = [{'from_binary', ?WHITELABEL_WELCOME_MIME_TYPES}],
cb_context:set_content_types_accepted(Context, CTA);
content_types_accepted(Context, _AttachType, _Verb) ->
Context.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function determines if the parameters and content are correct
%% for this request
%%
%% Failure here returns 400
%% @end
%%--------------------------------------------------------------------
-spec validate(cb_context:context()) -> cb_context:context().
validate(Context) ->
validate_whitelabel(Context, cb_context:req_verb(Context)).
-spec validate_whitelabel(cb_context:context(), http_method()) ->
cb_context:context().
validate_whitelabel(Context, ?HTTP_GET) ->
load_whitelabel_meta(Context, ?WHITELABEL_ID);
validate_whitelabel(Context, ?HTTP_PUT) ->
validate_request(Context, 'undefined');
validate_whitelabel(Context, ?HTTP_POST) ->
validate_request(Context, ?WHITELABEL_ID);
validate_whitelabel(Context, ?HTTP_DELETE) ->
load_whitelabel_meta(Context, ?WHITELABEL_ID).
-spec validate(cb_context:context(), path_token()) -> cb_context:context().
validate(Context, ?LOGO_REQ) ->
validate_attachment(Context, ?LOGO_REQ, cb_context:req_verb(Context));
validate(Context, ?ICON_REQ) ->
validate_attachment(Context, ?ICON_REQ, cb_context:req_verb(Context));
validate(Context, ?WELCOME_REQ) ->
validate_attachment(Context, ?WELCOME_REQ, cb_context:req_verb(Context));
validate(Context, ?DOMAINS_REQ) ->
validate_domains(Context, cb_context:req_verb(Context));
validate(Context, Domain) ->
validate_domain(Context, Domain, cb_context:req_verb(Context)).
-spec validate_attachment(cb_context:context(), path_token(), http_method()) ->
cb_context:context().
validate_attachment(Context, ?LOGO_REQ, ?HTTP_GET) ->
load_whitelabel_binary(Context, ?LOGO_REQ);
validate_attachment(Context, ?ICON_REQ, ?HTTP_GET) ->
load_whitelabel_binary(Context, ?ICON_REQ);
validate_attachment(Context, ?WELCOME_REQ, ?HTTP_GET) ->
load_whitelabel_binary(Context, ?WELCOME_REQ);
validate_attachment(Context, AttachType, ?HTTP_POST) ->
validate_attachment_post(Context, AttachType, cb_context:req_files(Context)).
-spec validate_attachment_post(cb_context:context(), path_token(), any()) ->
cb_context:context().
validate_attachment_post(Context, ?LOGO_REQ, []) ->
cb_context:add_validation_error(
<<"file">>
,<<"required">>
,kz_json:from_list(
[{<<"message">>, <<"Please provide an image file">>}]
)
,Context
);
validate_attachment_post(Context, ?ICON_REQ, []) ->
cb_context:add_validation_error(
<<"file">>
,<<"required">>
,kz_json:from_list(
[{<<"message">>, <<"Please provide an image file">>}]
)
,Context
);
validate_attachment_post(Context, ?WELCOME_REQ, []) ->
cb_context:add_validation_error(
<<"file">>
,<<"required">>
,kz_json:from_list(
[{<<"message">>, <<"Please provide an html file">>}]
)
,Context
);
validate_attachment_post(Context, ?LOGO_REQ, [{_Filename, FileJObj}]) ->
validate_upload(Context, FileJObj);
validate_attachment_post(Context, ?ICON_REQ, [{_Filename, FileJObj}]) ->
validate_upload(Context, FileJObj);
validate_attachment_post(Context, ?WELCOME_REQ, [{_Filename, FileJObj}]) ->
validate_upload(Context, FileJObj);
validate_attachment_post(Context, ?LOGO_REQ, _Files) ->
cb_context:add_validation_error(
<<"file">>
,<<"maxItems">>
,kz_json:from_list(
[{<<"message">>, <<"Please provide a single image file">>}]
)
,Context
);
validate_attachment_post(Context, ?ICON_REQ, _Files) ->
cb_context:add_validation_error(
<<"file">>
,<<"maxItems">>
,kz_json:from_list(
[{<<"message">>, <<"Please provide a single image file">>}]
)
,Context
);
validate_attachment_post(Context, ?WELCOME_REQ, _Files) ->
cb_context:add_validation_error(
<<"file">>
,<<"maxItems">>
,kz_json:from_list(
[{<<"message">>, <<"please provide a single html file">>}]
)
,Context
).
-spec validate_upload(cb_context:context(), kz_json:object()) ->
cb_context:context().
validate_upload(Context, FileJObj) ->
Context1 = load_whitelabel_meta(Context, ?WHITELABEL_ID),
case cb_context:resp_status(Context) of
'success' ->
Props = [{<<"content_type">>, content_type(FileJObj)}
,{<<"content_length">>, file_size(FileJObj)}
],
validate_request(cb_context:set_req_data(Context1
,kz_json:set_values(Props, cb_context:doc(Context))
)
,?WHITELABEL_ID
);
_Status -> Context1
end.
-spec content_type(kz_json:object()) -> ne_binary().
content_type(FileJObj) ->
kz_json:get_value([<<"headers">>, <<"content_type">>]
,FileJObj
,<<"application/octet-stream">>
).
-spec file_size(kz_json:object()) -> non_neg_integer().
file_size(FileJObj) ->
case kz_json:get_integer_value(
[<<"headers">>, <<"content_length">>]
,FileJObj
)
of
'undefined' ->
byte_size(kz_json:get_value(<<"contents">>, FileJObj, <<>>));
Size -> Size
end.
-spec validate_domains(cb_context:context(), http_method()) ->
cb_context:context().
validate_domains(Context, ?HTTP_GET) ->
load_domains(Context);
validate_domains(Context, ?HTTP_POST) ->
case cb_context:account_id(Context) of
'undefined' -> edit_domains(Context);
_AccountId -> test_account_domains(Context)
end.
-spec load_domains(cb_context:context()) ->
cb_context:context().
-spec load_domains(cb_context:context(), api_binary()) ->
cb_context:context().
-spec load_domains(cb_context:context(), ne_binary(), kz_json:object()) ->
cb_context:context().
load_domains(Context) ->
load_domains(Context, find_domain(Context)).
load_domains(Context, 'undefined') ->
missing_domain_error(Context);
load_domains(Context, Domain) ->
load_domains(Context, Domain, system_domains()).
load_domains(Context, Domain, SystemDomains) ->
AccountDomains = kzd_domains:format(SystemDomains, Domain),
cb_context:setters(Context
,[{fun cb_context:set_resp_data/2, AccountDomains}
,{fun cb_context:set_resp_status/2, 'success'}
]).
-spec missing_domain_error(cb_context:context()) -> cb_context:context().
missing_domain_error(Context) ->
cb_context:add_validation_error(<<"domain">>
,<<"required">>
,kz_json:from_list(
[{<<"message">>, <<"No domain found to use. Supply one on the request or create one via the whitelabel API">>}]
)
,Context
).
-spec find_domain(cb_context:context()) -> api_binary().
find_domain(Context) ->
case cb_context:req_value(Context, <<"domain">>) of
'undefined' -> find_existing_domain(Context);
Domain -> Domain
end.
-spec find_existing_domain(cb_context:context()) -> api_binary().
find_existing_domain(Context) ->
Context1 = load_whitelabel_meta(Context, ?WHITELABEL_ID),
case cb_context:resp_status(Context1) of
'success' -> kz_json:get_value(<<"domain">>, cb_context:doc(Context1));
_Status -> 'undefined'
end.
-spec system_domains() -> kz_json:object().
system_domains() ->
case kapps_config:get_json(<<"whitelabel">>, <<"domains">>) of
'undefined' ->
lager:info("initializing system domains to default"),
Default = kzd_domains:default(),
kapps_config:set_default(<<"whitelabel">>, <<"domains">>, Default),
Default;
Domains -> Domains
end.
-spec edit_domains(cb_context:context()) -> cb_context:context().
edit_domains(Context) ->
Domains = cb_context:req_data(Context),
PvtFields = crossbar_doc:update_pvt_parameters(
kz_json:new()
,Context
),
lager:debug("saving domains ~p", [Domains]),
lager:debug("with pvt fields: ~p", [PvtFields]),
case kzd_domains:save(Domains, PvtFields) of
{'error', 'not_found'} ->
lager:debug("schema for domains is missing"),
missing_schema_error(Context);
{'error', Errors} ->
lager:debug("failed to save with ~p", [Errors]),
cb_context:failed(Context, Errors);
{'ok', SavedDomains} ->
lager:debug("saved domains: ~p", [SavedDomains]),
crossbar_util:response(SavedDomains, Context)
end.
-spec missing_schema_error(cb_context:context()) ->
cb_context:context().
missing_schema_error(Context) ->
cb_context:add_validation_error(<<"domains">>
,<<"required">>
,kz_json:from_list(
[{<<"message">>, <<"The domains schema is missing, unable to validate request">>}]
)
,Context
).
-spec test_account_domains(cb_context:context()) ->
cb_context:context().
-spec test_account_domains(cb_context:context(), kzd_domains:doc()) ->
cb_context:context().
test_account_domains(Context) ->
Context1 = load_domains(Context),
case cb_context:resp_status(Context1) of
'success' ->
test_account_domains(Context, cb_context:resp_data(Context1));
_Status ->
Context1
end.
test_account_domains(Context, DomainsJObj) ->
Options = test_network_options(Context),
TestResults =
kz_json:map(fun(DomainType, DomainConfig) ->
test_domains(DomainType, DomainConfig, Options)
end
,DomainsJObj
),
crossbar_util:response(TestResults, Context).
-spec test_domains(ne_binary(), kz_json:object(), kz_network_utils:options()) ->
{ne_binary(), kz_json:object()}.
test_domains(DomainType, DomainConfig, Options) ->
{DomainType, test_domain_config(DomainType, DomainConfig, Options)}.
-spec test_domain_config(ne_binary(), kz_json:object(), kz_network_utils:options()) ->
kz_json:object().
test_domain_config(DomainType, DomainConfig, Options) ->
kz_json:map(fun(Host, HostConfig) ->
{Host, test_host(Host, HostConfig, DomainType, Options)}
end
,DomainConfig
).
-spec test_host(ne_binary(), kz_json:object(), ne_binary(), kz_network_utils:options()) ->
kz_json:object().
test_host(Host, HostConfig, DomainType, Options) ->
kz_json:from_list([{<<"expected">>, kzd_domains:mappings(HostConfig)}
,{<<"actual">>, lookup(Host, DomainType, Options)}
,{<<"name">>, kzd_domains:name(HostConfig)}
]).
-spec lookup(ne_binary(), ne_binary(), kz_network_utils:options()) ->
ne_binaries().
lookup(Host, DomainType, Options) ->
Type = kz_term:to_atom(kz_term:to_lower_binary(DomainType)),
{'ok', Lookup} = kz_network_utils:lookup_dns(Host, Type, Options),
lager:debug("lookup of ~s(~s): ~p", [Host, Type, Lookup]),
format_lookup_results(Type, Lookup).
-spec format_lookup_results(atom(), [inet_res:dns_data()]) ->
ne_binaries().
format_lookup_results(Type, Lookup) ->
[format_lookup_result(Type, Result) || Result <- Lookup].
-spec format_lookup_result(atom(), inet_res:dns_data()) -> ne_binary().
format_lookup_result('naptr', {_, _, _, _, _, _}=NAPTR) ->
kz_network_utils:naptrtuple_to_binary(NAPTR);
format_lookup_result('srv', {_, _, _, _}=Srv) ->
kz_network_utils:srvtuple_to_binary(Srv);
format_lookup_result('mx', {_, _}=MX) ->
kz_network_utils:mxtuple_to_binary(MX);
format_lookup_result(_, {_, _, _, _}=IP) ->
kz_network_utils:iptuple_to_binary(IP);
format_lookup_result(_, {_, _, _, _, _, _, _, _}=IP) ->
kz_network_utils:iptuple_to_binary(IP);
format_lookup_result(_Type, Value) ->
lager:debug("attempt to convert ~s result to binary: ~p", [Value]),
kz_term:to_binary(Value).
-spec test_network_options(cb_context:context()) -> kz_network_utils:options().
test_network_options(Context) ->
Domain = find_domain(Context),
DefaultOptions = kz_network_utils:default_options(),
case kz_network_utils:find_nameservers(Domain) of
[] -> DefaultOptions;
Nameservers ->
kz_network_utils:set_option_nameservers(DefaultOptions, Nameservers)
end.
-spec validate_domain(cb_context:context(), path_token(), http_method()) ->
cb_context:context().
validate_domain(Context, Domain, ?HTTP_GET) ->
case cb_context:account_id(Context) of
'undefined' -> find_whitelabel_meta(Context, Domain);
_AccountId -> load_whitelabel_meta(Context, ?WHITELABEL_ID)
end.
-spec validate(cb_context:context(), path_token(), path_token()) -> cb_context:context().
validate(Context, Domain, AttachType) ->
validate_domain_attachment(Context, Domain, AttachType, cb_context:req_verb(Context)).
validate_domain_attachment(Context, Domain, AttachType, ?HTTP_GET) ->
find_whitelabel_binary(Context, Domain, AttachType).
-spec put(cb_context:context()) -> cb_context:context().
put(Context) ->
Callback =
fun() ->
maybe_update_account_definition(crossbar_doc:save(Context))
end,
crossbar_services:maybe_dry_run(Context, Callback, <<"branding">>).
-spec post(cb_context:context()) -> cb_context:context().
post(Context) ->
maybe_update_account_definition(crossbar_doc:save(Context)).
-spec post(cb_context:context(), path_token()) -> cb_context:context().
post(Context, ?LOGO_REQ) ->
update_whitelabel_binary(?LOGO_REQ, ?WHITELABEL_ID, Context);
post(Context, ?ICON_REQ) ->
update_whitelabel_binary(?ICON_REQ, ?WHITELABEL_ID, Context);
post(Context, ?WELCOME_REQ) ->
update_whitelabel_binary(?WELCOME_REQ, ?WHITELABEL_ID, Context);
post(Context, ?DOMAINS_REQ) ->
cb_context:set_resp_status(Context, 'success').
-spec delete(cb_context:context()) -> cb_context:context().
delete(Context) ->
maybe_cleanup_account_definition(crossbar_doc:delete(Context, ?HARD_DELETE)).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Load the binary attachment of a whitelabel doc (based on a domain)
%% @end
%%--------------------------------------------------------------------
-spec find_whitelabel(cb_context:context(), ne_binary()) ->
cb_context:context().
find_whitelabel(Context, Domain) ->
ViewOptions = [{'key', kz_term:to_lower_binary(Domain)}],
Context1 = crossbar_doc:load_view(?AGG_VIEW_WHITELABEL_DOMAIN
,ViewOptions
,cb_context:set_account_db(Context, ?KZ_ACCOUNTS_DB)
),
case cb_context:resp_status(Context1) of
'success' ->
case cb_context:doc(Context1) of
[JObj] ->
Db = kz_json:get_ne_value([<<"value">>, <<"account_db">>], JObj),
Id = kz_json:get_ne_value([<<"value">>, <<"account_id">>], JObj),
cb_context:setters(Context1
,[{fun cb_context:set_account_db/2, Db}
,{fun cb_context:set_account_id/2, Id}
]);
_Doc ->
cb_context:add_system_error('bad_identifier'
,kz_json:from_list([{<<"cause">>, Domain}])
,Context1
)
end;
_Status -> Context1
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Load a whitelabel document from the database
%% @end
%%--------------------------------------------------------------------
-spec load_whitelabel_meta(cb_context:context(), ne_binary()) -> cb_context:context().
load_whitelabel_meta(Context, WhitelabelId) ->
crossbar_doc:load(WhitelabelId, Context, ?TYPE_CHECK_OPTION(<<"whitelabel">>)).
-spec find_whitelabel_meta(cb_context:context(), ne_binary()) -> cb_context:context().
find_whitelabel_meta(Context, Domain) ->
Context1 = find_whitelabel(Context, Domain),
case cb_context:resp_status(Context1) of
'success' -> load_whitelabel_meta(Context1, ?WHITELABEL_ID);
_Status -> Context1
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Load a whitelabel binary attachment from the database
%% @end
%%--------------------------------------------------------------------
-spec load_whitelabel_binary(cb_context:context(), ne_binary()) -> cb_context:context().
load_whitelabel_binary(Context, AttachType) ->
case whitelabel_binary_meta(Context, AttachType) of
'undefined' -> crossbar_util:response_bad_identifier(AttachType, Context);
{AttachmentId, JObj} ->
update_response_with_attachment(Context, AttachmentId, JObj)
end.
-spec find_whitelabel_binary(cb_context:context(), ne_binary(), ne_binary()) -> cb_context:context().
find_whitelabel_binary(Context, Domain, AttachType) ->
Context1 = find_whitelabel_meta(Context, Domain),
case cb_context:resp_status(Context1) of
'success' -> load_whitelabel_binary(Context1, AttachType);
_Status -> Context1
end.
-spec find_whitelabel_binary_meta(cb_context:context(), ne_binary(), ne_binary()) ->
'undefined' | {ne_binary(), kz_json:object()}.
find_whitelabel_binary_meta(Context, Domain, AttachType) ->
Context1 = find_whitelabel_meta(Context, Domain),
case cb_context:resp_status(Context1) of
'success' -> whitelabel_binary_meta(Context1, AttachType);
_Status -> 'undefined'
end.
-spec whitelabel_binary_meta(cb_context:context(), ne_binary()) ->
'undefined' | {ne_binary(), kz_json:object()}.
whitelabel_binary_meta(Context, AttachType) ->
case cb_context:resp_status(Context) of
'success' ->
JObj = kz_doc:attachments(cb_context:doc(Context), kz_json:new()),
case whitelabel_attachment_id(JObj, AttachType) of
'undefined' -> 'undefined';
AttachmentId ->
{AttachmentId, kz_json:get_value(AttachmentId, JObj)}
end;
_ -> 'undefined'
end.
-spec whitelabel_attachment_id(kz_json:object(), ne_binary()) ->
'undefined' | {ne_binary(), kz_json:object()}.
whitelabel_attachment_id(JObj, AttachType) ->
filter_attachment_type(kz_json:get_keys(JObj), AttachType).
-spec filter_attachment_type(ne_binaries(), ne_binary()) -> api_binary().
filter_attachment_type([], _) -> 'undefined';
filter_attachment_type([AttachmentId], <<"logo">>) ->
%% if there is only one attachment and it is not an icon
%% then it is the deprecated whitelabel when we assumed
%% the only attachment was the logo...
case binary:match(AttachmentId, <<"icon">>) of
{0, _} -> 'undefined';
_Else -> AttachmentId
end;
filter_attachment_type([AttachmentId|AttachmentIds], AttachType) ->
case binary:match(AttachmentId, AttachType) of
{0, _} -> AttachmentId;
_Else -> filter_attachment_type(AttachmentIds, AttachType)
end.
-spec update_response_with_attachment(cb_context:context(), ne_binary(), kz_json:object()) ->
cb_context:context().
update_response_with_attachment(Context, AttachmentId, JObj) ->
cb_context:set_resp_etag(
cb_context:add_resp_headers(
crossbar_doc:load_attachment(cb_context:doc(Context), AttachmentId, ?TYPE_CHECK_OPTION(<<"whitelabel">>), Context)
,[{<<"Content-Disposition">>, <<"attachment; filename=", AttachmentId/binary>>}
,{<<"Content-Type">>, kz_json:get_value([AttachmentId, <<"content_type">>], JObj)}
,{<<"Content-Length">>, kz_json:get_value([AttachmentId, <<"length">>], JObj)}
]
), 'undefined'
).
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec validate_request(cb_context:context(), api_binary()) -> cb_context:context().
validate_request(Context, WhitelabelId) ->
validate_unique_domain(Context, WhitelabelId).
-spec validate_unique_domain(cb_context:context(), api_binary()) -> cb_context:context().
validate_unique_domain(Context, WhitelabelId) ->
Domain = kz_json:get_ne_value(<<"domain">>, cb_context:req_data(Context)),
case is_domain_unique(cb_context:account_id(Context), Domain) of
'true' -> check_whitelabel_schema(Context, WhitelabelId);
'false' ->
Context1 =
cb_context:add_validation_error(
<<"domain">>
,<<"unique">>
,kz_json:from_list(
[{<<"message">>, <<"White label domain is already in use">>}
,{<<"cause">>, Domain}
])
,Context
),
check_whitelabel_schema(Context1, WhitelabelId)
end.
-spec check_whitelabel_schema(cb_context:context(), api_binary()) -> cb_context:context().
check_whitelabel_schema(Context, WhitelabelId) ->
OnSuccess = fun(C) -> on_successful_validation(C, WhitelabelId) end,
cb_context:validate_request_data(<<"whitelabel">>, Context, OnSuccess).
-spec on_successful_validation(cb_context:context(), api_binary()) -> cb_context:context().
on_successful_validation(Context, 'undefined') ->
Doc = kz_json:set_values([{<<"pvt_type">>, <<"whitelabel">>}
,{<<"_id">>, ?WHITELABEL_ID}
], cb_context:doc(Context)),
cb_context:set_doc(Context, Doc);
on_successful_validation(Context, WhitelabelId) ->
crossbar_doc:load_merge(WhitelabelId, Context, ?TYPE_CHECK_OPTION(<<"whitelabel">>)).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Load the binary attachment of a whitelabel doc
%% @end
%%--------------------------------------------------------------------
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Update the binary attachment of a whitelabel doc
%% @end
%%--------------------------------------------------------------------
-spec update_whitelabel_binary(ne_binary(), path_token(), cb_context:context()) ->
cb_context:context().
update_whitelabel_binary(AttachType, WhitelabelId, Context) ->
JObj = cb_context:doc(Context),
[{Filename, FileObj}] = cb_context:req_files(Context),
Contents = kz_json:get_value(<<"contents">>, FileObj),
CT = kz_json:get_value([<<"headers">>, <<"content_type">>], FileObj),
Opts = [{'content_type', CT} | ?TYPE_CHECK_OPTION(<<"whitelabel">>)],
JObj1 = case whitelabel_binary_meta(Context, AttachType) of
'undefined' -> JObj;
{AttachmentId, _} ->
kz_doc:delete_attachment(JObj, AttachmentId)
end,
Context1 = crossbar_doc:save(cb_context:set_doc(Context, JObj1)),
crossbar_doc:save_attachment(WhitelabelId
,attachment_name(AttachType, Filename, CT)
,Contents
,Context1
,Opts
).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Generate an attachment name if one is not provided and ensure
%% it has an extension (for the associated content type)
%% @end
%%--------------------------------------------------------------------
-spec attachment_name(ne_binary(), ne_binary()) -> ne_binary().
-spec attachment_name(ne_binary(), ne_binary(), ne_binary()) -> ne_binary().
attachment_name(Filename, CT) ->
Generators = [fun(A) ->
case kz_term:is_empty(A) of
'true' -> kz_term:to_hex_binary(crypto:strong_rand_bytes(16));
'false' -> A
end
end
,fun(A) ->
case kz_term:is_empty(filename:extension(A)) of
'false' -> A;
'true' ->
<<A/binary, ".", (kz_mime:to_extension(CT))/binary>>
end
end
],
lists:foldl(fun(F, A) -> F(A) end, Filename, Generators).
attachment_name(AttachType, Filename, CT) ->
<<AttachType/binary, "-", (attachment_name(Filename, CT))/binary>>.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec is_domain_unique(ne_binary(), ne_binary()) -> boolean().
is_domain_unique(AccountId, Domain) ->
ViewOptions = [{'key', kz_term:to_lower_binary(Domain)}],
case kz_datamgr:get_results(?KZ_ACCOUNTS_DB, ?AGG_VIEW_WHITELABEL_DOMAIN, ViewOptions) of
{'ok', []} -> 'true';
{'ok', [JObj]} ->
kz_json:get_ne_value([<<"value">>, <<"account_id">>], JObj) =:= AccountId;
{'ok', _} -> 'false';
{'error', _R} ->
lager:debug("unable to get whitelabel domain view: ~p", [_R]),
'false'
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec maybe_update_account_definition(cb_context:context()) -> cb_context:context().
maybe_update_account_definition(Context) ->
maybe_update_account_definition(Context, cb_context:resp_status(Context)).
maybe_update_account_definition(Context, 'success') ->
Context1 = crossbar_doc:load(cb_context:account_id(Context), Context, ?TYPE_CHECK_OPTION(kz_account:type())),
case cb_context:resp_status(Context1) of
'success' ->
AccountDoc = cb_context:doc(Context1),
Domain = kz_json:get_ne_value(<<"domain">>, cb_context:doc(Context)),
AccountDoc1 = kz_json:set_value(<<"pvt_whitelabel_domain">>, Domain, AccountDoc),
_ = cb_accounts:post(cb_context:set_doc(Context1, AccountDoc1), cb_context:account_id(Context)),
Context;
_Status -> Context1
end;
maybe_update_account_definition(Context, _Status) -> Context.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec maybe_cleanup_account_definition(cb_context:context()) -> cb_context:context().
maybe_cleanup_account_definition(Context) ->
maybe_cleanup_account_definition(Context, cb_context:resp_status(Context)).
maybe_cleanup_account_definition(Context, 'success') ->
Context1 = crossbar_doc:load(cb_context:account_id(Context), Context, ?TYPE_CHECK_OPTION(kz_account:type())),
case cb_context:resp_status(Context1) of
'success' ->
AccountDoc = cb_context:doc(Context1),
AccountDoc1 = kz_json:delete_key(<<"pvt_whitelabel_domain">>, AccountDoc),
_ = cb_accounts:post(cb_context:set_doc(Context1, AccountDoc1), cb_context:account_id(Context)),
Context;
_Status -> Context1
end;
maybe_cleanup_account_definition(Context, _Status) -> Context.