-
Notifications
You must be signed in to change notification settings - Fork 13
/
AuthDialogs.wl
697 lines (659 loc) · 16.2 KB
/
AuthDialogs.wl
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
(* ::Package:: *)
(* ::Section:: *)
(*AuthDialogs*)
(* ::Text:: *)
(*Direct copy of one of my BTools packages. Interface is pretty bad.*)
BeginPackage["AuthDialogs`"];
BannerDialog::usage=
"Generate a dialog with a banner";
BannerDialogInput::usage=
"Generates a dialog for with a banner";
AuthenticationDialog::usage=
"Generates an authentication dialog";
PasswordDialog::usage=
"A raw password dialog";
OAuthDialog::usage=
"Uses AuthenticationDialog to generate an OAuth dialog";
Begin["`Private`"];
Options[BannerDialogConfig]=
Join[
Options[DialogInput],{
Appearance->Automatic
}];
BannerDialogConfig[
banner_,
content_,
submit:_Button|Automatic:Automatic,
cancel:_Button|Automatic:Automatic,
ops:OptionsPattern[]
]:=
{
{
ExpressionCell[#,
CellFrameMargins->None,
CellMargins->8]&@
With[{
size=
Replace[OptionValue[WindowSize],{
{w_,h_}:>
{
Replace[w,
Automatic->400
],
Replace[h,
Automatic->450
]-110
},
w_Integer:>
{w,340},
Automatic:>
{400,340}
}]
},
Pane[
content,
ImageSize->
size,
BaseStyle->"Output",
Scrollbars->Automatic
]
],
ExpressionCell[#,
"Output",
Background->GrayLevel[.98],
CellFrameColor->GrayLevel[.8],
CellFrame->{{0,0},{0,1}},
CellMargins->0,
CellTags->{"FooterCell"}
]&@
Pane[
Row@
{
With[{
s=
Replace[submit,
Automatic:>
Button["Submit",DialogReturn[True]]
]
},
(*ExpressionCell[#,
CellTags\[Rule]"Return",
CellMargins\[Rule]0,
CellFrameMargins\[Rule]0
]&@*)
Button[
Style[First@s,
Replace[OptionValue@Appearance,{
c_?ColorQ:>
If[ColorDistance[c,Black,DistanceFunction->"DeltaL"]>.7,
Black,
White
],
_->White
}]
],
s[[2]];DialogReturn[],
ImageSize->{100,30},
Appearance->
Replace[
Replace[OptionValue@Appearance,{
c_?ColorQ:>
GradientButtonAppearance[
{c,c,
Replace[ColorConvert[c,Hue],
Hue[h_,s_,b_]:>
Hue[h,s,b*1.1]
]
}
],
"OAuth"->
{"Default"->Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UFAGiHmBmIUBBfwngFFAmrHxf2Ixsp6Zvr4E8TDW859EPf+Hgp5ZQDFCGF3P
HD8/ghhdz1x/f4IYXc98oBghjK5nYUAAQYyuZ1FgIEGMrmdJUBBBjK5nWXAw
QYyuZ3lICEGMrmcFUIwQplAPA4l6GIaCHhLLREJlLkYZDAA7N6DO
"], "Byte", ColorSpace -> "RGB", Interleaving -> True],"Hover"->Image[CompressedData["
1:eJzd1DEKwkAQheFBLWwED5DGW9haSrqIB0hwDTZrsruJeCgjiop6uXVKMxbP
xSou/IE8+NqZZNtk3SMiO+RPku5mxqT7xZh/ltpucq1Wc+1Ursw06/MYcSNu
QK3nQa3n4th/27s5FAXsj40PNL4LpilLmDRH3lDSnIyBSXO2FibNhTeUNFfn
YNLcqgomzZ03lDSPuoZJ8+QN9aOhQENdMIE3Ed3cjxv8AkYm/IA=
"], "Byte", ColorSpace -> "RGB", Interleaving -> True],"Pressed"->Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UFAGiHmBmIUBBfwngFFAjJ7ef2Ixsp5aOzuCeBjr+U+inv9012NvTxCj62lw
cCCI0fU0OzoSxOh6Wp2dCWJ0PR0uLgQxup5uV1eCGF1Pr7s7QYyuZ4KHB0GM
rmeylxdBjK5nmrc3QYyuZ7qPD0FMoR4GEvUwDAU9JJaJhMpcjDIYAHUsdEc=
"], "Byte", ColorSpace -> "RGB", Interleaving -> True]},
"Password"->
{"Default"->Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UFAGiHmBmIUBBfwngFGAa5rtf2Ixsp6MmTEE8TDW859EPf+Hgp7MWTEEMbqe
rDmxBDG6nuy5cQQxup6c+fEEMbqe3IUJBDG6nrxFiQQxup6CJUkEMbqewmXJ
BDG6nqLlKQQxhp4VqQQxhXoYSNTDMBT0kFgmEipzMcpgALzlzcw=
"], "Byte", ColorSpace -> "RGB", Interleaving -> True],"Hover"->Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UFAGiHmBmIUBBfwngFFAZEnwf2Ixsp62tTUE8TDW859EPf+Hgp72dbUEMbqe
jvV1BDG6ns6NdQQxhp5N9QQxup6uLQ0EMbqe7q2NBDG6np5tTQQxup7eHc0E
Mbqevp3NBDG6nv5dLQQxhXoYSNTDMBT0kFgmEipzMcpgAEESIOE=
"], "Byte", ColorSpace -> "RGB", Interleaving -> True],"Pressed"->Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UFAGiHmBmIUBBfwngFGAXYz5f2Ixsp7QWm+CeBjr+U+inv9DQU94gy9BjK4n
stmPIEbXE90aQBCj64npCCKI0fXEdQcTxOh6EnpDCWJ0PUkTwglidD0pkyMI
YnQ9adOiCGJ0PenTowliCvUwkKiHYSjoIbFMJFTmYpTBADDZm5A=
"], "Byte", ColorSpace -> "RGB", Interleaving -> True]},
_:>
{"Default"->Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UFAGiHmBmIUBBfwngFGBTv5/ojGyHqdlhPHw1fOfRD3/h4Qe5+WEMboel1WE
Mboe17WEMboetw2EMboe982EMboez62EMboerx2EMboe792EMboen32EMboe
3/2EMWV6GEjUwzAk9JBWJhIqczHKYAD7oFbD
"], "Byte", ColorSpace -> "RGB", Interleaving -> True],"Hover"->Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UFAGiHmBmIUBBfwngFGAkmvVf2Ixsh7jjB0E8TDW859EPf+Hhp6dBDGGnszd
BDGGnqy9BDGGnuwDBDGGnpxDBDGGntwjBDGGnrxjBDGGnvyTBDGGnoLTBDGm
njMEMYV6GEjUwzAU9JBYJhIqczHKYAATccEy
"], "Byte", ColorSpace -> "RGB", Interleaving -> True],"Pressed"->Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UFAGiHmBmIUBBfwngFGBRvJ/ojGyHvMOwnj46vlPop7/9NfTSRij67HoJozR
9Vj2EcboeqwmEsboeqwnE8boemymE8boeuxmE8boehwWEMboepyWEsboelxW
EcboelzXEMaU6WEgUQ/DkNBDWplIqMzFKIMBfRg0Sw==
"], "Byte", ColorSpace -> "RGB", Interleaving -> True]}
}
],
l_List:>
Append[l,"ButtonType" -> "Default"]
]
]
],
Spacer[{10,0}],
With[{
c=
Replace[cancel,
Automatic:>
Button["Cancel",DialogReturn[$Canceled]]
]
},
Button[
First@c,
c[[2]];
DialogReturn[$Canceled],
ImageSize->{100,30},
Appearance->
{
"Default"->Image[CompressedData["
1:eJzV1EEKgkAYhuGhWrQJOkCbbtG2ZduiAyhZtDGwIDqKRxNFUVFEEVF0bUMU
5NfiS1r1wzvMDDzbf66e1vuBEOI8lsdauS4NQ7ltpvKx1c/Hg67tVvpFO2jG
Qh3Kz5lsIhuJzrSkzpim2X7bu7Esi4bGtm0aGsdxaGhc16Wh8TyP9mYed9/3
aU/TvkwQBDQ0YRjS0ERRREMTxzENTZIkNDRpmtLQZFlGQ5PnOQ1NURQ0NGVZ
0tBUVUVDU9c1DU3TNLQfjehpxD+YnjuR7dyPHXwHy1co2w==
"], "Byte", ColorSpace -> "RGB", Interleaving -> True],
"Hover"->Image[CompressedData["
1:eJzV1E0KgkAYxvGhWrQJOkCbbtG2ZduiAyhZtDGwIDqKx/MDP1FRUZc2REE+
LZ6kVS/8h5mB3/adq6f1fiCEOI/lsVauS8NQbpupfGz18/Gga7uVftEOmrFQ
h/JzJpvIRqIzLakzpmm23/ZuLMuiobFtm4bGcRwaGtd1aWg8z6O9mcfd933a
07QvEwQBDU0YhjQ0URTR0MRxTEOTJAkNTZqmNDRZltHQ5HlOQ1MUBQ1NWZY0
NFVV0dDUdU1D0zQN7UcjehrxD6bnTmQ792MH3wFFhzOd
"], "Byte", ColorSpace -> "RGB", Interleaving -> True],
"Pressed"->Image[CompressedData["
1:eJxTTMoPSmNiYGAo5gASQYnljkVFiZXBAkBOaF5xZnpeaopnXklqemqRRRIz
UFAGiHmBmIUBBfwngFHAzJkz/xOLkfVs27aNIB7Gev6TqOc/vfVs376dIEbX
s2fPHoIYXc/BgwcJYnQ9x44dI4jR9Zw+fZogRtdz/vx5ghhdz6VLlwhidD1X
r14liNH13LhxgyBG13P79m2CGF3PnTt3CGIK9TCQqIdhKOghsUwkVOZilMEA
mEXTyg==
"], "Byte", ColorSpace -> "RGB", Interleaving -> True],
"ButtonType" -> "Cancel"
}
]
]
},
ImageSize->
Replace[OptionValue[WindowSize],{
{w_,h_}:>
{Replace[w,Automatic|Fit->400],100-70},
w_Integer:>
{w,100-70},
_->
{400,100-70}
}],
Alignment->{Left,Center}
]
},
WindowSize->
Replace[OptionValue@WindowSize,{
{w_,h_}:>
{
Replace[w,Automatic->400],
Replace[h,Automatic->450]
},
w_Integer:>
{w,450},
_->{400,450}
}],
Sequence@@
FilterRules[{
ops
},
Options@DialogInput
],
DockedCells->
Cell[
BoxData@ToBoxes@
Pane[
If[StringQ@banner,RawBoxes@banner,banner],
{Scaled[.7],30},
Alignment->{Center,Center},
ImageSizeAction->"ResizeToFit"
],
"Panel",
FontFamily->"Times",
FontColor->
Replace[OptionValue[Appearance],{
c_?ColorQ:>
Replace[ColorConvert[c,Hue],
Hue[h_,_,b_]:>
If[b>.7,
Hue[h,.1,.2],
Hue[h,.05,.95]
]
],
"OAuth"->
Hue[0,.03,.95],
"Password"->
Hue[.3,.03,.95],
_->Hue[.6,.05,.95]
}],
TextAlignment->Center,
Background->
Replace[OptionValue[Appearance],{
c_?ColorQ:>
c,
"OAuth"->
Hue[0,.5,.6],
"Password"->
Hue[.3,.4,.6],
_->
Hue[.6,1,.65]
}],
CellFrame->{{0,0},{1,0}}
],
NotebookDynamicExpression :>
Refresh[
FrontEnd`MoveCursorToInputField[
EvaluationNotebook[],
"password"
];
FrontEnd`MoveCursorToInputField[
EvaluationNotebook[],
"username"
];,
None
],
WindowFrameElements->{"CloseBox"},
WindowElements->None,
WindowTitle->"",
Background->White,
ImageMargins->0,
CellMargins->0
};
Options[BannerDialog]=
Options@BannerDialogConfig;
BannerDialog[
banner_:"",
content_,
submit:Button[_,__]|Automatic:Automatic,
cancel:Button[_,__]|Automatic:Automatic,
ops:OptionsPattern[]
]:=
CreateDialog@@
BannerDialogConfig[banner,content,submit,cancel,ops]
Options[BannerDialogInput]=
Options@BannerDialogConfig;
BannerDialogInput[
vars:{(_Symbol|_Set|_SetDelayed)...}|None:None,
banner_:"",
content_,
submit:Button[_,__]|Automatic:Automatic,
cancel:Button[_,__]|Automatic:Automatic,
ops:OptionsPattern[]
]:=
If[Unevaluated[vars]===None,
DialogInput[#,##2]&,
DialogInput[vars,Column@#,##2]&
]@@
BannerDialogConfig[banner,content,submit,cancel,ops];
BannerDialogInput~SetAttributes~HoldFirst
authUserNameField[Verbatim[Dynamic][var_],site_,default_]:=
InputField[
var[site]=
Replace[var[site],{
{v:Except[""],p_}:>
{v,p},
{"",p_}:>
{default,p},
_->{default,""}
}];
Dynamic[
Replace[var[site],{
{u_,_}:>u,
_->default
}],
var[site]=
Replace[var[site],{
{_,p_}:>
{#,p},
_->{#,""}
}];&
],
String,
BoxID->"username"
];
authPasswordField[Verbatim[Dynamic][var_],site_,default_]:=
InputField[
var[site]=
Replace[var[site],{
{v_,p:Except[""]}:>
{v,p},
{v_,""}:>
{v,default},
_->{"",default}
}];
Dynamic[
Replace[var[site],{
{_,p_}:>p,
_->default
}],
var[site]=
Replace[var[site],{
{u_,_}:>
{u,#},
_->{default,#}
}];&
],
String,
FieldMasked->$authDialogFieldMasked,
BoxID->"password"
];
authDialogFields[var_,
site:_String|{_String,_},
username:_String|None|{_String,_}:"",
password:_String|None|{_String,_}:""]:=
{
{
If[StringQ@site||Last@site===Automatic,
Style["Enter the password for ``:"~TemplateApply~site,GrayLevel[.2]],
Last@site
],
SpanFromLeft
},
If[username=!=None,
DeleteCases[None]@{
If[StringQ@username||Last@site===Automatic,
Style["Username:",GrayLevel[.5]],
Last@username
],
authUserNameField[var,
Replace[site,{s_,_}:>s],
Replace[username,
{u_,_}:>u
]
]
},
{}
],
If[password=!=None,
DeleteCases[None]@{
If[StringQ@password||Last@site===Automatic,
Style["Password:",GrayLevel[.5]],
Last@password
],
authPasswordField[var,
Replace[site,{s_,_}:>s],
Replace[password,
{p_,_}:>p
]]
},
{}
]
};
authDialogFields[var_,
{site:_String|{_String,_}}]:=
authDialogFields[var,site];
authDialogFields[var_,
{site:_String|{_String,_},
username:_String|None|{_String,_}
}
]:=
authDialogFields[var,site,username];
authDialogFields[var_,
{site:_String|{_String,_},
username:_String|None|{_String,_},
password:_String|None|{_String,_}}]:=
authDialogFields[var,site,username,password];
SetAttributes[#,
{
HoldFirst
}
]&/@{
authDialogFields,
authPasswordField,
authUserNameField
};
Options[AuthenticationDialog]=
Join[
Options@BannerDialogConfig,{
FieldMasked->True
}];
AuthenticationDialog[
var:Verbatim[Dynamic][_Symbol]:Dynamic[None],
banner_:"Authentication Credentials",
header_:None,
specs:(
_String|{_String|{_String,_}}|
{_String|{_String,_},_String|None|{_String,_}}|
{_String|{_String,_},_String|None|{_String,_},_String|None|{_String,_}}
)..,
ops:OptionsPattern[]
]:=
If[MatchQ[var,Verbatim[Dynamic][None]],
Clear@$AuthenticationCache;
(Clear@$AuthenticationCache;#)&@
AuthenticationDialog[Dynamic[$AuthenticationCache],
banner,
header,
specs,
ops],
BannerDialogInput[
None,
If[!AssociationQ@First@var,
Replace[var,
Verbatim[Dynamic][v_]:>
Set[v,<||>]
]
];
banner,
If[header=!=None,
Column@{header,#}&,
Identity]@
Block[{$authDialogFieldMasked=TrueQ@OptionValue[FieldMasked]},
Grid[
Join@@
Map[
Function[Null,
authDialogFields[var,#],
HoldFirst],
DeleteDuplicatesBy[{specs},
Replace[{
{s_String,___}:>s,
{{s_String,_},___}:>s
}]
]
],
Dividers->{None,{None,{None,None,GrayLevel[.8]},None}},
Spacings->{Automatic,{0,{.5,.5,1},0}},
Alignment->Left
]
],
Button[
"Submit",
DialogReturn[First@var]
],
Button[
"Cancel",
DialogReturn[$Canceled]
],
FilterRules[{ops},
Options@BannerDialogConfig
],
WindowTitle->"Authenticate",
WindowSize->
If[Length@{specs}>1,
Automatic,
{350,300}
],
WindowFrame->"MovableModalDialog",
CellContext->$Context
]
];
Options[PasswordDialog]=
Append[
Options[AuthenticationDialog],
"PromptString"->"Input the password for ``:"
];
PasswordDialog[
var:Verbatim[Dynamic][_Symbol]:Dynamic[None],
banner_:"",
spec_String,
ops:OptionsPattern[]
]:=
If[MatchQ[var,Verbatim[Dynamic][None]],
Clear@$PasswordCache;
(Clear@$PasswordCache;#)&@
PasswordDialog[Dynamic@$PasswordCache,
banner,
spec,
ops
],
Replace[
a_Association:>
Replace[a[spec],{
_Missing->None,
{_,p_String}:>
p
}]
]@
AuthenticationDialog[var,
banner,
OptionValue["PromptString"]~TemplateApply~spec,
{{spec,Null},None,{"",None}},
Evaluate@
FilterRules[{
ops,
Appearance->"Password",
WindowTitle->"Input Password"
},
Options@AuthenticationDialog
]
]
]
Options[OAuthDialog]=
Options[AuthenticationDialog];
OAuthDialog[
var:Verbatim[Dynamic][_Symbol]:Dynamic[None],
banner_:"OAuth",
header:_String?(URLParse[#,"Scheme"]=!=None&)|Except[_String]:None,
spec:_String|{_String,_String},
ops:OptionsPattern[]
]:=
If[MatchQ[var,Verbatim[Dynamic][None]],
Clear@$OAuthCache;
(Clear@$OAuthCache;#)&@
OAuthDialog[Dynamic@$OAuthCache,
banner,
header,
spec,
ops
],
Replace[
a_Association:>
Replace[a[First@Flatten@{spec}],{
_Missing->None,
{u_String,p_String}:>
If[StringQ@spec,
p,
{u,p}
]
}]
]@
AuthenticationDialog[var,
banner,
Replace[header,{
s_String:>
Row@{
"Go to the ",
Hyperlink["OAuth Site",s]
},
{s_String,example:Except[_String]}:>
Column@{
Row@{
"Go to the ",
Hyperlink["OAuth Site",s]
},
Mouseover[
Style["Trouble finding the key?","Message"],
If[ImageQ@example,
Image[example,ImageSize->325],
Pane[example,
ImageSize->325,
ImageSizeAction->"ShrinkToFit"
]
],
ImageSize->All
]
},
{label_,link_String}:>
Row@{
"Go to ",
Hyperlink[label,link]
},
{{label_,link_},example_}:>
Column@{
Row@{
"Go to ",
Hyperlink[label,link]
},
Mouseover[
Style["Trouble finding the key?","Message"],
If[ImageQ@example,
Image[example,ImageSize->325],
Pane[example,
ImageSize->325,
ImageSizeAction->"ShrinkToFit"
]
],
ImageSize->All
]
}
}],
Replace[spec,{
site_String:>
{
{site,Null},
None,
{"",None}
},
{site_,uname_}:>
{
{site,Null},
uname,
{"",None}
}
}],
ops,
Appearance->"OAuth",
WindowTitle->"OAuth",
FieldMasked->False
]
]
(* ::Subsection:: *)
(*End*)
End[];
EndPackage[]