Skip to content

Commit

Permalink
Fixing formatting, linting and imports
Browse files Browse the repository at this point in the history
  • Loading branch information
COMTOP1 committed Jun 26, 2024
1 parent b247df2 commit 3c9a1e1
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 48 deletions.
3 changes: 2 additions & 1 deletion infrastructure/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mail
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"html/template"
"log"
Expand Down Expand Up @@ -100,7 +101,7 @@ func (m *MailerInit) ConnectMailer() *Mailer {
// CheckSendable verifies that the email can be sent
func (m *Mailer) CheckSendable(item Mail) error {
if item.To == "" {
return fmt.Errorf("no To field is set")
return errors.New("no To field is set")
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions permission/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"

"github.com/ystv/web-auth/utils"

sq "github.com/Masterminds/squirrel"

"github.com/ystv/web-auth/utils"
)

// getPermissions returns all permissions
Expand Down
4 changes: 2 additions & 2 deletions role/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"fmt"

"github.com/ystv/web-auth/utils"

sq "github.com/Masterminds/squirrel"

"github.com/ystv/web-auth/utils"
)

// getRoles returns all roles for a user
Expand Down
9 changes: 8 additions & 1 deletion user/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (s *Store) addUser(ctx context.Context, u1 User) (User, error) {

defer stmt.Close()

//nolint:musttag
err = stmt.Get(&u, u1)
if err != nil {
return User{}, fmt.Errorf("failed to add user: %w", err)
Expand Down Expand Up @@ -128,6 +129,7 @@ func (s *Store) getUser(ctx context.Context, u1 User) (User, error) {
panic(fmt.Errorf("failed to build sql for getUser: %w", err))
}

//nolint:musttag
err = s.db.GetContext(ctx, &u, sql, args...)
if err != nil {
return u, fmt.Errorf("failed to get user from db: %w", err)
Expand Down Expand Up @@ -159,7 +161,9 @@ func (s *Store) getUsers(ctx context.Context, size, page int, search, sortBy, di
return nil, -1, fmt.Errorf("failed to get db users: %w", err)
}

defer rows.Close()
defer func() {
_ = rows.Close()
}()

type tempStruct struct {
User
Expand All @@ -171,6 +175,7 @@ func (s *Store) getUsers(ctx context.Context, size, page int, search, sortBy, di

var temp tempStruct

//nolint:musttag
err = rows.StructScan(&temp)
if err != nil {
return nil, -1, fmt.Errorf("failed to get db users: %w", err)
Expand Down Expand Up @@ -291,6 +296,7 @@ func (s *Store) getRolesForUser(ctx context.Context, u User) ([]role.Role, error
func (s *Store) getUsersForRole(ctx context.Context, r role.Role) ([]User, error) {
var u []User

//nolint:musttag
err := s.db.SelectContext(ctx, &u, `SELECT u.*
FROM people.users u
LEFT JOIN people.role_members rm ON rm.user_id = u.user_id
Expand Down Expand Up @@ -321,6 +327,7 @@ func (s *Store) getRoleUser(ctx context.Context, ru1 RoleUser) (RoleUser, error)
func (s *Store) getUsersNotInRole(ctx context.Context, r role.Role) ([]User, error) {
var u []User

//nolint:musttag
err := s.db.SelectContext(ctx, &u, `SELECT DISTINCT u.*
FROM people.users u
WHERE user_id NOT IN
Expand Down
25 changes: 13 additions & 12 deletions user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package user

import (
"context"
"errors"
"fmt"
"time"

"github.com/ystv/web-auth/permission"
"github.com/ystv/web-auth/role"

"github.com/Clarilab/gocloaksession"
"github.com/jmoiron/sqlx"
"github.com/ystv/web-auth/utils"
"gopkg.in/guregu/null.v4"

"github.com/ystv/web-auth/permission"
"github.com/ystv/web-auth/role"
"github.com/ystv/web-auth/utils"
)

type (
Expand Down Expand Up @@ -153,17 +154,17 @@ func (s *Store) GetUserValid(ctx context.Context, u User) (User, error) {
}

if !user.Enabled {
return u, fmt.Errorf("user not enabled, contact Computing Team for help")
return u, errors.New("user not enabled, contact Computing Team for help")
}

if user.DeletedBy.Valid {
return u, fmt.Errorf("user has been deleted, contact Computing Team for help")
return u, errors.New("user has been deleted, contact Computing Team for help")
}

if user.ResetPw {
u.UserID = user.UserID

return u, fmt.Errorf("password reset required")
return u, errors.New("password reset required")
}

return user, nil
Expand All @@ -184,31 +185,31 @@ func (s *Store) VerifyUser(ctx context.Context, u User) (User, bool, error) {
}

if !user.Enabled {
return u, false, fmt.Errorf("user not enabled, contact Computing Team for help")
return u, false, errors.New("user not enabled, contact Computing Team for help")
}

if user.DeletedBy.Valid {
return u, false, fmt.Errorf("user has been deleted, contact Computing Team for help")
return u, false, errors.New("user has been deleted, contact Computing Team for help")
}

if utils.HashPass(user.Salt.String+u.Password.String) == user.Password.String {
if user.ResetPw {
u.UserID = user.UserID

return user, true, fmt.Errorf("password reset required")
return user, true, errors.New("password reset required")
}

return user, false, nil
}

return u, false, fmt.Errorf("invalid credentials")
return u, false, errors.New("invalid credentials")
}

// AddUser adds a new User
func (s *Store) AddUser(ctx context.Context, u User, userID int) (User, error) {
_, err := s.GetUser(ctx, u)
if err == nil {
return User{}, fmt.Errorf("failed to add user for addUser: user already exists")
return User{}, errors.New("failed to add user for addUser: user already exists")
}

u.Password = null.StringFrom(utils.HashPass(u.Salt.String + u.Password.String))
Expand Down
17 changes: 9 additions & 8 deletions views/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package views
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -108,7 +109,7 @@ func (v *Views) TokenAddFunc(c echo.Context) error {
expiry := c.Request().FormValue("expiry")

if len(name) < 2 {
return fmt.Errorf("token name too short")
return errors.New("token name too short")
}

id := uuid.NewString()
Expand All @@ -120,7 +121,7 @@ func (v *Views) TokenAddFunc(c echo.Context) error {

diff := time.Now().Add(2 * time.Hour * 24).Compare(parse)
if diff != -1 {
return fmt.Errorf("expiry date must be more than 2 days away")
return errors.New("expiry date must be more than 2 days away")
}

t := api.Token{
Expand Down Expand Up @@ -161,7 +162,7 @@ func (v *Views) TokenDeleteFunc(c echo.Context) error {

tokenID := c.Param("tokenid")
if len(tokenID) != 36 {
return fmt.Errorf("failed to parse tokenid for tokenDelete: tokenid is the incorrect length")
return errors.New("failed to parse tokenid for tokenDelete: tokenid is the incorrect length")
}

token1, err := v.api.GetToken(c.Request().Context(), api.Token{TokenID: tokenID})
Expand All @@ -170,7 +171,7 @@ func (v *Views) TokenDeleteFunc(c echo.Context) error {
}

if token1.UserID != c1.User.UserID {
return fmt.Errorf("failed to get token in tokenDelete: unauthorized")
return errors.New("failed to get token in tokenDelete: unauthorized")
}

err = v.api.DeleteToken(c.Request().Context(), token1)
Expand Down Expand Up @@ -278,7 +279,7 @@ func (v *Views) newJWT(u user.User) (string, error) {
func (v *Views) newJWTCustom(u user.User, expiry time.Time, tokenID string) (string, error) {
compare := expiry.Compare(time.Now().AddDate(1, 0, 0))
if compare == 1 {
return "", fmt.Errorf("expiration date is more than a year away, can only have a maximum of 1 year")
return "", errors.New("expiration date is more than a year away, can only have a maximum of 1 year")
}

perms, err := v.user.GetPermissionsForUser(context.Background(), u)
Expand Down Expand Up @@ -402,20 +403,20 @@ func (v *Views) TestAPITokenFunc(c echo.Context) error {

// ValidateToken will validate the token
func (v *Views) ValidateToken(token string) (bool, *JWTClaims, error) {
parsedToken, err := jwt.ParseWithClaims(token, &JWTClaims{}, func(token *jwt.Token) (interface{}, error) {
parsedToken, err := jwt.ParseWithClaims(token, &JWTClaims{}, func(_ *jwt.Token) (interface{}, error) {
return []byte(v.conf.Security.SigningKey), nil
})
if err != nil {
return false, nil, fmt.Errorf("failed to parse token: %w", err)
}

if !parsedToken.Valid {
return false, nil, fmt.Errorf("failed to validate token: invalid token")
return false, nil, errors.New("failed to validate token: invalid token")
}

claims, ok := parsedToken.Claims.(*JWTClaims)
if !ok {
return false, nil, fmt.Errorf("failed to validate token: invalid token claim")
return false, nil, errors.New("failed to validate token: invalid token claim")
}

if len(claims.ID) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion views/changePassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (v *Views) ChangePasswordFunc(c echo.Context) error {

errString := minRequirementsMet(password)
if len(errString) > 0 {
message.Error = fmt.Sprintf("new password doesn't meet the old requirements: %s", errString)
message.Error = "new password doesn't meet the old requirements: " + errString

return c.JSON(status, message)
}
Expand Down
7 changes: 4 additions & 3 deletions views/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import (
_ "time/tzdata"

"github.com/labstack/echo/v4"
"gopkg.in/guregu/null.v4"

"github.com/ystv/web-auth/permission"
"github.com/ystv/web-auth/user"
"gopkg.in/guregu/null.v4"
)

type (
Expand Down Expand Up @@ -283,13 +284,13 @@ func DBUserToDetailedUser(dbUser user.User, store *user.Store) user.DetailedUser
u.UseGravatar = true
// #nosec
hash := md5.Sum([]byte(strings.ToLower(strings.TrimSpace(u.Email))))
u.Avatar = fmt.Sprintf("https://www.gravatar.com/avatar/%s", hex.EncodeToString(hash[:]))
u.Avatar = "https://www.gravatar.com/avatar/" + hex.EncodeToString(hash[:])
} else {
u.UseGravatar = false
if len(dbUser.Avatar) == 0 {
u.Avatar = "https://placehold.it/128x128"
} else {
u.Avatar = fmt.Sprintf("/avatar/%s", dbUser.Avatar)
u.Avatar = "/avatar/" + dbUser.Avatar
}
}

Expand Down
3 changes: 1 addition & 2 deletions views/index.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package views

import (
"fmt"
"net/http"
"net/url"
"strings"
Expand All @@ -25,7 +24,7 @@ func (v *Views) IndexFunc(c echo.Context) error {
return c.Redirect(http.StatusFound, c1.Callback)
}

loginCallback := fmt.Sprintf("login?callback=%s", c1.Callback)
loginCallback := "login?callback=" + c1.Callback

return c.Redirect(http.StatusFound, loginCallback)
}
6 changes: 4 additions & 2 deletions views/middleware.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package views

import (
"errors"
"fmt"
"log"
"net/http"

"github.com/labstack/echo/v4"

"github.com/ystv/web-auth/infrastructure/permission"
"github.com/ystv/web-auth/permission/permissions"
"github.com/ystv/web-auth/user"
Expand Down Expand Up @@ -137,7 +139,7 @@ func (v *Views) RequirePermission(p permissions.Permissions) echo.MiddlewareFunc
return func(c echo.Context) error {
c1 := v.getSessionData(c)
if c1 == nil {
return fmt.Errorf("failed to get session data")
return errors.New("failed to get session data")
}

perms, err := v.user.GetPermissionsForUser(c.Request().Context(), c1.User)
Expand All @@ -153,7 +155,7 @@ func (v *Views) RequirePermission(p permissions.Permissions) echo.MiddlewareFunc
}
}

return echo.NewHTTPError(http.StatusForbidden, fmt.Errorf("you are not authorised for accessing this"))
return echo.NewHTTPError(http.StatusForbidden, errors.New("you are not authorised for accessing this"))
}
}
}
5 changes: 3 additions & 2 deletions views/officership.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package views

import (
"errors"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -121,7 +122,7 @@ func (v *Views) OfficersFunc(c echo.Context) error {
sb.WriteString(err.Error())
}

return fmt.Errorf(sb.String())
return errors.New(sb.String())
}

p1, err := v.user.GetPermissionsForUser(c.Request().Context(), c1.User)
Expand Down Expand Up @@ -168,7 +169,7 @@ func (v *Views) OfficerAddFunc(c echo.Context) error {

diff := time.Now().Compare(parse)
if diff != 1 {
return fmt.Errorf("start date must be before today")
return errors.New("start date must be before today")
}

userID, err := strconv.Atoi(tempUserID)
Expand Down
8 changes: 5 additions & 3 deletions views/reset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package views

import (
"errors"
"fmt"
"html/template"
"log"
Expand All @@ -10,10 +11,11 @@ import (
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/patrickmn/go-cache"
"gopkg.in/guregu/null.v4"

"github.com/ystv/web-auth/infrastructure/mail"
"github.com/ystv/web-auth/templates"
"github.com/ystv/web-auth/user"
"gopkg.in/guregu/null.v4"
)

func (v *Views) ResetURLFunc(c echo.Context) error {
Expand All @@ -23,7 +25,7 @@ func (v *Views) ResetURLFunc(c echo.Context) error {

userID, found := v.cache.Get(url)
if !found {
return fmt.Errorf("failed to get url for reset")
return errors.New("failed to get url for reset")
}

originalUser, err := v.user.GetUser(c.Request().Context(), user.User{UserID: userID.(int)})
Expand Down Expand Up @@ -137,7 +139,7 @@ https://%s/reset/%s`, userFromDB.Email, v.conf.DomainName, url)
} else {
message.Message = fmt.Sprintf(`No mailer present\nPlease forward the link to this email: %s,
reset link: https://%s/reset/%s`, userFromDB.Email, v.conf.DomainName, url)
message.Error = fmt.Errorf("no mailer present")
message.Error = errors.New("no mailer present")
log.Printf("no Mailer present")
log.Printf("password reset requested for email: %s by user: %d", userFromDB.Email, c1.User.UserID)
}
Expand Down
Loading

0 comments on commit 3c9a1e1

Please sign in to comment.