Skip to content

Commit 5cd58fe

Browse files
committed
Migrate auth tokens
1 parent 3fee1dd commit 5cd58fe

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

init_db.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ CREATE TABLE IF NOT EXISTS users (
1010
admin BOOLEAN NOT NULL
1111
);
1212

13+
CREATE TABLE IF NOT EXISTS user_tokens (
14+
id INTEGER PRIMARY KEY,
15+
user_id INTEGER NOT NULL,
16+
description TEXT NOT NULL,
17+
token TEXT NOT NULL
18+
);
19+
1320
CREATE TABLE IF NOT EXISTS account_details (
1421
user_id INTEGER PRIMARY KEY,
1522
name TEXT NOT NULL,

src/Distribution/Server/Features/Database.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ initDatabaseFeature env = pure $ do
114114

115115
data HackageDb f = HackageDb
116116
{ _tblAccountDetails :: f (TableEntity AccountDetailsT),
117-
_tblUsers :: f (TableEntity UsersT)
117+
_tblUsers :: f (TableEntity UsersT),
118+
_tblUserTokens :: f (TableEntity UserTokensT)
118119
}
119120
deriving stock (Generic)
120121

src/Distribution/Server/Features/Users.hs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,17 @@ migrateStateToDatabase usersState adminsState DatabaseFeature{..} = do
303303
_uAuthInfo = authInfo,
304304
_uAdmin = Group.member uid admins
305305
}])
306+
307+
forM_ (Map.toList (userTokens uinfo)) $ \(token, desc) -> do
308+
Database.runInsert $
309+
insert
310+
(_tblUserTokens Database.hackageDb)
311+
(insertExpressions [UserTokensRow {
312+
_utId = default_,
313+
_utUserId = val_ uid,
314+
_utToken = val_ token,
315+
_utDescription = val_ desc
316+
}])
306317

307318

308319
usersStateComponent :: FilePath -> IO (StateComponent AcidState Users.Users)

src/Distribution/Server/Users/AuthToken.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE DeriveDataTypeable, GeneralizedNewtypeDeriving #-}
22
{-# LANGUAGE TypeFamilies #-}
3+
{-# LANGUAGE MultiParamTypeClasses #-}
34
module Distribution.Server.Users.AuthToken
45
( AuthToken
56
, parseAuthToken, parseAuthTokenM, renderAuthToken
@@ -25,6 +26,11 @@ import qualified Distribution.Compat.CharParsing as P
2526

2627
import Data.SafeCopy
2728

29+
import Database.Beam
30+
import Database.Beam.Backend
31+
import Database.Beam.Sqlite
32+
import Database.Beam.Sqlite.Syntax
33+
2834
-- | Contains the original token which will be shown to the user
2935
-- once and is NOT stored on the server. The user is expected
3036
-- to provide this token on each request that should be
@@ -36,6 +42,12 @@ newtype OriginalToken = OriginalToken Nonce
3642
newtype AuthToken = AuthToken BSS.ShortByteString
3743
deriving (Eq, Ord, Read, Show, MemSize)
3844

45+
instance FromBackendRow Sqlite AuthToken where
46+
fromBackendRow = AuthToken . BSS.toShort <$> fromBackendRow
47+
48+
instance HasSqlValueSyntax SqliteValueSyntax AuthToken where
49+
sqlValueSyntax (AuthToken v) = sqlValueSyntax (BSS.fromShort v)
50+
3951
convertToken :: OriginalToken -> AuthToken
4052
convertToken (OriginalToken bs) =
4153
AuthToken $ BSS.toShort $ SHA256.hash $ getRawNonceBytes bs

src/Distribution/Server/Users/State.hs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Control.Monad.Reader
2020
import qualified Control.Monad.State as State
2121
import qualified Data.Text as T
2222

23+
import Data.Int
2324
import Data.Text
2425
import Database.Beam
2526
import Database.Beam.Backend.SQL.SQL92 (HasSqlValueSyntax (..), autoSqlValueSyntax)
@@ -217,4 +218,25 @@ instance FromBackendRow Sqlite UsersStatus where
217218
fromBackendRow = read . unpack <$> fromBackendRow
218219

219220
newtype DBUserName = DBUserName Text
220-
deriving newtype (Eq, Ord, Read, Show, FromBackendRow Sqlite, HasSqlValueSyntax SqliteValueSyntax)
221+
deriving newtype (Eq, Ord, Read, Show, FromBackendRow Sqlite, HasSqlValueSyntax SqliteValueSyntax)
222+
223+
data UserTokensT f
224+
= UserTokensRow
225+
{ _utId :: Columnar f Int32,
226+
_utUserId :: Columnar f UserId,
227+
_utDescription :: Columnar f Text,
228+
_utToken :: Columnar f AuthToken
229+
}
230+
deriving (Generic, Beamable)
231+
232+
type UserTokenRow = UserTokensT Identity
233+
234+
deriving instance Show UserTokenRow
235+
236+
deriving instance Eq UserTokenRow
237+
238+
type UserTokensId = PrimaryKey UserTokensT Identity
239+
240+
instance Table UserTokensT where
241+
data PrimaryKey UserTokensT f = UserTokensId (Columnar f Int32) deriving (Generic, Beamable)
242+
primaryKey = UserTokensId . _utId

src/Distribution/Server/Users/Types.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import Data.Hashable
3535
import Data.Serialize (Serialize)
3636
import Database.Beam
3737
import Database.Beam.Backend
38-
import Database.Beam.Backend.SQL.SQL92 ()
3938
import Database.Beam.Sqlite
4039
import Database.Beam.Sqlite.Syntax
4140

0 commit comments

Comments
 (0)