@@ -20,6 +20,7 @@ import Data.X509 (SignedCertificate,
20
20
import qualified Data.X509 as X509
21
21
import Data.X509.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,21 @@ 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 =
75
+ clientHooksL . lens TLS. onServerCertificate (\ ch osc -> ch { TLS. onServerCertificate = osc })
76
+
70
77
-- | Don't check whether the cert presented by the server matches the name of the server you are connecting to.
71
78
-- This is necessary if you specify the server host by its IP address.
72
79
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
- }
80
+ disableServerNameValidation =
81
+ set onServerCertificateL (X509. validate X509. HashSHA256 def (def { X509. checkFQHN = False }))
81
82
82
83
-- | Insecure mode. The client will not validate the server cert at all.
83
84
disableServerCertValidation :: TLS. ClientParams -> TLS. ClientParams
84
- disableServerCertValidation cp = cp
85
- { TLS. clientHooks = (TLS. clientHooks cp)
86
- { TLS. onServerCertificate = (\ _ _ _ _ -> return [] )
87
- }
88
- }
85
+ disableServerCertValidation = set onServerCertificateL (\ _ _ _ _ -> return [] )
89
86
90
87
-- | Use a custom CA store.
91
88
setCAStore :: [SignedCertificate ] -> TLS. ClientParams -> TLS. ClientParams
@@ -95,13 +92,12 @@ setCAStore certs cp = cp
95
92
}
96
93
}
97
94
95
+ onCertificateRequestL =
96
+ clientHooksL . lens TLS. onCertificateRequest (\ ch ocr -> ch { TLS. onCertificateRequest = ocr })
97
+
98
98
-- | Use a client cert for authentication.
99
99
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
- }
100
+ setClientCert cred = set onCertificateRequestL (\ _ -> return $ Just cred)
105
101
106
102
-- | Parses a PEM-encoded @ByteString@ into a list of certificates.
107
103
parsePEMCerts :: B. ByteString -> Either String [SignedCertificate ]
0 commit comments