-
Notifications
You must be signed in to change notification settings - Fork 18
/
xo_auth.erl
287 lines (254 loc) · 10.9 KB
/
xo_auth.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
-module(xo_auth).
-export([generate_cookied_response_json/3]).
-export([determine_username/4]).
-export([update_service_details/3,
update_service_details/4,
update_service_details/5]).
-export([extract_config_values/2]).
-export([apply_username_restrictions/1]).
-include_lib("couch/include/couch_db.hrl").
-include("xo_auth.hrl").
-define(XO_DDOC_ID, <<"_design/xo_auth">>).
-define(XREF_VIEW_NAME, <<"xrefbyid">>).
-define(ACCESS_TOKEN, <<"access_token">>).
-define(ACCESS_TOKEN_SECRET, <<"access_token_secret">>).
-define(replace(L, K, V), lists:keystore(K, 1, L, {K, V})).
%% Exported functions
generate_cookied_response_json(Name, Req, RedirectUri) ->
%% Create an auth cookie in the same way that couch_httpd_auth.erl does.
%% NOTE: This could be fragile! If couch_httpd_auth.erl changes the way it handles
%% auth cookie then this code will break. However, couch_httpd_auth.erl doesn't
%% seem to expose enough method for us to make it do all the work.
User = case couch_auth_cache:get_user_creds(?l2b(Name)) of
nil -> [];
Result -> Result
end,
UserSalt = couch_util:get_value(<<"salt">>, User, <<>>),
Secret=?l2b( case couch_config:get("couch_httpd_auth", "secret", nil) of
nil ->
NewSecret = ?b2l(couch_uuids:random()),
couch_config:set("couch_httpd_auth", "secret", NewSecret),
NewSecret;
Sec -> Sec
end ),
%% Create a json response containing some useful info and the AuthSession
%% cookie.
Cookie = couch_httpd_auth:cookie_auth_header(Req#httpd{user_ctx=#user_ctx{name=?l2b(Name)},auth={<<Secret/binary,UserSalt/binary>>,true}},[]),
couch_httpd:send_json(Req, 302, [{"Location", RedirectUri}] ++ Cookie, nil).
check_user_database(ServiceName, ID) ->
%% Check the Auth database for a user document containg this ID
%% for the supplied service name
AuthDb = open_auth_db(),
{ok, Db} = ensure_xo_views_exist(AuthDb),
try
case query_xref_view(Db, [ServiceName, ID], [ServiceName, ID, <<"{}">>]) of
[] ->
nil;
[Row] ->
Row;
[_ | _] ->
Reason = iolist_to_binary(
io_lib:format("Found multiple matching entries for ~p ID: ~p", [ServiceName, ID])),
{error, {<<"oauth_token_consumer_key_pair">>, Reason}}
end
after
couch_db:close(Db)
end.
%%
%% Determine the username from the request and the token response.
%% If there is an authenticated session user, that user is used. If the
%% user is new, the username will be determined from the username of the
%% service.
%%
determine_username(Req, Provider, ProviderID, ProviderUsername) ->
case get_username_from_request(Req) of
undefined ->
%% No other account may be associated already
case check_user_database(?l2b(Provider), ?l2b(ProviderID)) of
{Result} ->
?b2l(couch_util:get_value(<<"name">>, Result, []));
_ ->
?LOG_DEBUG("Not an existing user - creating new account", []),
{ok, _DocID, NewUsername} = create_user_skeleton(ProviderUsername),
NewUsername
end;
ExistingUsername ->
?LOG_DEBUG("Auth session found. Adding service to user: ~p", [ExistingUsername]),
%% If there is already a account registered with this username,
%% it must be this user (otherwise multiple users could register with the
%% same provider ID).
case check_user_database(?l2b(Provider), ?l2b(ProviderID)) of
{Result} ->
case ?b2l(couch_util:get_value(<<"name">>, Result, [])) of
ExistingUsername ->
ExistingUsername;
_ ->
throw(account_already_associated_with_another_user)
end;
_ ->
ExistingUsername
end
end.
create_user_skeleton(UsernamePrototype) ->
%% Create user auth doc with access token
TrimmedName = re:replace(UsernamePrototype, "[^A-Za-z0-9_-]", "", [global, {return, list}]),
?LOG_DEBUG("Trimmed name is ~p", [TrimmedName]),
Db = open_auth_db(),
try
{A1,A2,A3} = now(),
random:seed(A1, A2, A3),
Username = get_unused_name(Db, TrimmedName),
?LOG_DEBUG("Creating user skeleton for username ~p", [Username]),
DocID=?l2b("org.couchdb.user:"++Username),
Salt=couch_uuids:random(),
NewDoc = #doc{
id=DocID,
body={[
{<<"_id">>, DocID},
{<<"salt">>, Salt},
{<<"name">>, ?l2b(Username)},
{<<"roles">>, []},
{<<"type">>, <<"user">>}
]}
},
DbWithoutValidationFunc = Db#db{ validate_doc_funs=[] },
case couch_db:update_doc(DbWithoutValidationFunc, NewDoc, []) of
{ok, _} ->
?LOG_DEBUG("User doc created for ~p:~p", [Username, DocID]),
{ok, DocID, Username};
Error ->
?LOG_ERROR("Could not create user doc for ~p:~p Reason:", [Username, DocID, Error]),
throw(could_not_create_user_skeleton)
end
after
couch_db:close(Db)
end.
update_service_details(Username, ServiceName, ServiceUserID) ->
update_service_details(Username, ServiceName, ServiceUserID, [], []).
update_service_details(Username, ServiceName, ServiceUserID, AccessToken) ->
update_service_details(Username, ServiceName, ServiceUserID, AccessToken, []).
update_service_details(Username, ServiceName, ServiceUserID, AccessToken, AccessTokenSecret) ->
Db = open_auth_db(),
DocID = "org.couchdb.user:" ++ Username,
%% Update a _users record with a new access key
try
case (catch couch_db:open_doc(Db, ?l2b(DocID), [ejson_body])) of
{ok, Doc} ->
{DocBody} = Doc#doc.body,
?LOG_DEBUG("User doc before update: ~p", [DocBody]),
%% Update values that are not empty
ServiceDetails = [{<<"id">>, ?l2b(ServiceUserID)}],
ServiceDetails1 = {lists:foldl(fun({_Key, <<>>}, Acc) ->
Acc;
({Key, Value}, Acc) ->
?replace(Acc, Key, Value)
end,
ServiceDetails,
[{?ACCESS_TOKEN, ?l2b(AccessToken)},
{?ACCESS_TOKEN_SECRET, ?l2b(AccessTokenSecret)}])},
NewDocBody = ?replace(DocBody, ?l2b(ServiceName), ServiceDetails1),
?LOG_DEBUG("Updated Body: ~p", [NewDocBody]),
%% To prevent the validation functions for the db taking umbrage at our
%% behind the scenes twiddling, we blank them out.
%% NOTE: Potentially fragile. Possibly dangerous?
DbWithoutValidationFunc = Db#db{ validate_doc_funs=[] },
{ok, _} = couch_db:update_doc(DbWithoutValidationFunc, Doc#doc{body = {NewDocBody}}, []),
ok;
_ ->
?LOG_ERROR("No doc found for Doc ID ~p.", [DocID]),
throw(document_not_found_for_user)
end
catch throw:conflict ->
%% Shouldn't happen but you can never be too careful
?LOG_ERROR("Conflict error when updating user document ~p.", [DocID])
after
couch_db:close(Db)
end.
open_auth_db() ->
DbName = ?l2b(couch_config:get("couch_httpd_auth", "authentication_db")),
DbOptions = [{user_ctx, #user_ctx{roles = [<<"_admin">>]}}],
{ok, AuthDb} = couch_db:open_int(DbName, DbOptions),
AuthDb.
get_unused_name(AuthDB, Name) ->
FullID=?l2b("org.couchdb.user:"++Name),
?LOG_DEBUG("Checking for existence of ~p", [FullID]),
case (catch couch_db:open_doc_int(AuthDB, FullID, [])) of
{ok, _} ->
get_unused_name(AuthDB, lists:concat([Name, random:uniform(9)]));
_ ->
Name
end.
ensure_xo_views_exist(AuthDb) ->
case couch_db:open_doc(AuthDb, ?XO_DDOC_ID, []) of
{ok, _DDoc} ->
{ok, AuthDb};
_ ->
{ok, DDoc} = get_xo_ddoc(),
{ok, _Rev} = couch_db:update_doc(AuthDb, DDoc, []),
{ok, _AuthDb2} = couch_db:reopen(AuthDb)
end.
get_xo_ddoc() ->
Json = {[
{<<"_id">>, ?XO_DDOC_ID},
{<<"language">>, <<"javascript">>},
{<<"views">>,
{[
{?XREF_VIEW_NAME,
{[
{<<"map">>, ?XREFBYID_MAP_FUN}
]}
}
]}
}
]},
{ok, couch_doc:from_json_obj(Json)}.
query_xref_view(Db, StartKey, EndKey) ->
ViewOptions = [
{start_key, StartKey},
{end_key, EndKey}
],
Callback = fun({row, Row}, Acc) ->
{ok, [couch_util:get_value(value, Row) | Acc]};
(_, Acc) ->
{ok, Acc}
end,
{ok, Result} = couch_mrview:query_view(
Db, ?XO_DDOC_ID, ?XREF_VIEW_NAME, ViewOptions, Callback, []),
Result.
extract_config_values(Category, Keys) ->
lists:map(fun(K) ->
case couch_config:get(Category, K, undefined) of
undefined -> throw({missing_config_value,
"Cannot find key '" ++ K ++ "' in [" ++ Category ++ "] section of config"});
V -> V
end
end, Keys).
get_username_from_request(#httpd{ user_ctx=UserCtx }) ->
case UserCtx of
#user_ctx{name=Username} when Username =/= null ->
?b2l(Username);
_ ->
undefined
end.
apply_username_restrictions(UncasedUsername) ->
Username = string:to_lower(UncasedUsername),
case {couch_config:get("xo_auth", "illegal_username_prefixes"),
couch_config:get("xo_auth", "illegal_username_prepend")} of
{undefined, undefined} ->
Username;
{_, undefined} ->
throw(illegal_prefixes_specified_but_no_prepend);
{PrefixesString, Prepend} ->
Prefixes = string:tokens(PrefixesString, ","),
case lists:any(fun(Prefix) ->
string:str(Username, Prefix) =:= 1
end,
Prefixes) of
true ->
PrependedUsername = Prepend ++ Username,
?LOG_DEBUG("Illegal username ~p: prepending. Result: ~p", [Username, PrependedUsername]),
PrependedUsername;
false ->
Username
end
end.