Changeset 1757
- Timestamp:
- 08/20/08 16:19:16 (3 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
community_hive/trunk/community_hive_service/src/hive_app_web.erl
r1620 r1757 22 22 23 23 %% Extract the Authentication Key 24 Key = get_authentication_key(Req),24 %%Key = get_authentication_key(Req), 25 25 26 case authenticated(Key) of 27 true -> 26 %% CHECK THE SIGNATURE FIRST 27 case authenticated(Req) of 28 {true,Key} -> 28 29 case Req:get(method) of 29 30 Method when Method =:= 'GET'; Method =:= 'HEAD' -> … … 36 37 Req:not_found() 37 38 end; 38 false->39 {false,_Key} -> 39 40 %% NOT AUTHORIZED 40 41 Req:respond({401, [{"Content-Type", "application/json"}], mochijson:encode(<<"Not Authorized.">>)}) … … 44 45 45 46 %% Batch insert history URLS 47 %% Incoming format: 48 %% [ [{"url",1},{"status",2},{"time_at",3}],[{"url",1},{"status",2},{"time_at",3}],...] 46 49 handle_request(Req,"history",Key) -> 47 50 D = Req:recv_body(), 48 Deflated = zlib:uncompress(D), 49 {array,Decoded} = mochijson:decode(Deflated), 51 %%Deflated = zlib:uncompress(D), 52 io:format("HIstory: ~p~n",[D]), 53 {array,Decoded} = mochijson:decode(D), 54 %%Decoded = binary_to_term(D), 50 55 ok = hive_database:save_history(Key,Decoded), 51 Req:ok({?JSON,mochijson:encode( ok)});56 Req:ok({?JSON,mochijson:encode({array,[ok]})}); 52 57 53 58 %% Return an array of URLS for an organization to process 59 %% Outgoing format: 60 %% {array,[ "http://111","http://222",...]} 54 61 handle_request(Req,"jobs",Key) -> 55 62 case hive_job_scheduler:get_work(Key) of … … 68 75 69 76 %% Extract Authentication Key 70 get_authentication_key(Request) ->71 Headers = Request:get(headers),72 mochiweb_headers:get_value("Authorization",Headers).77 %%get_authentication_key(Request) -> 78 %% Headers = Request:get(headers), 79 %% mochiweb_headers:get_value("Authorization",Headers). 73 80 74 81 %% spec authenticated(Req:headers()) -> true | false 75 authenticated(Key) -> 76 %%Value = mochiweb_headers:get_value("Authorization",Headers), 77 hive_database:is_authorized(Key). 82 authenticated(Req) -> 83 Headers = Req:get(headers), 84 Method = string:to_upper(atom_to_list(Req:get(method))), 85 Path = Req:get(path), 86 Date = mochiweb_headers:get_value("Date",Headers), 87 AuthInfo = mochiweb_headers:get_value("Authorization",Headers), 88 [_Hive,Key,InSignature] = string:tokens(AuthInfo,":"), 89 case hive_database:is_authorized(Key) of 90 {true,Sk} -> 91 {InSignature == sign_request(Sk,Method,Path,Date), Key}; 92 false -> {false,Key} 93 end. 94 78 95 79 %%get_option(Option, Options) -> 80 %% {proplists:get_value(Option, Options), proplists:delete(Option, Options)}. 96 sign_request(SecretKey,Method,Path,Date) -> 97 Data1 = [Method,Path,Date,"application/json"], 98 Data2 = string:join(Data1,"\n"), 99 binary_to_list(base64:encode( crypto:sha_mac(SecretKey,lists:flatten(Data2)) )). 81 100 82 101 %% Clean Urls coming from backend before encoding to JSON community_hive/trunk/community_hive_service/src/hive_database.erl
r1620 r1757 105 105 %% Authenticate a user via their access key. True if authorized 106 106 handle_call({authenticate,Key},_From,State) -> 107 Sql = erlsql:sql({select, access_key,{from,users},{where, {access_key,'=',Key}}}),107 Sql = erlsql:sql({select,secret_key,{from,users},{where, {access_key,'=',Key}}}), 108 108 {data,Result} = mysql:fetch(?DB_POOL, list_to_binary(Sql)), 109 109 Reply = case mysql:get_result_rows(Result) of 110 [] -> 111 false; 112 _Any -> 113 true 110 [] -> false; 111 [Sk] -> {true,Sk} 114 112 end, 115 113 {reply,Reply,State}. … … 168 166 convert_json_to_sql([],SqlAcc,UrlAcc) -> 169 167 {SqlAcc,UrlAcc}; 170 convert_json_to_sql([{struct,H}|T],SqlAcc,UrlAcc) -> 168 169 170 convert_json_to_sql([ {array,[{struct,H}]} |T ],SqlAcc,UrlAcc) -> 171 171 Url = proplists:get_value("url",H,"?"), 172 172 Status = proplists:get_value("status",H,"?"), community_hive/trunk/community_hive_service/src/hive_job_scheduler.erl
r1620 r1757 76 76 %% Check URLS 77 77 GoodUrls = list_good_urls(HistoryUrls,Bloom), 78 io:format("Good URLs for work: ~p~n",[GoodUrls]), 78 79 ets:insert(Tab,{Org,GoodUrls}); 79 80 Any -> … … 129 130 Bloom; 130 131 load_bloom([H|T],Bloom) -> 131 Bloom1 = bloom:add_element(H,Bloom), 132 %% Move outer array on incoming URL and 133 %% convert to binary so they compare properly 134 %% with the URLS coming from the History DB 135 [U] = H, 136 U1 = list_to_binary(U), 137 Bloom1 = bloom:add_element(U1,Bloom), 132 138 load_bloom(T,Bloom1). 133 139 134 140 %% Find URLS that are not in the bloom 135 141 %% @spec list_good_urls([Urls],State) -> [good urls] | [] 136 %%list_good_urls(_Urls,Bloom) when Bloom =:= undefined -> 137 %% []; 142 %% 138 143 list_good_urls(Urls,Bloom) -> 139 144 verify_url(Urls,Bloom,[]). … … 142 147 Acc; 143 148 verify_url([H|T],Bloom,Acc) -> 144 case bloom:is_element(H,Bloom) of 149 [U] = H, 150 case bloom:is_element(U,Bloom) of 145 151 true -> 146 152 verify_url(T,Bloom,Acc);
