Changeset 1757

Show
Ignore:
Timestamp:
08/20/08 16:19:16 (3 months ago)
Author:
dbryson
Message:

some fixes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • community_hive/trunk/community_hive_service/src/hive_app_web.erl

    r1620 r1757  
    2222     
    2323    %% Extract the Authentication Key 
    24     Key = get_authentication_key(Req), 
     24    %%Key = get_authentication_key(Req), 
    2525     
    26     case authenticated(Key) of 
    27     true -> 
     26    %% CHECK THE SIGNATURE FIRST 
     27    case authenticated(Req) of 
     28    {true,Key} -> 
    2829        case Req:get(method) of 
    2930        Method when Method =:= 'GET'; Method =:= 'HEAD' -> 
     
    3637            Req:not_found() 
    3738        end; 
    38     false -> 
     39    {false,_Key} -> 
    3940        %% NOT AUTHORIZED 
    4041        Req:respond({401, [{"Content-Type", "application/json"}], mochijson:encode(<<"Not Authorized.">>)}) 
     
    4445 
    4546%% Batch insert history URLS  
     47%% Incoming format: 
     48%% [ [{"url",1},{"status",2},{"time_at",3}],[{"url",1},{"status",2},{"time_at",3}],...] 
    4649handle_request(Req,"history",Key) -> 
    4750    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), 
    5055    ok = hive_database:save_history(Key,Decoded), 
    51     Req:ok({?JSON,mochijson:encode(ok)}); 
     56    Req:ok({?JSON,mochijson:encode({array,[ok]})}); 
    5257 
    5358%% Return an array of URLS for an organization to process 
     59%% Outgoing format: 
     60%% {array,[ "http://111","http://222",...]} 
    5461handle_request(Req,"jobs",Key) -> 
    5562    case hive_job_scheduler:get_work(Key) of 
     
    6875 
    6976%% 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). 
    7380 
    7481%% spec authenticated(Req:headers()) -> true | false 
    75 authenticated(Key) -> 
    76     %%Value = mochiweb_headers:get_value("Authorization",Headers), 
    77     hive_database:is_authorized(Key). 
     82authenticated(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         
    7895     
    79 %%get_option(Option, Options) -> 
    80 %%    {proplists:get_value(Option, Options), proplists:delete(Option, Options)}. 
     96sign_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)) )). 
    81100 
    82101%% Clean Urls coming from backend before encoding to JSON 
  • community_hive/trunk/community_hive_service/src/hive_database.erl

    r1620 r1757  
    105105%% Authenticate a user via their access key. True if authorized 
    106106handle_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}}}), 
    108108    {data,Result} = mysql:fetch(?DB_POOL, list_to_binary(Sql)), 
    109109    Reply = case mysql:get_result_rows(Result) of 
    110         [] ->  
    111             false; 
    112         _Any -> 
    113             true 
     110        [] -> false; 
     111        [Sk] -> {true,Sk} 
    114112        end, 
    115113    {reply,Reply,State}. 
     
    168166convert_json_to_sql([],SqlAcc,UrlAcc) -> 
    169167    {SqlAcc,UrlAcc}; 
    170 convert_json_to_sql([{struct,H}|T],SqlAcc,UrlAcc) -> 
     168 
     169 
     170convert_json_to_sql([ {array,[{struct,H}]} |T ],SqlAcc,UrlAcc) -> 
    171171    Url = proplists:get_value("url",H,"?"), 
    172172    Status = proplists:get_value("status",H,"?"), 
  • community_hive/trunk/community_hive_service/src/hive_job_scheduler.erl

    r1620 r1757  
    7676        %% Check URLS 
    7777        GoodUrls = list_good_urls(HistoryUrls,Bloom), 
     78        io:format("Good URLs for work: ~p~n",[GoodUrls]), 
    7879        ets:insert(Tab,{Org,GoodUrls}); 
    7980    Any -> 
     
    129130    Bloom; 
    130131load_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), 
    132138    load_bloom(T,Bloom1). 
    133139 
    134140%% Find URLS that are not in the bloom 
    135141%% @spec list_good_urls([Urls],State) -> [good urls] | [] 
    136 %%list_good_urls(_Urls,Bloom) when Bloom =:= undefined -> 
    137 %%    []; 
     142%%  
    138143list_good_urls(Urls,Bloom) -> 
    139144    verify_url(Urls,Bloom,[]). 
     
    142147    Acc; 
    143148verify_url([H|T],Bloom,Acc) -> 
    144     case bloom:is_element(H,Bloom) of 
     149    [U] = H, 
     150    case bloom:is_element(U,Bloom) of 
    145151    true -> 
    146152        verify_url(T,Bloom,Acc);