MCPcopy Create free account
hub / github.com/IceFireDB/IceFireDB / writeAuthHandshake

Method writeAuthHandshake

IceFireDB-SQLite/pkg/mysql/client/auth.go:134–260  ·  view source on GitHub ↗

See: http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse

()

Source from the content-addressed store, hash-verified

132
133// See: http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::HandshakeResponse
134func (c *Conn) writeAuthHandshake() error {
135 if !slices.Contains(supportedAuthPlugins, c.authPluginName) {
136 return fmt.Errorf("unknown auth plugin name '%s'", c.authPluginName)
137 }
138
139 // Set default client capabilities that reflect the abilities of this library
140 capability := CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION |
141 CLIENT_LONG_PASSWORD | CLIENT_TRANSACTIONS | CLIENT_PLUGIN_AUTH
142 // Adjust client capability flags based on server support
143 capability |= c.capability & CLIENT_LONG_FLAG
144 // Adjust client capability flags on specific client requests
145 // Only flags that would make any sense setting and aren't handled elsewhere
146 // in the library are supported here
147 capability |= c.ccaps&CLIENT_FOUND_ROWS | c.ccaps&CLIENT_IGNORE_SPACE |
148 c.ccaps&CLIENT_MULTI_STATEMENTS | c.ccaps&CLIENT_MULTI_RESULTS |
149 c.ccaps&CLIENT_PS_MULTI_RESULTS | c.ccaps&CLIENT_CONNECT_ATTRS
150
151 // To enable TLS / SSL
152 if c.tlsConfig != nil {
153 capability |= CLIENT_SSL
154 }
155
156 auth, addNull, err := c.genAuthResponse(c.salt)
157 if err != nil {
158 return err
159 }
160
161 // encode length of the auth plugin data
162 // here we use the Length-Encoded-Integer(LEI) as the data length may not fit into one byte
163 // see: https://dev.mysql.com/doc/internals/en/integer.html#length-encoded-integer
164 var authRespLEIBuf [9]byte
165 authRespLEI := AppendLengthEncodedInteger(authRespLEIBuf[:0], uint64(len(auth)))
166 if len(authRespLEI) > 1 {
167 // if the length can not be written in 1 byte, it must be written as a
168 // length encoded integer
169 capability |= CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA
170 }
171
172 // packet length
173 // capability 4
174 // max-packet size 4
175 // charset 1
176 // reserved all[0] 23
177 // username
178 // auth
179 // mysql_native_password + null-terminated
180 length := 4 + 4 + 1 + 23 + len(c.user) + 1 + len(authRespLEI) + len(auth) + 21 + 1
181 if addNull {
182 length++
183 }
184 // db name
185 if len(c.db) > 0 {
186 capability |= CLIENT_CONNECT_WITH_DB
187 length += len(c.db) + 1
188 }
189
190 data := make([]byte, length+4)
191

Callers 1

handshakeMethod · 0.95

Calls 5

genAuthResponseMethod · 0.95
WritePacketMethod · 0.95
NewConnFunction · 0.92
HandshakeMethod · 0.45

Tested by

no test coverage detected