Created
December 10, 2016 16:30
-
-
Save TrevorBasinger/490e63decd8d833f038c135c97da54ff to your computer and use it in GitHub Desktop.
Load Secret File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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