auth
auth/profile.go
Validate profile changes. Implement validation for ProfileChange and BulkProfileChange structs. Add unit tests to ensure that validation is working as intended.
1.1 --- a/profile.go Sat Sep 27 22:25:06 2014 -0400 1.2 +++ b/profile.go Mon Sep 29 00:58:42 2014 -0400 1.3 @@ -7,11 +7,22 @@ 1.4 "code.secondbit.org/uuid" 1.5 ) 1.6 1.7 +const ( 1.8 + MinPassphraseLength = 6 1.9 + MaxPassphraseLength = 64 1.10 +) 1.11 + 1.12 var ( 1.13 ErrProfileAlreadyExists = errors.New("profile already exists in ProfileStore") 1.14 ErrProfileNotFound = errors.New("profile not found in ProfileStore") 1.15 ErrLoginAlreadyExists = errors.New("login already exists in ProfileStore") 1.16 ErrLoginNotFound = errors.New("login not found in ProfileStore") 1.17 + 1.18 + ErrMissingPassphrase = errors.New("missing passphrase") 1.19 + ErrMissingPassphraseReset = errors.New("missing passphrase reset") 1.20 + ErrMissingPassphraseResetCreated = errors.New("missing passphrase reset created timestamp") 1.21 + ErrPassphraseTooShort = errors.New("passphrase too short") 1.22 + ErrPassphraseTooLong = errors.New("passphrase too long") 1.23 ) 1.24 1.25 type Profile struct { 1.26 @@ -82,7 +93,30 @@ 1.27 } 1.28 1.29 func (c ProfileChange) Validate() error { 1.30 - // TODO: validate profile changes 1.31 + 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 { 1.32 + return ErrEmptyChange 1.33 + } 1.34 + if c.PassphraseScheme != nil && c.Passphrase == nil { 1.35 + return ErrMissingPassphrase 1.36 + } 1.37 + if c.PassphraseReset != nil && c.PassphraseResetCreated == nil { 1.38 + return ErrMissingPassphraseResetCreated 1.39 + } 1.40 + if c.PassphraseReset == nil && c.PassphraseResetCreated != nil { 1.41 + return ErrMissingPassphraseReset 1.42 + } 1.43 + if c.Salt != nil && c.Passphrase == nil { 1.44 + return ErrMissingPassphrase 1.45 + } 1.46 + if c.Iterations != nil && c.Passphrase == nil { 1.47 + return ErrMissingPassphrase 1.48 + } 1.49 + if c.Passphrase != nil && len(*c.Passphrase) < MinPassphraseLength { 1.50 + return ErrPassphraseTooShort 1.51 + } 1.52 + if c.Passphrase != nil && len(*c.Passphrase) > MaxPassphraseLength { 1.53 + return ErrPassphraseTooLong 1.54 + } 1.55 return nil 1.56 } 1.57 1.58 @@ -91,7 +125,9 @@ 1.59 } 1.60 1.61 func (b BulkProfileChange) Validate() error { 1.62 - // TODO: validate bulk profile changs 1.63 + if b.Compromised == nil { 1.64 + return ErrEmptyChange 1.65 + } 1.66 return nil 1.67 } 1.68