Skip to content

Commit

Permalink
Merge pull request #34 from ozkutuk/add-password-file
Browse files Browse the repository at this point in the history
Add `--userPasswordFile` option to migration utility
  • Loading branch information
jonschoning authored Apr 3, 2022
2 parents b2b19cf + 59956c6 commit ade826d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
37 changes: 35 additions & 2 deletions app/migration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,42 @@ import ClassyPrelude
import Lens.Micro

import Options.Generic
import qualified Options.Applicative as OA
import qualified Data.Text as T

data Password
= PasswordText Text
| PasswordFile FilePath
deriving (Show, Read)

parsePassword :: OA.Parser Password
parsePassword = passwordText <|> passwordFile
where
passwordText = PasswordText <$> OA.strOption
( OA.long "userPassword"
<> OA.metavar "PASSWORD"
<> OA.help "Password in plain-text"
)

passwordFile = PasswordFile <$> OA.strOption
( OA.long "userPasswordFile"
<> OA.metavar "FILE"
<> OA.help "Password file"
)

instance ParseFields Password

instance ParseRecord Password where
parseRecord = fmap getOnly parseRecord

instance ParseField Password where
parseField _ _ _ _ = parsePassword

data MigrationOpts
= CreateDB { conn :: Text }
| CreateUser { conn :: Text
, userName :: Text
, userPassword :: Text
, userPassword :: Password
, privateDefault :: Maybe Bool
, archiveDefault :: Maybe Bool
, privacyLock :: Maybe Bool }
Expand Down Expand Up @@ -54,7 +84,10 @@ main = do

CreateUser{..} ->
P.runSqlite conn $ do
hash' <- liftIO (hashPassword userPassword)
passwordText <- liftIO . fmap T.strip $ case userPassword of
PasswordText s -> pure s
PasswordFile f -> readFileUtf8 f
hash' <- liftIO (hashPassword passwordText)
void $ P.upsertBy
(UniqueUserName userName)
(User userName hash' Nothing False False False)
Expand Down
5 changes: 3 additions & 2 deletions espial.cabal
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.4.
-- This file has been generated from package.yaml by hpack version 0.34.6.
--
-- see: https://github.com/sol/hpack
--
-- hash: 6176e4be5a9b09fa50173b5bb2f280e5d8bc4c70374c91f1eac67ab03a49426f
-- hash: 2d724440a0ea54ad278837400547fc97cde79d5998c51d59b4882db32eeb5372

name: espial
version: 0.0.9
Expand Down Expand Up @@ -412,6 +412,7 @@ executable migration
, microlens
, monad-logger ==0.3.*
, mtl
, optparse-applicative
, optparse-generic >=1.2.3
, parser-combinators
, persistent >=2.8 && <2.14
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ executables:
dependencies:
- espial
- optparse-generic >= 1.2.3
- optparse-applicative

# Test suite
tests:
Expand Down

0 comments on commit ade826d

Please sign in to comment.