mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-19 05:07:56 +00:00
Add HTTP GET /restful/keyring/add
This commit is contained in:
parent
c2a47c7960
commit
586c6b3060
@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|||||||
#define alloca_keyring_token(bid, offset) keyring_ token_to_str(alloca(keyring_TOKEN_STRLEN + 1), (bid), (offset))
|
#define alloca_keyring_token(bid, offset) keyring_ token_to_str(alloca(keyring_TOKEN_STRLEN + 1), (bid), (offset))
|
||||||
|
|
||||||
static HTTP_HANDLER restful_keyring_identitylist_json;
|
static HTTP_HANDLER restful_keyring_identitylist_json;
|
||||||
|
static HTTP_HANDLER restful_keyring_add;
|
||||||
|
|
||||||
int restful_keyring_(httpd_request *r, const char *remainder)
|
int restful_keyring_(httpd_request *r, const char *remainder)
|
||||||
{
|
{
|
||||||
@ -40,13 +41,16 @@ int restful_keyring_(httpd_request *r, const char *remainder)
|
|||||||
const char *verb = HTTP_VERB_GET;
|
const char *verb = HTTP_VERB_GET;
|
||||||
http_size_t content_length = CONTENT_LENGTH_UNKNOWN;
|
http_size_t content_length = CONTENT_LENGTH_UNKNOWN;
|
||||||
HTTP_HANDLER *handler = NULL;
|
HTTP_HANDLER *handler = NULL;
|
||||||
|
|
||||||
if (strcmp(remainder, "identities.json") == 0) {
|
if (strcmp(remainder, "identities.json") == 0) {
|
||||||
handler = restful_keyring_identitylist_json;
|
handler = restful_keyring_identitylist_json;
|
||||||
verb = HTTP_VERB_GET;
|
verb = HTTP_VERB_GET;
|
||||||
remainder = "";
|
remainder = "";
|
||||||
}
|
}
|
||||||
|
else if (strcmp(remainder, "add") == 0) {
|
||||||
|
handler = restful_keyring_add;
|
||||||
|
verb = HTTP_VERB_GET;
|
||||||
|
remainder = "";
|
||||||
|
}
|
||||||
if (handler == NULL)
|
if (handler == NULL)
|
||||||
return 404;
|
return 404;
|
||||||
if ( content_length != CONTENT_LENGTH_UNKNOWN
|
if ( content_length != CONTENT_LENGTH_UNKNOWN
|
||||||
@ -60,16 +64,20 @@ int restful_keyring_(httpd_request *r, const char *remainder)
|
|||||||
return handler(r, remainder);
|
return handler(r, remainder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int http_request_keyring_response(struct httpd_request *r, uint16_t result, const char *message)
|
||||||
|
{
|
||||||
|
http_request_simple_response(&r->http, result, message);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
static HTTP_CONTENT_GENERATOR restful_keyring_identitylist_json_content;
|
static HTTP_CONTENT_GENERATOR restful_keyring_identitylist_json_content;
|
||||||
|
|
||||||
static int restful_keyring_identitylist_json(httpd_request *r, const char *remainder)
|
static int restful_keyring_identitylist_json(httpd_request *r, const char *remainder)
|
||||||
{
|
{
|
||||||
if (*remainder)
|
if (*remainder)
|
||||||
return 404;
|
return 404;
|
||||||
|
|
||||||
r->u.sidlist.phase = LIST_HEADER;
|
r->u.sidlist.phase = LIST_HEADER;
|
||||||
keyring_iterator_start(keyring, &r->u.sidlist.it);
|
keyring_iterator_start(keyring, &r->u.sidlist.it);
|
||||||
|
|
||||||
http_request_response_generated(&r->http, 200, CONTENT_TYPE_JSON, restful_keyring_identitylist_json_content);
|
http_request_response_generated(&r->http, 200, CONTENT_TYPE_JSON, restful_keyring_identitylist_json_content);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -147,3 +155,25 @@ static int restful_keyring_identitylist_json_content_chunk(struct http_request *
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int restful_keyring_add(httpd_request *r, const char *remainder)
|
||||||
|
{
|
||||||
|
if (*remainder)
|
||||||
|
return 404;
|
||||||
|
const keyring_identity *id = keyring_create_identity(keyring, "");
|
||||||
|
if (id == NULL)
|
||||||
|
return http_request_keyring_response(r, 501, "Could not create identity");
|
||||||
|
const sid_t *sidp = NULL;
|
||||||
|
const char *did = "";
|
||||||
|
const char *name = "";
|
||||||
|
keyring_identity_extract(id, &sidp, &did, &name);
|
||||||
|
if (!sidp)
|
||||||
|
return http_request_keyring_response(r, 501, "New identity has no SID");
|
||||||
|
if (keyring_commit(keyring) == -1)
|
||||||
|
return http_request_keyring_response(r, 501, "Could not store new identity");
|
||||||
|
strbuf s = strbuf_alloca(200);
|
||||||
|
strbuf_puts(s, "{\n \"sid\":");
|
||||||
|
strbuf_json_hex(s, sidp->binary, sizeof sidp->binary);
|
||||||
|
strbuf_puts(s, "\n}");
|
||||||
|
http_request_response_static(&r->http, 200, CONTENT_TYPE_JSON, strbuf_str(s), strbuf_len(s));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
@ -759,6 +759,14 @@ create_identities() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Assertion function:
|
||||||
|
# - asserts that the list contains N identities that have the correct format
|
||||||
|
assert_keyring_list() {
|
||||||
|
unpack_stdout_list __X
|
||||||
|
assert --stdout --stderr [ $__XNROWS -eq $1 ]
|
||||||
|
assertStdoutGrep --stderr --matches=$1 "^$rexp_sid:\($rexp_did\)\?:.*\$"
|
||||||
|
}
|
||||||
|
|
||||||
# Utility function, to be overridden as needed:
|
# Utility function, to be overridden as needed:
|
||||||
# - set up the configuration immediately prior to starting a servald server process
|
# - set up the configuration immediately prior to starting a servald server process
|
||||||
# - called by start_servald_instances
|
# - called by start_servald_instances
|
||||||
|
@ -46,13 +46,6 @@ setup_instances() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_keyring_list() {
|
|
||||||
unpack_stdout_list X
|
|
||||||
assert --stdout --stderr [ $XNROWS -eq $1 ]
|
|
||||||
assertStdoutGrep --stderr --matches=$1 "^[0-9a-fA-F]\{64\}:[0-9*#+]*:.*\$"
|
|
||||||
tfw_cat --stdout
|
|
||||||
}
|
|
||||||
|
|
||||||
doc_KeyringCreate="Create keyring destroys existing keys"
|
doc_KeyringCreate="Create keyring destroys existing keys"
|
||||||
test_KeyringCreate() {
|
test_KeyringCreate() {
|
||||||
executeOk_servald keyring add ''
|
executeOk_servald keyring add ''
|
||||||
|
@ -29,6 +29,7 @@ setup() {
|
|||||||
setup_json
|
setup_json
|
||||||
setup_servald
|
setup_servald
|
||||||
set_instance +A
|
set_instance +A
|
||||||
|
set_keyring_config
|
||||||
executeOk_servald config \
|
executeOk_servald config \
|
||||||
set api.restful.users.harry.password potter \
|
set api.restful.users.harry.password potter \
|
||||||
set api.restful.users.ron.password weasley \
|
set api.restful.users.ron.password weasley \
|
||||||
@ -60,7 +61,6 @@ set_extra_config() {
|
|||||||
|
|
||||||
set_keyring_config() {
|
set_keyring_config() {
|
||||||
executeOk_servald config \
|
executeOk_servald config \
|
||||||
set debug.http_server on \
|
|
||||||
set debug.httpd on \
|
set debug.httpd on \
|
||||||
set debug.rhizome_manifest on \
|
set debug.rhizome_manifest on \
|
||||||
set debug.rhizome_store on \
|
set debug.rhizome_store on \
|
||||||
@ -70,27 +70,46 @@ set_keyring_config() {
|
|||||||
set log.console.level debug
|
set log.console.level debug
|
||||||
}
|
}
|
||||||
|
|
||||||
doc_keyringListIdentities="HTTP RESTful list SIDs as JSON"
|
doc_keyringList="HTTP RESTful list keyring identities as JSON"
|
||||||
setup_keyringListIdentities() {
|
setup_keyringList() {
|
||||||
IDENTITY_COUNT=10
|
IDENTITY_COUNT=10
|
||||||
setup
|
setup
|
||||||
}
|
}
|
||||||
|
test_keyringList() {
|
||||||
test_keyringListIdentities() {
|
|
||||||
executeOk curl \
|
executeOk curl \
|
||||||
--silent --fail --show-error \
|
--silent --fail --show-error \
|
||||||
--output identitylist1.json \
|
--output list.json \
|
||||||
--dump-header http.headers \
|
--dump-header http.headers \
|
||||||
--basic --user harry:potter \
|
--basic --user harry:potter \
|
||||||
"http://$addr_localhost:$PORTA/restful/keyring/identities.json"
|
"http://$addr_localhost:$PORTA/restful/keyring/identities.json"
|
||||||
tfw_cat http.headers identitylist1.json
|
tfw_cat http.headers list.json
|
||||||
tfw_preserve identitylist1.json
|
tfw_preserve list.json
|
||||||
|
assert [ "$(jq '.rows | length' list.json)" = $IDENTITY_COUNT ]
|
||||||
assert [ "$(jq '.rows | length' identitylist1.json)" = $IDENTITY_COUNT ]
|
assert [ "$(jq -r '.rows[0][0]' list.json)" = $SIDA1 ]
|
||||||
|
assert [ "$(jq -r '.rows[4][0]' list.json)" = $SIDA5 ]
|
||||||
|
assert [ "$(jq -r '.rows[9][0]' list.json)" = $SIDA10 ]
|
||||||
|
}
|
||||||
|
|
||||||
assert [ "$(jq -r '.rows[0][0]' identitylist1.json)" = $SIDA1 ]
|
doc_keyringAdd="HTTP RESTful add keyring identity with empty PIN"
|
||||||
assert [ "$(jq -r '.rows[4][0]' identitylist1.json)" = $SIDA5 ]
|
setup_keyringAdd() {
|
||||||
assert [ "$(jq -r '.rows[9][0]' identitylist1.json)" = $SIDA10 ]
|
IDENTITY_COUNT=2
|
||||||
|
setup
|
||||||
|
}
|
||||||
|
test_keyringAdd() {
|
||||||
|
executeOk curl \
|
||||||
|
--silent --show-error --write-out '%{http_code}' \
|
||||||
|
--output add.json \
|
||||||
|
--dump-header http.headers \
|
||||||
|
--basic --user harry:potter \
|
||||||
|
"http://$addr_localhost:$PORTA/restful/keyring/add"
|
||||||
|
tfw_cat http.headers add.json
|
||||||
|
tfw_preserve add.json
|
||||||
|
assertStdoutIs '200'
|
||||||
|
SID="$(jq -r '.sid' add.json)"
|
||||||
|
assert matches_rexp "^${rexp_sid}$" "$SID"
|
||||||
|
executeOk_servald keyring list
|
||||||
|
assert_keyring_list 3
|
||||||
|
assertStdoutGrep --stderr --matches=1 "^$SID::\$"
|
||||||
}
|
}
|
||||||
|
|
||||||
runTests "$@"
|
runTests "$@"
|
||||||
|
Loading…
Reference in New Issue
Block a user