t_peertube.ml 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. open Seppo_lib
  2. (*
  3. Peertube signature tests:
  4. https://github.com/Chocobozzz/PeerTube/blob/develop/server/tests/api/activitypub/helpers.ts
  5. *)
  6. (*
  7. Keys from https://github.com/Chocobozzz/PeerTube/blob/develop/server/tests/fixtures/ap-json/peertube/keys.json
  8. fixture from https://github.com/Chocobozzz/PeerTube/blob/develop/server/tests/fixtures/ap-json/mastodon/http-signature.json
  9. Inbox https://github.com/Chocobozzz/PeerTube/blob/develop/server/tests/api/activitypub/helpers.ts#L151
  10. *)
  11. let test_signature () =
  12. Logr.info (fun m -> m "peertube.test.test_signature");
  13. let
  14. pk =
  15. Lwt.bind
  16. (As2.PubKeyPem.pk_from_pem ("data/peertube.priv.pem", "data/peertube.pub.pem"))
  17. (fun (_pub, pk) -> Lwt.return pk)
  18. |> Lwt_main.run
  19. and
  20. j = As2.json_from_file "data/peertube.json"
  21. |> Result.get_ok
  22. in
  23. let js k = Ezjsonm.find j [ "headers"; k ]
  24. |> Ezjsonm.get_string
  25. and _rt = ""
  26. and ho = "localhost"
  27. and da = "Mon, 22 Oct 2018 13:34:22 GMT"
  28. and di = "SHA-256=FEr5j2WSSfdEMcG3NTOXuGU0lUchfTJx4+BtUlWOwDk="
  29. and co = "application/activity+json"
  30. and si' =
  31. "oLKbgxdFXdXsHJ3x/UsG9Svu7oa8Dyqiy6Jif4wqNuhAqRVMRaG18f+dd2OcfFX3XRGF8p8flZkU6vvoEQBauTwGRGcgXAJuKC1zYIWGk+PeiW8lNUnE4qGapWcTiFnIo7FKauNdsgqg/tvgs1pQIdHkDDjZMI64twP7sTN/4vG1PCq+kyqi/DM+ORLi/W7vFuLVHt2Iz7ikfw/R3/mMtS4FwLops+tVYBQ2iQ9DVRhTwLKVbeL/LLVB/tdGzNZ4F4nImBAQQ9I7WpPM6J/k+cBmoEbrUKs8ptx9gbX3OSsl5wlvPVMNzU9F9yb2MrB/Y/J4qssKz+LbiaktKGj7OQ=="
  32. in
  33. let si =
  34. "keyId=\"http://localhost:3000/users/ronan2#main-key\",\
  35. algorithm=\"rsa-sha256\",\
  36. headers=\"(request-target) host date digest content-type\",\
  37. signature=\"" ^ si' ^ "\""
  38. and da',_,_ = Ptime.of_rfc3339 "2018-10-22T13:34:22+00:00" |> Result.get_ok in
  39. js "host" |> Assrt.equals_string __LOC__ ho;
  40. js "date" |> Assrt.equals_string __LOC__ da;
  41. js "digest" |> Assrt.equals_string __LOC__ di;
  42. js "content-type" |> Assrt.equals_string __LOC__ co;
  43. js "signature" |> Assrt.equals_string __LOC__ si;
  44. let open Cohttp in
  45. let h = Http.signed_headers
  46. (As2.PubKeyPem.sign pk)
  47. (Uri.of_string "http://localhost:3000/users/ronan2")
  48. da'
  49. di
  50. (Uri.of_string "http://localhost:3000/accounts/ronan/inbox")
  51. in
  52. Header.get h "signature"
  53. |> Option.get
  54. |> Assrt.equals_string ""
  55. ("keyId=\"http://localhost:3000/users/ronan2#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target)
  56. host date digest content-type\",signature=\"" ^ si' ^ "\"")
  57. ;
  58. assert true
  59. let () =
  60. Unix.chdir "../../../test/";
  61. test_signature ()