Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/os_comet.eliom
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ let already_send_ref =

let%client handle_error =
ref (fun exn ->
Eliom_lib.Lwt_log.ign_info_f ~exn
"Exception received on Os_comet's monitor channel: ";
Logs.info (fun fmt ->
fmt
("Exception received on Os_comet's monitor channel: " ^^ "@\n%s")
(Printexc.to_string exn));
restart_process ();
Lwt.return_unit)

Expand All @@ -96,7 +98,7 @@ let%client set_error_handler f = handle_error := f
let%client handle_message = function
| Error exn -> !handle_error exn
| Ok Heartbeat ->
Eliom_lib.Lwt_log.ign_info_f "poum";
Logs.info (fun fmt -> fmt "poum");
Lwt.return_unit
| Ok Connection_changed ->
Os_msg.msg ~level:`Err
Expand All @@ -121,8 +123,7 @@ let%server _ =
(Lwt.async (fun () ->
Lwt_stream.iter_s handle_message
(Lwt_stream.wrap_exn ~%(fst channel)))
: unit)];
Lwt.return_unit);
: unit)]);
let warn c =
(* User connected or disconnected.
I want to send the message on all tabs of the browser: *)
Expand All @@ -136,8 +137,7 @@ let%server _ =
~scope:Os_session.user_indep_session_scope ()) (fun state ->
match Eliom_reference.Volatile.Ext.get state monitor_channel_ref with
| Some (_, send) as v -> if not (v == cur) then send c
| None -> ()));
Lwt.return_unit
| None -> ()))
in
let warn_connection_change _ = warn Connection_changed in
Os_session.on_open_session warn_connection_change;
Expand Down
143 changes: 65 additions & 78 deletions src/os_connect_phone.eliom
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open Eio.Std

(* Ocsigen Start
* http://www.ocsigen.org/ocsigen-start
*
Expand All @@ -18,8 +20,6 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)

open%server Lwt.Syntax

type%shared sms_error_core = [`Unknown | `Send | `Limit | `Invalid_number]
type%shared sms_error = [`Ownership | sms_error_core]

