Skip to content

Instantly share code, notes, and snippets.

@TrevorBasinger
Created December 10, 2016 16:30
Show Gist options
  • Save TrevorBasinger/490e63decd8d833f038c135c97da54ff to your computer and use it in GitHub Desktop.
Save TrevorBasinger/490e63decd8d833f038c135c97da54ff to your computer and use it in GitHub Desktop.
Load Secret File
loadSecretFile :: AppConfig -> IO AppConfig
loadSecretFile conf = extractAndTransform mSecret
where
mSecret = configJwtSecretOrFile conf
isB64 = configJwtSecretIsBase64 conf
extractAndTransform :: Maybe Text -> IO AppConfig
extractAndTransform Nothing = return conf
extractAndTransform (Just s) =
case stripPrefix "@" s of
Nothing -> setSecret <$> transformString isB64 s
Just filename -> fmap setSecret $ transformString isB64 =<< readFile (toS filename)
transformString :: Bool -> Text -> IO ByteString
transformString False t = return . encodeUtf8 $ t
transformString True t =
case decode (encodeUtf8 t) of
Left errMsg -> panic $ pack errMsg
Right bs -> return bs
setSecret bs = conf { configJwtSecret = Just bs }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment