Upgrade to API 2.0

parent ffcc232f
Pipeline #37135124 passed with stage
in 4 minutes and 40 seconds
......@@ -9,3 +9,4 @@ ebin/
doc/*
!doc/overview.edoc
_rel/*
include/beacon.hrl
PROJECT = nist_beacon
PROJECT_DESCRIPTION = An application used to obtain entropy from NIST Randomness Beacon
PROJECT_VERSION = 0.1.1
PROJECT_VERSION = 0.2.0
DEPS = erlsom
dep_erlsom = git https://github.com/willemdj/erlsom.git df4526ce3c50ea27a4d37f4be3582037368f8ce7
......@@ -10,3 +10,15 @@ LOCAL_DEPS = inets ssl
SP = 4
include erlang.mk
$(PROJECT).d:: include/beacon.hrl
include/beacon.hrl: priv/beacon-2.0.xsd
$(verbose) mkdir -p include
$(gen_verbose) $(SHELL_ERL) -pa $(SHELL_PATHS) -noshell -s ssl -eval "\
application:ensure_all_started(erlsom),\
erlsom:write_xsd_hrl_file(\"priv/beacon-2.0.xsd\", \"include/beacon.hrl\"),\
halt()"
clean::
$(verbose) rm -f include/beacon.hrl
%% HRL file generated by ERLSOM
%%
%% It is possible (and in some cases necessary) to change the name of
%% the record fields.
%%
%% It is possible to add default values, but be aware that these will
%% only be used when *writing* an xml document.
-ifndef(ERLSOM_ANY_ATTRIB_TYPES).
-define(ERLSOM_ANY_ATTRIB_TYPES, true).
-type anyAttrib() :: {{string(), %% name of the attribute
string()}, %% namespace
string()}. %% value
-type anyAttribs() :: [anyAttrib()] | undefined.
-endif.
-ifndef(ERLSOM_QNAME_TYPES).
-define(ERLSOM_QNAME_TYPES, true).
%% xsd:QName values are translated to #qname{} records.
-record(qname, {uri :: string(),
localPart :: string(),
prefix :: string(),
mappedPrefix :: string()}).
-endif.
-record(chainType, {anyAttribs :: anyAttribs(),
pulse :: ['chainType/pulse'()]}).
-type chainType() :: #chainType{}.
-record('chainType/pulse', {anyAttribs :: anyAttribs(),
type :: string() | undefined,
version :: string(),
frequency :: integer(),
certificateId :: string(),
timeStamp :: string(),
localRandomValue :: string(),
external :: 'pulseType/external'() | undefined,
listValue :: ['pulseType/listValue'()],
precommitmentValue :: string() | undefined,
statusCode :: string(),
signatureValue :: string(),
outputValue :: string()}).
-type 'chainType/pulse'() :: #'chainType/pulse'{}.
-record(pulseType, {anyAttribs :: anyAttribs(),
version :: string(),
frequency :: integer(),
certificateId :: string(),
timeStamp :: string(),
localRandomValue :: string(),
external :: 'pulseType/external'() | undefined,
listValue :: ['pulseType/listValue'()],
precommitmentValue :: string() | undefined,
statusCode :: string(),
signatureValue :: string(),
outputValue :: string()}).
-type pulseType() :: #pulseType{}.
-record('pulseType/listValue', {anyAttribs :: anyAttribs(),
type :: string(),
'#text' :: string() | undefined}).
-type 'pulseType/listValue'() :: #'pulseType/listValue'{}.
-record('pulseType/external', {anyAttribs :: anyAttribs(),
sourceId :: string(),
statusCode :: string(),
value :: string()}).
-type 'pulseType/external'() :: #'pulseType/external'{}.
-record(errorType, {anyAttribs :: anyAttribs(),
code :: integer(),
description :: ['errorType/description'()]}).
-type errorType() :: #errorType{}.
-record('errorType/description', {anyAttribs :: anyAttribs(),
lang :: string(),
'#text' :: string() | undefined}).
-type 'errorType/description'() :: #'errorType/description'{}.
-record('response-chain', {anyAttribs :: anyAttribs(),
pulse :: ['chainType/pulse'()]}).
-type 'response-chain'() :: #'response-chain'{}.
-record('response-skipList', {anyAttribs :: anyAttribs(),
pulse :: ['chainType/pulse'()]}).
-type 'response-skipList'() :: #'response-skipList'{}.
-record(response, {anyAttribs :: anyAttribs(),
choice :: 'response-skipList'() | [pulseType() | 'chainType/pulse'()] | errorType() | 'response-chain'()}).
-type response() :: #response{}.
\ No newline at end of file
This diff is collapsed.
......@@ -22,8 +22,8 @@
-include_lib("include/beacon.hrl").
%% @type query() = list(string()). List of query-specific path components.
%% For example, to perform a request to <code>https://beacon.nist.gov/beacon/1.0/pulse/previous/1515699180000?type=xml</code>,
%% query needs to be <code>["pulse", "previous", "1515699180000"]</code>.
%% For example, to perform a request to <code>https://beacon.nist.gov/beacon/2.0/chain/1/pulse/1515699180000?type=xml</code>,
%% query needs to be <code>["chain", "1", "pulse", "1515699180000"]</code>.
-type query() :: list(string()).
-type result() :: {ok, erlsom:response()} | {error, term()}.
......@@ -38,7 +38,7 @@
model :: erlsom:model(),
certificates=#{} :: #{string() => #'OTPCertificate'{}}}).
-define(ENDPOINT, "https://beacon.nist.gov/beacon/1.0/").
-define(ENDPOINT, "https://beacon.nist.gov/beacon/2.0/").
%% @private
-spec start_link() -> {ok, pid()}.
......@@ -108,7 +108,7 @@ pulse(Timestamp) ->
-spec handle_pulse(timestamp(), From :: {pid(), Tag :: term()}, #state{}) -> ok | {error, term()}.
handle_pulse(Timestamp, From, State) ->
Reply =
case fetch_response(["pulse", integer_to_list(Timestamp)], State) of
case fetch_response(["chain", "1", "pulse", integer_to_list(Timestamp)], State) of
{ok, #response{choice=[Pulse=#pulseType{outputValue=OutputValue}]}} ->
case verify_pulse(Pulse, State) of
ok ->
......@@ -154,7 +154,7 @@ fetch_response(Args, #state{model=Model}) ->
-spec xsd_model() -> erlsom:model().
xsd_model() ->
XSDPath = filename:join(code:priv_dir(nist_beacon), "beacon-0.9.xsd"),
XSDPath = filename:join(code:priv_dir(nist_beacon), "beacon-2.0.xsd"),
{ok, Model} = erlsom:compile_xsd_file(XSDPath),
Model.
......@@ -193,8 +193,8 @@ fetch_certificate(Id, #state{certificates=Certs}) ->
_ ->
URL = lists:flatten([?ENDPOINT, "certificate/", Id]),
case httpc:request(URL) of
{ok, {{_, 200, _}, _, CertB64}} ->
Cert = public_key:pkix_decode_cert(base64:decode(CertB64), otp),
{ok, {{_, 200, _}, _, CertPEM}} ->
[Cert] = public_key:pem_decode(list_to_binary(CertPEM)),
ok = gen_server:cast(?MODULE, {add_cert, Id, Cert}),
Cert;
_ ->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment