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. open Astring
  29. (* Password reset:
  30. *
  31. * delete the file Auth.fn
  32. *)
  33. let fn = "app/etc/passwd.s"
  34. type uid = Uid of string
  35. type bcrypt = Bcrypt of string
  36. let dummy = Uid ""
  37. let is_setup = File.exists
  38. let to_file fn (Uid uid, pwd) =
  39. Logr.debug (fun m -> m "to_file '%s' ..." uid);
  40. let h = Bcrypt.hash pwd |> Bcrypt.string_of_hash in
  41. fn |> File.out_channel_replace (fun oc ->
  42. Csexp.(List [ Atom "uid"; Atom uid; Atom "bcrypt"; Atom h ] |> to_channel oc);
  43. Ok fn )
  44. let from_file fn =
  45. fn |> File.in_channel (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. let uid_from_file fn =
  52. Logr.debug (fun m -> m "Auth.uid_from_file");
  53. try
  54. match from_file fn with
  55. | Ok (uid, _) -> Ok uid
  56. | Error _ as e -> e
  57. with
  58. | Sys_error e -> Error e
  59. (* https://opam.ocaml.org/packages/safepass/ *)
  60. let chk (Uid uid', Bcrypt hash') (Uid uid, pwd) =
  61. Logr.debug (fun m -> m "Auth.chk '%s' '%s'" uid "***");
  62. if hash'
  63. |> Bcrypt.hash_of_string
  64. |> Bcrypt.verify pwd
  65. && String.equal uid' uid
  66. then Ok (Uid uid)
  67. else Error "invalid username or password"
  68. let chk_file fn cred =
  69. match from_file fn with
  70. | Ok v -> chk v cred
  71. | Error _ as e -> e
  72. (* https://opam.ocaml.org/packages/safepass/ *)
  73. let verify cred (uid', hash') =
  74. let level = Logs.Debug
  75. and error = Http.s403' in
  76. chk (uid',hash') cred
  77. |> Result.map_error (Http.err500 ~error ~level "Auth.verify")
  78. let verify_file fn cred =
  79. let level = Logs.Debug
  80. and error = Http.s403' in
  81. chk_file fn cred
  82. |> Result.map_error (Http.err500 ~error ~level "Auth.verify_file")