| Conditions | 29 |
| Total Lines | 122 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like mysql.*mysqlConn.handleAuthResult often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | // Go MySQL Driver - A MySQL-Driver for Go's database/sql package |
||
| 298 | func (mc *mysqlConn) handleAuthResult(oldAuthData []byte, plugin string) error { |
||
| 299 | // Read Result Packet |
||
| 300 | authData, newPlugin, err := mc.readAuthResult() |
||
| 301 | if err != nil { |
||
| 302 | return err |
||
| 303 | } |
||
| 304 | |||
| 305 | // handle auth plugin switch, if requested |
||
| 306 | if newPlugin != "" { |
||
| 307 | // If CLIENT_PLUGIN_AUTH capability is not supported, no new cipher is |
||
| 308 | // sent and we have to keep using the cipher sent in the init packet. |
||
| 309 | if authData == nil { |
||
| 310 | authData = oldAuthData |
||
| 311 | } else { |
||
| 312 | // copy data from read buffer to owned slice |
||
| 313 | copy(oldAuthData, authData) |
||
| 314 | } |
||
| 315 | |||
| 316 | plugin = newPlugin |
||
| 317 | |||
| 318 | authResp, err := mc.auth(authData, plugin) |
||
| 319 | if err != nil { |
||
| 320 | return err |
||
| 321 | } |
||
| 322 | if err = mc.writeAuthSwitchPacket(authResp); err != nil { |
||
| 323 | return err |
||
| 324 | } |
||
| 325 | |||
| 326 | // Read Result Packet |
||
| 327 | authData, newPlugin, err = mc.readAuthResult() |
||
| 328 | if err != nil { |
||
| 329 | return err |
||
| 330 | } |
||
| 331 | |||
| 332 | // Do not allow to change the auth plugin more than once |
||
| 333 | if newPlugin != "" { |
||
| 334 | return ErrMalformPkt |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | switch plugin { |
||
| 339 | |||
| 340 | // https://insidemysql.com/preparing-your-community-connector-for-mysql-8-part-2-sha256/ |
||
| 341 | case "caching_sha2_password": |
||
| 342 | switch len(authData) { |
||
| 343 | case 0: |
||
| 344 | return nil // auth successful |
||
| 345 | case 1: |
||
| 346 | switch authData[0] { |
||
| 347 | case cachingSha2PasswordFastAuthSuccess: |
||
| 348 | if err = mc.readResultOK(); err == nil { |
||
| 349 | return nil // auth successful |
||
| 350 | } |
||
| 351 | |||
| 352 | case cachingSha2PasswordPerformFullAuthentication: |
||
| 353 | if mc.cfg.tls != nil || mc.cfg.Net == "unix" { |
||
| 354 | // write cleartext auth packet |
||
| 355 | err = mc.writeAuthSwitchPacket(append([]byte(mc.cfg.Passwd), 0)) |
||
| 356 | if err != nil { |
||
| 357 | return err |
||
| 358 | } |
||
| 359 | } else { |
||
| 360 | pubKey := mc.cfg.pubKey |
||
| 361 | if pubKey == nil { |
||
| 362 | // request public key from server |
||
| 363 | data := mc.buf.takeSmallBuffer(4 + 1) |
||
| 364 | data[4] = cachingSha2PasswordRequestPublicKey |
||
| 365 | mc.writePacket(data) |
||
| 366 | |||
| 367 | // parse public key |
||
| 368 | data, err := mc.readPacket() |
||
| 369 | if err != nil { |
||
| 370 | return err |
||
| 371 | } |
||
| 372 | |||
| 373 | block, _ := pem.Decode(data[1:]) |
||
| 374 | pkix, err := x509.ParsePKIXPublicKey(block.Bytes) |
||
| 375 | if err != nil { |
||
| 376 | return err |
||
| 377 | } |
||
| 378 | pubKey = pkix.(*rsa.PublicKey) |
||
| 379 | } |
||
| 380 | |||
| 381 | // send encrypted password |
||
| 382 | err = mc.sendEncryptedPassword(oldAuthData, pubKey) |
||
| 383 | if err != nil { |
||
| 384 | return err |
||
| 385 | } |
||
| 386 | } |
||
| 387 | return mc.readResultOK() |
||
| 388 | |||
| 389 | default: |
||
| 390 | return ErrMalformPkt |
||
| 391 | } |
||
| 392 | default: |
||
| 393 | return ErrMalformPkt |
||
| 394 | } |
||
| 395 | |||
| 396 | case "sha256_password": |
||
| 397 | switch len(authData) { |
||
| 398 | case 0: |
||
| 399 | return nil // auth successful |
||
| 400 | default: |
||
| 401 | block, _ := pem.Decode(authData) |
||
| 402 | pub, err := x509.ParsePKIXPublicKey(block.Bytes) |
||
| 403 | if err != nil { |
||
| 404 | return err |
||
| 405 | } |
||
| 406 | |||
| 407 | // send encrypted password |
||
| 408 | err = mc.sendEncryptedPassword(oldAuthData, pub.(*rsa.PublicKey)) |
||
| 409 | if err != nil { |
||
| 410 | return err |
||
| 411 | } |
||
| 412 | return mc.readResultOK() |
||
| 413 | } |
||
| 414 | |||
| 415 | default: |
||
| 416 | return nil // auth successful |
||
| 417 | } |
||
| 418 | |||
| 419 | return err |
||
| 420 | } |
||
| 421 |