(session: Session)
| 113 | } |
| 114 | |
| 115 | const authenticate = async (session: Session): Promise<void> => { |
| 116 | session.write(PgProtocol.encodeStartupMessage({ |
| 117 | user: USER, |
| 118 | database: DATABASE, |
| 119 | application_name: "effect-pg-codec" |
| 120 | })) |
| 121 | let started: { readonly state: PgAuth.ScramFirst; readonly response: Uint8Array } | undefined |
| 122 | let continued: { readonly state: PgAuth.ScramFinal; readonly response: Uint8Array } | undefined |
| 123 | for (;;) { |
| 124 | const message = await session.next() |
| 125 | switch (message._tag) { |
| 126 | case "AuthenticationCleartextPassword": { |
| 127 | session.write(PgProtocol.encodePasswordMessage({ password: PASSWORD })) |
| 128 | break |
| 129 | } |
| 130 | case "AuthenticationMD5Password": { |
| 131 | const password = success(PgAuth.md5Password({ user: USER, password: PASSWORD, salt: message.salt })) |
| 132 | console.log(`md5.salt ${toHex(message.salt)}`) |
| 133 | console.log(`md5.expected ${password}`) |
| 134 | session.write(PgProtocol.encodePasswordMessage({ password })) |
| 135 | break |
| 136 | } |
| 137 | case "AuthenticationSASL": { |
| 138 | started = success(PgAuth.scramInit({ password: PASSWORD, nonce: CLIENT_NONCE })) |
| 139 | session.write(PgProtocol.encodeSASLInitialResponse({ |
| 140 | mechanism: PgAuth.SCRAM_SHA_256, |
| 141 | initialResponse: started.response |
| 142 | })) |
| 143 | break |
| 144 | } |
| 145 | case "AuthenticationSASLContinue": { |
| 146 | continued = success(PgAuth.scramContinue(started!.state, message.data)) |
| 147 | console.log(`scram.serverFirstMessage ${new TextDecoder().decode(message.data)}`) |
| 148 | console.log(`scram.clientFinalMessage ${new TextDecoder().decode(continued.response)}`) |
| 149 | session.write(PgProtocol.encodeSASLResponse({ data: continued.response })) |
| 150 | break |
| 151 | } |
| 152 | case "AuthenticationSASLFinal": { |
| 153 | success(PgAuth.scramFinish(continued!.state, message.data)) |
| 154 | console.log(`scram.serverFinalMessage ${new TextDecoder().decode(message.data)}`) |
| 155 | break |
| 156 | } |
| 157 | case "ErrorResponse": |
| 158 | throw new Error(`Authentication failed: ${JSON.stringify(message.fields)}`) |
| 159 | case "ReadyForQuery": |
| 160 | return |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | const rowCases: ReadonlyArray< |
| 166 | { readonly name: string; readonly type: string; readonly oid: number; readonly value: unknown } |
no test coverage detected