-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extension that merges authorize credential and signDocuments, since t…
…he algorithm is now set in configuration
- Loading branch information
1 parent
afd90f2
commit 28aa266
Showing
8 changed files
with
180 additions
and
2 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
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
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
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
22 changes: 22 additions & 0 deletions
22
docs/rqes-core/eu.europa.ec.eudi.rqes.core/sign-documents.md
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,22 @@ | ||
//[rqes-core](../../index.md)/[eu.europa.ec.eudi.rqes.core](index.md)/[signDocuments](sign-documents.md) | ||
|
||
# signDocuments | ||
|
||
[androidJvm]\ | ||
suspend fun [RQESService.Authorized](-r-q-e-s-service/-authorized/index.md).[signDocuments](sign-documents.md)(authorizationCode: AuthorizationCode): [Result](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/index.html)<[SignedDocuments](-signed-documents/index.md)> | ||
|
||
Sign the documents with the given authorization code. | ||
|
||
Extension function for [RQESService.Authorized](-r-q-e-s-service/-authorized/index.md) that merges the authorization and signing of the documents. | ||
|
||
#### Return | ||
|
||
The signed documents | ||
|
||
#### Parameters | ||
|
||
androidJvm | ||
|
||
| | | | ||
|---|---| | ||
| authorizationCode | The authorization code | |
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
33 changes: 33 additions & 0 deletions
33
rqes-core/src/main/kotlin/eu/europa/ec/eudi/rqes/core/Extensions.kt
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,33 @@ | ||
/* | ||
* Copyright (c) 2024 European Commission | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.europa.ec.eudi.rqes.core | ||
|
||
import eu.europa.ec.eudi.rqes.AuthorizationCode | ||
|
||
/** | ||
* Sign the documents with the given authorization code. | ||
* | ||
* Extension function for [RQESService.Authorized] that merges the authorization and signing of | ||
* the documents. | ||
* | ||
* @param authorizationCode The authorization code | ||
* @return The signed documents | ||
*/ | ||
suspend fun RQESService.Authorized.signDocuments( | ||
authorizationCode: AuthorizationCode | ||
): Result<SignedDocuments> = this.authorizeCredential(authorizationCode) | ||
.mapCatching { it.signDocuments().getOrThrow() } |
114 changes: 114 additions & 0 deletions
114
rqes-core/src/test/kotlin/eu/europa/ec/eudi/rqes/core/ExtensionsTest.kt
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,114 @@ | ||
/* | ||
* Copyright (c) 2024 European Commission | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package eu.europa.ec.eudi.rqes.core | ||
|
||
import eu.europa.ec.eudi.rqes.AuthorizationCode | ||
import eu.europa.ec.eudi.rqes.CSCClient | ||
import eu.europa.ec.eudi.rqes.CredentialAuthorizationRequestPrepared | ||
import eu.europa.ec.eudi.rqes.CredentialAuthorized | ||
import eu.europa.ec.eudi.rqes.DocumentDigestList | ||
import eu.europa.ec.eudi.rqes.DocumentToSign | ||
import eu.europa.ec.eudi.rqes.HashAlgorithmOID | ||
import eu.europa.ec.eudi.rqes.ServiceAccessAuthorized | ||
import eu.europa.ec.eudi.rqes.Signature | ||
import eu.europa.ec.eudi.rqes.SignaturesList | ||
import eu.europa.ec.eudi.rqes.SigningAlgorithmOID | ||
import eu.europa.ec.eudi.rqes.core.RQESServiceImpl.AuthorizedImpl | ||
import io.mockk.coEvery | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.unmockkAll | ||
import kotlinx.coroutines.test.runTest | ||
import java.io.InputStream | ||
import java.time.Instant | ||
import kotlin.test.AfterTest | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
|
||
class ExtensionsTest { | ||
val mockClient = mockk<CSCClient>(relaxed = true) | ||
val serverState = "server-state" | ||
lateinit var authorizedService: AuthorizedImpl | ||
val serviceAccessAuthorized: ServiceAccessAuthorized = mockk(relaxed = true) | ||
|
||
@BeforeTest | ||
fun setUp() { | ||
authorizedService = AuthorizedImpl( | ||
serverState = serverState, | ||
client = mockClient, | ||
serviceAccessAuthorized = serviceAccessAuthorized, | ||
hashAlgorithm = HashAlgorithmOID.SHA_256, | ||
signingAlgorithm = SigningAlgorithmOID.ECDSA_SHA256, | ||
) | ||
} | ||
|
||
@AfterTest | ||
fun tearDown() { | ||
unmockkAll() | ||
} | ||
|
||
@Test | ||
fun `test Authorized signDocuments extension`() = runTest { | ||
val authorizationCode = AuthorizationCode(code = "let me in") | ||
authorizedService.documentsToSign = listOf<DocumentToSign>(mockk()) | ||
authorizedService.documentDigestList = mockk<DocumentDigestList> { | ||
every { hashAlgorithmOID } returns HashAlgorithmOID.SHA_256 | ||
every { hashCalculationTime } returns Instant.now() | ||
} | ||
val signatureList = mockk<SignaturesList> { | ||
every { signatures } returns listOf<Signature>(mockk()) | ||
} | ||
val signedDocumentsInputStreams = listOf<InputStream>(mockk()) | ||
val credentialAuthorized = mockk<CredentialAuthorized.SCAL2>() { | ||
every { credentialCertificate } returns mockk() | ||
coEvery { | ||
with(mockClient) { | ||
signHash(authorizedService.signingAlgorithm) | ||
} | ||
} returns Result.success(signatureList) | ||
} | ||
|
||
authorizedService.credAuthRequestPrepared = | ||
mockk<CredentialAuthorizationRequestPrepared> { | ||
coEvery { | ||
with(mockClient) { | ||
authorizeWithAuthorizationCode( | ||
authorizationCode, | ||
authorizedService.serverState | ||
) | ||
} | ||
} returns Result.success(credentialAuthorized) | ||
|
||
} | ||
|
||
coEvery { | ||
with(mockClient) { | ||
getSignedDocuments( | ||
documents = authorizedService.documentsToSign, | ||
signatures = signatureList.signatures, | ||
credentialCertificate = credentialAuthorized.credentialCertificate, | ||
// hashAlgorithmOID = documentDigestList.hashAlgorithmOID, | ||
hashAlgorithmOID = any(), // TODO: Fix this | ||
signatureTimestamp = authorizedService.documentDigestList.hashCalculationTime | ||
) | ||
} | ||
} returns signedDocumentsInputStreams | ||
|
||
val result = authorizedService.signDocuments(authorizationCode) | ||
assert(result.isSuccess) | ||
} | ||
} |