]> gitweb.factorcode.org Git - factor.git/blob - basis/furnace/auth/features/recover-password/recover-password.factor
aeaf9e9471a4d1667d4799c63bde32232a2df352
[factor.git] / basis / furnace / auth / features / recover-password / recover-password.factor
1 ! Copyright (c) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces make accessors kernel assocs arrays io.sockets
4 threads fry urls smtp validators html.forms present http
5 http.server.responses http.server.redirection
6 http.server.dispatchers furnace.actions furnace.auth
7 furnace.auth.providers furnace.redirection furnace.utilities ;
8 IN: furnace.auth.features.recover-password
9
10 SYMBOL: lost-password-from
11
12 : current-host ( -- string )
13     url get host>> host-name or ;
14
15 : new-password-url ( user -- url )
16     URL" recover-3" clone
17         swap
18         [ username>> "username" set-query-param ]
19         [ ticket>> "ticket" set-query-param ]
20         bi
21     adjust-url ;
22
23 : password-email ( user -- email )
24     <email>
25         [ "[ " % current-host % " ] password recovery" % ] "" make >>subject
26         lost-password-from get >>from
27         over email>> 1array >>to
28         [
29             "This e-mail was sent by the application server on " % current-host % "\n" %
30             "because somebody, maybe you, clicked on a “recover password” link in the\n" %
31             "login form, and requested a new password for the user named ``" %
32             over username>> % "''.\n" %
33             "\n" %
34             "If you believe that this request was legitimate, you may click the below link in\n" %
35             "your browser to set a new password for your account:\n" %
36             "\n" %
37             swap new-password-url present %
38             "\n\n" %
39             "Love,\n" %
40             "\n" %
41             "  FactorBot\n" %
42         ] "" make >>body ;
43
44 : send-password-email ( user -- )
45     '[ _ password-email send-email ]
46     "E-mail send thread" spawn drop ;
47
48 : <recover-action-1> ( -- action )
49     <page-action>
50         { realm "features/recover-password/recover-1" } >>template
51
52         [
53             {
54                 { "username" [ v-username ] }
55                 { "email" [ v-email ] }
56                 { "captcha" [ v-captcha ] }
57             } validate-params
58         ] >>validate
59
60         [
61             "email" value "username" value
62             users issue-ticket [
63                 send-password-email
64             ] when*
65
66             URL" $realm/recover-2" <redirect>
67         ] >>submit ;
68
69 : <recover-action-2> ( -- action )
70     <page-action>
71         { realm "features/recover-password/recover-2" } >>template ;
72
73 : <recover-action-3> ( -- action )
74     <page-action>
75         [
76             {
77                 { "username" [ v-username ] }
78                 { "ticket" [ v-required ] }
79             } validate-params
80         ] >>init
81
82         { realm "features/recover-password/recover-3" } >>template
83
84         [
85             {
86                 { "username" [ v-username ] }
87                 { "ticket" [ v-required ] }
88                 { "new-password" [ v-password ] }
89                 { "verify-password" [ v-password ] }
90             } validate-params
91
92             same-password-twice
93         ] >>validate
94
95         [
96             "ticket" value
97             "username" value
98             users claim-ticket [
99                 "new-password" value >>encoded-password
100                 users update-user
101
102                 URL" $realm/recover-4" <redirect>
103             ] [
104                 <403>
105             ] if*
106         ] >>submit ;
107
108 : <recover-action-4> ( -- action )
109     <page-action>
110         { realm "features/recover-password/recover-4" } >>template ;
111
112 : allow-password-recovery ( realm -- realm )
113     <recover-action-1> <auth-boilerplate>
114         "recover-password" add-responder
115     <recover-action-2> <auth-boilerplate>
116         "recover-2" add-responder
117     <recover-action-3> <auth-boilerplate>
118         "recover-3" add-responder
119     <recover-action-4> <auth-boilerplate>
120         "recover-4" add-responder ;
121
122 : allow-password-recovery? ( -- ? )
123     realm get responders>> "recover-password" swap key? ;