-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from niscy-eudiw/feature/dpop-support
Feature - DPoP support
- Loading branch information
Showing
23 changed files
with
650 additions
and
107 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,89 @@ | ||
/* | ||
* Copyright (c) 2023 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. | ||
*/ | ||
import Foundation | ||
import JOSESwift | ||
import CryptoKit | ||
|
||
public protocol DPoPConstructorType { | ||
func jwt(endpoint: URL, accessToken: String?) throws -> String | ||
} | ||
|
||
public class DPoPConstructor: DPoPConstructorType { | ||
|
||
private enum Methods: String { | ||
case get = "GET" | ||
case head = "HEAD" | ||
case post = "POST" | ||
case put = "PUT" | ||
case delete = "DELETE" | ||
case connect = "CONNECT" | ||
case options = "OPTIONS" | ||
case trace = "TRACE" | ||
} | ||
|
||
public let algorithm: JWSAlgorithm | ||
public let jwk: JWK | ||
public let privateKey: SecKey | ||
|
||
public init(algorithm: JWSAlgorithm, jwk: JWK, privateKey: SecKey) { | ||
self.algorithm = algorithm | ||
self.jwk = jwk | ||
self.privateKey = privateKey | ||
} | ||
|
||
public func jwt(endpoint: URL, accessToken: String?) throws -> String { | ||
|
||
let header = try JWSHeader(parameters: [ | ||
"typ": "dpop+jwt", | ||
"alg": algorithm.name, | ||
"jwk": jwk.toDictionary() | ||
]) | ||
|
||
var dictionary: [String: Any] = [ | ||
JWTClaimNames.issuedAt: Int(Date().timeIntervalSince1970.rounded()), | ||
JWTClaimNames.htm: Methods.post.rawValue, | ||
JWTClaimNames.htu: endpoint.absoluteString, | ||
JWTClaimNames.jwtId: String.randomBase64URLString(length: 20) | ||
] | ||
|
||
if let data = accessToken?.data(using: .utf8) { | ||
let hashed = SHA256.hash(data: data) | ||
let hash = Data(hashed).base64URLEncodedString() | ||
dictionary["ath"] = hash | ||
} | ||
|
||
let payload = Payload(try dictionary.toThrowingJSONData()) | ||
|
||
guard let signatureAlgorithm = SignatureAlgorithm(rawValue: algorithm.name) else { | ||
throw CredentialIssuanceError.cryptographicAlgorithmNotSupported | ||
} | ||
|
||
guard let signer = Signer( | ||
signingAlgorithm: signatureAlgorithm, | ||
key: privateKey | ||
) else { | ||
throw ValidationError.error(reason: "Unable to create JWS signer") | ||
} | ||
|
||
let jws = try JWS( | ||
header: header, | ||
payload: payload, | ||
signer: signer | ||
) | ||
|
||
return jws.compactSerializedString | ||
} | ||
} |
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) 2023 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. | ||
*/ | ||
import Foundation | ||
|
||
public enum AuthorizationToken: Codable { | ||
|
||
case bearer(accessToken: String) | ||
case dpop(accessToken: String) | ||
|
||
public init(accessToken: String, useDPoP: Bool) throws { | ||
if accessToken.isEmpty { | ||
throw ValidationError.error(reason: "AuthorizationToken access token cannot be empty") | ||
} | ||
if useDPoP { | ||
self = .dpop(accessToken: accessToken) | ||
} else { | ||
self = .bearer(accessToken: accessToken) | ||
} | ||
} | ||
} |
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
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,25 @@ | ||
/* | ||
* Copyright (c) 2023 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. | ||
*/ | ||
import Foundation | ||
|
||
public struct IssuanceRefreshToken: Codable { | ||
public let refreshToken: String? | ||
|
||
public init(refreshToken: String?) throws { | ||
self.refreshToken = refreshToken | ||
} | ||
} | ||
|
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
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
Oops, something went wrong.