From c98946ac9906488de0b22aeb24933b283c371a02 Mon Sep 17 00:00:00 2001 From: Berk Ozkutuk Date: Sat, 2 Apr 2022 00:17:25 +0300 Subject: [PATCH] Add --userPasswordFile option to migration utility --- app/migration/Main.hs | 37 +++++++++++++++++++++++++++++++++++-- espial.cabal | 5 +++-- package.yaml | 1 + 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/migration/Main.hs b/app/migration/Main.hs index 4b683ef..a3bac05 100644 --- a/app/migration/Main.hs +++ b/app/migration/Main.hs @@ -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 } @@ -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) diff --git a/espial.cabal b/espial.cabal index c4ab35b..e04266d 100644 --- a/espial.cabal +++ b/espial.cabal @@ -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 @@ -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 diff --git a/package.yaml b/package.yaml index 63c2e1d..b5da187 100644 --- a/package.yaml +++ b/package.yaml @@ -185,6 +185,7 @@ executables: dependencies: - espial - optparse-generic >= 1.2.3 + - optparse-applicative # Test suite tests: