-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Tests for Entities, JWS for binary data
chore: Initial articles for docs fix: JSONWebStorage equality fix: UUID lowercased fix: storage handling Locale and array fix: Stack overflow when updating ProtectedJSONWebContainer fix: HMAC key size camputation fix: RSA PKCS#1 encoding
- Loading branch information
Showing
35 changed files
with
1,037 additions
and
235 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Amir Abbas Mousavian on 9/19/23. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Data value that must be protected by JWS. | ||
public protocol ProtectedWebContainer: Hashable { | ||
/// Signed data. | ||
var protected: Data { get set } | ||
|
||
init(protected: Data) throws | ||
} | ||
|
||
public struct ProtectedDataWebContainer: ProtectedWebContainer, Codable { | ||
public var protected: Data | ||
|
||
public init(protected: Data) throws { | ||
self.protected = protected | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
|
||
let encoded = try container.decode(String.self) | ||
guard let protected = Data(urlBase64Encoded: encoded) else { | ||
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Protected is not a valid bas64url.")) | ||
} | ||
self.protected = protected | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
let encoded = protected.urlBase64EncodedData() | ||
try container.encode(encoded) | ||
} | ||
} | ||
|
||
/// A JSON Web Signature/Encryption (JWS/JWE) header or payload with can be signed. | ||
/// | ||
/// This cotainer preserves original data to keep consistancy of signature as re-encoding payload | ||
/// may change sorting. | ||
public struct ProtectedJSONWebContainer<Container: JSONWebContainer>: ProtectedWebContainer, Codable { | ||
private var _protected: Data | ||
private var _value: Container | ||
|
||
/// Serialized protected data of JOSE. | ||
public var protected: Data { | ||
get { | ||
_protected | ||
} | ||
set { | ||
_protected = newValue | ||
if newValue.isEmpty { | ||
_value.storage = .init() | ||
return | ||
} | ||
do { | ||
_value = try JSONDecoder().decode(Container.self, from: newValue) | ||
} catch { | ||
_protected = .init() | ||
} | ||
} | ||
} | ||
|
||
/// Parsed value of data. | ||
public var value: Container { | ||
get { | ||
_value | ||
} | ||
set { | ||
_value = newValue | ||
if _value.storage == .init() { | ||
_protected = .init() | ||
return | ||
} | ||
do { | ||
_protected = try JSONEncoder().encode(newValue) | ||
} catch { | ||
_value = try! .create(storage: .init()) | ||
} | ||
} | ||
} | ||
|
||
/// Initialized protected container from a JOSE data. | ||
/// | ||
/// - Parameter protected: Serialzed json object but **not** in `base64url` . | ||
public init(protected: Data) throws { | ||
self._protected = protected | ||
self._value = try JSONDecoder().decode(Container.self, from: protected) | ||
} | ||
|
||
/// Initialized protected container from object. | ||
/// | ||
/// - Parameter value: Object that will be presented in `base64url` json. | ||
public init(value: Container) throws { | ||
self._value = value | ||
self._protected = try JSONEncoder().encode(value).urlBase64EncodedData() | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
|
||
let encoded = try container.decode(String.self) | ||
guard let protected = Data(urlBase64Encoded: encoded) else { | ||
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Protected is not a valid bas64url.")) | ||
} | ||
self._protected = protected | ||
self._value = try JSONDecoder().decode(Container.self, from: protected) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
let encoded = _protected.urlBase64EncodedData() | ||
try container.encode(encoded) | ||
} | ||
|
||
public static func == (lhs: Self, rhs: Self) -> Bool { | ||
lhs._protected == rhs._protected | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(_protected) | ||
} | ||
} |
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.