Expand All @@ -46,112 +46,99 @@ let send_sms_handler =
Printf.printf
"INFO: send SMS %s to %s\nYou have not defined an SMS handler.\nPlease see Os_connect_phone.set_send_sms_handler\n%!"
message number;
Lwt.return (Error `Send)
Error `Send

let set_send_sms_handler = ( := ) send_sms_handler

let send_sms ~number message : (unit, sms_error_core) result Lwt.t =
let send_sms ~number message : (unit, sms_error_core) result =
!send_sms_handler ~number message

let%server request_code reference number =
Lwt.catch
(fun () ->
let* attempt =
Lwt.bind (Eliom_reference.get reference) (function
| Some (_, _, attempt) -> Lwt.return attempt
| None -> Lwt.return 0)
in
if attempt <= 3
then
let attempt = attempt + 1 and code = activation_code () in
let* () =
Eliom_reference.set reference (Some (number, code, attempt))
in
Lwt.catch
(fun () -> (send_sms ~number code :> (unit, sms_error) result Lwt.t))
(fun _ -> Lwt.return (Error `Send))
else Lwt.return (Error `Limit))
(fun _ -> Lwt.return (Error `Unknown))
try
let attempt =
match Eliom_reference.get reference with
| Some (_, _, attempt) -> attempt
| None -> 0
in
if attempt <= 3
then
let attempt = attempt + 1 and code = activation_code () in
let () = Eliom_reference.set reference (Some (number, code, attempt)) in
try (send_sms ~number code :> (unit, sms_error) result Promise.t)
with _ -> Error `Send
else Error `Limit
with _ -> Error `Unknown

let%server request_wrapper number f =
if Re.Str.string_match Os_lib.phone_regexp number 0
then f number
else Lwt.return (Error `Invalid_number)
else Error `Invalid_number

let%rpc request_recovery_code (number : string) : (unit, sms_error) result Lwt.t
=
let%rpc request_recovery_code (number : string) : (unit, sms_error) result =
request_wrapper number @@ fun number ->
let* b = Os_db.Phone.exists number in
if not b
then Lwt.return (Error `Ownership)
else request_code recovery_code_ref number
let b = Os_db.Phone.exists number in
if not b then Error `Ownership else request_code recovery_code_ref number

let%rpc request_code (number : string) : (unit, sms_error) result Lwt.t =
let%rpc request_code (number : string) : (unit, sms_error) result =
request_wrapper number @@ fun number ->
let* b = Os_db.Phone.exists number in
if b
then Lwt.return (Error `Ownership)
else request_code activation_code_ref number
let b = Os_db.Phone.exists number in
if b then Error `Ownership else request_code activation_code_ref number

let%server confirm_code myid code =
Lwt.bind (Eliom_reference.get activation_code_ref) (function
| Some (number, code', _) when code = code' -> Os_db.Phone.add myid number
| _ -> Lwt.return_false)
match Eliom_reference.get activation_code_ref with
| Some (number, code', _) when code = code' -> Os_db.Phone.add myid number
| _ -> false

let%rpc confirm_code_extra myid (code : string) : bool Lwt.t =
confirm_code myid code
let%rpc confirm_code_extra myid (code : string) : bool = confirm_code myid code

let%server
confirm_code_signup_no_connect ~first_name ~last_name ~code ~password ()
=
Lwt.bind (Eliom_reference.get activation_code_ref) (function
| Some (number, code', _) when code = code' ->
let* () = Eliom_reference.set activation_code_ref None in
let* user =
Os_user.create ~password ~firstname:first_name ~lastname:last_name ()
in
let userid = Os_user.userid_of_user user in
let* _ = Os_db.Phone.add userid number in
Lwt.return_some userid
| _ -> Lwt.return_none)
match Eliom_reference.get activation_code_ref with
| Some (number, code', _) when code = code' ->
let () = Eliom_reference.set activation_code_ref None in
let user =
Os_user.create ~password ~firstname:first_name ~lastname:last_name ()
in
let userid = Os_user.userid_of_user user in
let _ = Os_db.Phone.add userid number in
Some userid
| _ -> None

let%rpc
confirm_code_signup
~(first_name : string)
~(last_name : string)
~(code : string)
~(password : string)
() : bool Lwt.t
() : bool
=
Lwt.bind
(confirm_code_signup_no_connect ~first_name ~last_name ~code ~password ())
(function
| None -> Lwt.return_false
match
confirm_code_signup_no_connect ~first_name ~last_name ~code ~password ()
with
| None -> false
| Some userid ->
let () = Os_session.connect userid in
true

let%rpc confirm_code_recovery (code : string) : bool =
match Eliom_reference.get recovery_code_ref with
| Some (number, code', _) when code = code' -> (
match Os_db.Phone.userid number with
| Some userid ->
let* () = Os_session.connect userid in
Lwt.return_true)

let%rpc confirm_code_recovery (code : string) : bool Lwt.t =
Lwt.bind (Eliom_reference.get recovery_code_ref) (function
| Some (number, code', _) when code = code' ->
Lwt.bind (Os_db.Phone.userid number) (function
| Some userid ->
let* () = Os_session.connect userid in
Lwt.return_true
| None -> Lwt.return_false)
| _ -> Lwt.return_false)
let () = Os_session.connect userid in
true
| None -> false)
| _ -> false

let%rpc connect ~(keepmeloggedin : bool) ~(password : string) (number : string)
: [`Login_ok | `Wrong_password | `No_such_user | `Password_not_set] Lwt.t
: [`Login_ok | `Wrong_password | `No_such_user | `Password_not_set]
=
Lwt.catch
(fun () ->
let* userid = Os_db.User.verify_password_phone ~password ~number in
let* () = Os_session.connect ~expire:(not keepmeloggedin) userid in
Lwt.return `Login_ok)
(function
| Os_db.Empty_password | Os_db.Wrong_password ->
Lwt.return `Wrong_password
| Os_db.No_such_user -> Lwt.return `No_such_user
| Os_db.Password_not_set -> Lwt.return `Password_not_set
| exc -> Lwt.reraise exc)
try
let userid = Os_db.User.verify_password_phone ~password ~number in
let () = Os_session.connect ~expire:(not keepmeloggedin) userid in
`Login_ok
with
| Os_db.Empty_password | Os_db.Wrong_password -> `Wrong_password
| Os_db.No_such_user -> `No_such_user
| Os_db.Password_not_set -> `Password_not_set
18 changes: 9 additions & 9 deletions src/os_connect_phone.eliomi
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type sms_error_core = [`Unknown | `Send | `Limit | `Invalid_number]
[%%server.start]

val set_send_sms_handler :
(number:string -> string -> (unit, sms_error_core) result Lwt.t)
(number:string -> string -> (unit, sms_error_core) result)
-> unit
(** [set_send_sms_handler f] registers [f] as the function to be
called to send SMS messages. Used to send activation codes for
Expand All @@ -38,25 +38,25 @@ val confirm_code_signup_no_connect :
-> code:string
-> password:string
-> unit
-> Os_types.User.id option Lwt.t
-> Os_types.User.id option
(** Confirm validation code and create corresponding user. *)

val confirm_code : Os_types.User.id -> string -> bool Lwt.t
val confirm_code : Os_types.User.id -> string -> bool
(** Confirm validation code and add extra phone to account of the given
user *)

[%%shared.start]

type sms_error = [`Ownership | sms_error_core]

val request_code : string -> (unit, sms_error) result Lwt.t
val request_code : string -> (unit, sms_error) result
(** Send a validation code for a new e-mail address (corresponds to
[confirm_code_signup] and [confirm_code_extra]). *)

val request_recovery_code : string -> (unit, sms_error) result Lwt.t
val request_recovery_code : string -> (unit, sms_error) result
(** Send a validation code for recovering an existing address. *)

val confirm_code_extra : string -> bool Lwt.t
val confirm_code_extra : string -> bool
(** Confirm validation code and add extra phone to account of the currently
connected user*)

Expand All @@ -66,16 +66,16 @@ val confirm_code_signup :
-> code:string
-> password:string
-> unit
-> bool Lwt.t
-> bool
(** Confirm validation code and complete sign-up with the phone
number. *)

val confirm_code_recovery : string -> bool Lwt.t
val confirm_code_recovery : string -> bool
(** Confirm validation code and recover account. We redirect to the
settings page for setting a new password. *)

val connect :
keepmeloggedin:bool
-> password:string
-> string
-> [`Login_ok | `No_such_user | `Wrong_password | `Password_not_set] Lwt.t
-> [`Login_ok | `No_such_user | `Wrong_password | `Password_not_set]
Loading
Loading