summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2016-01-24 13:10:04 -0500
committerAndrew Cady <d@jerkface.net>2016-01-24 13:10:04 -0500
commit7b91afaf4e74fd7fa43e0d7821055bcc651a9b1a (patch)
tree218d74b02b099759488f162477c2d4767dc32855
parent5921fc9e6876536178f903cd5c18be0308af89cf (diff)
Function 'certify' now returns certificate data
(previously it saved to a file)
-rw-r--r--acme.hs20
1 files changed, 11 insertions, 9 deletions
diff --git a/acme.hs b/acme.hs
index 3ebc911..2df70ce 100644
--- a/acme.hs
+++ b/acme.hs
@@ -167,10 +167,12 @@ go CmdOpts { .. } = do
167 167
168 let email = either (error . ("Error: invalid email address: " ++)) id . validate . fromString <$> optEmail 168 let email = either (error . ("Error: invalid email address: " ++)) id . validate . fromString <$> optEmail
169 169
170 certify directoryUrl keys email terms requestDomains challengeDir csrData domainCertFile 170 certificate <- certify directoryUrl keys email terms requestDomains challengeDir csrData
171 171
172certify :: URI -> Keys -> Maybe EmailAddress -> URI -> [DomainName] -> WritableDir -> CSR -> FilePath -> IO () 172 either (error . ("Error: " ++)) (LC.writeFile domainCertFile) certificate
173certify directoryUrl keys optEmail terms requestDomains optChallengeDir csrData domainCertFile = 173
174certify :: URI -> Keys -> Maybe EmailAddress -> URI -> [DomainName] -> WritableDir -> CSR -> IO (Either String LC.ByteString)
175certify directoryUrl keys optEmail terms requestDomains optChallengeDir csrData =
174 176
175 runACME directoryUrl keys $ do 177 runACME directoryUrl keys $ do
176 forM_ optEmail $ register terms >=> statusReport 178 forM_ optEmail $ register terms >=> statusReport
@@ -185,7 +187,7 @@ certify directoryUrl keys optEmail terms requestDomains optChallengeDir csrData
185 187
186 runEffect $ producer >-> consumer 188 runEffect $ producer >-> consumer
187 189
188 retrieveCert csrData >>= statusReport >>= saveCert domainCertFile 190 retrieveCert csrData >>= statusReport <&> checkCertResponse
189 191
190newtype CSR = CSR ByteString 192newtype CSR = CSR ByteString
191 193
@@ -237,14 +239,14 @@ ncErrorReport r =
237 putStrLn "Unexpected response to challenge-response request:" 239 putStrLn "Unexpected response to challenge-response request:"
238 print r 240 print r
239 241
240saveCert :: MonadIO m => FilePath -> Response LC.ByteString -> m () 242checkCertResponse :: Response LC.ByteString -> Either String LC.ByteString
241saveCert domainCertFile r = 243checkCertResponse r =
242 if isSuccess $ r ^. responseStatus . statusCode 244 if isSuccess $ r ^. responseStatus . statusCode
243 then liftIO $ LC.writeFile domainCertFile $ r ^. responseBody 245 then Right $ r ^. responseBody
244 else liftIO $ do 246 else
245 let (summary, details) = (k "type", k "detail") 247 let (summary, details) = (k "type", k "detail")
246 k x = r ^?! responseBody . JSON.key x . _String . to T.unpack 248 k x = r ^?! responseBody . JSON.key x . _String . to T.unpack
247 liftIO $ putStrLn $ summary ++ " ---- " ++ details 249 in Left $ summary ++ " ---- " ++ details
248 where 250 where
249 isSuccess n = n >= 200 && n <= 300 251 isSuccess n = n >= 200 && n <= 300
250 252