auth.ml 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * auth.ml
  12. *
  13. * Copyright (C) The #Seppo contributors. All rights reserved.
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *)
  28. (* Password reset:
  29. *
  30. * delete the file Auth.fn
  31. *)
  32. let fn = "app/etc/passwd.s"
  33. type uid = Uid of string
  34. type bcrypt = Bcrypt of string
  35. let dummy = Uid ""
  36. let is_setup = File.exists
  37. let to_file fn (Uid uid, pwd) =
  38. Logr.debug (fun m -> m "to_file '%s' ..." uid);
  39. let h = Bcrypt.hash pwd |> Bcrypt.string_of_hash in
  40. File.out_channel fn (fun oc ->
  41. Csexp.(List [ Atom "uid"; Atom uid; Atom "bcrypt"; Atom h ] |> to_channel oc);
  42. Ok fn
  43. )
  44. let from_file fn =
  45. File.in_channel fn (fun ic ->
  46. let open Csexp in
  47. match input ic with
  48. | Ok List [ Atom "uid"; Atom uid; Atom "bcrypt"; Atom hash ] -> Ok (Uid uid, Bcrypt hash)
  49. | Error _ as e -> e
  50. | _ -> Error "invalid credential store"
  51. )
  52. let uid_from_file fn =
  53. Logr.debug (fun m -> m "Auth.uid_from_file");
  54. try
  55. match from_file fn with
  56. | Ok (uid, _) -> Ok uid
  57. | Error _ as e -> e
  58. with
  59. | Sys_error e -> Error e
  60. (* https://opam.ocaml.org/packages/safepass/ *)
  61. let chk (Uid uid', Bcrypt hash') (Uid uid, pwd) =
  62. Logr.debug (fun m -> m "Auth.chk '%s' '%s'" uid "***");
  63. if hash'
  64. |> Bcrypt.hash_of_string
  65. |> Bcrypt.verify pwd
  66. && String.equal uid' uid
  67. then Ok (Uid uid)
  68. else Error "invalid username or password"
  69. let chk_file fn cred =
  70. match from_file fn with
  71. | Ok v -> chk v cred
  72. | Error _ as e -> e
  73. (* https://opam.ocaml.org/packages/safepass/ *)
  74. let verify cred (uid', hash') =
  75. let level = Logs.Debug
  76. and error = Http.s403' in
  77. chk (uid',hash') cred
  78. |> Result.map_error (Http.err500 ~error ~level "Auth.verify")
  79. let verify_file fn cred =
  80. let level = Logs.Debug
  81. and error = Http.s403' in
  82. chk_file fn cred
  83. |> Result.map_error (Http.err500 ~error ~level "Auth.verify_file")