Conditions | 31 |
Total Lines | 127 |
Code Lines | 76 |
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, err := mc.buf.takeSmallBuffer(4 + 1) |
||
364 | if err != nil { |
||
365 | return err |
||
366 | } |
||
367 | data[4] = cachingSha2PasswordRequestPublicKey |
||
368 | mc.writePacket(data) |
||
369 | |||
370 | // parse public key |
||
371 | if data, err = mc.readPacket(); err != nil { |
||
372 | return err |
||
373 | } |
||
374 | |||
375 | block, rest := pem.Decode(data[1:]) |
||
376 | if block == nil { |
||
377 | return fmt.Errorf("No Pem data found, data: %s", rest) |
||
378 | } |
||
379 | pkix, err := x509.ParsePKIXPublicKey(block.Bytes) |
||
380 | if err != nil { |
||
381 | return err |
||
382 | } |
||
383 | pubKey = pkix.(*rsa.PublicKey) |
||
384 | } |
||
385 | |||
386 | // send encrypted password |
||
387 | err = mc.sendEncryptedPassword(oldAuthData, pubKey) |
||
388 | if err != nil { |
||
389 | return err |
||
390 | } |
||
391 | } |
||
392 | return mc.readResultOK() |
||
393 | |||
394 | default: |
||
395 | return ErrMalformPkt |
||
396 | } |
||
397 | default: |
||
398 | return ErrMalformPkt |
||
399 | } |
||
400 | |||
401 | case "sha256_password": |
||
402 | switch len(authData) { |
||
403 | case 0: |
||
404 | return nil // auth successful |
||
405 | default: |
||
406 | block, _ := pem.Decode(authData) |
||
407 | pub, err := x509.ParsePKIXPublicKey(block.Bytes) |
||
408 | if err != nil { |
||
409 | return err |
||
410 | } |
||
411 | |||
412 | // send encrypted password |
||
413 | err = mc.sendEncryptedPassword(oldAuthData, pub.(*rsa.PublicKey)) |
||
414 | if err != nil { |
||
415 | return err |
||
416 | } |
||
417 | return mc.readResultOK() |
||
418 | } |
||
419 | |||
420 | default: |
||
421 | return nil // auth successful |
||
422 | } |
||
423 | |||
424 | return err |
||
425 | } |
||
426 |