auth

Paddy 2015-03-21 Parent:06fb735031bb Child:3223a8e679db

149:8267e1c8bcd1 Go to Latest

auth/profile.go

Test our Postgres profileStore implementation. Update all our test cases to use time.Now().Round(time.Millisecond), because Go uses nanosecond precision on time values, but Postgres silently truncates that to millisecond precision. This caused our tests to report false failures that were just silent precision loss, not actual failures. Set up our authd server to use the Postgres store for profiles and automatically create a test scope when starting up. Log errors when creating Clients through the API, instead of just swallowing them and sending back cryptic act of god errors. Add a NewPostgres helper that returns a postgres profileStore from a connection string (passed through pq transparently). Add an Empty() bool helper to ProfileChange and BulkProfileChange types, so we can determine if there are any changes we need to act on easily. Log errors when creating Pofiles through the API, instead of just swalloing them and sending back cryptic act of god errors. Remove the ` quotes around field and table names, which are not supported in Postgres. This required adding a few functions/methods to pan. Detect situations where a profile was expected and not found, and return ErrProfileNotFound. Detect pq errors thrown when the profiles_pkey constraint is violated, and transform them to the ErrProfileAlreadyExists error. Detect empty ProfileChange and BulkProfileChange variables and abort the updateProfile and updateProfiles methods early, before invalid SQL is generated. Detect pq errors thrown when the logins_pkey constraint is violated, and transform them to the ErrLoginAlreadyExists error. Detect when removing a Login and no rows were affected, and return an ErrLoginNotFound. Create an sql dir with a postgres_init script that will initialize the schema of the tables expected in the database.

History
     1.1 --- a/profile.go	Sat Mar 21 01:23:33 2015 -0400
     1.2 +++ b/profile.go	Sat Mar 21 14:53:15 2015 -0400
     1.3 @@ -3,6 +3,7 @@
     1.4  import (
     1.5  	"encoding/json"
     1.6  	"errors"
     1.7 +	"log"
     1.8  	"net/http"
     1.9  	"regexp"
    1.10  	"strings"
    1.11 @@ -146,12 +147,16 @@
    1.12  	Deleted                *bool
    1.13  }
    1.14  
    1.15 +func (c ProfileChange) Empty() bool {
    1.16 +	return (c.Name == nil && c.Passphrase == nil && c.Iterations == nil && c.Salt == nil && c.PassphraseScheme == nil && c.Compromised == nil && c.LockedUntil == nil && c.PassphraseReset == nil && c.PassphraseResetCreated == nil && c.LastSeen == nil && c.Deleted == nil)
    1.17 +}
    1.18 +
    1.19  // Validate checks the ProfileChange it is called on
    1.20  // and asserts its internal validity, or lack thereof.
    1.21  // A descriptive error will be returned in the case of
    1.22  // an invalid change.
    1.23  func (c ProfileChange) Validate() error {
    1.24 -	if c.Name == nil && c.Passphrase == nil && c.Iterations == nil && c.Salt == nil && c.PassphraseScheme == nil && c.Compromised == nil && c.LockedUntil == nil && c.PassphraseReset == nil && c.PassphraseResetCreated == nil && c.LastSeen == nil && c.Deleted == nil {
    1.25 +	if c.Empty() {
    1.26  		return ErrEmptyChange
    1.27  	}
    1.28  	if c.PassphraseScheme != nil && c.Passphrase == nil {
    1.29 @@ -179,12 +184,16 @@
    1.30  	Compromised *bool
    1.31  }
    1.32  
    1.33 +func (b BulkProfileChange) Empty() bool {
    1.34 +	return b.Compromised == nil
    1.35 +}
    1.36 +
    1.37  // Validate checks the BulkProfileChange it is called on
    1.38  // and asserts its internal validity, or lack thereof.
    1.39  // A descriptive error will be returned in the case of an
    1.40  // invalid change.
    1.41  func (b BulkProfileChange) Validate() error {
    1.42 -	if b.Compromised == nil {
    1.43 +	if b.Empty() {
    1.44  		return ErrEmptyChange
    1.45  	}
    1.46  	return nil
    1.47 @@ -430,6 +439,7 @@
    1.48  func CreateProfileHandler(w http.ResponseWriter, r *http.Request, context Context) {
    1.49  	scheme, ok := passphraseSchemes[CurPassphraseScheme]
    1.50  	if !ok {
    1.51 +		log.Printf("Error selecting passphrase scheme #%d\n", CurPassphraseScheme)
    1.52  		encode(w, r, http.StatusInternalServerError, actOfGodResponse)
    1.53  		return
    1.54  	}
    1.55 @@ -448,6 +458,7 @@
    1.56  	}
    1.57  	passphrase, salt, err := scheme.create(req.Passphrase, context.config.iterations)
    1.58  	if err != nil {
    1.59 +		log.Printf("Error creating encoded passphrase: %#+v\n", err)
    1.60  		encode(w, r, http.StatusInternalServerError, actOfGodResponse)
    1.61  		return
    1.62  	}
    1.63 @@ -467,6 +478,7 @@
    1.64  			encode(w, r, http.StatusBadRequest, response{Errors: []requestError{{Slug: requestErrConflict, Field: "/id"}}})
    1.65  			return
    1.66  		}
    1.67 +		log.Printf("Error saving profile: %#+v\n", err)
    1.68  		encode(w, r, http.StatusInternalServerError, actOfGodResponse)
    1.69  		return
    1.70  	}
    1.71 @@ -484,6 +496,7 @@
    1.72  			encode(w, r, http.StatusBadRequest, response{Errors: []requestError{{Slug: requestErrConflict, Field: "/email"}}})
    1.73  			return
    1.74  		}
    1.75 +		log.Printf("Error adding login: %#+v\n", err)
    1.76  		encode(w, r, http.StatusInternalServerError, actOfGodResponse)
    1.77  		return
    1.78  	}
    1.79 @@ -497,6 +510,7 @@
    1.80  				encode(w, r, http.StatusBadRequest, response{Errors: []requestError{{Slug: requestErrConflict, Field: "/username"}}})
    1.81  				return
    1.82  			}
    1.83 +			log.Printf("Error adding login: %#+v\n", err)
    1.84  			encode(w, r, http.StatusInternalServerError, actOfGodResponse)
    1.85  			return
    1.86  		}
    1.87 @@ -550,6 +564,7 @@
    1.88  	decoder := json.NewDecoder(r.Body)
    1.89  	err = decoder.Decode(&req)
    1.90  	if err != nil {
    1.91 +		log.Printf("Error decoding request: %#+v\n", err)
    1.92  		encode(w, r, http.StatusBadRequest, invalidFormatResponse)
    1.93  		return
    1.94  	}