Skip to content

Commit

Permalink
feat: username normalisation (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
abbas-gheydi committed Feb 12, 2024
1 parent fb4d98d commit fb7310a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
19 changes: 12 additions & 7 deletions pkgs/authentiate/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,28 @@ func (l LdapProvider) isUserAuthorized(groups []string) bool {
return false
}

func (l LdapProvider) IsUserAuthenticated(username string, password string, checkForVendorFortinetGroup bool) (authStat bool, vendorFortinetGroupName []string) {
func (l LdapProvider) IsUserAuthenticated(username string, password string, checkForVendorFortinetGroup bool) (isAuthenticated bool, vendorFortinetGroupName []string) {

verifyPasswordAndRetrieveGroupsFromLdap := func(groups []string) (authStat bool, joinedGroupsName []string, err error) {
if strings.Contains(username, "\\") {
splitChar := "\\"
username = strings.Split(username, splitChar)[0]
}

verifyPasswordAndRetrieveGroupsFromLdap := func(groups []string) (isAuthenticated bool, joinedGroupsName []string, err error) {
ldapMutex.RLock()
defer ldapMutex.RLocker().Unlock()
authStat, _, joinedGroupsName, err = ldapAuth.AuthenticateExtended(l.LdapConfig, username, password, []string{"cn"}, groups)
isAuthenticated, _, joinedGroupsName, err = ldapAuth.AuthenticateExtended(l.LdapConfig, username, password, []string{"cn"}, groups)
return
}

authStat, joinedGroupsName, err := verifyPasswordAndRetrieveGroupsFromLdap(l.LdapGroupsFilter)
isAuthenticated, joinedGroupsName, err := verifyPasswordAndRetrieveGroupsFromLdap(l.LdapGroupsFilter)

if authStat {
authStat = l.isUserAuthorized(joinedGroupsName)
if isAuthenticated {
isAuthenticated = l.isUserAuthorized(joinedGroupsName)
}

if checkForVendorFortinetGroup {
if authStat {
if isAuthenticated {
_, vendorFortinetGroupName, err = verifyPasswordAndRetrieveGroupsFromLdap(l.FortiGroups)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/rad/safe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"unicode"
)

var usernameRegexp = regexp.MustCompile(`^[0-9A-Za-z_.@\-]{1,30}$`)
var usernameRegexp = regexp.MustCompile(`^[0-9A-Za-z_.\-]{1,30}[@|\\]?[0-9A-Za-z_.\-]{1,30}$`)

func IsOtpCodeSafe(input string) bool {
if len([]rune(input)) != 6 {
Expand Down
29 changes: 28 additions & 1 deletion pkgs/storage/postgres_otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package storage

import (
"errors"
"fmt"
"log"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -33,6 +35,16 @@ type otps struct {
type postgresOtp struct{}

func (p postgresOtp) Set(username string, secret string) error {
username = strings.ToLower(username)

if strings.Contains(username, "@") || strings.Contains(username, "\\") {
splitChar := "@"
if strings.Contains(username, "\\") {
splitChar = "\\"
}
return fmt.Errorf("username is not valid. please insert a username without %v", splitChar)

}

otpUser := otps{
Username: username,
Expand All @@ -49,6 +61,7 @@ func (p postgresOtp) Set(username string, secret string) error {
}

func (p postgresOtp) Update(username string, secret string) error {
username = strings.ToLower(username)

otpUser := otps{
Username: username,
Expand All @@ -71,6 +84,7 @@ func (p postgresOtp) Update(username string, secret string) error {
}

func (p postgresOtp) Delete(username string) error {
username = strings.ToLower(username)

otpUser := otps{Username: username}
tx := db_otp.Model(&otpUser).Where("username = ?", username).Delete(otpUser)
Expand All @@ -88,11 +102,24 @@ func (p postgresOtp) Delete(username string) error {
}

func (p postgresOtp) Get(username string) (password string, err error) {

username = strings.ToLower(username)
if strings.Contains(username, "\\") {
splitChar := "\\"
username = strings.Split(username, splitChar)[0]
}
otpUser := otps{Username: username}

tx := db_otp.First(&otpUser, "Username = ?", username)
if tx.Error != nil && strings.Contains(tx.Error.Error(), "record not found") {
if strings.Contains(username, "@") {
splitChar := "@"
username = strings.Split(username, splitChar)[0]
tx = db_otp.First(&otpUser, "Username = ?", username)
}

}
if tx.Error != nil {

return "", tx.Error
}

Expand Down

0 comments on commit fb7310a

Please sign in to comment.