@@ -2,21 +2,19 @@ package auth
22
33import (
44 "fmt"
5- "slices "
5+ "strings "
66
77 "github.com/cpp-cyber/proclone/internal/ldap"
8- "github.com/cpp-cyber/proclone/internal/proxmox"
98)
109
11- func NewAuthService (proxmoxService * proxmox. ProxmoxService ) (* AuthService , error ) {
10+ func NewAuthService () (* AuthService , error ) {
1211 ldapService , err := ldap .NewLDAPService ()
1312 if err != nil {
1413 return nil , fmt .Errorf ("failed to create LDAP service: %w" , err )
1514 }
1615
1716 return & AuthService {
18- ldapService : ldapService ,
19- proxmoxService : proxmoxService ,
17+ ldapService : ldapService ,
2018 }, nil
2119}
2220
@@ -53,36 +51,76 @@ func (s *AuthService) Authenticate(username string, password string) (bool, erro
5351}
5452
5553func (s * AuthService ) IsAdmin (username string ) (bool , error ) {
56- // Get user's groups from Proxmox
57- userGroups , err := s .proxmoxService .GetUserGroups (username )
54+ // Input validation
55+ if username == "" {
56+ return false , fmt .Errorf ("username cannot be empty" )
57+ }
58+
59+ // Get user DN
60+ userDN , err := s .ldapService .GetUserDN (username )
61+ if err != nil {
62+ return false , fmt .Errorf ("failed to get user DN: %w" , err )
63+ }
64+
65+ // Get user's groups
66+ userGroups , err := s .ldapService .GetUserGroups (userDN )
5867 if err != nil {
5968 return false , fmt .Errorf ("failed to get user groups: %w" , err )
6069 }
6170
62- // Get the admin group name from config
63- adminGroupName := s .proxmoxService .Config .AdminGroupName
71+ // Load LDAP config to get admin group DN
72+ config , err := ldap .LoadConfig ()
73+ if err != nil {
74+ return false , fmt .Errorf ("failed to load LDAP config: %w" , err )
75+ }
76+
77+ if config .AdminGroupDN == "" {
78+ return false , fmt .Errorf ("admin group DN not configured" )
79+ }
6480
6581 // Check if user is in the admin group
66- if slices .Contains (userGroups , adminGroupName ) {
67- return true , nil
82+ for _ , groupDN := range userGroups {
83+ if strings .EqualFold (groupDN , "Proxmox-Admins" ) {
84+ return true , nil
85+ }
6886 }
6987
7088 return false , nil
7189}
7290
7391func (s * AuthService ) IsCreator (username string ) (bool , error ) {
74- // Get user's groups from Proxmox
75- userGroups , err := s .proxmoxService .GetUserGroups (username )
92+ // Input validation
93+ if username == "" {
94+ return false , fmt .Errorf ("username cannot be empty" )
95+ }
96+
97+ // Get user DN
98+ userDN , err := s .ldapService .GetUserDN (username )
99+ if err != nil {
100+ return false , fmt .Errorf ("failed to get user DN: %w" , err )
101+ }
102+
103+ // Get user's groups
104+ userGroups , err := s .ldapService .GetUserGroups (userDN )
76105 if err != nil {
77106 return false , fmt .Errorf ("failed to get user groups: %w" , err )
78107 }
79108
80- // Get the creator group name from config
81- creatorGroupName := s .proxmoxService .Config .CreatorGroupName
109+ // Load LDAP config to get creator group DN
110+ config , err := ldap .LoadConfig ()
111+ if err != nil {
112+ return false , fmt .Errorf ("failed to load LDAP config: %w" , err )
113+ }
114+
115+ if config .CreatorGroupDN == "" {
116+ return false , fmt .Errorf ("creator group DN not configured" )
117+ }
82118
83119 // Check if user is in the creator group
84- if slices .Contains (userGroups , creatorGroupName ) {
85- return true , nil
120+ for _ , groupDN := range userGroups {
121+ if strings .EqualFold (groupDN , config .CreatorGroupDN ) {
122+ return true , nil
123+ }
86124 }
87125
88126 return false , nil
0 commit comments