forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLAuthenticationRequest.swift
45 lines (41 loc) · 1.32 KB
/
PostgreSQLAuthenticationRequest.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
import Foundation
/// Authentication request returned by the server.
enum PostgreSQLAuthenticationRequest: Decodable {
/// AuthenticationOk
case ok
/// AuthenticationCleartextPassword
case plaintext
/// AuthenticationMD5Password
case md5(Data)
/// See `Decodable.init(from:)`
init(from decoder: Decoder) throws {
let single = try decoder.singleValueContainer()
let type = try single.decode(PostgreSQLAuthenticationType.self)
switch type {
case .ok: self = .ok
case .plaintext: self = .plaintext
case .md5:
let salt = try single.decode(Int32.self)
self = .md5(salt.data)
}
}
}
/// Supported authentication types.
enum PostgreSQLAuthenticationType: Int32, Decodable {
/// Specifies that the authentication was successful.
case ok = 0
/// Specifies that a clear-text password is required.
case plaintext = 3
/// Specifies that an MD5-encrypted password is required.
case md5 = 5
}
extension PostgreSQLAuthenticationType: CustomStringConvertible {
/// CustomStringConvertible.description
var description: String {
switch self {
case .ok: return "none"
case .plaintext: return "plaintext password required"
case .md5: return "md5-hashed password required"
}
}
}