Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. (import base16)
  2. (import ./redis :as redis)
  3. (import ./pq :as pq)
  4. #(import ./pgjobq :as pgjobq)
  5. (import ./mailer :as mailer)
  6.  
  7. (defn secure-code
  8. []
  9. (->
  10. (os/cryptorand 16)
  11. (base16/encode)))
  12.  
  13. (def- SIGNUP-EXPIRY (string (* 2 60 60 24) ))
  14.  
  15. (defn- account-with-email-exists?
  16. [email]
  17. (truthy? (pq/val "select 1 from accounts where primaryemail ilike $1" email)))
  18.  
  19. (defn begin-signup
  20. "
  21. Create an account signup code, registering it in
  22. the redis database.
  23.  
  24. If the account already exists then returns nil.
  25. Otherwise returns a secure code for completing account creation.
  26. "
  27. [email]
  28. (if (account-with-email-exists? email)
  29. nil
  30. (do
  31. (def code (secure-code))
  32. (def key (string "signup/" code))
  33. (redis/multi
  34. ["SET" key (marshal @{:email email})]
  35. ["EXPIRE" key SIGNUP-EXPIRY])
  36. code)))
  37.  
  38. (defn confirm-signup
  39. "
  40. Confirm an account signup.
  41.  
  42. Returns one of:
  43.  
  44. :expired-signup
  45. :account-exists
  46. id
  47. "
  48. [code]
  49. (label _
  50. (def key (string "signup/" code))
  51. (def info (-?>
  52. (redis/command "GET" key) (unmarshal)))
  53. (unless info
  54. (return _ :expired-signup))
  55. (def id
  56. (pq/txn []
  57. (when (account-with-email-exists? (info :email))
  58. (return _ :account-exists))
  59. (pq/val "insert into accounts(primaryemail) values($1) returning id;" (info :email))))
  60. (redis/command "DEL" key)
  61. id))
  62.  
  63. # repl helpers
  64. # (redis/connect "../dev-state/run/redis.sock")
  65. # (pq/connect "host=localhost dbname=postgres")
  66. # (begin-signup "ac@acha.ninja")
  67. # (confirm-signup _)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement