Merge pull request #111 from dullgiulio/early-returns-nazi

Remove unnecessary else statements
This commit is contained in:
bobbyrullo 2015-09-04 13:02:28 -07:00
commit b340660d6d
14 changed files with 55 additions and 46 deletions

View file

@ -103,14 +103,14 @@ func getDriver() (drv driver) {
case len(global.endpoint) > 0: case len(global.endpoint) > 0:
if len(global.creds.ID) == 0 || len(global.creds.Secret) == 0 { if len(global.creds.ID) == 0 || len(global.creds.Secret) == 0 {
err = errors.New("--client-id/--client-secret flags unset") err = errors.New("--client-id/--client-secret flags unset")
} else { break
pcfg, err := oidc.FetchProviderConfig(http.DefaultClient, global.endpoint)
if err != nil {
stderr("Unable to fetch provider config: %v", err)
os.Exit(1)
}
drv, err = newAPIDriver(pcfg, global.creds)
} }
pcfg, err := oidc.FetchProviderConfig(http.DefaultClient, global.endpoint)
if err != nil {
stderr("Unable to fetch provider config: %v", err)
os.Exit(1)
}
drv, err = newAPIDriver(pcfg, global.creds)
default: default:
err = errors.New("--endpoint/--db-url flags unset") err = errors.New("--endpoint/--db-url flags unset")
} }

View file

@ -66,11 +66,11 @@ func (gc *GarbageCollector) Run() chan struct{} {
next = ptime.ExpBackoff(next, time.Minute) next = ptime.ExpBackoff(next, time.Minute)
} }
log.Errorf("Failed garbage collection, retrying in %v", next) log.Errorf("Failed garbage collection, retrying in %v", next)
} else { break
failing = false
next = gc.interval
log.Infof("Garbage collection complete, running again in %v", next)
} }
failing = false
next = gc.interval
log.Infof("Garbage collection complete, running again in %v", next)
case <-stop: case <-stop:
return return
} }

View file

@ -49,13 +49,12 @@ func (r *passwordInfoRepo) Create(tx repo.Transaction, pw user.PasswordInfo) (er
} }
_, err = r.get(tx, pw.UserID) _, err = r.get(tx, pw.UserID)
if err != nil { if err == nil {
if err != user.ErrorNotFound {
return err
}
} else {
return user.ErrorDuplicateID return user.ErrorDuplicateID
} }
if err != user.ErrorNotFound {
return err
}
err = r.insert(tx, pw) err = r.insert(tx, pw)
if err != nil { if err != nil {

View file

@ -76,13 +76,12 @@ func (r *userRepo) Create(tx repo.Transaction, usr user.User) (err error) {
} }
_, err = r.get(tx, usr.ID) _, err = r.get(tx, usr.ID)
if err != nil { if err == nil {
if err != user.ErrorNotFound {
return err
}
} else {
return user.ErrorDuplicateID return user.ErrorDuplicateID
} }
if err != user.ErrorNotFound {
return err
}
if !user.ValidEmail(usr.Email) { if !user.ValidEmail(usr.Email) {
return user.ErrorInvalidEmail return user.ErrorInvalidEmail
@ -90,13 +89,12 @@ func (r *userRepo) Create(tx repo.Transaction, usr user.User) (err error) {
// make sure there's no other user with the same Email // make sure there's no other user with the same Email
_, err = r.getByEmail(tx, usr.Email) _, err = r.getByEmail(tx, usr.Email)
if err != nil { if err == nil {
if err != user.ErrorNotFound {
return err
}
} else {
return user.ErrorDuplicateEmail return user.ErrorDuplicateEmail
} }
if err != user.ErrorNotFound {
return err
}
err = r.insert(tx, usr) err = r.insert(tx, usr)
if err != nil { if err != nil {

View file

@ -10,7 +10,8 @@ func RandBytes(n int) ([]byte, error) {
got, err := rand.Read(b) got, err := rand.Read(b)
if err != nil { if err != nil {
return nil, err return nil, err
} else if n != got { }
if n != got {
return nil, errors.New("unable to generate enough random data") return nil, errors.New("unable to generate enough random data")
} }
return b, nil return b, nil

View file

@ -39,7 +39,8 @@ func (rr *RequestRecorder) Do(req *http.Request) (*http.Response, error) {
if rr.Response == nil && rr.Error == nil { if rr.Response == nil && rr.Error == nil {
panic("RequestRecorder Response and Error cannot both be nil") panic("RequestRecorder Response and Error cannot both be nil")
} else if rr.Response != nil && rr.Error != nil { }
if rr.Response != nil && rr.Error != nil {
panic("RequestRecorder Response and Error cannot both be non-nil") panic("RequestRecorder Response and Error cannot both be non-nil")
} }

View file

@ -33,7 +33,8 @@ func DefaultRefreshTokenGenerator() ([]byte, error) {
n, err := rand.Read(b) n, err := rand.Read(b)
if err != nil { if err != nil {
return nil, err return nil, err
} else if n != DefaultRefreshTokenPayloadLength { }
if n != DefaultRefreshTokenPayloadLength {
return nil, errors.New("unable to read enough random bytes") return nil, errors.New("unable to read enough random bytes")
} }
return b, nil return b, nil

View file

@ -56,7 +56,8 @@ func (c *clientTokenMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request
writeAPIError(w, http.StatusUnauthorized, newAPIError(errorAccessDenied, "")) writeAPIError(w, http.StatusUnauthorized, newAPIError(errorAccessDenied, ""))
respondError() respondError()
return return
} else if len(keys) == 0 { }
if len(keys) == 0 {
log.Error("No keys available for verification in client token middleware") log.Error("No keys available for verification in client token middleware")
writeAPIError(w, http.StatusUnauthorized, newAPIError(errorAccessDenied, "")) writeAPIError(w, http.StatusUnauthorized, newAPIError(errorAccessDenied, ""))
respondError() respondError()
@ -68,7 +69,8 @@ func (c *clientTokenMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request
log.Errorf("Failed to verify signature: %v", err) log.Errorf("Failed to verify signature: %v", err)
respondError() respondError()
return return
} else if !ok { }
if !ok {
log.Info("Invalid token") log.Info("Invalid token")
respondError() respondError()
return return
@ -112,7 +114,8 @@ func getClientIDFromAuthorizedRequest(r *http.Request) (string, error) {
sub, ok, err := claims.StringClaim("sub") sub, ok, err := claims.StringClaim("sub")
if err != nil { if err != nil {
return "", fmt.Errorf("failed to parse 'sub' claim: %v", err) return "", fmt.Errorf("failed to parse 'sub' claim: %v", err)
} else if !ok || sub == "" { }
if !ok || sub == "" {
return "", errors.New("missing required 'sub' claim") return "", errors.New("missing required 'sub' claim")
} }

View file

@ -185,7 +185,8 @@ func renderLoginPage(w http.ResponseWriter, r *http.Request, srv OIDCServer, idp
td.Message = "Server Error" td.Message = "Server Error"
execTemplate(w, tpl, td) execTemplate(w, tpl, td)
return return
} else if cm == nil { }
if cm == nil {
td.Error = true td.Error = true
td.Message = "Authentication Error" td.Message = "Authentication Error"
td.Detail = "Invalid client ID" td.Detail = "Invalid client ID"

View file

@ -183,14 +183,13 @@ func handleRegisterFunc(s *Server) http.HandlerFunc {
data.FormErrors = formErrors data.FormErrors = formErrors
execTemplate(w, tpl, data) execTemplate(w, tpl, data)
return return
} else { }
if err == user.ErrorDuplicateRemoteIdentity { if err == user.ErrorDuplicateRemoteIdentity {
errPage(w, "You already registered an account with this identity", "", http.StatusConflict) errPage(w, "You already registered an account with this identity", "", http.StatusConflict)
return
}
internalError(w, err)
return return
} }
internalError(w, err)
return
} }
ses, err = s.SessionManager.AttachUser(sessionID, userID) ses, err = s.SessionManager.AttachUser(sessionID, userID)
if err != nil { if err != nil {

View file

@ -351,7 +351,8 @@ func (s *Server) ClientCredsToken(creds oidc.ClientCredentials) (*jose.JWT, erro
if err != nil { if err != nil {
log.Errorf("Failed fetching client %s from repo: %v", creds.ID, err) log.Errorf("Failed fetching client %s from repo: %v", creds.ID, err)
return nil, oauth2.NewError(oauth2.ErrorServerError) return nil, oauth2.NewError(oauth2.ErrorServerError)
} else if !ok { }
if !ok {
return nil, oauth2.NewError(oauth2.ErrorInvalidClient) return nil, oauth2.NewError(oauth2.ErrorInvalidClient)
} }
@ -382,7 +383,8 @@ func (s *Server) CodeToken(creds oidc.ClientCredentials, sessionKey string) (*jo
if err != nil { if err != nil {
log.Errorf("Failed fetching client %s from repo: %v", creds.ID, err) log.Errorf("Failed fetching client %s from repo: %v", creds.ID, err)
return nil, "", oauth2.NewError(oauth2.ErrorServerError) return nil, "", oauth2.NewError(oauth2.ErrorServerError)
} else if !ok { }
if !ok {
log.Errorf("Failed to Authenticate client %s", creds.ID) log.Errorf("Failed to Authenticate client %s", creds.ID)
return nil, "", oauth2.NewError(oauth2.ErrorInvalidClient) return nil, "", oauth2.NewError(oauth2.ErrorInvalidClient)
} }
@ -450,7 +452,8 @@ func (s *Server) RefreshToken(creds oidc.ClientCredentials, token string) (*jose
if err != nil { if err != nil {
log.Errorf("Failed fetching client %s from repo: %v", creds.ID, err) log.Errorf("Failed fetching client %s from repo: %v", creds.ID, err)
return nil, oauth2.NewError(oauth2.ErrorServerError) return nil, oauth2.NewError(oauth2.ErrorServerError)
} else if !ok { }
if !ok {
log.Errorf("Failed to Authenticate client %s", creds.ID) log.Errorf("Failed to Authenticate client %s", creds.ID)
return nil, oauth2.NewError(oauth2.ErrorInvalidClient) return nil, oauth2.NewError(oauth2.ErrorInvalidClient)
} }

View file

@ -20,7 +20,8 @@ func DefaultGenerateCode() (string, error) {
n, err := rand.Read(b) n, err := rand.Read(b)
if err != nil { if err != nil {
return "", err return "", err
} else if n != 8 { }
if n != 8 {
return "", errors.New("unable to read enough random bytes") return "", errors.New("unable to read enough random bytes")
} }
return base64.URLEncoding.EncodeToString(b), nil return base64.URLEncoding.EncodeToString(b), nil

View file

@ -233,7 +233,8 @@ func generateTempHash() (string, error) {
n, err := rand.Read(b) n, err := rand.Read(b)
if err != nil { if err != nil {
return "", err return "", err
} else if n != 32 { }
if n != 32 {
return "", errors.New("unable to read enough random bytes") return "", errors.New("unable to read enough random bytes")
} }
return base64.URLEncoding.EncodeToString(b), nil return base64.URLEncoding.EncodeToString(b), nil

View file

@ -169,12 +169,13 @@ func (u *PasswordInfo) UnmarshalJSON(data []byte) error {
return ErrorInvalidPassword return ErrorInvalidPassword
} }
u.Password = Password(dec.PasswordHash) u.Password = Password(dec.PasswordHash)
} else if dec.PasswordPlaintext != "" { return nil
}
if dec.PasswordPlaintext != "" {
u.Password, err = NewPasswordFromPlaintext(dec.PasswordPlaintext) u.Password, err = NewPasswordFromPlaintext(dec.PasswordPlaintext)
if err != nil { if err != nil {
return err return err
} }
} }
return nil return nil
} }