@@ -18,8 +18,9 @@ import Data.Typeable (Typeable)
18
18
import Data.X509 (SignedCertificate ,
19
19
decodeSignedCertificate )
20
20
import qualified Data.X509 as X509
21
- import Data.X509.CertificateStore (makeCertificateStore )
21
+ import Data.X509.CertificateStore (CertificateStore , makeCertificateStore )
22
22
import qualified Data.X509.Validation as X509
23
+ import Lens.Micro (Lens' , lens , set )
23
24
import Network.Connection (TLSSettings (.. ))
24
25
import qualified Network.HTTP.Client as NH
25
26
import Network.HTTP.Client.TLS (mkManagerSettings )
@@ -67,25 +68,22 @@ defaultTLSClientParams = do
67
68
}
68
69
}
69
70
71
+ clientHooksL :: Lens' TLS. ClientParams TLS. ClientHooks
72
+ clientHooksL = lens TLS. clientHooks (\ cp ch -> cp { TLS. clientHooks = ch })
73
+
74
+ onServerCertificateL :: Lens' TLS. ClientParams (CertificateStore -> TLS. ValidationCache -> X509. ServiceID -> X509. CertificateChain -> IO [X509. FailedReason ])
75
+ onServerCertificateL =
76
+ clientHooksL . lens TLS. onServerCertificate (\ ch osc -> ch { TLS. onServerCertificate = osc })
77
+
70
78
-- | Don't check whether the cert presented by the server matches the name of the server you are connecting to.
71
79
-- This is necessary if you specify the server host by its IP address.
72
80
disableServerNameValidation :: TLS. ClientParams -> TLS. ClientParams
73
- disableServerNameValidation cp = cp
74
- { TLS. clientHooks = (TLS. clientHooks cp)
75
- { TLS. onServerCertificate = X509. validate
76
- X509. HashSHA256
77
- def
78
- def { X509. checkFQHN = False }
79
- }
80
- }
81
+ disableServerNameValidation =
82
+ set onServerCertificateL (X509. validate X509. HashSHA256 def (def { X509. checkFQHN = False }))
81
83
82
84
-- | Insecure mode. The client will not validate the server cert at all.
83
85
disableServerCertValidation :: TLS. ClientParams -> TLS. ClientParams
84
- disableServerCertValidation cp = cp
85
- { TLS. clientHooks = (TLS. clientHooks cp)
86
- { TLS. onServerCertificate = (\ _ _ _ _ -> return [] )
87
- }
88
- }
86
+ disableServerCertValidation = set onServerCertificateL (\ _ _ _ _ -> return [] )
89
87
90
88
-- | Use a custom CA store.
91
89
setCAStore :: [SignedCertificate ] -> TLS. ClientParams -> TLS. ClientParams
@@ -95,13 +93,13 @@ setCAStore certs cp = cp
95
93
}
96
94
}
97
95
96
+ onCertificateRequestL :: Lens' TLS. ClientParams (([TLS. CertificateType ], Maybe [TLS. HashAndSignatureAlgorithm ], [X509. DistinguishedName ]) -> IO (Maybe (X509. CertificateChain , TLS. PrivKey )))
97
+ onCertificateRequestL =
98
+ clientHooksL . lens TLS. onCertificateRequest (\ ch ocr -> ch { TLS. onCertificateRequest = ocr })
99
+
98
100
-- | Use a client cert for authentication.
99
101
setClientCert :: Credential -> TLS. ClientParams -> TLS. ClientParams
100
- setClientCert cred cp = cp
101
- { TLS. clientHooks = (TLS. clientHooks cp)
102
- { TLS. onCertificateRequest = (\ _ -> return (Just cred))
103
- }
104
- }
102
+ setClientCert cred = set onCertificateRequestL (\ _ -> return $ Just cred)
105
103
106
104
-- | Parses a PEM-encoded @ByteString@ into a list of certificates.
107
105
parsePEMCerts :: B. ByteString -> Either String [SignedCertificate ]
0 commit comments