-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow passing Order when creating CSR
- Loading branch information
1 parent
1f40636
commit 73c38cb
Showing
2 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Foundation | ||
import Crypto | ||
|
||
extension AcmeSwift { | ||
/// APIs related to CSRs. | ||
public var csr: CsrAPI { | ||
.init(client: self) | ||
} | ||
|
||
public struct CsrAPI { | ||
fileprivate var client: AcmeSwift | ||
|
||
/// Downloads the certificate chain for a finalized Order. | ||
/// The certificates are returned a a list of PEM strings. | ||
/// The first item is the final certificate for the domain. | ||
/// The second item, if any, is the issuer certificate. | ||
public func rsa(`for` order: AcmeOrderInfo) async throws -> [String] { | ||
try await self.client.ensureLoggedIn() | ||
|
||
guard order.status == .valid, let certURL = order.certificate else { | ||
throw AcmeError.certificateNotReady(order.status, "Order must have a `valid` status. Some challenges might not have been completed yet") | ||
} | ||
|
||
let separator = "-----END CERTIFICATE-----\n" | ||
let ep = DownloadCertificateEndpoint(certURL: certURL) | ||
let (certificateChain, _) = try await self.client.run(ep, privateKey: self.client.login!.key, accountURL: client.accountURL!) | ||
var certificates: [String] = [] | ||
for certificate in certificateChain.components(separatedBy: separator) { | ||
if certificate != "" { | ||
certificates.append("\(certificate)\(separator)".trimmingCharacters(in: .newlines)) | ||
} | ||
} | ||
return certificates | ||
} | ||
|
||
/// Revokes a previously issued certificate. | ||
/// - Parameters: | ||
/// - certificatePem: The Certificate **in PEM format**. | ||
public func ecdsa(certificatePem: String, reason: AcmeRevokeReason? = nil) async throws { | ||
try await self.client.ensureLoggedIn() | ||
|
||
let csrBytes = certificatePem.pemToData() | ||
let pemStr = csrBytes.toBase64UrlString() | ||
|
||
let ep = RevokeCertificateEndpoint( | ||
directory: self.client.directory, | ||
spec: .init(certificate: pemStr, reason: reason) | ||
) | ||
let (_, _) = try await self.client.run(ep, privateKey: self.client.login!.key, accountURL: client.accountURL!) | ||
} | ||
} | ||
} |
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