Skip to content

Commit

Permalink
fix: golangci linter
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyjoygh committed Sep 22, 2024
1 parent efb631e commit 8fb0fe7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/backend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.56.1
version: v1.61.0
args: --verbose --timeout=3m
skip-cache: true

Expand Down
48 changes: 24 additions & 24 deletions server/route/api/v1/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *APIV1Service) SignIn(ctx context.Context, request *v1pb.SignInRequest)
Email: &request.Email,
})
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to find user by email %s", request.Email))
return nil, status.Errorf(codes.Internal, "failed to get user: %v", err)
}
if user == nil {
return nil, status.Errorf(codes.InvalidArgument, unmatchedEmailAndPasswordError)
Expand All @@ -53,17 +53,17 @@ func (s *APIV1Service) SignIn(ctx context.Context, request *v1pb.SignInRequest)

workspaceSecuritySetting, err := s.Store.GetWorkspaceSecuritySetting(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get workspace security setting, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to get workspace security setting: %v", err)
}
if workspaceSecuritySetting.DisallowPasswordAuth && user.Role == store.RoleUser {
return nil, status.Errorf(codes.PermissionDenied, "password authentication is not allowed")
}
if user.RowStatus == storepb.RowStatus_ARCHIVED {
return nil, status.Errorf(codes.PermissionDenied, fmt.Sprintf("user has been archived with email %s", request.Email))
return nil, status.Errorf(codes.PermissionDenied, "user has been archived")
}

if err := s.doSignIn(ctx, user, time.Now().Add(AccessTokenDuration)); err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to sign in, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to sign in: %v", err)
}
return convertUserFromStore(user), nil
}
Expand All @@ -77,7 +77,7 @@ func (s *APIV1Service) SignInWithSSO(ctx context.Context, request *v1pb.SignInWi
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_IDENTITY_PROVIDER,
})
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get workspace setting, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to get workspace setting, err: %s", err)
}
if identityProviderSetting == nil || identityProviderSetting.GetIdentityProvider() == nil {
return nil, status.Errorf(codes.InvalidArgument, "identity provider not found")
Expand All @@ -90,34 +90,34 @@ func (s *APIV1Service) SignInWithSSO(ctx context.Context, request *v1pb.SignInWi
}
}
if identityProvider == nil {
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("identity provider not found with name %s", request.IdpId))
return nil, status.Errorf(codes.InvalidArgument, "identity provider not found")
}

var userInfo *idp.IdentityProviderUserInfo
if identityProvider.Type == storepb.IdentityProvider_OAUTH2 {
oauth2IdentityProvider, err := oauth2.NewIdentityProvider(identityProvider.Config.GetOauth2())
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to create oauth2 identity provider, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to create oauth2 identity provider, err: %s", err)
}
token, err := oauth2IdentityProvider.ExchangeToken(ctx, request.RedirectUri, request.Code)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to exchange token, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to exchange token, err: %s", err)
}
userInfo, err = oauth2IdentityProvider.UserInfo(token)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get user info, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to get user info, err: %s", err)
}
}

email := userInfo.Identifier
if !util.ValidateEmail(email) {
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid email %s", email))
return nil, status.Errorf(codes.InvalidArgument, "invalid email address")
}
user, err := s.Store.GetUser(ctx, &store.FindUser{
Email: &email,
})
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to find user by email %s", email))
return nil, status.Errorf(codes.Internal, "failed to get user, err: %s", err)
}
if user == nil {
if err := s.checkSeatAvailability(ctx); err != nil {
Expand All @@ -131,32 +131,32 @@ func (s *APIV1Service) SignInWithSSO(ctx context.Context, request *v1pb.SignInWi
}
password, err := util.RandomString(20)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to generate random password, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to generate random password, err: %s", err)
}
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to generate password hash, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to generate password hash, err: %s", err)
}
userCreate.PasswordHash = string(passwordHash)
user, err = s.Store.CreateUser(ctx, userCreate)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to create user, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to create user, err: %s", err)
}
}
if user.RowStatus == storepb.RowStatus_ARCHIVED {
return nil, status.Errorf(codes.PermissionDenied, fmt.Sprintf("user has been archived with email %s", email))
return nil, status.Errorf(codes.PermissionDenied, "user has been archived")
}

if err := s.doSignIn(ctx, user, time.Now().Add(AccessTokenDuration)); err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to sign in, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to sign in, err: %s", err)
}
return convertUserFromStore(user), nil
}

func (s *APIV1Service) SignUp(ctx context.Context, request *v1pb.SignUpRequest) (*v1pb.User, error) {
workspaceSecuritySetting, err := s.Store.GetWorkspaceSecuritySetting(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get workspace security setting, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to get workspace security setting: %v", err)
}
if workspaceSecuritySetting.DisallowUserRegistration {
return nil, status.Errorf(codes.PermissionDenied, "sign up is not allowed")
Expand All @@ -169,7 +169,7 @@ func (s *APIV1Service) SignUp(ctx context.Context, request *v1pb.SignUpRequest)

passwordHash, err := bcrypt.GenerateFromPassword([]byte(request.Password), bcrypt.DefaultCost)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to generate password hash, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to generate password hash: %v", err)
}

create := &store.User{
Expand All @@ -179,7 +179,7 @@ func (s *APIV1Service) SignUp(ctx context.Context, request *v1pb.SignUpRequest)
}
existingUsers, err := s.Store.ListUsers(ctx, &store.FindUser{})
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to list users, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to list users: %v", err)
}
// The first user to sign up is an admin by default.
if len(existingUsers) == 0 {
Expand All @@ -190,21 +190,21 @@ func (s *APIV1Service) SignUp(ctx context.Context, request *v1pb.SignUpRequest)

user, err := s.Store.CreateUser(ctx, create)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to create user, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to create user: %v", err)
}
if err := s.doSignIn(ctx, user, time.Now().Add(AccessTokenDuration)); err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to sign in, err: %s", err))
return nil, status.Errorf(codes.Internal, "failed to sign in: %v", err)
}
return convertUserFromStore(user), nil
}

func (s *APIV1Service) doSignIn(ctx context.Context, user *store.User, expireTime time.Time) error {
accessToken, err := GenerateAccessToken(user.Email, user.ID, expireTime, []byte(s.Secret))
if err != nil {
return status.Errorf(codes.Internal, fmt.Sprintf("failed to generate tokens, err: %s", err))
return status.Errorf(codes.Internal, "failed to generate access token: %v", err)
}
if err := s.UpsertAccessTokenToStore(ctx, user, accessToken, "user login"); err != nil {
return status.Errorf(codes.Internal, fmt.Sprintf("failed to upsert access token to store, err: %s", err))
return status.Errorf(codes.Internal, "failed to upsert access token to store: %v", err)
}

cookie := fmt.Sprintf("%s=%s; Path=/; Expires=%s; HttpOnly; SameSite=Strict", AccessTokenCookieName, accessToken, time.Now().Add(AccessTokenDuration).Format(time.RFC1123))
Expand All @@ -231,7 +231,7 @@ func (s *APIV1Service) checkSeatAvailability(ctx context.Context) error {
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeUnlimitedAccounts) {
userList, err := s.Store.ListUsers(ctx, &store.FindUser{})
if err != nil {
return status.Errorf(codes.Internal, fmt.Sprintf("failed to list users, err: %s", err))
return status.Errorf(codes.Internal, "failed to list users: %v", err)
}
seats := s.LicenseService.GetSubscription().Seats
if len(userList) >= int(seats) {
Expand Down

0 comments on commit 8fb0fe7

Please sign in to comment.