Skip to content

Commit 2e1458c

Browse files
Updated initializers to be explicit when possible, and dropped return keywords
1 parent c45be97 commit 2e1458c

13 files changed

+60
-65
lines changed

Sources/WebAuthn/Ceremonies/Registration/PublicKeyCredentialCreationOptions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ extension Array where Element == PublicKeyCredentialParameters {
9393
/// A list of `PublicKeyCredentialParameters` Swift WebAuthn currently supports.
9494
public static var supported: [Element] {
9595
COSEAlgorithmIdentifier.allCases.map {
96-
Element.init(type: .publicKey, alg: $0)
96+
Element(type: .publicKey, alg: $0)
9797
}
9898
}
9999
}

Sources/WebAuthn/Ceremonies/Shared/COSE/COSEAlgorithmIdentifier.swift

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,36 @@ import Crypto
1919
/// [https://www.w3.org/TR/webauthn/#biblio-iana-cose-algs-reg], for instance, -7 for "ES256" and -257 for "RS256".
2020
public enum COSEAlgorithmIdentifier: Int, RawRepresentable, CaseIterable, Encodable, Sendable {
2121
/// AlgES256 ECDSA with SHA-256
22-
case algES256 = -7
23-
/// AlgES384 ECDSA with SHA-384
24-
case algES384 = -35
25-
/// AlgES512 ECDSA with SHA-512
26-
case algES512 = -36
22+
case algES256 = -7
23+
/// AlgES384 ECDSA with SHA-384
24+
case algES384 = -35
25+
/// AlgES512 ECDSA with SHA-512
26+
case algES512 = -36
2727

28-
// We don't support RSA yet
28+
// We don't support RSA yet
2929

30-
// /// AlgRS1 RSASSA-PKCS1-v1_5 with SHA-1
31-
// case algRS1 = -65535
32-
// /// AlgRS256 RSASSA-PKCS1-v1_5 with SHA-256
33-
// case algRS256 = -257
34-
// /// AlgRS384 RSASSA-PKCS1-v1_5 with SHA-384
35-
// case algRS384 = -258
36-
// /// AlgRS512 RSASSA-PKCS1-v1_5 with SHA-512
37-
// case algRS512 = -259
38-
// /// AlgPS256 RSASSA-PSS with SHA-256
39-
// case algPS256 = -37
40-
// /// AlgPS384 RSASSA-PSS with SHA-384
41-
// case algPS384 = -38
42-
// /// AlgPS512 RSASSA-PSS with SHA-512
43-
// case algPS512 = -39
44-
// // AlgEdDSA EdDSA
45-
// case algEdDSA = -8
30+
// /// AlgRS1 RSASSA-PKCS1-v1_5 with SHA-1
31+
// case algRS1 = -65535
32+
// /// AlgRS256 RSASSA-PKCS1-v1_5 with SHA-256
33+
// case algRS256 = -257
34+
// /// AlgRS384 RSASSA-PKCS1-v1_5 with SHA-384
35+
// case algRS384 = -258
36+
// /// AlgRS512 RSASSA-PKCS1-v1_5 with SHA-512
37+
// case algRS512 = -259
38+
// /// AlgPS256 RSASSA-PSS with SHA-256
39+
// case algPS256 = -37
40+
// /// AlgPS384 RSASSA-PSS with SHA-384
41+
// case algPS384 = -38
42+
// /// AlgPS512 RSASSA-PSS with SHA-512
43+
// case algPS512 = -39
44+
// // AlgEdDSA EdDSA
45+
// case algEdDSA = -8
4646

47-
func hashAndCompare(data: Data, to compareHash: Data) -> Bool {
48-
switch self {
49-
case .algES256:
50-
return SHA256.hash(data: data) == compareHash
51-
case .algES384:
52-
return SHA384.hash(data: data) == compareHash
53-
case .algES512:
54-
return SHA512.hash(data: data) == compareHash
55-
}
56-
}
47+
func hashAndCompare(data: Data, to compareHash: Data) -> Bool {
48+
switch self {
49+
case .algES256: SHA256.hash(data: data) == compareHash
50+
case .algES384: SHA384.hash(data: data) == compareHash
51+
case .algES512: SHA512.hash(data: data) == compareHash
52+
}
53+
}
5754
}

