Skip to content

Commit 62bba59

Browse files
committed
password encryption
1 parent 135ff55 commit 62bba59

6 files changed

Lines changed: 27 additions & 12 deletions

File tree

.psc-ide-port

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
15328
1+
15822

sql/CreateDB.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
DROP DATABASE IF EXISTS conduit;
22

33
CREATE DATABASE conduit;
4+
45
CREATE USER a WITH ENCRYPTED PASSWORD 'password';
6+
57
GRANT ALL PRIVILEGES ON DATABASE conduit TO a;
68

79
\connect conduit
810
CREATE EXTENSION IF NOT EXISTS citext;
911

12+
CREATE EXTENSION IF NOT EXISTS pgcrypto;
13+

src/Server/User/Persistence/Postgres.purs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
module Server.User.Persistence.Postgres (mkHandle) where
44

55
import Prelude
6-
76
import Control.Monad.Except (throwError)
87
import Data.Array (head)
98
import Data.Either (Either(..))
@@ -29,7 +28,7 @@ mkHandle p =
2928
findByCredentials :: Pool -> Credentials -> Aff Result
3029
findByCredentials pool { email, password } =
3130
withClient pool \conn ->
32-
query readJson (Query """SELECT * FROM "user" WHERE email = $1 AND password = $2""" :: Query User)
31+
query readJson (Query """SELECT * FROM "user" WHERE email = $1 AND password = crypt($2, password)""" :: Query User)
3332
[ p_ email, p_ password ]
3433
conn
3534
>>= validate
@@ -53,15 +52,25 @@ findById pool id =
5352
insert :: Pool -> Raw -> Aff Result
5453
insert pool u =
5554
withClient pool \conn ->
56-
query readJson (Query """INSERT INTO "user" (bio, email, image, password, username) VALUES ($1, $2, $3, $4, $5) RETURNING *""" :: Query User)
55+
query readJson (Query """INSERT INTO "user" (bio, email, image, password, username) VALUES ($1, $2, $3, crypt($4, gen_salt('bf')), $5) RETURNING *""" :: Query User)
5756
[ p_ u.bio, p_ u.email, p_ u.image, p_ u.password, p_ u.username ]
5857
conn
5958
>>= validate
6059

6160
update :: Pool -> Raw -> UserId -> Aff Result
6261
update pool r id =
6362
withClient pool \conn ->
64-
query readJson (Query """UPDATE "user" SET bio = $1, email = $2, image = $3, password = $4, username = $5 WHERE id = $6 RETURNING *""" :: Query User)
63+
query readJson
64+
( Query
65+
"""UPDATE "user" SET bio = $1, email = $2, image = $3
66+
, password =
67+
CASE
68+
WHEN password = crypt($4, password) THEN password
69+
ELSE crypt($4, gen_salt('bf'))
70+
END
71+
, username = $5 WHERE id = $6 RETURNING *""" ::
72+
Query User
73+
)
6574
[ p_ r.bio, p_ r.email, p_ r.image, p_ r.password, p_ r.username, p_ id ]
6675
conn
6776
>>= validate

src/Shared/Type/Misc.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Offset
4949
= Int
5050

5151
type Password
52-
= ShortString
52+
= LongString
5353

5454
type Secret
5555
= String
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"user": {
3-
"username": "jake",
4-
"image": null,
3+
"bio": "I work at statefarm",
54
"email": "jake@jake.jake",
6-
"bio": "I work at statefarm"
5+
"image": null,
6+
"password": "jake",
7+
"username": "jake"
78
}
89
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"user": {
3-
"username": "jim",
4-
"image": null,
3+
"bio": "I work at statefarm",
54
"email": "jim@jjim.jim",
6-
"bio": "I work at statefarm"
5+
"image": null,
6+
"password": "jim",
7+
"username": "jim"
78
}
89
}

0 commit comments

Comments
 (0)