Skip to content

Commit

Permalink
Add file extension parameter to RQESService and related classes
Browse files Browse the repository at this point in the history
  • Loading branch information
phisakel committed Nov 24, 2024
1 parent 6951ae1 commit dea620e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
12 changes: 6 additions & 6 deletions Sources/RqesKit/RQESService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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)
}


Expand All @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/RqesKit/RQESServiceAuthorized.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}
6 changes: 4 additions & 2 deletions Sources/RqesKit/RQESServiceCredentialAuthorized.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/RqesKit/RqesProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dea620e

Please sign in to comment.