summaryrefslogtreecommitdiff
path: root/src/signing.erl
blob: 86ccb88f7ecd51bc1440054e3965c7b8739dbc33 (plain)
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
%%% Copyright (c) 2014-2015, NORDUnet A/S.
%%% See LICENSE for licensing information.

%%% @doc Signing node API

-module(signing).
%% API (URL)
-export([request/4]).

-define(APPURL_PLOP_SIGNING, "plop/v1/signing").

request(post, ?APPURL_PLOP_SIGNING, "sct", Input) ->
    case (catch mochijson2:decode(Input)) of
        {error, E} ->
            html("sendentry: bad input:", E);
        {struct, PropList} ->
            Data = base64:decode(proplists:get_value(<<"data">>, PropList)),

            Result = sign:sign_sct(Data),
            success({[{result, base64:encode(Result)}]})
    end;
request(post, ?APPURL_PLOP_SIGNING, "sth", Input) ->
    case (catch mochijson2:decode(Input)) of
        {error, E} ->
            html("sendentry: bad input:", E);
        {struct, PropList} ->
            Data = base64:decode(proplists:get_value(<<"data">>, PropList)),

            Result = sign:sign_sth(Data),
            success({[{result, base64:encode(Result)}]})
    end.

%% Private functions.
html(Text, Input) ->
    {400, [{"Content-Type", "text/html"}],
     io_lib:format(
       "<html><body><p>~n" ++
           "~s~n" ++
           "~p~n" ++
           "</body></html>~n", [Text, Input])}.

success(Data) ->
    lager:debug("returning ~p", [Data]),
    {200, [{"Content-Type", "text/json"}], mochijson2:encode(Data)}.