Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create user validation #1076

Merged
merged 5 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions server/memphis_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"memphis/models"
"memphis/utils"
"net/http"
"regexp"
"runtime"
"sort"
"strconv"
Expand Down Expand Up @@ -1215,10 +1214,34 @@ func (umh UserMgmtHandler) AddUser(c *gin.Context) {

var subscription, pending bool
team := strings.ToLower(body.Team)
teamError := validateUserTeam(team)
if teamError != nil {
serv.Warnf("[tenant: %v][user: %v]AddUser at validateUserTeam: %v", user.TenantName, user.Username, teamError.Error())
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": teamError.Error()})
return
}
position := strings.ToLower(body.Position)
positionError := validateUserPosition(position)
if positionError != nil {
serv.Warnf("[tenant: %v][user: %v]AddUser at validateUserPosition: %v", user.TenantName, user.Username, positionError.Error())
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": positionError.Error()})
return
}
fullName := strings.ToLower(body.FullName)
fullNameError := validateUserFullName(fullName)
if fullNameError != nil {
serv.Warnf("[tenant: %v][user: %v]AddUser at validateUserFullName: %v", user.TenantName, user.Username, fullNameError.Error())
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": fullNameError.Error()})
return
}
owner := user.Username
description := strings.ToLower(body.Description)
descriptionError := validateUserDescription(description)
if descriptionError != nil {
serv.Warnf("[tenant: %v][user: %v]AddUser at validateUserDescription: %v", user.TenantName, user.Username, descriptionError.Error())
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": descriptionError.Error()})
return
}

user.TenantName = strings.ToLower(user.TenantName)
username := strings.ToLower(body.Username)
Expand Down Expand Up @@ -1254,13 +1277,20 @@ func (umh UserMgmtHandler) AddUser(c *gin.Context) {
avatarId = body.AvatarId
}

if body.Password == "" {
serv.Warnf("[tenant: %v][user: %v]AddUser: Password was not provided for user %v", user.TenantName, user.Username, username)
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": "Password was not provided"})
return
}
passwordErr := validatePassword(body.Password)
if passwordErr != nil {
serv.Warnf("[tenant: %v][user: %v]AddUser validate password : User %v: %v", user.TenantName, user.Username, body.Username, passwordErr.Error())
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": passwordErr.Error()})
return
}

var password string
if userType == "management" {
if body.Password == "" {
serv.Warnf("[tenant: %v][user: %v]AddUser: Password was not provided for user %v", user.TenantName, user.Username, username)
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": "Password was not provided"})
return
}

hashedPwd, err := bcrypt.GenerateFromPassword([]byte(body.Password), bcrypt.MinCost)
if err != nil {
Expand All @@ -1282,12 +1312,6 @@ func (umh UserMgmtHandler) AddUser(c *gin.Context) {
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": "Password was not provided"})
return
}
err = validatePassword(body.Password)
if err != nil {
serv.Warnf("[tenant: %v][user: %v]AddUser validate password : User %v: %v", user.TenantName, user.Username, body.Username, err.Error())
c.AbortWithStatusJSON(SHOWABLE_ERROR_STATUS_CODE, gin.H{"message": err.Error()})
return
}
password, err = EncryptAES([]byte(body.Password))
if err != nil {
serv.Errorf("[tenant: %v][user: %v]AddUser at EncryptAES: User %v: %v", user.TenantName, user.Username, body.Username, err.Error())
Expand Down Expand Up @@ -1413,16 +1437,6 @@ func (umh UserMgmtHandler) RemoveUser(c *gin.Context) {
c.IndentedJSON(200, gin.H{})
}

func validateUsername(username string) error {
re := regexp.MustCompile("^[a-z0-9_.-]*$")

validName := re.MatchString(username)
if !validName || len(username) == 0 {
return errors.New("username has to include only letters/numbers/./_/- ")
}
return nil
}

func (umh UserMgmtHandler) RemoveMyUser(c *gin.Context) {
user, err := getUserDetailsFromMiddleware(c)
if err != nil {
Expand Down
80 changes: 80 additions & 0 deletions server/memphis_handlers_user_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strconv"
"strings"
"time"
"unicode"

"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
Expand Down Expand Up @@ -777,3 +778,82 @@ func (umh UserMgmtHandler) GetFilterDetails(c *gin.Context) {
return
}
}

func validateUsername(username string) error {
if len(username) > 20 {
return errors.New("username exceeds the maximum allowed length of 20 characters")
}
re := regexp.MustCompile("^[a-z0-9_.-]*$")
validName := re.MatchString(username)
if !validName || len(username) == 0 {
return errors.New("username has to include only letters/numbers/./_/- ")
}
return nil
}

func validateUserDescription(description string) error {
if len(description) > 100 {
return errors.New("description exceeds the maximum allowed length of 100 characters")
}
return nil
}

func validateUserTeam(team string) error {
if len(team) > 20 {
return errors.New("team exceeds the maximum allowed length of 20 characters")
}
return nil
}

func validateUserPosition(position string) error {
if len(position) > 30 {
return errors.New("position exceeds the maximum allowed length of 30 characters")
}
return nil
}

func validateUserFullName(fullName string) error {
if len(fullName) > 30 {
return errors.New("full name exceeds the maximum allowed length of 30 characters")
}
return nil
}

func validatePassword(password string) error {
if len(password) > 20 {
return errors.New("password exceeds the maximum allowed length of 20 characters")
}
pattern := `^[A-Za-z0-9!?\-@#$%]+$`
match, _ := regexp.MatchString(pattern, password)
if !match {
return errors.New("Password must be at least 8 characters long, contain both uppercase and lowercase, and at least one number and one special character")
}
if len(password) < 8 {
return errors.New("Password must be at least 8 characters long, contain both uppercase and lowercase, and at least one number and one special character")
}
var (
hasUppercase bool
hasLowercase bool
hasDigit bool
hasSpecialChar bool
)

for _, char := range password {
switch {
case unicode.IsUpper(char):
hasUppercase = true
case unicode.IsLower(char):
hasLowercase = true
case unicode.IsDigit(char):
hasDigit = true
case char == '!' || char == '?' || char == '-' || char == '@' || char == '#' || char == '$' || char == '%':
hasSpecialChar = true
}
}

if hasUppercase && hasLowercase && hasDigit && hasSpecialChar {
return nil
}

return errors.New("Password must be at least 8 characters long, contain both uppercase and lowercase, and at least one number and one special character")
}
38 changes: 0 additions & 38 deletions server/memphis_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ import (
"memphis/models"
"net/textproto"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode"

"github.com/gofrs/uuid"
"github.com/nats-io/nuid"
Expand Down Expand Up @@ -1455,39 +1453,3 @@ func (s *Server) MoveResourcesFromOldToNewDefaultAcc() error {
}
return nil
}

func validatePassword(password string) error {
pattern := `^[A-Za-z0-9!?\-@#$%]+$`
match, _ := regexp.MatchString(pattern, password)
if !match {
return errors.New("Password must be at least 8 characters long, contain both uppercase and lowercase, and at least one number and one special character")
}
if len(password) < 8 {
return errors.New("Password must be at least 8 characters long, contain both uppercase and lowercase, and at least one number and one special character")
}
var (
hasUppercase bool
hasLowercase bool
hasDigit bool
hasSpecialChar bool
)

for _, char := range password {
switch {
case unicode.IsUpper(char):
hasUppercase = true
case unicode.IsLower(char):
hasLowercase = true
case unicode.IsDigit(char):
hasDigit = true
case char == '!' || char == '?' || char == '-' || char == '@' || char == '#' || char == '$' || char == '%':
hasSpecialChar = true
}
}

if hasUppercase && hasLowercase && hasDigit && hasSpecialChar {
return nil
}

return errors.New("Password must be at least 8 characters long, contain both uppercase and lowercase, and at least one number and one special character")
}
7 changes: 7 additions & 0 deletions ui_src/src/domain/users/createUserDetails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ const CreateUserDetails = ({ createUserRef, closeModal, handleLoader }) => {
placeholder={userType === 'management' && isCloud() ? 'Type email' : 'Type username'}
type="text"
radiusType="semi-round"
maxLength={20}
colorType="black"
backgroundColorType="none"
borderColorType="gray"
Expand Down Expand Up @@ -175,6 +176,7 @@ const CreateUserDetails = ({ createUserRef, closeModal, handleLoader }) => {
<Input
placeholder="Type full name"
type="text"
maxLength={30}
radiusType="semi-round"
colorType="black"
backgroundColorType="none"
Expand All @@ -194,6 +196,7 @@ const CreateUserDetails = ({ createUserRef, closeModal, handleLoader }) => {
<Input
placeholder="Type your team"
type="text"
maxLength={20}
radiusType="semi-round"
colorType="black"
backgroundColorType="none"
Expand All @@ -212,6 +215,7 @@ const CreateUserDetails = ({ createUserRef, closeModal, handleLoader }) => {
<Input
placeholder="Type your position"
type="text"
maxLength={30}
radiusType="semi-round"
colorType="black"
backgroundColorType="none"
Expand All @@ -235,6 +239,7 @@ const CreateUserDetails = ({ createUserRef, closeModal, handleLoader }) => {
<Input
placeholder="Type your description"
type="text"
maxLength={100}
radiusType="semi-round"
colorType="black"
backgroundColorType="none"
Expand Down Expand Up @@ -307,6 +312,7 @@ const CreateUserDetails = ({ createUserRef, closeModal, handleLoader }) => {
<Input
placeholder="Type Password"
type="password"
maxLength={20}
radiusType="semi-round"
colorType="black"
backgroundColorType="none"
Expand Down Expand Up @@ -341,6 +347,7 @@ const CreateUserDetails = ({ createUserRef, closeModal, handleLoader }) => {
<Input
placeholder="Type Password"
type="password"
maxLength={20}
radiusType="semi-round"
colorType="black"
backgroundColorType="none"
Expand Down
6 changes: 2 additions & 4 deletions ui_static_files/build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": {
"main.css": "/static/css/main.3d4f795d.css",
"main.js": "/static/js/main.3cb50e81.js",
"main.js": "/static/js/main.9c84a1f8.js",
"static/js/617.a5f8c4fc.chunk.js": "/static/js/617.a5f8c4fc.chunk.js",
"static/js/2542.27de8743.chunk.js": "/static/js/2542.27de8743.chunk.js",
"static/js/1737.e134cfd4.chunk.js": "/static/js/1737.e134cfd4.chunk.js",
Expand Down Expand Up @@ -116,7 +116,6 @@
"static/media/noSchemasFound.svg": "/static/media/noSchemasFound.2997919164772bdba31339efbe98cedc.svg",
"static/media/pagerDutyIcon.svg": "/static/media/pagerDutyIcon.3125f9535963570c1b3fcffb2fc31cf4.svg",
"static/media/uptodateIcon.svg": "/static/media/uptodateIcon.9e422510ad1f8c01f74e90ce91c9817b.svg",
"static/media/rdk.umd.cjs": "/static/media/rdk.umd.a7e14d5d64e2a94eec3a.cjs",
"static/media/noStations.svg": "/static/media/noStations.8dc1367d04b5a51dd65e12292ceddd68.svg",
"static/media/elasticBanner.webp": "/static/media/elasticBanner.5137ba7446b120da38b2.webp",
"static/media/grafanaBannerPopup.webp": "/static/media/grafanaBannerPopup.04ea20c13ab4520bc130.webp",
Expand Down Expand Up @@ -181,7 +180,6 @@
"static/media/dls_enable_icon.svg": "/static/media/dls_enable_icon.03a3706804e0d1fc7b44336af88b1314.svg",
"static/media/finishFlag.svg": "/static/media/finishFlag.cdd2d450a2a93dbfa465c9e00803394f.svg",
"static/media/grayFinish.svg": "/static/media/grayFinish.5ebcc02830185386ebc7d136451079dd.svg",
"static/media/index.umd.cjs": "/static/media/index.umd.411bcbb098030aa2d966.cjs",
"static/media/file_download.svg": "/static/media/file_download.7010ceaf1a68c3e0941959b6f558ee5b.svg",
"static/media/deadLetter.svg": "/static/media/deadLetter.0806dd1e9dce011b6b909e31634b0311.svg",
"static/media/errorindication.svg": "/static/media/errorindication.6f6e388f5a1f899e9d6c810addcaf2d9.svg",
Expand Down Expand Up @@ -276,6 +274,6 @@
},
"entrypoints": [
"static/css/main.3d4f795d.css",
"static/js/main.3cb50e81.js"
"static/js/main.9c84a1f8.js"
]
}
2 changes: 1 addition & 1 deletion ui_static_files/build/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Memphis.dev console is designed to simplify your work and give you a graphical user interface for controlling your stations, security, integrations, and observing your data and other vital metrics"/><link rel="manifest" href="/manifest.json"/><title>Memphis.dev Console</title><link rel="apple-touch-icon" sizes="57x57" href="/img/favicon/apple-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/img/favicon/apple-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/img/favicon/apple-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/img/favicon/apple-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/img/favicon/apple-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/img/favicon/apple-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/img/favicon/apple-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/img/favicon/apple-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/img/favicon/apple-icon-180x180.png"><link rel="icon" type="image/png" sizes="192x192" href="/img/favicon/android-icon-192x192.png"><link rel="icon" type="image/png" sizes="32x32" href="/img/favicon/favicon-32x32.png"><link rel="icon" type="image/png" sizes="96x96" href="/img/favicon/favicon-96x96.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/favicon/favicon-16x16.png"><meta name="msapplication-TileColor" content="#ffffff"><meta name="msapplication-TileImage" content="/img/favicon/ms-icon-144x144.png"><meta name="theme-color" content="#ffffff"><script defer="defer" src="/static/js/main.3cb50e81.js"></script><link href="/static/css/main.3d4f795d.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Memphis.dev console is designed to simplify your work and give you a graphical user interface for controlling your stations, security, integrations, and observing your data and other vital metrics"/><link rel="manifest" href="/manifest.json"/><title>Memphis.dev Console</title><link rel="apple-touch-icon" sizes="57x57" href="/img/favicon/apple-icon-57x57.png"><link rel="apple-touch-icon" sizes="60x60" href="/img/favicon/apple-icon-60x60.png"><link rel="apple-touch-icon" sizes="72x72" href="/img/favicon/apple-icon-72x72.png"><link rel="apple-touch-icon" sizes="76x76" href="/img/favicon/apple-icon-76x76.png"><link rel="apple-touch-icon" sizes="114x114" href="/img/favicon/apple-icon-114x114.png"><link rel="apple-touch-icon" sizes="120x120" href="/img/favicon/apple-icon-120x120.png"><link rel="apple-touch-icon" sizes="144x144" href="/img/favicon/apple-icon-144x144.png"><link rel="apple-touch-icon" sizes="152x152" href="/img/favicon/apple-icon-152x152.png"><link rel="apple-touch-icon" sizes="180x180" href="/img/favicon/apple-icon-180x180.png"><link rel="icon" type="image/png" sizes="192x192" href="/img/favicon/android-icon-192x192.png"><link rel="icon" type="image/png" sizes="32x32" href="/img/favicon/favicon-32x32.png"><link rel="icon" type="image/png" sizes="96x96" href="/img/favicon/favicon-96x96.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/favicon/favicon-16x16.png"><meta name="msapplication-TileColor" content="#ffffff"><meta name="msapplication-TileImage" content="/img/favicon/ms-icon-144x144.png"><meta name="theme-color" content="#ffffff"><script defer="defer" src="/static/js/main.9c84a1f8.js"></script><link href="/static/css/main.3d4f795d.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ object-assign
http://jedwatson.github.io/classnames
*/

/*!
Copyright (c) 2015 Jed Watson.
Based on code that is Copyright 2013-2015, Facebook, Inc.
All rights reserved.
*/

/*!
* Chart.js v2.9.4
* https://www.chartjs.org
Expand Down Expand Up @@ -398,6 +404,31 @@ object-assign

/** @license URI.js v4.4.1 (c) 2011 Gary Court. License: http://github.com/garycourt/uri-js */

/**!
* @fileOverview Kickass library to create and place poppers near their reference elements.
* @version 1.16.1
* @license
* Copyright (c) 2016 Federico Zivolo and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

//! goToAndStop must be relative to the start of the current segment

//! moment.js
Loading