Code Monkey home page Code Monkey logo

http_sig_ocaml's Introduction

Http_sig - Http Signature Signing Scheme for OCaml

This library implements the Http_sig RFC (or a subset therein).

Examples

  • For validating incoming requests:
let resolve_public_key url =
  let+ (_, body) = activity_req (Uri.of_string url) in
  let+ pub_key = Cohttp_lwt.Body.to_string body in
  let pub_key =
    pub_key
    |> Cstruct.of_string
    |> X509.Public_key.decode_pem
    |> Result.map_err (fun (`Msg err) -> err) in
  Lwt.return pub_key


let handle_post req =
  let+ valid_request =
    Http_sig.verify_request
      ~resolve_public_key req in
  if not valid 
  then Dream.respond ~status:`Bad_Request ""
  else Dream.respond "OK"
  • For sending requests:
let req_post ~headers url body =
  let body = Cohttp_lwt.Body.of_string body in
  try
    Cohttp_lwt_unix.Client.post
      ~headers
      ~body
      url >> Result.return
  with exn ->
    Lwt.return (Result.of_exn exn)

let signed_post ~headers (key_id, priv_key) uri body_str =
  let current_time = Ptime_clock.now () in
  let headers =
    Http_sig.build_signed_headers
      ~current_time ~method_:"POST" ~body_str
      ~headers ~key_id ~priv_key ~uri
    |> Cohttp.Header.of_list in
  req_post ~headers uri body_str

http_sig_ocaml's People

Contributors

gopiandcode avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.