2025-10-12 19:33:45 +00:00
|
|
|
// ===----------------------------------------------------------------------===
|
|
|
|
|
//
|
|
|
|
|
// This source file is part of the DiscogsService open source project
|
|
|
|
|
//
|
2026-03-24 01:22:53 +00:00
|
|
|
// Copyright (c) 2026 Röck+Cöde VoF. and the DiscogsService project authors
|
2025-10-12 19:33:45 +00:00
|
|
|
// Licensed under Apache license v2.0
|
|
|
|
|
//
|
|
|
|
|
// See LICENSE for license information
|
|
|
|
|
// See CONTRIBUTORS for the list of DiscogsService project authors
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
//
|
|
|
|
|
// ===----------------------------------------------------------------------===
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
/// A validation rule type that checks whether an input is secure or not.
|
|
|
|
|
struct SecureValidationRule: InputValidationRule {
|
|
|
|
|
|
|
|
|
|
// MARK: Properties
|
|
|
|
|
|
|
|
|
|
/// A representation of the available security input types.
|
|
|
|
|
private let inputType: SecurityInput
|
|
|
|
|
|
|
|
|
|
// MARK: Initializers
|
|
|
|
|
|
|
|
|
|
/// Initializes this validation rule.
|
|
|
|
|
/// - Parameter inputType: A representation of the available security input types.
|
|
|
|
|
init(inputType: SecurityInput) {
|
|
|
|
|
self.inputType = inputType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
|
|
#if swift(>=6.0)
|
|
|
|
|
func validate(_ input: String?) throws(InputValidationError) -> Bool {
|
|
|
|
|
try validate(input: input)
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
func validate(_ input: String?) throws -> Bool {
|
|
|
|
|
try validate(input: input)
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Definitions
|
|
|
|
|
|
|
|
|
|
extension InputValidationRule where Self == SecureValidationRule {
|
|
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
|
|
/// A validation rule that checks whether an input is secure or not.
|
|
|
|
|
/// - Parameter securityInput: A representation of the security input type to validate
|
|
|
|
|
/// - Returns: A validation rule that has been configured and it is ready to use.
|
|
|
|
|
static func secure(_ securityInput: SecurityInput) -> Self {
|
|
|
|
|
.init(inputType: securityInput)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Enumerations
|
|
|
|
|
|
|
|
|
|
/// A representation of all the possible security input types, based on their respective character length expectations.
|
|
|
|
|
enum SecurityInput: Int {
|
|
|
|
|
/// A consumer key is 20 characters long.
|
|
|
|
|
case consumerKey = 20
|
2026-03-24 01:22:53 +00:00
|
|
|
/// A consumer secret is 32 characters long.
|
2025-10-12 19:33:45 +00:00
|
|
|
case consumerSecret = 32
|
2026-03-24 01:22:53 +00:00
|
|
|
/// A user token is 40 characters long.
|
2025-10-12 19:33:45 +00:00
|
|
|
case userToken = 40
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
|
|
private extension SecureValidationRule {
|
|
|
|
|
|
|
|
|
|
// MARK: Functions
|
|
|
|
|
|
|
|
|
|
/// Validates a given input.
|
|
|
|
|
///
|
|
|
|
|
/// > note: This helper function would not be necessary when support for *Swift 5.10* is discontinued.
|
|
|
|
|
///
|
|
|
|
|
/// - Parameter input: An input to be validated.
|
|
|
|
|
/// - Returns: A flag that indicates whether a given input has been validated or not.
|
2026-03-24 01:22:53 +00:00
|
|
|
/// - Throws: An error of type ``InputValidationError`` in case the validation failed.
|
2025-10-12 19:33:45 +00:00
|
|
|
func validate(input: String?) throws -> Bool {
|
|
|
|
|
guard let input else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2025-10-13 00:54:17 +00:00
|
|
|
|
|
|
|
|
guard input.fullyMatch(
|
|
|
|
|
pattern: .init(format: .Pattern.securityInput, inputType.rawValue)
|
|
|
|
|
) else {
|
2025-10-12 19:33:45 +00:00
|
|
|
switch inputType {
|
|
|
|
|
case .consumerKey: throw InputValidationError.inputNotConsumerKey
|
|
|
|
|
case .consumerSecret: throw InputValidationError.inputNotConsumerSecret
|
|
|
|
|
case .userToken: throw InputValidationError.inputNotUserToken
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Constants
|
|
|
|
|
|
|
|
|
|
private extension String.Pattern {
|
2025-10-13 00:54:17 +00:00
|
|
|
/// A regular expression pattern that represents security inputs.
|
2025-10-12 19:33:45 +00:00
|
|
|
static let securityInput = "^([a-z]|[A-Z]){%d}$"
|
|
|
|
|
}
|