-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCryptIdent.html
2276 lines (1358 loc) · 75.8 KB
/
CryptIdent.html
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
Module: CryptIdent
— crypt_ident Module Documentation
</title>
<link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
<script type="text/javascript" charset="utf-8">
pathId = "CryptIdent";
relpath = '';
</script>
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
</head>
<body>
<div class="nav_wrap">
<iframe id="nav" src="class_list.html?1"></iframe>
<div id="resizer"></div>
</div>
<div id="main" tabindex="-1">
<div id="header">
<div id="menu">
<a href="_index.html">Index (C)</a> »
<span class="title">CryptIdent</span>
</div>
<div id="search">
<a class="full_list_link" id="class_list_link"
href="class_list.html">
<svg width="24" height="24">
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
</svg>
</a>
</div>
<div class="clear"></div>
</div>
<div id="content"><h1>Module: CryptIdent
</h1>
<div class="box_info">
<dl>
<dt>Extended by:</dt>
<dd>Dry::Configurable</dd>
</dl>
<dl>
<dt>Defined in:</dt>
<dd>lib/crypt_ident/config.rb<span class="defines">,<br />
lib/crypt_ident/sign_in.rb,<br /> lib/crypt_ident/sign_up.rb,<br /> lib/crypt_ident/version.rb,<br /> lib/crypt_ident/sign_out.rb,<br /> lib/crypt_ident/reset_password.rb,<br /> lib/crypt_ident/change_password.rb,<br /> lib/crypt_ident/session_expired.rb,<br /> lib/crypt_ident/generate_reset_token.rb,<br /> lib/crypt_ident/update_session_expiry.rb</span>
</dd>
</dl>
</div>
<h2>Overview</h2><div class="docstring">
<div class="discussion">
<p>Include and interact with <code>CryptIdent</code> to add authentication to a
Hanami controller action.</p>
<p>Note the emphasis on <em>controller action</em>; this module interacts with session
data, which is quite theoretically possible in an Interactor but practically
<em>quite</em> the PITA. YHBW.</p>
</div>
</div>
<div class="tags">
<p class="tag_title">Author:</p>
<ul class="author">
<li>
<div class='inline'><p>Jeff Dickey</p>
</div>
</li>
</ul>
<p class="tag_title">Version:</p>
<ul class="version">
<li>
<div class='inline'><p>0.2.2</p>
</div>
</li>
</ul>
</div>
<h2>
Constant Summary
<small><a href="#" class="constants_summary_toggle">collapse</a></small>
</h2>
<dl class="constants">
<dt id="VERSION-constant" class="">VERSION =
<div class="docstring">
<div class="discussion">
<p>Version number for Gem. Uses Semantic Versioning.</p>
</div>
</div>
<div class="tags">
</div>
</dt>
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>0.2.3</span><span class='tstring_end'>'</span></span></pre></dd>
</dl>
<h2>
Instance Method Summary
<small><a href="#" class="summary_toggle">collapse</a></small>
</h2>
<ul class="summary">
<li class="public ">
<span class="summary_signature">
<a href="#change_password-instance_method" title="#change_password (instance method)">#<strong>change_password</strong>(user_in, current_password, new_password) {|result| ... } </a>
</span>
<span class="summary_desc"><div class='inline'><p>Change an Authenticated User's password.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#generate_reset_token-instance_method" title="#generate_reset_token (instance method)">#<strong>generate_reset_token</strong>(user_name, current_user: nil) {|result| ... } </a>
</span>
<span class="summary_desc"><div class='inline'><p>Generate a Password Reset Token.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#reset_password-instance_method" title="#reset_password (instance method)">#<strong>reset_password</strong>(token, new_password, current_user: nil) {|result| ... } </a>
</span>
<span class="summary_desc"><div class='inline'><p>Reset the password for the User associated with a Password Reset Token.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#session_expired%3F-instance_method" title="#session_expired? (instance method)">#<strong>session_expired?</strong>(session_data = {}) ⇒ Boolean </a>
</span>
<span class="summary_desc"><div class='inline'><p>Determine whether the Session has Expired due to User inactivity.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#sign_in-instance_method" title="#sign_in (instance method)">#<strong>sign_in</strong>(user_in, password, current_user: nil) {|result| ... } </a>
</span>
<span class="summary_desc"><div class='inline'><p>Attempt to Authenticate a User, passing in an Entity for that User (which <strong>must</strong> contain a <code>password_hash</code> attribute), and a Clear-Text Password.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#sign_out-instance_method" title="#sign_out (instance method)">#<strong>sign_out</strong>(current_user:) {|result| ... } </a>
</span>
<span class="summary_desc"><div class='inline'><p>Sign out a previously Authenticated User.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#sign_up-instance_method" title="#sign_up (instance method)">#<strong>sign_up</strong>(attribs, current_user:) {|result| ... } </a>
</span>
<span class="summary_desc"><div class='inline'><p>Persist a new User to a Repository based on passed-in attributes, where the resulting Entity (on success) contains a <code>:password_hash</code> attribute containing the encrypted value of a <strong>random</strong> Clear-Text Password; any <code>password</code> value within <code>attribs</code> is ignored.</p>
</div></span>
</li>
<li class="public ">
<span class="summary_signature">
<a href="#update_session_expiry-instance_method" title="#update_session_expiry (instance method)">#<strong>update_session_expiry</strong>(session_data = {}) ⇒ Hash </a>
</span>
<span class="summary_desc"><div class='inline'><p>Generate a Hash containing an updated Session Expiration timestamp, which can then be used for session management.</p>
</div></span>
</li>
</ul>
<div id="instance_method_details" class="method_details_list">
<h2>Instance Method Details</h2>
<div class="method_details first">
<h3 class="signature first" id="change_password-instance_method">
#<strong>change_password</strong>(user_in, current_password, new_password) {|result| ... }
</h3><div class="docstring">
<div class="discussion">
<p class="note returns_void">This method returns an undefined value.</p><p>Change an Authenticated User's password.</p>
<p>To change an Authenticated User's password, an Entity for that User, the
current Clear-Text Password, and the new Clear-Text Password are required.
The method accepts an optional <code>repo</code> parameter to specify a Repository
instance to which the updated User Entity should be persisted; if none is
specified (i.e., if the parameter has its default value of <code>nil</code>), then the
<code>UserRepository</code> specified in the Configuration is used.</p>
<p>The method <em>requires</em> a block, to which a <code>result</code> indicating success or
failure is yielded. That block <strong>must</strong> in turn call <strong>both</strong>
<code>result.success</code> and <code>result.failure</code> to handle success and failure results,
respectively. On success, the block yielded to by <code>result.success</code> is called
and passed a <code>user:</code> parameter, which is identical to the <code>user</code> parameter
passed in to <code>#change_password</code> <em>except</em> that the <code>:password_hash</code> attribute
has been updated to reflect the changed password. The updated value for the
encrypted password will also have been saved to the Repository.</p>
<p>On failure, the <code>result.failure</code> call will yield a <code>code:</code> parameter to its
block, which indicates the cause of failure as follows:</p>
<p>If the specified password <em>did not</em> match the passed-in <code>user</code> Entity, then
the <code>code:</code> for failure will be <code>:bad_password</code>.</p>
<p>If the specified <code>user</code> was <em>other than</em> a User Entity representing a
Registered User, then the <code>code:</code> for failure will be <code>:invalid_user</code>.</p>
<p>Note that no check for the Current User is done here; this method trusts the
Controller Action Class that (possibly indirectly) invokes it to guard that
contingency properly.</p>
</div>
</div>
<div class="tags">
<div class="examples">
<p class="tag_title">Examples:</p>
<p class="example_title"><div class='inline'><p>for a Controller Action Class (refactor in real use; demo only)</p>
</div></p>
<pre class="example code"><code><span class='kw'>def</span> <span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_params'>params</span><span class='rparen'>)</span>
<span class='id identifier rubyid_user_in'>user_in</span> <span class='op'>=</span> <span class='id identifier rubyid_session'>session</span><span class='lbracket'>[</span><span class='symbol'>:current_user</span><span class='rbracket'>]</span>
<span class='id identifier rubyid_error_code'>error_code</span> <span class='op'>=</span> <span class='symbol'>:unassigned</span>
<span class='id identifier rubyid_config'>config</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="" title="CryptIdent (module)">CryptIdent</a></span></span><span class='period'>.</span><span class='id identifier rubyid_config'>config</span>
<span class='id identifier rubyid_change_password'>change_password</span><span class='lparen'>(</span><span class='id identifier rubyid_user_in'>user_in</span><span class='comma'>,</span> <span class='id identifier rubyid_params'>params</span><span class='lbracket'>[</span><span class='symbol'>:password</span><span class='rbracket'>]</span><span class='comma'>,</span>
<span class='id identifier rubyid_params'>params</span><span class='lbracket'>[</span><span class='symbol'>:new_password</span><span class='rbracket'>]</span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_result'>result</span><span class='op'>|</span>
<span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_success'>success</span> <span class='kw'>do</span> <span class='op'>|</span><span class='label'>user:</span><span class='op'>|</span>
<span class='ivar'>@user</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span>
<span class='id identifier rubyid_flash'>flash</span><span class='lbracket'>[</span><span class='id identifier rubyid_config'>config</span><span class='period'>.</span><span class='id identifier rubyid_success_key'>success_key</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>User </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_user'>user</span><span class='period'>.</span><span class='id identifier rubyid_name'>name</span><span class='embexpr_end'>}</span><span class='tstring_content'> password changed.</span><span class='tstring_end'>"</span></span>
<span class='id identifier rubyid_redirect_to'>redirect_to</span> <span class='id identifier rubyid_routes'>routes</span><span class='period'>.</span><span class='id identifier rubyid_root_path'>root_path</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_result'>result</span><span class='period'>.</span><span class='id identifier rubyid_failure'>failure</span> <span class='kw'>do</span> <span class='op'>|</span><span class='label'>code:</span><span class='op'>|</span>
<span class='id identifier rubyid_flash'>flash</span><span class='lbracket'>[</span><span class='id identifier rubyid_config'>config</span><span class='period'>.</span><span class='id identifier rubyid_error_key'>error_key</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_error_message_for'>error_message_for</span><span class='lparen'>(</span><span class='id identifier rubyid_code'>code</span><span class='rparen'>)</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='kw'>end</span>
<span class='id identifier rubyid_private'>private</span>
<span class='kw'>def</span> <span class='id identifier rubyid_error_message_for'>error_message_for</span><span class='lparen'>(</span><span class='id identifier rubyid_code'>code</span><span class='rparen'>)</span>
<span class='comment'># ...
</span><span class='kw'>end</span></code></pre>
</div>
<p class="tag_title">Parameters:</p>
<ul class="param">
<li>
<span class='name'>user_in</span>
<span class='type'>(<tt>User</tt>)</span>
—
<div class='inline'><p>The User Entity from which to get the valid Encrypted
Password and other non-Password attributes</p>
</div>
</li>
<li>
<span class='name'>current_password</span>
<span class='type'>(<tt>String</tt>)</span>
—
<div class='inline'><p>The current Clear-Text Password for the
specified User</p>
</div>
</li>
<li>
<span class='name'>new_password</span>
<span class='type'>(<tt>String</tt>)</span>
—
<div class='inline'><p>The new Clear-Text Password to encrypt and add
to the returned Entity, and persist to the Repository</p>
</div>
</li>
</ul>
<p class="tag_title">Yield Parameters:</p>
<ul class="yieldparam">
<li>
<span class='name'>result</span>
<span class='type'>(<tt>Dry::Matcher::Evaluator</tt>)</span>
—
<div class='inline'><p>Indicates whether the attempt
to create a new User succeeded or failed. Block <strong>must</strong>
call <strong>both</strong> <code>result.success</code> and <code>result.failure</code> methods,
where the block passed to <code>result.success</code> accepts a parameter
for <code>user:</code> (which is the newly-created User Entity). The
block passed to <code>result.failure</code> accepts a parameter for
<code>code:</code>, which is a Symbol reporting the reason for the
failure (as described above).</p>
</div>
</li>
</ul>
<p class="tag_title">Yield Returns:</p>
<ul class="yieldreturn">
<li>
<span class='type'>(<tt>void</tt>)</span>
—
<div class='inline'><p>Use the <code>result.success</code> and <code>result.failure</code>
method-call blocks to retrieve data from the method.</p>
</div>
</li>
</ul>
<p class="tag_title">Since:</p>
<ul class="since">
<li>
<div class='inline'><p>0.1.0</p>
</div>
</li>
</ul>
<p class="tag_title">Required Authentication Status:</p>
<ul class="authenticated">
<li>
<div class='inline'><p>Must be Authenticated.</p>
</div>
</li>
</ul>
<p class="tag_title">Session Data Interacted With:</p>
<ul class="session_data">
<li>
<div class='inline'><p>Implies that <code>:current_user</code> <strong>must</strong> be an Entity for a Registered User</p>
</div>
</li>
</ul>
<p class="tag_title">Ubiquitous Language Terms:</p>
<ul class="ubiq_lang">
<li>
<div class='inline'><ul>
<li>Authentication</li>
<li>Clear-Text Password</li>
<li>Encrypted Password</li>
<li>Entity</li>
<li>Guest User</li>
<li>Registered User</li>
<li>Repository</li>
</ul>
</div>
</li>
</ul>
</div><table class="source_code">
<tr>
<td>
<pre class="lines">
103
104
105
106
107
108</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/crypt_ident/change_password.rb', line 103</span>
<span class='kw'>def</span> <span class='id identifier rubyid_change_password'>change_password</span><span class='lparen'>(</span><span class='id identifier rubyid_user_in'>user_in</span><span class='comma'>,</span> <span class='id identifier rubyid_current_password'>current_password</span><span class='comma'>,</span> <span class='id identifier rubyid_new_password'>new_password</span><span class='rparen'>)</span>
<span class='id identifier rubyid_call_params'>call_params</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='id identifier rubyid_current_password'>current_password</span><span class='comma'>,</span> <span class='id identifier rubyid_new_password'>new_password</span><span class='rbracket'>]</span>
<span class='const'>ChangePassword</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='lparen'>(</span><span class='label'>user:</span> <span class='id identifier rubyid_user_in'>user_in</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_call_params'>call_params</span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_result'>result</span><span class='op'>|</span>
<span class='kw'>yield</span> <span class='id identifier rubyid_result'>result</span>
<span class='kw'>end</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="generate_reset_token-instance_method">
#<strong>generate_reset_token</strong>(user_name, current_user: nil) {|result| ... }
</h3><div class="docstring">
<div class="discussion">
<p class="note returns_void">This method returns an undefined value.</p><p>Generate a Password Reset Token</p>
<p>Password Reset Tokens are useful for verifying that the person requesting a
Password Reset for an existing User is sufficiently likely to be the person
who Registered that User or, if not, that no compromise or other harm is
done.</p>
<p>Typically, this is done by sending a link through email or other such medium
to the address previously associated with the User purportedly requesting
the Password Reset. <code>CryptIdent</code> <em>does not</em> automate generation or sending
of the email message. What it <em>does</em> provide is a method to generate a new
Password Reset Token to be embedded into an HTML anchor link within an email
that you construct, and then another method (<code>#reset_password</code>) to actually
change the password given a valid, correct token.</p>
<p>It also implements an expiry system, such that if the confirmation of the
Password Reset request is not completed within a configurable time, that the
token is no longer valid (and so cannot be later reused by unauthorised
persons).</p>
<p>This method <em>requires</em> a block, to which a <code>result</code> indicating success or
failure is yielded. That block <strong>must</strong> in turn call <strong>both</strong>
<code>result.success</code> and <code>result.failure</code> to handle success and failure results,
respectively. On success, the block yielded to by <code>result.success</code> is called
and passed a <code>user:</code> parameter, which is identical to the <code>user</code> parameter
passed in to <code>#generate_reset_token</code> <em>except</em> that the <code>:token</code> and
<code>:password_reset_expires_at</code> attributes have been updated to reflect the
token request. An updated record matching that <code>:user</code> Entity will also have
been saved to the Repository.</p>
<p>On failure, the <code>result.failure</code> call will yield three parameters: <code>:code</code>,
<code>:current_user</code>, and <code>:name</code>, and will be set as follows:</p>
<p>If the <code>:code</code> value is <code>:user_logged_in</code>, that indicates that the
<code>current_user</code> parameter to this method represented a Registered User. In
this event, the <code>:current_user</code> value passed in to the <code>result.failure</code> call
will be the same User Entity passed into the method, and the <code>:name</code> value
will be <code>:unassigned</code>.</p>
<p>If the <code>:code</code> value is <code>:user_not_found</code>, the named User was not found in
the Repository. The <code>:current_user</code> parameter will be the Guest User Entity,
and the <code>:name</code> parameter to the <code>result.failure</code> block will be the
<code>user_name</code> value passed into the method.</p>
</div>
</div>
<div class="tags">
<div class="examples">
<p class="tag_title">Examples:</p>
<p class="example_title"><div class='inline'><p>Demonstrating a (refactorable) Controller Action Class #call method</p>
</div></p>
<pre class="example code"><code>
def call(params)
config = CryptIdent.config
# Remember that reading an Entity stored in session data will in fact
# return a *Hash of its attribute values*. This is acceptable.
other_params = { current_user: session[:current_user] }
generate_reset_token(params[:name], other_params) do |result|
result.success do |user:|
@user = user
flash[config.success_key] = 'Request for #{user.name} sent'
end
result.failure do |code:, current_user:, name:| do
respond_to_error(code, current_user, name)
end
end
end
private
def respond_to_error(code, current_user, name)
# ...
end</code></pre>
</div>
<p class="tag_title">Parameters:</p>
<ul class="param">
<li>
<span class='name'>user_name</span>
<span class='type'>(<tt>String</tt>)</span>
—
<div class='inline'><p>The name of the User for whom a Password Reset
Token is to be generated.</p>
</div>
</li>
<li>
<span class='name'>current_user</span>
<span class='type'>(<tt>User</tt>, <tt>Hash</tt>)</span>
—
<div class='inline'><p>Entity representing the currently
Authenticated User Entity. This <strong>must</strong> be a Registered
User, either as an Entity or as a Hash of attributes.</p>
</div>
</li>
</ul>
<p class="tag_title">Yield Parameters:</p>
<ul class="yieldparam">
<li>
<span class='name'>result</span>
<span class='type'>(<tt>Dry::Matcher::Evaluator</tt>)</span>
—
<div class='inline'><p>Indicates whether the attempt
to generate a new Reset Token succeeded or failed. The lock
<strong>must</strong> call <strong>both</strong> <code>result.success</code> and <code>result.failure</code>
methods, where the block passed to <code>result.success</code> accepts a
parameter for <code>user:</code>, which is a User Entity with the
specified <code>name</code> value as well as non-<code>nil</code> values for its
<code>:token</code> and <code>:password_reset_expires_at</code> attributes. The
block passed to <code>result.failure</code> accepts parameters for
<code>code:</code>, <code>current_user:</code>, and <code>name</code> as described above.</p>
</div>
</li>
</ul>
<p class="tag_title">Yield Returns:</p>
<ul class="yieldreturn">
<li>
<span class='type'>(<tt>void</tt>)</span>
—
<div class='inline'><p>Use the <code>result.success</code> and <code>result.failure</code>
method-call blocks to retrieve data from the method.</p>
</div>
</li>
</ul>
<p class="tag_title">Since:</p>
<ul class="since">
<li>
<div class='inline'><p>0.1.0</p>
</div>
</li>
</ul>
<p class="tag_title">Required Authentication Status:</p>
<ul class="authenticated">
<li>
<div class='inline'><p>Must not be Authenticated.</p>
</div>
</li>
</ul>
<p class="tag_title">Session Data Interacted With:</p>
<ul class="session_data">
<li>
<div class='inline'><p><code>:current_user</code> <strong>must not</strong> be a Registered User.</p>
</div>
</li>
</ul>
<p class="tag_title">Ubiquitous Language Terms:</p>
<ul class="ubiq_lang">
<li>
<div class='inline'><ul>
<li>Authentication</li>
<li>Guest User</li>
<li>Password Reset Token</li>
<li>Registered User</li>
</ul>
</div>
</li>
</ul>
</div><table class="source_code">
<tr>
<td>
<pre class="lines">
113
114
115
116
117
118</pre>
</td>
<td>
<pre class="code"><span class="info file"># File 'lib/crypt_ident/generate_reset_token.rb', line 113</span>
<span class='kw'>def</span> <span class='id identifier rubyid_generate_reset_token'>generate_reset_token</span><span class='lparen'>(</span><span class='id identifier rubyid_user_name'>user_name</span><span class='comma'>,</span> <span class='label'>current_user:</span> <span class='kw'>nil</span><span class='rparen'>)</span>
<span class='id identifier rubyid_other_params'>other_params</span> <span class='op'>=</span> <span class='lbrace'>{</span> <span class='label'>current_user:</span> <span class='id identifier rubyid_current_user'>current_user</span> <span class='rbrace'>}</span>
<span class='const'>GenerateResetToken</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span><span class='lparen'>(</span><span class='id identifier rubyid_user_name'>user_name</span><span class='comma'>,</span> <span class='id identifier rubyid_other_params'>other_params</span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_result'>result</span><span class='op'>|</span>
<span class='kw'>yield</span> <span class='id identifier rubyid_result'>result</span>
<span class='kw'>end</span>
<span class='kw'>end</span></pre>
</td>
</tr>
</table>
</div>
<div class="method_details ">
<h3 class="signature " id="reset_password-instance_method">
#<strong>reset_password</strong>(token, new_password, current_user: nil) {|result| ... }
</h3><div class="docstring">
<div class="discussion">
<p class="note returns_void">This method returns an undefined value.</p><p>Reset the password for the User associated with a Password Reset Token.</p>
<p>After a Password Reset Token has been
<a href="#generate_reset_token-instance_method">generated</a> and sent to a User, that
User would then exercise the Client system and perform a Password Reset.</p>
<p>Calling <code>#reset_password</code> is different than calling <code>#change_password</code> in
one vital respect: with <code>#change_password</code>, the User involved <strong>must</strong> be
the Current User (as presumed by passing the appropriate User Entity in as
the <code>current_user:</code> parameter), whereas <code>#reset_password</code> <strong>must not</strong> be
called with <em>any</em> User other than the Guest User as the <code>current_user:</code>
parameter (and, again presumably, the Current User for the session). How can
we assure ourselves that the request is legitimate for a specific User? By
use of the Token generated by a previous call to <code>#generate_reset_token</code>,
which is used <em>in place of</em> a User Name for this request.</p>
<p>Given a valid set of parameters, and given that the updated User is
successfully persisted, the method calls the <strong>required</strong> block with a
<code>result</code> whose <code>result.success</code> matcher is yielded a <code>user:</code> parameter with
the updated User as its value.</p>
<p>NOTE: Each of the error returns documented below calls the <strong>required</strong>
block with a <code>result</code> whose <code>result.failure</code> matcher is yielded a <code>code:</code>
parameter as described, and a <code>token:</code> parameter that has the same value
as the passed-in <code>token</code> parameter.</p>
<p>If the passed-in <code>token</code> parameter matches the <code>token</code> field of a record in
the Repository <em>and</em> that Token is determined to have Expired, then the
<code>code:</code> parameter mentioned earlier will have the value <code>:expired_token</code>.</p>
<p>If the passed-in <code>token</code> parameter <em>does not</em> match the <code>token</code> field of any
record in the Repository, then the <code>code:</code> parameter will have the value
<code>:token_not_found</code>.</p>
<p>If the passed-in <code>current_user:</code> parameter is a Registered User, then the
<code>code:</code> parameter will have the value <code>:invalid_current_user</code>.</p>
<p>In no event are session values, including the Current User, changed. After a
successful Password Reset, the User must Authenticate as usual.</p>
</div>
</div>
<div class="tags">
<div class="examples">
<p class="tag_title">Examples:</p>
<pre class="example code"><code>def call(params)
reset_password(params[:token], params[:new_password],
current_user: session[:current_user]) do |result
result.success do |user:|
@user = user
message = "Password for #{user.name} successfully reset."
flash[CryptIdent.config.success_key] = message
redirect_to routes.root_path
end
result.failure do |code:, token:|
failure_key = CryptIdent.config.failure_key
flash[failure_key] = failure_message_for(code, token)
end
end
end
private
def failure_message_for(code, token)
# ...
end</code></pre>
</div>
<p class="tag_title">Parameters:</p>
<ul class="param">
<li>
<span class='name'>token</span>