forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresMessage+SASLResponse.swift
73 lines (60 loc) · 2.83 KB
/
PostgresMessage+SASLResponse.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import NIOCore
extension PostgresMessage {
/// SASL ongoing challenge response message sent by the client.
public struct SASLResponse: PostgresMessageType {
public static var identifier: PostgresMessage.Identifier {
return .saslResponse
}
public let responseData: [UInt8]
public static func parse(from buffer: inout ByteBuffer) throws -> SASLResponse {
guard let data = buffer.readBytes(length: buffer.readableBytes) else {
throw PostgresError.protocol("Could not parse SASL response from response message")
}
return SASLResponse(responseData: data)
}
public func serialize(into buffer: inout ByteBuffer) throws {
buffer.writeBytes(responseData)
}
public var description: String {
return "SASLResponse(\(responseData))"
}
}
}
extension PostgresMessage {
/// SASL initial challenge response message sent by the client.
public struct SASLInitialResponse: PostgresMessageType {
public static var identifier: PostgresMessage.Identifier {
return .saslInitialResponse
}
public let mechanism: String
public let initialData: [UInt8]
public static func parse(from buffer: inout ByteBuffer) throws -> PostgresMessage.SASLInitialResponse {
guard let mechanism = buffer.readNullTerminatedString() else {
throw PostgresError.protocol("Could not parse SASL mechanism from initial response message")
}
guard let dataLength = buffer.readInteger(as: Int32.self) else {
throw PostgresError.protocol("Could not parse SASL initial data length from initial response message")
}
var actualData: [UInt8] = []
if dataLength != -1 {
guard let data = buffer.readBytes(length: Int(dataLength)) else {
throw PostgresError.protocol("Could not parse SASL initial data from initial response message")
}
actualData = data
}
return SASLInitialResponse(mechanism: mechanism, initialData: actualData)
}
public func serialize(into buffer: inout ByteBuffer) throws {
buffer.writeNullTerminatedString(mechanism)
if initialData.count > 0 {
buffer.writeInteger(Int32(initialData.count), as: Int32.self) // write(array:) writes Int16, which is incorrect here
buffer.writeBytes(initialData)
} else {
buffer.writeInteger(-1, as: Int32.self)
}
}
public var description: String {
return "SASLInitialResponse(\(mechanism), data: \(initialData))"
}
}
}