open Lwt.Syntax open Lwt.Infix exception ClosedStream exception InsufficientEncryption type feature = | Mechanisms of Sasl.mechanism list | STARTTLS | Other of Xml.element (** [features] is a tuple of features list, mandatory and optional. *) type features = (feature list * feature list) (** [parse_features stanza] is a tuple of the list of all mandatory features and all optional features described in the [stanza]. *) let parse_features (stanza : Xml.element) : features = let open Xml in let open Either in let children = if not (List.for_all is_left stanza.children) then raise (InvalidStanza (element_to_string stanza)) else List.filter_map find_left stanza.children in let parse_single_mechanism = function | Left {local_name = "mechanism"; children = [Right mechanism]; _} -> Sasl.parse_mechanism mechanism | _ -> raise (InvalidStanza (element_to_string stanza)) in let parse_feature (stanza : Xml.element) : (feature, feature) Either.t = let parse_mechanisms mech_stanza = List.map parse_single_mechanism mech_stanza in match stanza with | {local_name="mechanisms"; _} -> Left (Mechanisms (parse_mechanisms stanza.children)) | {local_name="starttls"; children=[Left {local_name="required"; _}]; _} -> Left STARTTLS | {local_name="starttls"; children=[]; _} -> Right STARTTLS | _ -> Right (Other stanza) in let features = List.partition_map parse_feature children (* The XMPP spec mandates that sending a features element that contains only a means the STARTTLS negotiation is required. *) in match features with | [], [STARTTLS] -> [STARTTLS], [] | _ -> features (** [negotiate domain portal auth] is a promise containing the features supported by the XMPP server [portal], after eventual STARTTLS negotiation and authentication using the auth config [auth]. This function should be called every time a stream needs to be reopened and stream negotiation takes place. When the XMPP server advertises optional STARTTLS support, whether the connection will be upgraded to STARTTLS depends on [prefer_starttls]. Basically, it conforms to {{: https://datatracker.ietf.org/doc/html/rfc6120#section-4.3 }}. *) let negotiate ?(prefer_starttls = true) (domain : string) (portal : Portal.t) (auth : Sasl.auth_config) : feature list Lwt.t = (* Restart a stream: Send the usual business, ask for features. *) let start_stream () : (feature list * feature list) Lwt.t = let* _id = Portal.header domain portal in Wire.get portal.stream >|= parse_features in let starttls features = let starttls, other_features = List.partition_map (function STARTTLS s -> Left s | f -> Right f) features in match starttls, prefer_starttls with | [`Optional], true | [`Required], _ -> Starttls.upgrade portal >>= start_stream | [`Optional], false | [], _ -> if Portal._encrypted portal._socket || Option.is_some (Sys.getenv_opt "FLESH_ALLOW_STRIPTLS") then Lwt.return other_features else Lwt.fail InsufficientEncryption | _ -> Lwt.fail_with "Invalid number of STARTLS declarations in features." in let sasl_auth features = let mechanisms, _other_features = List.partition_map (function Mechanisms m -> Left m | f -> Right f) features in let* mechanisms = match mechanisms with | [m] -> Lwt.return m | [] | _ -> Lwt.fail_with "Invalid number of mechanisms declarations in features" in let* auth_result = Sasl.authenticate portal auth mechanisms in match auth_result with | Error (NotAuthorized, Some (_, text)) -> Lwt.fail_with ("Not authorized: " ^ text) | Error (MalformedRequest, Some (_, text)) -> Lwt.fail_with ("Malformed request: " ^ text) | Error _ -> Lwt.fail_with "Unknown error!" | Ok _ -> print_endline "Success!"; start_stream () in start_stream () >>= starttls >>= sasl_auth (** [initiate domain] initiates a stream with the XMPP server [domain]. Once [None] is pushed into the stream, the receiving stream is drained and the socket is closed. *) let initiate (domain : string) (auth : Sasl.auth_config) : (Portal.t * feature list) Lwt.t = let open Portal in let* p = connect domain in let+ features = negotiate domain p auth in (p, features)