]> gitweb.factorcode.org Git - factor.git/blob - basis/furnace/auth/providers/couchdb/couchdb.factor
assocs.extras: Move some often-used words to core
[factor.git] / basis / furnace / auth / providers / couchdb / couchdb.factor
1 USING: accessors assocs base64 byte-arrays
2 combinators.short-circuit continuations couchdb
3 furnace.auth.providers json kernel make mirrors namespaces
4 sequences strings urls urls.encoding ;
5 IN: furnace.auth.providers.couchdb
6
7 ! !!! Implement the authentication protocol for CouchDB.
8 ! !!!
9 ! !!! 'user' tuples are copied verbatim into the DB as objects.
10 ! !!! Special 'reservation' records are inserted into the DB to
11 ! !!! reserve usernames and email addresses. These reservation records
12 ! !!! all have ids with the prefix given to couchdb-auth-provider.
13 ! !!! A reservation in the email domain for the email address "foo@bar.com"
14 ! !!! would have id "PREFIXemail!foo%40bar.com". Both the domain name
15 ! !!! and the value are url-encoded, to ensure that the use of '!' as
16 ! !!! a separator guarantees a unique ID for any given (domain,value)
17 ! !!! pairing.
18 ! !!!
19 ! !!! It would be nice to use CouchDB attachments to avoid junking the
20 ! !!! global namespace like this. However, attachments in CouchDB
21 ! !!! inherit their revision ids from their parent document, which would
22 ! !!! make various operations on users unnecessairly non-independent
23 ! !!! of each other.
24 ! !!!
25 ! !!! On the basic technique used here, see:
26 ! !!!
27 ! !!! http://kfalck.net/2009/06/29/enforcing-unique-usernames-on-couchdb
28 ! !!!
29
30 ! Many of the words below assume that this symbol is bound to an
31 ! appropriate instance.
32 TUPLE: couchdb-auth-provider
33     base-url
34     { username-view string }
35     { prefix string initial: "user_reservation_" }
36     { field-map assoc initial: { } } ;
37
38 <PRIVATE
39
40 ! >json does weird things for mirrors, so we copy the mirror into
41 ! a real hashtable before serializing it.
42 : hash-mirror ( obj -- hash )
43     make-mirror H{ } assoc-like ;
44
45 : is-couchdb-conflict-error? ( error -- ? )
46     { [ couchdb-error? ] [ data>> "error" of "conflict" = ] } 1&& ;
47 : is-couchdb-not-found-error? ( error -- ? )
48     { [ couchdb-error? ] [ data>> "error" of "not_found" = ] } 1&& ;
49
50 : get-url ( url -- url' )
51     couchdb-auth-provider get
52     base-url>> >url swap >url derive-url ;
53
54 : reservation-id ( value name -- id )
55     couchdb-auth-provider get
56     prefix>> [ % url-encode-full % "!" % url-encode-full % ] "" make ;
57
58 : (reserve) ( value name -- id/f )
59     '[
60         _ _ reservation-id get-url
61         H{ } clone swap couch-put
62     ] [ is-couchdb-conflict-error? ] ignore-error/f ;
63
64 ! Don't reserve false values (e.g. if the email field is f, don't reserve f,
65 ! or the first user who registers without an email address will block all
66 ! others who wish to do so).
67 : reserve ( value name -- id/f )
68     over [ (reserve) ] [ 2drop t ] if ;
69
70 : unreserve ( couch-rval -- )
71     [ "id" of get-url ]
72     [ "rev" of "rev" set-query-param ]
73     bi
74     couch-delete drop ;
75
76 : unreserve-from-id ( id -- )
77     '[
78         _ get-url dup couch-get
79         "_rev" of "rev" set-query-param
80         couch-delete drop
81     ] [ is-couchdb-not-found-error? ] ignore-error ;
82
83 :: (reserve-multiple) ( hash keys made -- ? )
84     keys empty? [ t ] [
85         keys first hash at keys first reserve [
86             made push
87             hash keys rest-slice made (reserve-multiple)
88         ] [
89             ! Delete reservations that were already successfully made.
90             made [ unreserve ] each
91             f
92         ] if*
93     ] if ;
94
95 ! Try to reserve all of the given name/value pairs; if not all reservations
96 ! can be made, delete those that were made.
97 : reserve-multiple ( hash -- ? )
98     dup keys V{ } clone (reserve-multiple) ;
99
100 : change-at* ( key assoc quot -- assoc )
101     over [ change-at ] dip ; inline
102
103 ! Should be given a view URL.
104 : url>user ( couchdb-url -- user/f )
105     couch-get
106     "rows" of dup empty? [ drop f ] [ first "value" of ] if ;
107
108 : (get-user) ( username -- user/f )
109     couchdb-auth-provider get
110     username-view>> get-url
111     swap >json "key" set-query-param
112     url>user ;
113
114 : strip-hash ( hash1 -- hash2 )
115     [ first CHAR: _ = ] reject-keys ;
116
117 : at-or-k ( key hash -- newkey ) ?at drop ;
118 : value-at-or-k ( key hash -- newkey ) ?value-at drop ;
119
120 : map-fields-forward ( assoc field-map -- assoc )
121     [ swapd at-or-k swap ] curry assoc-map ;
122
123 : map-fields-backward ( assoc field-map -- assoc )
124     [ swapd value-at-or-k swap ] curry assoc-map ;
125
126 : user-hash>user ( hash -- user )
127     couchdb-auth-provider get field-map>> map-fields-backward
128     [ "password" swap [ base64> >byte-array ] change-at ]
129     [
130         strip-hash
131         user new dup [ make-mirror swap assoc-union! drop ] dip
132         f >>changed?
133     ]
134     bi ;
135
136 : user>user-hash ( user -- hash )
137     hash-mirror
138     [ [ "password" ] dip [ >base64 >string ] change-at ] keep
139     couchdb-auth-provider get field-map>> map-fields-forward ;
140
141 ! Used when the user is guaranteed to exist if the logic of the Factor
142 ! code is correct (e.g. when update-user is called).
143 ! In the unlikely event that the user does not exist, an error is thrown.
144 : (get-user)/throw-on-no-user ( username -- user/f )
145     (get-user) [ ] [ "User not found" throw ] if* ;
146
147 : (new-user) ( user -- user/f )
148     dup
149     [
150         [ username>> "username" ,, ]
151         [ email>> "email" ,, ]
152         bi
153     ] H{ } make
154     reserve-multiple
155     [
156         user>user-hash
157         "" get-url
158         couch-post
159     ] [
160         drop f
161     ] if ;
162
163 : unify-users ( old new -- new )
164     swap
165     [ "_rev" of "_rev" rot set-at ]
166     [ "_id" of "_id" rot set-at ]
167     [ swap assoc-union ]
168     2tri ;
169
170 ! If the user has changed username or email address,
171 ! we should let other registrants use the old ones,
172 ! and make sure that the new ones are reserved.
173 ! (This word is called by the 'update-user' method.)
174 : check-update ( old new -- ? )
175     [
176         2dup [ "email" of ] same? not [
177             [ "email" of ] bi@
178             [ drop "email" reservation-id unreserve-from-id ]
179             [ nip "email" reserve ]
180             2bi
181         ] [ 2drop t ] if
182     ] [
183         2dup [ "username" of ] same? not [
184             [ "username" of ] bi@
185             [ drop "username" reservation-id unreserve-from-id ]
186             [ nip "username" reserve ]
187             2bi
188         ] [ 2drop t ] if
189     ] 2bi and ;
190
191 PRIVATE>
192
193 : <couchdb-auth-provider> ( base-url username-view -- couchdb-auth-provider )
194     couchdb-auth-provider new swap >>username-view swap >>base-url ;
195
196 M: couchdb-auth-provider get-user
197     couchdb-auth-provider [
198         (get-user) [ user-hash>user ] [ f ] if*
199     ] with-variable ;
200
201 M: couchdb-auth-provider new-user
202     couchdb-auth-provider [
203         dup (new-user) [
204             username>> couchdb-auth-provider get get-user
205         ] [ drop f ] if
206     ] with-variable ;
207
208 M: couchdb-auth-provider update-user
209     couchdb-auth-provider [
210         [ username>> (get-user)/throw-on-no-user dup ]
211         [ drop "_id" of get-url ]
212         [ user>user-hash swapd
213           2dup check-update drop
214           unify-users swap couch-put drop
215         ]
216         tri
217     ] with-variable ;