Skip to content

Commit 07d808b

Browse files
committed
Clean up models
1 parent b5565c4 commit 07d808b

26 files changed

+564
-442
lines changed

BitwardenKit/Core/Platform/Extensions/Data.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,31 @@ import CryptoKit
22
import Foundation
33

44
public extension Data {
5+
// MARK: Initializers
6+
7+
/// Parses bytes from a base64url-encoded string.
8+
init?(base64UrlEncoded str: String) throws {
9+
try self.init(base64Encoded: str.urlDecoded())
10+
}
11+
12+
// MARK: Functions
13+
14+
/// Encodes bytes in Data as a base64url string.
515
func base64UrlEncodedString(trimPadding shouldTrim: Bool) -> String {
6-
let encoded = base64EncodedString().replacingOccurrences(of: "+", with: "-").replacingOccurrences(of: "/", with: "_")
16+
let encoded = base64EncodedString().replacingOccurrences(
17+
of: "+",
18+
with: "-"
19+
).replacingOccurrences(
20+
of: "/",
21+
with: "_"
22+
)
723
if shouldTrim {
824
return encoded.trimmingCharacters(in: CharacterSet(["="]))
925
} else {
1026
return encoded
1127
}
1228
}
29+
1330
/// Generates a hash value for the provided data.
1431
///
1532
/// - Parameter using: The type of cryptographically secure hashing being performed.
@@ -39,11 +56,7 @@ public extension Data {
3956
let digest = hashFunction.hash(data: self)
4057
return Data(digest).base64EncodedString()
4158
}
42-
43-
init?(base64UrlEncoded str: String) throws {
44-
self.init(base64Encoded: try str.urlDecoded())
45-
}
46-
59+
4760
/// Transforms this Data in a hex formatted string.
4861
/// - Returns: Hex formatted string.
4962
func asHexString() -> String {
Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
1-
//
2-
// SecretVerificationRequestModel.swift
3-
// Bitwarden
4-
//
5-
// Created by Isaiah Inuwa on 2025-10-03.
6-
//
7-
8-
91
import Foundation
102
import Networking
113

124
struct SecretVerificationRequestModel: JSONRequestBody, Equatable {
13-
static let encoder = JSONEncoder()
14-
155
// MARK: Properties
166

177
let authRequestAccessCode: String?
188
let masterPasswordHash: String?
199
let otp: String?
2010

21-
22-
init(passwordHash: String) {
23-
authRequestAccessCode = nil
24-
masterPasswordHash = passwordHash
11+
// MARK: Initializers
12+
13+
init(accessCode: String) {
14+
authRequestAccessCode = accessCode
15+
masterPasswordHash = nil
2516
otp = nil
2617
}
27-
18+
2819
init(otp: String) {
2920
masterPasswordHash = nil
3021
self.otp = otp
3122
authRequestAccessCode = nil
3223
}
33-
34-
init(accessCode: String) {
35-
authRequestAccessCode = accessCode
36-
masterPasswordHash = nil
24+
25+
init(passwordHash: String) {
26+
authRequestAccessCode = nil
27+
masterPasswordHash = passwordHash
3728
otp = nil
3829
}
39-
}
30+
}
Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,59 @@
1-
//
2-
// WebAuthnLoginSaveCredentialRequestModel.swift
3-
// Bitwarden
4-
//
5-
// Created by Isaiah Inuwa on 2025-10-03.
6-
//
7-
8-
91
import Foundation
102
import Networking
113

12-
// MARK: - SaveCredentialRequestModel
4+
// MARK: WebAuthnLoginSaveCredentialRequestModel
135

14-
/// The request body for an answer login request request.
15-
///
6+
/// The request body for a request to save a WebAuthn credential.
167
struct WebAuthnLoginSaveCredentialRequestModel: JSONRequestBody, Equatable {
17-
static let encoder = JSONEncoder()
18-
198
// MARK: Properties
20-
// The response received from the authenticator.
21-
// This contains all information needed for future authentication flows.
22-
let deviceResponse: WebAuthnLoginAttestationResponseRequest
23-
24-
// Nickname chosen by the user to identify this credential
25-
let name: String
269

27-
// Token required by the server to complete the creation.
28-
// It contains encrypted information that the server needs to verify the credential.
29-
let token: String
30-
31-
// True if the credential was created with PRF support.
32-
let supportsPrf: Bool
10+
/// The response received from the authenticator.
11+
/// This contains all information needed for future authentication flows.
12+
let deviceResponse: WebAuthnLoginAttestationResponseRequest
3313

34-
// Used for vault encryption. See {@link RotateableKeySet.encryptedUserKey }
14+
/// Encapsulated user key in rotateable key set.
3515
let encryptedUserKey: String?
3616

37-
// Used for vault encryption. See {@link RotateableKeySet.encryptedPublicKey }
17+
/// Encrypted public key in rotateable key set.
3818
let encryptedPublicKey: String?
3919

40-
// Used for vault encryption. See {@link RotateableKeySet.encryptedPrivateKey }
20+
/// Encrypted private key in rotatable key set.
4121
let encryptedPrivateKey: String?
22+
23+
/// Nickname chosen by the user to identify this credential
24+
let name: String
25+
26+
/// `true` if the credential was created with PRF support.
27+
let supportsPrf: Bool
28+
29+
/// Token required by the server to complete the creation.
30+
/// It contains encrypted information that the server needs to verify the credential.
31+
let token: String
4232
}
4333

34+
// MARK: WebAuthnLoginAttestationResponseRequest
35+
36+
/// Fields corresponding to a WebAuthn PublicKeyCredential with an AuthenticatorAttestationResponse.
37+
///
38+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#iface-pkcredential)
4439
struct WebAuthnLoginAttestationResponseRequest: Encodable, Equatable {
4540
let id: String
4641
let rawId: String
47-
let type: String
48-
// let extensions: [String: Any]
4942
let response: WebAuthnLoginAttestationResponseRequestInner
43+
let type: String
44+
// We are currently not sending back any extension results to the server, so we are omitting this slot.
45+
// let clientExtensionsResults: [String: Any]
5046
}
5147

48+
// MARK: WebAuthnLoginAttestationResponseRequestInner
49+
50+
/// Fields corresponding to a WebAuthn AuthenticatorAttestationResponse.
51+
///
52+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#authenticatorattestationresponse)
5253
struct WebAuthnLoginAttestationResponseRequestInner: Encodable, Equatable {
54+
/// Attestation object received from the authenticator, encoded in base64url.
5355
let attestationObject: String
56+
57+
/// JSON object of Client Data used for the request.
5458
let clientDataJson: String
55-
56-
}
59+
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
//
2-
// WebAuthnLoginCredentialAssertionOptionsResponse.swift
3-
// Bitwarden
4-
//
5-
// Created by Isaiah Inuwa on 2025-10-03.
6-
//
7-
8-
91
import Foundation
102
import Networking
113

4+
// MARK: WebAuthnLoginCredentialAssertionOptionsResponse
5+
6+
/// Parameters received from the server for initializing a WebAuthn credential assertion flow.
127
struct WebAuthnLoginCredentialAssertionOptionsResponse: JSONResponse, Equatable, Sendable {
138
/// Options to be provided to the webauthn authenticator.
14-
let options: PublicKeyCredentialAssertionOptions;
9+
let options: PublicKeyCredentialRequestOptions
1510

1611
/// Contains an encrypted version of the {@link options}.
1712
/// Used by the server to validate the attestation response of newly created credentials.
18-
let token: String;
13+
let token: String
1914
}
2015

21-
struct PublicKeyCredentialAssertionOptions: Codable, Equatable, Hashable {
16+
// MARK: PublicKeyCredentialAssertionOptions
17+
18+
/// WebAuthn PublicKeyCredentialRequestOptions.
19+
struct PublicKeyCredentialRequestOptions: Codable, Equatable, Hashable {
2220
let allowCredentials: [BwPublicKeyCredentialDescriptor]?
2321
let challenge: String
2422
let extensions: AuthenticationExtensionsClientInputs?
2523
let rpId: String
2624
let timeout: Int?
27-
}
25+
}
Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,137 @@
1-
//
2-
// WebAuthnLoginCredentialCreationOptionsResponse.swift
3-
// Bitwarden
4-
//
5-
// Created by Isaiah Inuwa on 2025-10-03.
6-
//
7-
8-
91
import Foundation
102
import Networking
113

4+
// swiftlint:disable line_length
5+
6+
// MARK: WebAuthnLoginCredentialCreationOptionsResponse
7+
8+
/// Parameters received from the server to initiate a WebAuthn credential creation flow.
129
struct WebAuthnLoginCredentialCreationOptionsResponse: JSONResponse, Equatable, Sendable {
1310
/// Options to be provided to the webauthn authenticator.
14-
let options: PublicKeyCredentialCreationOptions;
11+
let options: PublicKeyCredentialCreationOptions
1512

1613
/// Contains an encrypted version of the {@link options}.
1714
/// Used by the server to validate the attestation response of newly created credentials.
18-
let token: String;
15+
let token: String
1916
}
2017

18+
// MARK: PublicKeyCredentialCreationOptions
19+
20+
/// WebAuthn [PublicKeyCredentialCreationOptions](https://www.w3.org/TR/webauthn-3/#dictdef-publickeycredentialcreationoptions).
2121
struct PublicKeyCredentialCreationOptions: Codable, Equatable, Hashable {
22-
// attestation?: AttestationConveyancePreference
23-
// let authenticatorSelection: AuthenticatorSelectionCriteria?
22+
/// A base64-encoded challenge that the authenticator signs, along with other data, when producing an attestation
23+
/// object for the newly created credential.
24+
///
25+
/// ([Link to specification](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-challenge))
26+
///
27+
/// Note that the server sends this challenge as a padded base64 string, not as a unpadded base64url string as is
28+
/// used in most places in the WebAuthn spec.
2429
let challenge: String
30+
31+
/// Credential IDs received from the server which should not appear on the authenticator used to complete the
32+
/// ceremony.
33+
///
34+
/// ([Link to specification](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-excludecredentials))
2535
let excludeCredentials: [BwPublicKeyCredentialDescriptor]?
36+
37+
/// WebAuthn client extension inputs.
38+
///
39+
/// ([Link to specification](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-extensions)).
2640
let extensions: AuthenticationExtensionsClientInputs?
41+
42+
/// Types of WebAuthn credentials that the server supports.
43+
///
44+
/// ([Link to specification](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-pubkeycredparams))
2745
let pubKeyCredParams: [BwPublicKeyCredentialParameters]
28-
let rp: BwPublicKeyCredentialRpEntity
46+
47+
/// Relying party information for the request.
48+
///
49+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-rp).
50+
let rp: BwPublicKeyCredentialRpEntity // swiftlint:disable:this identifier_name
51+
52+
/// Time, in milliseconds, that the server is willing to wait for a response.
53+
///
54+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-timeout)
2955
let timeout: Int?
56+
57+
/// Information about the user for whom the credential is being created.
58+
///
59+
/// [Link to spec](https://www.w3.org/TR/webauthn-3/#dom-publickeycredentialcreationoptions-user).
3060
let user: BwPublicKeyCredentialUserEntity
3161
}
3262

63+
// MARK: AuthenticationExtensionsClientInputs
3364

65+
/// Inputs for WebAuthn extensions.
3466
struct AuthenticationExtensionsClientInputs: Codable, Equatable, Hashable {
67+
/// Input values for PRF extensions.
68+
///
69+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#dom-authenticationextensionsclientinputs-prf)
3570
let prf: AuthenticationExtensionsPRFInputs?
3671
}
3772

73+
// MARK: AuthenticationExtensionsPRFInputs
74+
75+
/// Input values for WebAutn PRF extension.
76+
///
77+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#dictdef-authenticationextensionsprfinputs)
3878
struct AuthenticationExtensionsPRFInputs: Codable, Equatable, Hashable {
3979
let eval: AuthenticationExtensionsPRFValues?
4080
let evalByCredential: [String: AuthenticationExtensionsPRFValues]?
4181
}
4282

83+
// MARK: AuthenticationExtensionsPRFValues
84+
85+
/// WebAuthn PRF input values.
86+
///
87+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#dictdef-authenticationextensionsprfvalues)
4388
struct AuthenticationExtensionsPRFValues: Codable, Equatable, Hashable {
4489
let first: String
4590
let second: String?
4691
}
4792

93+
// MARK: BwPublicKeyCredentialDescriptor
94+
95+
/// WebAuthn Credential Descriptor.
96+
///
97+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#dictionary-credential-descriptor)
98+
/// Distinct from ``BitwardenSdk.PublicKeyCredentialDescriptor`` for deserialization purposes.
4899
struct BwPublicKeyCredentialDescriptor: Codable, Equatable, Hashable {
49100
let type: String
50101
let id: String
51-
// let transports: [String]?
52102
}
53103

104+
// MARK: BwPublicKeyCredentialParameters
105+
106+
/// WebAuthn parameters for credential generation.
107+
///
108+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#dictdef-publickeycredentialparameters)
109+
/// Distinct from ``BitwardenSdk.PublicKeyCredentialParameters`` for serialization purposes.
54110
struct BwPublicKeyCredentialParameters: Codable, Equatable, Hashable {
55111
let type: String
56112
let alg: Int
57113
}
58114

115+
// MARK: BwPublicKeyCredentialRpEntity
116+
117+
/// WebAuthn relying party information.
118+
///
119+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#dictdef-publickeycredentialrpentity)
120+
/// Distinct from ``BitwardenSdk.PublicKeyCredentialRpEntity`` for serialization purposes.
59121
struct BwPublicKeyCredentialRpEntity: Codable, Equatable, Hashable {
60122
let id: String
61123
let name: String
62124
}
63125

126+
// MARK: BwPublicKeyCredentialUserEntity
127+
128+
/// WebAuthn user account parameters.
129+
///
130+
/// [Link to specification](https://www.w3.org/TR/webauthn-3/#dictdef-publickeycredentialuserentity)
131+
/// Distinct from ``BitwardenSdk.PublicKeyCredentialUserEntity`` for serialization purposes.
64132
struct BwPublicKeyCredentialUserEntity: Codable, Equatable, Hashable {
65133
let id: String
66134
let name: String
67-
}
135+
}
136+
137+
// swiftlint:enable line_length

0 commit comments

Comments
 (0)