Skip to content

Commit

Permalink
add comments, migrate deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Gerullis committed Aug 2, 2024
1 parent 6735ff1 commit b3fa74d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
@CapacitorPlugin(name = "SSLCertificateChecker")
public class SSLCertificateChecker extends Plugin {

// pluginCall is basically the wrapper for the JS code that calls the plugin
// it's wrapper than gives us getters for the options passed in from JS as well as resolve and reject methods to send back to JS
@PluginMethod
public void checkCertificate(PluginCall call) {
String url = call.getString("url");
Expand All @@ -38,8 +40,8 @@ public void checkCertificate(PluginCall call) {
JSObject result = new JSObject();
if (cert instanceof X509Certificate) {
X509Certificate x509cert = (X509Certificate) cert;
result.put("subject", x509cert.getSubjectDN().getName());
result.put("issuer", x509cert.getIssuerDN().getName());
result.put("subject", x509cert.getSubjectX500Principal().getName());
result.put("issuer", x509cert.getIssuerX500Principal().getName());
result.put("validFrom", x509cert.getNotBefore().toString());
result.put("validTo", x509cert.getNotAfter().toString());
result.put("fingerprint", actualFingerprint);
Expand All @@ -51,6 +53,7 @@ public void checkCertificate(PluginCall call) {
}
}

// getCertificate is a private method that gets the certificate from the server
private Certificate getCertificate(String urlString) throws Exception {
URL url = new URL(urlString);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
Expand Down Expand Up @@ -78,6 +81,7 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) throws
return cert;
}

// getFingerprint is a private method that gets the fingerprint of the certificate
private String getFingerprint(Certificate cert) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] der = cert.getEncoded();
Expand All @@ -86,6 +90,7 @@ private String getFingerprint(Certificate cert) throws Exception {
return bytesToHex(digest);
}

// bytesToHex is a private method that converts the byte array to a hex string
private String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

// Plugin annotation is used to register the plugin with the Capacitor framework
@CapacitorPlugin(name = "SSLCertificateChecker")
public class SSLCertificateCheckerPlugin extends Plugin {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ class CertificateCheckDelegate: NSObject, URLSessionDelegate {
private let expectedFingerprint: String
private let completion: (Bool) -> Void

// init is the initializer for the class
init(expectedFingerprint: String, completion: @escaping (Bool) -> Void) {
self.expectedFingerprint = expectedFingerprint
self.completion = completion
}

// urlSession is the method that is called when a URLSession receives a challenge
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
guard let serverTrust = challenge.protectionSpace.serverTrust,
let certificate = SecTrustGetCertificateAtIndex(serverTrust, 0) else {
Expand All @@ -58,6 +60,7 @@ class CertificateCheckDelegate: NSObject, URLSessionDelegate {
}
}

// certificateFingerprint is the method that gets the fingerprint of the certificate
private func certificateFingerprint(_ certificate: SecCertificate) -> String {
if let data = SecCertificateCopyData(certificate) as Data? {
let hash = SHA256.hash(data: data)
Expand Down

0 comments on commit b3fa74d

Please sign in to comment.