Sources/WebAuthn/Ceremonies/Shared/CredentialPublicKey.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@ enum CredentialPublicKey: Sendable {
2929

3030
var key: PublicKey {
3131
switch self {
32-
case let .okp(key):
33-
return key
34-
case let .ec2(key):
35-
return key
36-
case let .rsa(key):
37-
return key
32+
case let .okp(key): key
33+
case let .ec2(key): key
34+
case let .rsa(key): key
3835
}
3936
}
4037

Sources/WebAuthn/Helpers/Base64Utilities.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public struct EncodedBase64: ExpressibleByStringLiteral, Codable, Hashable, Equa
3838

3939
/// Return as Base64URL
4040
public var urlEncoded: URLEncodedBase64 {
41-
return .init(
41+
URLEncodedBase64(
4242
self.base64.replacingOccurrences(of: "+", with: "-")
4343
.replacingOccurrences(of: "/", with: "_")
4444
.replacingOccurrences(of: "=", with: "")
@@ -47,12 +47,12 @@ public struct EncodedBase64: ExpressibleByStringLiteral, Codable, Hashable, Equa
4747

4848
/// Decodes Base64 string and transforms result into `Data`
4949
public var decoded: Data? {
50-
return Data(base64Encoded: self.base64)
50+
Data(base64Encoded: self.base64)
5151
}
5252

5353
/// Returns Base64 data as a String
5454
public func asString() -> String {
55-
return self.base64
55+
self.base64
5656
}
5757
}
5858

@@ -74,12 +74,12 @@ public struct URLEncodedBase64: ExpressibleByStringLiteral, Codable, Hashable, E
7474
self.init(value)
7575
}
7676

77-
public init(from decoder: Decoder) throws {
77+
public init(from decoder: any Decoder) throws {
7878
let container = try decoder.singleValueContainer()
7979
self.base64URL = try container.decode(String.self)
8080
}
8181

82-
public func encode(to encoder: Encoder) throws {
82+
public func encode(to encoder: any Encoder) throws {
8383
var container = encoder.singleValueContainer()
8484
try container.encode(self.base64URL)
8585
}
@@ -90,12 +90,12 @@ public struct URLEncodedBase64: ExpressibleByStringLiteral, Codable, Hashable, E
9090
while result.count % 4 != 0 {
9191
result = result.appending("=")
9292
}
93-
return .init(result)
93+
return EncodedBase64(result)
9494
}
9595

9696
/// Return Base64URL as a String
9797
public func asString() -> String {
98-
return self.base64URL
98+
self.base64URL
9999
}
100100
}
101101

@@ -110,20 +110,20 @@ extension Array where Element == UInt8 {
110110
/// Encodes an array of bytes into a base64 string
111111
/// - Returns: A base64-encoded string
112112
public func base64EncodedString() -> EncodedBase64 {
113-
return .init(Data(bytes: self, count: self.count).base64EncodedString())
113+
EncodedBase64(Data(bytes: self, count: self.count).base64EncodedString())
114114
}
115115
}
116116

117117
extension Data {
118118
/// Encodes data into a base64url-encoded string
119119
/// - Returns: A base64url-encoded string
120120
public func base64URLEncodedString() -> URLEncodedBase64 {
121-
return [UInt8](self).base64URLEncodedString()
121+
[UInt8](self).base64URLEncodedString()
122122
}
123123
}
124124

125125
extension String {
126126
func toBase64() -> EncodedBase64 {
127-
return .init(Data(self.utf8).base64EncodedString())
127+
EncodedBase64(Data(self.utf8).base64EncodedString())
128128
}
129129
}

Sources/WebAuthn/Helpers/ChallengeGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ package struct ChallengeGenerator: Sendable {
1515
var generate: @Sendable () -> [UInt8]
1616

1717
package static var live: Self {
18-
.init(generate: { [UInt8].random(count: 32) })
18+
ChallengeGenerator(generate: { [UInt8].random(count: 32) })
1919
}
2020
}

Sources/WebAuthn/Helpers/UInt8+random.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313

1414
extension FixedWidthInteger {
1515
public static func random() -> Self {
16-
return Self.random(in: .min ... .max)
16+
Self.random(in: .min ... .max)
1717
}
1818

1919
public static func random<T>(using generator: inout T) -> Self where T: RandomNumberGenerator {
20-
return Self.random(in: .min ... .max, using: &generator)
20+
Self.random(in: .min ... .max, using: &generator)
2121
}
2222
}
2323

2424
extension Array where Element: FixedWidthInteger {
2525
public static func random(count: Int) -> [Element] {
26-
var array: [Element] = .init(repeating: 0, count: count)
26+
var array = Array(repeating: Element.zero, count: count)
2727
(0..<count).forEach { array[$0] = Element.random() }
2828
return array
2929
}
3030

3131
public static func random<T>(count: Int, using generator: inout T) -> [Element] where T: RandomNumberGenerator {
32-
var array: [Element] = .init(repeating: 0, count: count)
32+
var array = Array(repeating: Element.zero, count: count)
3333
(0..<count).forEach { array[$0] = Element.random(using: &generator) }
3434
return array
3535
}

Sources/WebAuthn/WebAuthnManager.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public struct WebAuthnManager: Sendable {
6666
return PublicKeyCredentialCreationOptions(
6767
challenge: challenge,
6868
user: user,
69-
relyingParty: .init(id: configuration.relyingPartyID, name: configuration.relyingPartyName),
69+
relyingParty: PublicKeyCredentialRelyingPartyEntity(
70+
id: configuration.relyingPartyID,
71+
name: configuration.relyingPartyName
72+
),
7073
publicKeyCredentialParameters: publicKeyCredentialParameters,
7174
timeout: timeout,
7275
attestation: attestation

Tests/WebAuthnTests/Utils/Hexadecimal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension String {
3434

3535
extension Data {
3636
var hexadecimal: String {
37-
return map { String(format: "%02x", $0) }
37+
self.map { String(format: "%02x", $0) }
3838
.joined()
3939
}
4040
}

Tests/WebAuthnTests/Utils/TestModels/TestAttestationObject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct TestAttestationObjectBuilder {
5353
}
5454

5555
func build() -> TestAttestationObject {
56-
return wrapped
56+
wrapped
5757
}
5858

5959
func buildBase64URLEncoded() -> URLEncodedBase64 {

Tests/WebAuthnTests/Utils/TestModels/TestCredentialPublicKey.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ struct TestCredentialPublicKeyBuilder {
5050
}
5151

5252
func buildAsByteArray() -> [UInt8] {
53-
return wrapped.byteArrayRepresentation
53+
wrapped.byteArrayRepresentation
5454
}
5555

5656
func validMock() -> Self {
57-
return self
58-
.kty(.ellipticKey)
57+
self.kty(.ellipticKey)
5958
.crv(.p256)
6059
.alg(.algES256)
6160
.xCoordinate(TestECCKeyPair.publicKeyXCoordinate)

Tests/WebAuthnTests/Utils/TestModels/TestECCKeyPair.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ struct TestECCKeyPair {
3434
static let publicKeyYCoordinate = "6637312afe0ae9a2bec08bcf4611e0e9081e6f120311a8986605d5d3b4b248f8".hexadecimal!
3535

3636
static func signature(data: Data) throws -> P256.Signing.ECDSASignature {
37-
let privateKey = try P256.Signing.PrivateKey(pemRepresentation: privateKeyPEM)
38-
return try privateKey.signature(for: data)
37+
try P256.Signing.PrivateKey(pemRepresentation: privateKeyPEM).signature(for: data)
3938
}
4039

4140
static var signature: [UInt8] {

Tests/WebAuthnTests/WebAuthnManagerAuthenticationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ final class WebAuthnManagerAuthenticationTests: XCTestCase {
3030
relyingPartyName: relyingPartyName,
3131
relyingPartyOrigin: relyingPartyOrigin
3232
)
33-
webAuthnManager = .init(configuration: configuration, challengeGenerator: .mock(generate: challenge))
33+
webAuthnManager = WebAuthnManager(configuration: configuration, challengeGenerator: .mock(generate: challenge))
3434
}
3535

3636
func testBeginAuthentication() async throws {
37-
let allowCredentials: [PublicKeyCredentialDescriptor] = [.init(type: .publicKey, id: [1, 0, 2, 30])]
37+
let allowCredentials = [PublicKeyCredentialDescriptor(type: .publicKey, id: [1, 0, 2, 30])]
3838
let options = try webAuthnManager.beginAuthentication(
3939
timeout: .seconds(1234),
4040
allowCredentials: allowCredentials,

Tests/WebAuthnTests/WebAuthnManagerRegistrationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class WebAuthnManagerRegistrationTests: XCTestCase {
3030
relyingPartyName: relyingPartyDisplayName,
3131
relyingPartyOrigin: relyingPartyOrigin
3232
)
33-
webAuthnManager = .init(configuration: configuration, challengeGenerator: .mock(generate: challenge))
33+
webAuthnManager = WebAuthnManager(configuration: configuration, challengeGenerator: .mock(generate: challenge))
3434
}
3535

3636
// MARK: - beginRegistration()

0 commit comments

Comments
 (0)