|
| 1 | +// https://github.com/apple/swift-package-manager/blob/main/Sources/Basics/Netrc.swift |
| 2 | +// |
| 3 | +//===----------------------------------------------------------------------===// |
| 4 | +// |
| 5 | +// This source file is part of the Swift open source project |
| 6 | +// |
| 7 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 8 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 9 | +// |
| 10 | +// See http://swift.org/LICENSE.txt for license information |
| 11 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | + |
| 17 | +/// Representation of Netrc configuration |
| 18 | +public struct Netrc { |
| 19 | + /// Representation of `machine` connection settings & `default` connection settings. |
| 20 | + /// If `default` connection settings present, they will be last element. |
| 21 | + public let machines: [Machine] |
| 22 | + |
| 23 | + fileprivate init(machines: [Machine]) { |
| 24 | + self.machines = machines |
| 25 | + } |
| 26 | + |
| 27 | + /// Returns auth information |
| 28 | + /// |
| 29 | + /// - Parameters: |
| 30 | + /// - url: The url to retrieve authorization information for. |
| 31 | + public func authorization(for url: URL) -> Authorization? { |
| 32 | + guard let index = machines.firstIndex(where: { $0.name == url.host }) ?? machines |
| 33 | + .firstIndex(where: { $0.isDefault }) |
| 34 | + else { |
| 35 | + return .none |
| 36 | + } |
| 37 | + let machine = self.machines[index] |
| 38 | + return Authorization(login: machine.login, password: machine.password) |
| 39 | + } |
| 40 | + |
| 41 | + /// Representation of connection settings |
| 42 | + public struct Machine: Equatable { |
| 43 | + public let name: String |
| 44 | + public let login: String |
| 45 | + public let password: String |
| 46 | + |
| 47 | + public var isDefault: Bool { |
| 48 | + self.name == "default" |
| 49 | + } |
| 50 | + |
| 51 | + public init(name: String, login: String, password: String) { |
| 52 | + self.name = name |
| 53 | + self.login = login |
| 54 | + self.password = password |
| 55 | + } |
| 56 | + |
| 57 | + init?(for match: NSTextCheckingResult, string: String, variant: String = "") { |
| 58 | + guard let name = RegexUtil.Token.machine.capture(in: match, string: string) ?? RegexUtil.Token.default |
| 59 | + .capture(in: match, string: string), |
| 60 | + let login = RegexUtil.Token.login.capture(prefix: variant, in: match, string: string), |
| 61 | + let password = RegexUtil.Token.password.capture(prefix: variant, in: match, string: string) |
| 62 | + else { |
| 63 | + return nil |
| 64 | + } |
| 65 | + self = Machine(name: name, login: login, password: password) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /// Representation of authorization information |
| 70 | + public struct Authorization: Equatable { |
| 71 | + public let login: String |
| 72 | + public let password: String |
| 73 | + |
| 74 | + public init(login: String, password: String) { |
| 75 | + self.login = login |
| 76 | + self.password = password |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +public struct NetrcParser { |
| 82 | + /// Parses a netrc file at the give location |
| 83 | + /// |
| 84 | + /// - Parameters: |
| 85 | + /// - fileSystem: The file system to use. |
| 86 | + /// - path: The file to parse |
| 87 | + public static func parse(file: URL) throws -> Netrc { |
| 88 | + guard FileManager.default.fileExists(atPath: file.path()) else { |
| 89 | + throw NetrcError.fileNotFound(file) |
| 90 | + } |
| 91 | + guard FileManager.default.isReadableFile(atPath: file.path()) else { |
| 92 | + throw NetrcError.unreadableFile(file) |
| 93 | + } |
| 94 | + let content = try String(contentsOf: file) |
| 95 | + return try Self.parse(content) |
| 96 | + } |
| 97 | + |
| 98 | + /// Parses stringified netrc content |
| 99 | + /// |
| 100 | + /// - Parameters: |
| 101 | + /// - content: The content to parse |
| 102 | + public static func parse(_ content: String) throws -> Netrc { |
| 103 | + let content = self.trimComments(from: content) |
| 104 | + let regex = try! NSRegularExpression(pattern: RegexUtil.netrcPattern, options: []) |
| 105 | + let matches = regex.matches( |
| 106 | + in: content, |
| 107 | + options: [], |
| 108 | + range: NSRange(content.startIndex ..< content.endIndex, in: content) |
| 109 | + ) |
| 110 | + |
| 111 | + let machines: [Netrc.Machine] = matches.compactMap { |
| 112 | + Netrc.Machine(for: $0, string: content, variant: "lp") ?? Netrc |
| 113 | + .Machine(for: $0, string: content, variant: "pl") |
| 114 | + } |
| 115 | + |
| 116 | + if let defIndex = machines.firstIndex(where: { $0.isDefault }) { |
| 117 | + guard defIndex == machines.index(before: machines.endIndex) else { |
| 118 | + throw NetrcError.invalidDefaultMachinePosition |
| 119 | + } |
| 120 | + } |
| 121 | + guard machines.count > 0 else { |
| 122 | + throw NetrcError.machineNotFound |
| 123 | + } |
| 124 | + return Netrc(machines: machines) |
| 125 | + } |
| 126 | + |
| 127 | + /// Utility method to trim comments from netrc content |
| 128 | + /// - Parameter text: String text of netrc file |
| 129 | + /// - Returns: String text of netrc file *sans* comments |
| 130 | + private static func trimComments(from text: String) -> String { |
| 131 | + let regex = try! NSRegularExpression(pattern: RegexUtil.comments, options: .anchorsMatchLines) |
| 132 | + let nsString = text as NSString |
| 133 | + let range = NSRange(location: 0, length: nsString.length) |
| 134 | + let matches = regex.matches(in: text, range: range) |
| 135 | + var trimmedCommentsText = text |
| 136 | + matches.forEach { |
| 137 | + trimmedCommentsText = trimmedCommentsText |
| 138 | + .replacingOccurrences(of: nsString.substring(with: $0.range), with: "") |
| 139 | + } |
| 140 | + return trimmedCommentsText |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +public enum NetrcError: Error, Equatable { |
| 145 | + case fileNotFound(URL) |
| 146 | + case unreadableFile(URL) |
| 147 | + case machineNotFound |
| 148 | + case invalidDefaultMachinePosition |
| 149 | +} |
| 150 | + |
| 151 | +private enum RegexUtil { |
| 152 | + @frozen |
| 153 | + fileprivate enum Token: String, CaseIterable { |
| 154 | + case machine, login, password, account, macdef, `default` |
| 155 | + |
| 156 | + func capture(prefix: String = "", in match: NSTextCheckingResult, string: String) -> String? { |
| 157 | + guard let range = Range(match.range(withName: prefix + rawValue), in: string) else { return nil } |
| 158 | + return String(string[range]) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + static let comments: String = "\\#[\\s\\S]*?.*$" |
| 163 | + static let `default`: String = #"(?:\s*(?<default>default))"# |
| 164 | + static let accountOptional: String = #"(?:\s*account\s+\S++)?"# |
| 165 | + static let loginPassword: String = |
| 166 | + #"\#(namedTrailingCapture("login", prefix: "lp"))\#(accountOptional)\#(namedTrailingCapture("password", prefix: "lp"))"# |
| 167 | + static let passwordLogin: String = |
| 168 | + #"\#(namedTrailingCapture("password", prefix: "pl"))\#(accountOptional)\#(namedTrailingCapture("login", prefix: "pl"))"# |
| 169 | + static let netrcPattern = |
| 170 | + #"(?:(?:(\#(namedTrailingCapture("machine"))|\#(namedMatch("default"))))(?:\#(loginPassword)|\#(passwordLogin)))"# |
| 171 | + |
| 172 | + static func namedMatch(_ string: String) -> String { |
| 173 | + #"(?:\s*(?<\#(string)>\#(string)))"# |
| 174 | + } |
| 175 | + |
| 176 | + static func namedTrailingCapture(_ string: String, prefix: String = "") -> String { |
| 177 | + #"\s*\#(string)\s+(?<\#(prefix + string)>\S++)"# |
| 178 | + } |
| 179 | +} |
0 commit comments