From dea620eef3d12203cc07f10e285a94516ebd7eca Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Sun, 24 Nov 2024 23:25:39 +0200 Subject: [PATCH] Add file extension parameter to RQESService and related classes --- Sources/RqesKit/RQESService.swift | 12 ++++++------ Sources/RqesKit/RQESServiceAuthorized.swift | 6 ++++-- .../RqesKit/RQESServiceCredentialAuthorized.swift | 6 ++++-- Sources/RqesKit/RqesProtocols.swift | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Sources/RqesKit/RQESService.swift b/Sources/RqesKit/RQESService.swift index 8505a78..ef737b6 100644 --- a/Sources/RqesKit/RQESService.swift +++ b/Sources/RqesKit/RQESService.swift @@ -28,19 +28,20 @@ public typealias HashAlgorithmOID = RQES_LIBRARY.HashAlgorithmOID // --------------------------- public class RQESService: RQESServiceProtocol, @unchecked Sendable { - var baseProviderUrl: String? var clientConfig: CSCClientConfig var state: String? var rqes: RQES! var defaultHashAlgorithmOID: HashAlgorithmOID var defaultSigningAlgorithmOID: SigningAlgorithmOID + var fileExtension: String /// Initialize the RQES service /// - Parameter clientConfig: CSC client configuration - required public init(clientConfig: CSCClientConfig, defaultHashAlgorithmOID: HashAlgorithmOID = .SHA256, defaultSigningAlgorithmOID: SigningAlgorithmOID = .RSA) { + required public init(clientConfig: CSCClientConfig, defaultHashAlgorithmOID: HashAlgorithmOID = .SHA256, defaultSigningAlgorithmOID: SigningAlgorithmOID = .RSA, fileExtension: String = ".pdf") { self.clientConfig = clientConfig self.defaultHashAlgorithmOID = defaultHashAlgorithmOID self.defaultSigningAlgorithmOID = defaultSigningAlgorithmOID + self.fileExtension = fileExtension } /// Retrieve the RSSP metadata @@ -51,7 +52,6 @@ public class RQESService: RQESServiceProtocol, @unchecked Sendable { // STEP 2: Retrieve service information using the InfoService let request = InfoServiceRequest(lang: "en-US") let response = try await rqes.getInfo(request: request) - baseProviderUrl = response.oauth2 return response } @@ -74,7 +74,7 @@ public class RQESService: RQESServiceProtocol, @unchecked Sendable { let tokenRequest = OAuth2TokenDto(code: authorizationCode, state: state!) let tokenResponse = try await rqes.getOAuth2Token(request: tokenRequest) let accessToken = tokenResponse.accessToken - return RQESServiceAuthorized(rqes, clientConfig: self.clientConfig, defaultHashAlgorithmOID: defaultHashAlgorithmOID, defaultSigningAlgorithmOID: defaultSigningAlgorithmOID, state: state!, accessToken: accessToken, baseProviderUrl: baseProviderUrl!) + return RQESServiceAuthorized(rqes, clientConfig: self.clientConfig, defaultHashAlgorithmOID: defaultHashAlgorithmOID, defaultSigningAlgorithmOID: defaultSigningAlgorithmOID, fileExtension: fileExtension, state: state!, accessToken: accessToken) } @@ -85,9 +85,9 @@ public class RQESService: RQESServiceProtocol, @unchecked Sendable { return try await rqes.calculateHash(request: request, accessToken: accessToken) } - static func saveToTempFile(data: Data, ext: String = ".pdf") throws -> URL { + static func saveToTempFile(data: Data, fileExtension: String = ".pdf") throws -> URL { let tempDir = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) - let tempFile = tempDir.appendingPathComponent("\(UUID().uuidString)\(ext)") + let tempFile = tempDir.appendingPathComponent("\(UUID().uuidString)\(fileExtension)") try data.write(to: tempFile) return tempFile } diff --git a/Sources/RqesKit/RQESServiceAuthorized.swift b/Sources/RqesKit/RQESServiceAuthorized.swift index 7ba4b8e..1ab4b99 100644 --- a/Sources/RqesKit/RQESServiceAuthorized.swift +++ b/Sources/RqesKit/RQESServiceAuthorized.swift @@ -33,12 +33,14 @@ public class RQESServiceAuthorized: RQESServiceAuthorizedProtocol, @unchecked Se var hashAlgorithmOID: HashAlgorithmOID? var defaultHashAlgorithmOID: HashAlgorithmOID var defaultSigningAlgorithmOID: SigningAlgorithmOID + var fileExtension: String - public init(_ rqes: RQES, clientConfig: CSCClientConfig, defaultHashAlgorithmOID: HashAlgorithmOID, defaultSigningAlgorithmOID: SigningAlgorithmOID, state: String, accessToken: String, baseProviderUrl: String) { + public init(_ rqes: RQES, clientConfig: CSCClientConfig, defaultHashAlgorithmOID: HashAlgorithmOID, defaultSigningAlgorithmOID: SigningAlgorithmOID, fileExtension: String, state: String, accessToken: String) { self.rqes = rqes self.clientConfig = clientConfig self.defaultHashAlgorithmOID = defaultHashAlgorithmOID self.defaultSigningAlgorithmOID = defaultSigningAlgorithmOID + self.fileExtension = fileExtension self.state = state self.accessToken = accessToken } @@ -86,6 +88,6 @@ public class RQESServiceAuthorized: RQESServiceAuthorizedProtocol, @unchecked Se let tokenCredentialRequest = OAuth2TokenDto(code: authorizationCode, state: state, authorizationDetails: authorizationDetailsJsonString) let tokenCredentialResponse = try await rqes.getOAuth2Token(request: tokenCredentialRequest) let credentialAccessToken = tokenCredentialResponse.accessToken - return RQESServiceCredentialAuthorized(rqes: rqes, clientConfig: clientConfig, credentialInfo: credentialInfo!, credentialAccessToken: credentialAccessToken, documents: documents!, calculateHashResponse: calculateHashResponse!, hashAlgorithmOID: hashAlgorithmOID!, defaultSigningAlgorithmOID: defaultSigningAlgorithmOID) + return RQESServiceCredentialAuthorized(rqes: rqes, clientConfig: clientConfig, credentialInfo: credentialInfo!, credentialAccessToken: credentialAccessToken, documents: documents!, calculateHashResponse: calculateHashResponse!, hashAlgorithmOID: hashAlgorithmOID!, defaultSigningAlgorithmOID: defaultSigningAlgorithmOID, fileExtension: fileExtension) } } diff --git a/Sources/RqesKit/RQESServiceCredentialAuthorized.swift b/Sources/RqesKit/RQESServiceCredentialAuthorized.swift index 7eab9d5..cb3e5e1 100644 --- a/Sources/RqesKit/RQESServiceCredentialAuthorized.swift +++ b/Sources/RqesKit/RQESServiceCredentialAuthorized.swift @@ -32,8 +32,9 @@ public class RQESServiceCredentialAuthorized: RQESServiceCredentialAuthorizedPro var calculateHashResponse: CalculateHashResponse var hashAlgorithmOID: HashAlgorithmOID var defaultSigningAlgorithmOID: SigningAlgorithmOID + var fileExtension: String - public init(rqes: RQES, clientConfig: CSCClientConfig, credentialInfo: CredentialInfo, credentialAccessToken: String, documents: [Document], calculateHashResponse: CalculateHashResponse, hashAlgorithmOID: HashAlgorithmOID, defaultSigningAlgorithmOID: SigningAlgorithmOID) { + public init(rqes: RQES, clientConfig: CSCClientConfig, credentialInfo: CredentialInfo, credentialAccessToken: String, documents: [Document], calculateHashResponse: CalculateHashResponse, hashAlgorithmOID: HashAlgorithmOID, defaultSigningAlgorithmOID: SigningAlgorithmOID, fileExtension: String) { self.rqes = rqes self.clientConfig = clientConfig self.credentialInfo = credentialInfo @@ -42,6 +43,7 @@ public class RQESServiceCredentialAuthorized: RQESServiceCredentialAuthorizedPro self.calculateHashResponse = calculateHashResponse self.hashAlgorithmOID = hashAlgorithmOID self.defaultSigningAlgorithmOID = defaultSigningAlgorithmOID + self.fileExtension = fileExtension } /// Signs the documents using the specified hash algorithm and certificates. @@ -65,7 +67,7 @@ public class RQESServiceCredentialAuthorized: RQESServiceCredentialAuthorizedPro endEntityCertificate: certs.first!, certificateChain: Array(certs.dropFirst()), hashAlgorithmOID: hashAlgorithmOID, date: calculateHashResponse.signatureDate, signatures: signHashResponse.signatures ?? []) let obtainSignedDocResponse = try await rqes.obtainSignedDoc(request: obtainSignedDocRequest, accessToken: credentialAccessToken) - let documentsWithSignature = obtainSignedDocResponse.documentWithSignature.enumerated().map { i, d in Document(id: documents[i].id, fileURL: try! RQESService.saveToTempFile(data: Data(base64Encoded: d)!)) } + let documentsWithSignature = obtainSignedDocResponse.documentWithSignature.enumerated().map { i, d in Document(id: documents[i].id, fileURL: try! RQESService.saveToTempFile(data: Data(base64Encoded: d)!, fileExtension: fileExtension)) } return documentsWithSignature } diff --git a/Sources/RqesKit/RqesProtocols.swift b/Sources/RqesKit/RqesProtocols.swift index 8e06384..dae3918 100644 --- a/Sources/RqesKit/RqesProtocols.swift +++ b/Sources/RqesKit/RqesProtocols.swift @@ -25,7 +25,7 @@ public protocol RQESServiceProtocol { associatedtype RQESServiceAuthorizedImpl: RQESServiceAuthorizedProtocol /// Initialize the RQES service /// - Parameter clientConfig: CSC client configuration - init(clientConfig: CSCClientConfig, defaultHashAlgorithmOID: HashAlgorithmOID, defaultSigningAlgorithmOID: SigningAlgorithmOID) + init(clientConfig: CSCClientConfig, defaultHashAlgorithmOID: HashAlgorithmOID, defaultSigningAlgorithmOID: SigningAlgorithmOID, fileExtension: String) /// Retrieve the RSSP metadata func getRSSPMetadata() async throws -> RSSPMetadata /// Retrieve the service authorization URL