Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
0689ee46fd | |||
ca0832a010 | |||
5d41b605b9 | |||
03177eb4d9 | |||
95ad75b20d | |||
8cefdb0b01 | |||
eacc349cac | |||
bf0596d385 |
61
app/app.go
61
app/app.go
@@ -15,16 +15,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var LDAPSessions map[string]*LDAPClient
|
var LDAPSessions map[string]*LDAPClient
|
||||||
var APIVersion = "1.0.0"
|
var APIVersion = "1.0.4"
|
||||||
|
|
||||||
func Run() {
|
func Run() {
|
||||||
gob.Register(LDAPClient{})
|
gob.Register(LDAPClient{})
|
||||||
|
|
||||||
|
log.Printf("Starting ProxmoxAAS-LDAP version %s\n", APIVersion)
|
||||||
|
|
||||||
configPath := flag.String("config", "config.json", "path to config.json file")
|
configPath := flag.String("config", "config.json", "path to config.json file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
config := GetConfig(*configPath)
|
config, err := GetConfig(*configPath)
|
||||||
log.Println("Initialized config from " + *configPath)
|
if err != nil {
|
||||||
|
log.Fatal("Error when reading config file: ", err)
|
||||||
|
}
|
||||||
|
log.Printf("Read in config from %s\n", *configPath)
|
||||||
|
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
@@ -37,6 +42,8 @@ func Run() {
|
|||||||
})
|
})
|
||||||
router.Use(sessions.Sessions(config.SessionCookieName, store))
|
router.Use(sessions.Sessions(config.SessionCookieName, store))
|
||||||
|
|
||||||
|
log.Printf("Started API router and cookie store (Name: %s Params: %+v)\n", config.SessionCookieName, config.SessionCookie)
|
||||||
|
|
||||||
LDAPSessions = make(map[string]*LDAPClient)
|
LDAPSessions = make(map[string]*LDAPClient)
|
||||||
|
|
||||||
router.GET("/version", func(c *gin.Context) {
|
router.GET("/version", func(c *gin.Context) {
|
||||||
@@ -105,7 +112,7 @@ func Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
status, res := LDAPSession.GetAllUsers()
|
status, res := LDAPSession.GetAllUsers()
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/users/:userid", func(c *gin.Context) {
|
router.POST("/users/:userid", func(c *gin.Context) {
|
||||||
@@ -122,20 +129,24 @@ func Run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var body User
|
|
||||||
if err := c.ShouldBind(&body); err != nil { // bad request from binding
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"auth": false, "error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if user already exists
|
// check if user already exists
|
||||||
status, res := LDAPSession.GetUser(c.Param("userid"))
|
status, res := LDAPSession.GetUser(c.Param("userid"))
|
||||||
if status != 200 && ldap.IsErrorWithCode(res["error"].(error), ldap.LDAPResultNoSuchObject) { // user does not already exist, create new user
|
if status != 200 && ldap.IsErrorWithCode(res["error"].(error), ldap.LDAPResultNoSuchObject) { // user does not already exist, create new user
|
||||||
|
var body UserRequired // all user attributes required for new users
|
||||||
|
if err := c.ShouldBind(&body); err != nil { // attempt to bind user data
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"auth": false, "error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
status, res = LDAPSession.AddUser(c.Param("userid"), body)
|
status, res = LDAPSession.AddUser(c.Param("userid"), body)
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
} else { // user already exists, attempt to modify user
|
} else { // user already exists, attempt to modify user
|
||||||
|
var body UserOptional // all user attributes optional for new users
|
||||||
|
if err := c.ShouldBind(&body); err != nil { // attempt to bind user data
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"auth": false, "error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
status, res = LDAPSession.ModUser(c.Param("userid"), body)
|
status, res = LDAPSession.ModUser(c.Param("userid"), body)
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -154,7 +165,7 @@ func Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
status, res := LDAPSession.GetUser(c.Param("userid"))
|
status, res := LDAPSession.GetUser(c.Param("userid"))
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
})
|
})
|
||||||
|
|
||||||
router.DELETE("/users/:userid", func(c *gin.Context) {
|
router.DELETE("/users/:userid", func(c *gin.Context) {
|
||||||
@@ -172,7 +183,7 @@ func Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
status, res := LDAPSession.DelUser(c.Param("userid"))
|
status, res := LDAPSession.DelUser(c.Param("userid"))
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
})
|
})
|
||||||
|
|
||||||
router.GET("/groups", func(c *gin.Context) {
|
router.GET("/groups", func(c *gin.Context) {
|
||||||
@@ -190,7 +201,7 @@ func Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
status, res := LDAPSession.GetAllGroups()
|
status, res := LDAPSession.GetAllGroups()
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
})
|
})
|
||||||
|
|
||||||
router.GET("/groups/:groupid", func(c *gin.Context) {
|
router.GET("/groups/:groupid", func(c *gin.Context) {
|
||||||
@@ -208,7 +219,7 @@ func Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
status, res := LDAPSession.GetGroup(c.Param("groupid"))
|
status, res := LDAPSession.GetGroup(c.Param("groupid"))
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/groups/:groupid", func(c *gin.Context) {
|
router.POST("/groups/:groupid", func(c *gin.Context) {
|
||||||
@@ -231,14 +242,14 @@ func Run() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if user already exists
|
// check if group already exists
|
||||||
status, res := LDAPSession.GetGroup(c.Param("groupid"))
|
status, res := LDAPSession.GetGroup(c.Param("groupid"))
|
||||||
if status != 200 && ldap.IsErrorWithCode(res["error"].(error), ldap.LDAPResultNoSuchObject) { // user does not already exist, create new user
|
if status != 200 && ldap.IsErrorWithCode(res["error"].(error), ldap.LDAPResultNoSuchObject) { // group does not already exist, create new group
|
||||||
status, res = LDAPSession.AddGroup(c.Param("groupid"), body)
|
status, res = LDAPSession.AddGroup(c.Param("groupid"), body)
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
} else { // user already exists, attempt to modify user
|
} else { // group already exists, attempt to modify group
|
||||||
status, res = LDAPSession.ModGroup(c.Param("groupid"), body)
|
status, res = LDAPSession.ModGroup(c.Param("groupid"), body)
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -257,7 +268,7 @@ func Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
status, res := LDAPSession.DelGroup(c.Param("groupid"))
|
status, res := LDAPSession.DelGroup(c.Param("groupid"))
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/groups/:groupid/members/:userid", func(c *gin.Context) {
|
router.POST("/groups/:groupid/members/:userid", func(c *gin.Context) {
|
||||||
@@ -275,7 +286,7 @@ func Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
status, res := LDAPSession.AddUserToGroup(c.Param("userid"), c.Param("groupid"))
|
status, res := LDAPSession.AddUserToGroup(c.Param("userid"), c.Param("groupid"))
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
})
|
})
|
||||||
|
|
||||||
router.DELETE("/groups/:groupid/members/:userid", func(c *gin.Context) {
|
router.DELETE("/groups/:groupid/members/:userid", func(c *gin.Context) {
|
||||||
@@ -293,8 +304,10 @@ func Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
status, res := LDAPSession.DelUserFromGroup(c.Param("userid"), c.Param("groupid"))
|
status, res := LDAPSession.DelUserFromGroup(c.Param("userid"), c.Param("groupid"))
|
||||||
c.JSON(status, res)
|
c.JSON(status, HandleResponse(res))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
log.Printf("Starting LDAP API on port %s\n", strconv.Itoa(config.ListenPort))
|
||||||
|
|
||||||
router.Run("0.0.0.0:" + strconv.Itoa(config.ListenPort))
|
router.Run("0.0.0.0:" + strconv.Itoa(config.ListenPort))
|
||||||
}
|
}
|
||||||
|
144
app/ldap.go
144
app/ldap.go
@@ -1,6 +1,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -8,6 +9,7 @@ import (
|
|||||||
"github.com/go-ldap/ldap/v3"
|
"github.com/go-ldap/ldap/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LDAPClient wrapper struct containing the connection, baseDN, peopleDN, and groupsDN
|
||||||
type LDAPClient struct {
|
type LDAPClient struct {
|
||||||
client *ldap.Conn
|
client *ldap.Conn
|
||||||
basedn string
|
basedn string
|
||||||
@@ -15,6 +17,7 @@ type LDAPClient struct {
|
|||||||
groupsdn string
|
groupsdn string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns a new LDAPClient from the config
|
||||||
func NewLDAPClient(config Config) (*LDAPClient, error) {
|
func NewLDAPClient(config Config) (*LDAPClient, error) {
|
||||||
LDAPConn, err := ldap.DialURL(config.LdapURL)
|
LDAPConn, err := ldap.DialURL(config.LdapURL)
|
||||||
return &LDAPClient{
|
return &LDAPClient{
|
||||||
@@ -25,6 +28,7 @@ func NewLDAPClient(config Config) (*LDAPClient, error) {
|
|||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bind a user using username and password to the LDAPClient
|
||||||
func (l LDAPClient) BindUser(username string, password string) error {
|
func (l LDAPClient) BindUser(username string, password string) error {
|
||||||
userdn := fmt.Sprintf("uid=%s,%s", username, l.peopledn)
|
userdn := fmt.Sprintf("uid=%s,%s", username, l.peopledn)
|
||||||
return l.client.Bind(userdn, password)
|
return l.client.Bind(userdn, password)
|
||||||
@@ -34,8 +38,8 @@ func (l LDAPClient) GetAllUsers() (int, gin.H) {
|
|||||||
searchRequest := ldap.NewSearchRequest(
|
searchRequest := ldap.NewSearchRequest(
|
||||||
l.peopledn, // The base dn to search
|
l.peopledn, // The base dn to search
|
||||||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||||
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
||||||
[]string{"dn", "cn", "sn", "mail", "uid"}, // A list attributes to retrieve
|
[]string{"dn", "cn", "sn", "mail", "uid", "memberOf"}, // A list attributes to retrieve
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,15 +54,8 @@ func (l LDAPClient) GetAllUsers() (int, gin.H) {
|
|||||||
var results = []gin.H{} // create list of results
|
var results = []gin.H{} // create list of results
|
||||||
|
|
||||||
for _, entry := range searchResponse.Entries { // for each result,
|
for _, entry := range searchResponse.Entries { // for each result,
|
||||||
results = append(results, gin.H{
|
user := LDAPEntryToLDAPUser(entry)
|
||||||
"dn": entry.DN,
|
results = append(results, LDAPUserToGin(user))
|
||||||
"attributes": gin.H{
|
|
||||||
"cn": entry.GetAttributeValue("cn"),
|
|
||||||
"sn": entry.GetAttributeValue("sn"),
|
|
||||||
"mail": entry.GetAttributeValue("mail"),
|
|
||||||
"uid": entry.GetAttributeValue("uid"),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.StatusOK, gin.H{
|
return http.StatusOK, gin.H{
|
||||||
@@ -68,11 +65,43 @@ func (l LDAPClient) GetAllUsers() (int, gin.H) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l LDAPClient) AddUser(uid string, user User) (int, gin.H) {
|
func (l LDAPClient) GetUser(uid string) (int, gin.H) {
|
||||||
if user.CN == "" || user.SN == "" || user.UserPassword == "" {
|
searchRequest := ldap.NewSearchRequest( // setup search for user by uid
|
||||||
|
fmt.Sprintf("uid=%s,%s", uid, l.peopledn), // The base dn to search
|
||||||
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||||
|
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
||||||
|
[]string{"dn", "cn", "sn", "mail", "uid", "memberOf"}, // A list attributes to retrieve
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
|
||||||
|
searchResponse, err := l.client.Search(searchRequest) // perform search
|
||||||
|
if err != nil {
|
||||||
return http.StatusBadRequest, gin.H{
|
return http.StatusBadRequest, gin.H{
|
||||||
"ok": false,
|
"ok": false,
|
||||||
"error": "Missing one of required fields: cn, sn, userpassword",
|
"error": err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := searchResponse.Entries[0]
|
||||||
|
|
||||||
|
user := LDAPEntryToLDAPUser(entry)
|
||||||
|
result := LDAPUserToGin(user)
|
||||||
|
|
||||||
|
return http.StatusOK, gin.H{
|
||||||
|
"ok": true,
|
||||||
|
"error": nil,
|
||||||
|
"user": result,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l LDAPClient) AddUser(uid string, user UserRequired) (int, gin.H) {
|
||||||
|
if user.CN == "" || user.SN == "" || user.UserPassword == "" || user.Mail == "" {
|
||||||
|
return http.StatusBadRequest, gin.H{
|
||||||
|
"ok": false,
|
||||||
|
"error": ldap.NewError(
|
||||||
|
ldap.LDAPResultUnwillingToPerform,
|
||||||
|
errors.New("missing one of required fields: cn, sn, mail, userpassword"),
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +111,8 @@ func (l LDAPClient) AddUser(uid string, user User) (int, gin.H) {
|
|||||||
)
|
)
|
||||||
addRequest.Attribute("sn", []string{user.SN})
|
addRequest.Attribute("sn", []string{user.SN})
|
||||||
addRequest.Attribute("cn", []string{user.CN})
|
addRequest.Attribute("cn", []string{user.CN})
|
||||||
addRequest.Attribute("userPassword", []string{user.CN})
|
addRequest.Attribute("mail", []string{user.Mail})
|
||||||
|
addRequest.Attribute("userPassword", []string{user.UserPassword})
|
||||||
addRequest.Attribute("objectClass", []string{"inetOrgPerson"})
|
addRequest.Attribute("objectClass", []string{"inetOrgPerson"})
|
||||||
|
|
||||||
err := l.client.Add(addRequest)
|
err := l.client.Add(addRequest)
|
||||||
@@ -99,46 +129,14 @@ func (l LDAPClient) AddUser(uid string, user User) (int, gin.H) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l LDAPClient) GetUser(uid string) (int, gin.H) {
|
func (l LDAPClient) ModUser(uid string, user UserOptional) (int, gin.H) {
|
||||||
searchRequest := ldap.NewSearchRequest( // setup search for user by uid
|
if user.CN == "" && user.SN == "" && user.UserPassword == "" && user.Mail == "" {
|
||||||
fmt.Sprintf("uid=%s,%s", uid, l.peopledn), // The base dn to search
|
|
||||||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
|
||||||
"(&(objectClass=inetOrgPerson))", // The filter to apply
|
|
||||||
[]string{"dn", "cn", "sn", "mail", "uid"}, // A list attributes to retrieve
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
|
|
||||||
searchResponse, err := l.client.Search(searchRequest) // perform search
|
|
||||||
if err != nil {
|
|
||||||
return http.StatusBadRequest, gin.H{
|
return http.StatusBadRequest, gin.H{
|
||||||
"ok": false,
|
"ok": false,
|
||||||
"error": err,
|
"error": ldap.NewError(
|
||||||
}
|
ldap.LDAPResultUnwillingToPerform,
|
||||||
}
|
errors.New("requires one of fields: cn, sn, mail, userpassword"),
|
||||||
|
),
|
||||||
entry := searchResponse.Entries[0]
|
|
||||||
result := gin.H{
|
|
||||||
"dn": entry.DN,
|
|
||||||
"attributes": gin.H{
|
|
||||||
"cn": entry.GetAttributeValue("cn"),
|
|
||||||
"sn": entry.GetAttributeValue("sn"),
|
|
||||||
"mail": entry.GetAttributeValue("mail"),
|
|
||||||
"uid": entry.GetAttributeValue("uid"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return http.StatusOK, gin.H{
|
|
||||||
"ok": true,
|
|
||||||
"error": nil,
|
|
||||||
"user": result,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l LDAPClient) ModUser(uid string, user User) (int, gin.H) {
|
|
||||||
if user.CN == "" && user.SN == "" && user.UserPassword == "" {
|
|
||||||
return http.StatusBadRequest, gin.H{
|
|
||||||
"ok": false,
|
|
||||||
"error": "Requires one of fields: cn, sn, userpassword",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,6 +150,9 @@ func (l LDAPClient) ModUser(uid string, user User) (int, gin.H) {
|
|||||||
if user.SN != "" {
|
if user.SN != "" {
|
||||||
modifyRequest.Replace("sn", []string{user.SN})
|
modifyRequest.Replace("sn", []string{user.SN})
|
||||||
}
|
}
|
||||||
|
if user.Mail != "" {
|
||||||
|
modifyRequest.Replace("mail", []string{user.Mail})
|
||||||
|
}
|
||||||
if user.UserPassword != "" {
|
if user.UserPassword != "" {
|
||||||
modifyRequest.Replace("userPassword", []string{user.UserPassword})
|
modifyRequest.Replace("userPassword", []string{user.UserPassword})
|
||||||
}
|
}
|
||||||
@@ -214,13 +215,8 @@ func (l LDAPClient) GetAllGroups() (int, gin.H) {
|
|||||||
var results = []gin.H{} // create list of results
|
var results = []gin.H{} // create list of results
|
||||||
|
|
||||||
for _, entry := range searchResponse.Entries { // for each result,
|
for _, entry := range searchResponse.Entries { // for each result,
|
||||||
results = append(results, gin.H{
|
group := LDAPEntryToLDAPGroup(entry)
|
||||||
"dn": entry.DN,
|
results = append(results, LDAPGroupToGin(group))
|
||||||
"attributes": gin.H{
|
|
||||||
"cn": entry.GetAttributeValue("cn"),
|
|
||||||
"member": entry.GetAttributeValues("member"),
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return http.StatusOK, gin.H{
|
return http.StatusOK, gin.H{
|
||||||
@@ -248,13 +244,8 @@ func (l LDAPClient) GetGroup(gid string) (int, gin.H) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
entry := searchResponse.Entries[0]
|
entry := searchResponse.Entries[0]
|
||||||
result := gin.H{
|
group := LDAPEntryToLDAPGroup(entry)
|
||||||
"dn": entry.DN,
|
result := LDAPGroupToGin(group)
|
||||||
"attributes": gin.H{
|
|
||||||
"cn": entry.GetAttributeValue("cn"),
|
|
||||||
"member": entry.GetAttributeValues("member"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return http.StatusOK, gin.H{
|
return http.StatusOK, gin.H{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
@@ -287,7 +278,22 @@ func (l LDAPClient) AddGroup(gid string, group Group) (int, gin.H) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l LDAPClient) ModGroup(gid string, group Group) (int, gin.H) {
|
func (l LDAPClient) ModGroup(gid string, group Group) (int, gin.H) {
|
||||||
return 200, gin.H{
|
modifyRequest := ldap.NewModifyRequest(
|
||||||
|
fmt.Sprintf("cn=%s,%s", gid, l.groupsdn),
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
|
||||||
|
modifyRequest.Replace("cn", []string{gid})
|
||||||
|
|
||||||
|
err := l.client.Modify(modifyRequest)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusBadRequest, gin.H{
|
||||||
|
"ok": false,
|
||||||
|
"error": err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return http.StatusOK, gin.H{
|
||||||
"ok": true,
|
"ok": true,
|
||||||
"error": nil,
|
"error": nil,
|
||||||
}
|
}
|
||||||
|
106
app/utils.go
106
app/utils.go
@@ -2,8 +2,10 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-ldap/ldap/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@@ -20,17 +22,17 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfig(configPath string) Config {
|
func GetConfig(configPath string) (Config, error) {
|
||||||
content, err := os.ReadFile(configPath)
|
content, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error when opening config file: ", err)
|
return Config{}, err
|
||||||
}
|
}
|
||||||
var config Config
|
var config Config
|
||||||
err = json.Unmarshal(content, &config)
|
err = json.Unmarshal(content, &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error during parsing config file: ", err)
|
return Config{}, err
|
||||||
}
|
}
|
||||||
return config
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Login struct { // login body struct
|
type Login struct { // login body struct
|
||||||
@@ -38,11 +40,103 @@ type Login struct { // login body struct
|
|||||||
Password string `form:"password" binding:"required"`
|
Password string `form:"password" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct { // add or modify user body struct
|
type LDAPUserAttributes struct {
|
||||||
|
CN string
|
||||||
|
SN string
|
||||||
|
Mail string
|
||||||
|
UID string
|
||||||
|
MemberOf []string
|
||||||
|
}
|
||||||
|
|
||||||
|
type LDAPUser struct {
|
||||||
|
DN string
|
||||||
|
Attributes LDAPUserAttributes
|
||||||
|
}
|
||||||
|
|
||||||
|
func LDAPEntryToLDAPUser(entry *ldap.Entry) LDAPUser {
|
||||||
|
return LDAPUser{
|
||||||
|
DN: entry.DN,
|
||||||
|
Attributes: LDAPUserAttributes{
|
||||||
|
CN: entry.GetAttributeValue("cn"),
|
||||||
|
SN: entry.GetAttributeValue("sn"),
|
||||||
|
Mail: entry.GetAttributeValue("mail"),
|
||||||
|
UID: entry.GetAttributeValue("uid"),
|
||||||
|
MemberOf: entry.GetAttributeValues("memberOf"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LDAPUserToGin(user LDAPUser) gin.H {
|
||||||
|
return gin.H{
|
||||||
|
"dn": user.DN,
|
||||||
|
"attributes": gin.H{
|
||||||
|
"cn": user.Attributes.CN,
|
||||||
|
"sn": user.Attributes.SN,
|
||||||
|
"mail": user.Attributes.Mail,
|
||||||
|
"uid": user.Attributes.UID,
|
||||||
|
"memberOf": user.Attributes.MemberOf,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type LDAPGroupAttributes struct {
|
||||||
|
CN string
|
||||||
|
Member []string
|
||||||
|
}
|
||||||
|
|
||||||
|
type LDAPGroup struct {
|
||||||
|
DN string
|
||||||
|
Attributes LDAPGroupAttributes
|
||||||
|
}
|
||||||
|
|
||||||
|
func LDAPEntryToLDAPGroup(entry *ldap.Entry) LDAPGroup {
|
||||||
|
return LDAPGroup{
|
||||||
|
DN: entry.DN,
|
||||||
|
Attributes: LDAPGroupAttributes{
|
||||||
|
CN: entry.GetAttributeValue("cn"),
|
||||||
|
Member: entry.GetAttributeValues("member"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func LDAPGroupToGin(group LDAPGroup) gin.H {
|
||||||
|
return gin.H{
|
||||||
|
"dn": group.DN,
|
||||||
|
"attributes": gin.H{
|
||||||
|
"cn": group.Attributes.CN,
|
||||||
|
"member": group.Attributes.Member,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserOptional struct { // add or modify user body struct
|
||||||
CN string `form:"cn"`
|
CN string `form:"cn"`
|
||||||
SN string `form:"sn"`
|
SN string `form:"sn"`
|
||||||
|
Mail string `form:"mail"`
|
||||||
UserPassword string `form:"userpassword"`
|
UserPassword string `form:"userpassword"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserRequired struct { // add or modify user body struct
|
||||||
|
CN string `form:"cn" binding:"required"`
|
||||||
|
SN string `form:"sn" binding:"required"`
|
||||||
|
Mail string `form:"mail" binding:"required"`
|
||||||
|
UserPassword string `form:"userpassword" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
type Group struct { // add or modify group body struct
|
type Group struct { // add or modify group body struct
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HandleResponse(response gin.H) gin.H {
|
||||||
|
if response["error"] != nil {
|
||||||
|
err := response["error"].(error)
|
||||||
|
LDAPerr := err.(*ldap.Error)
|
||||||
|
response["error"] = gin.H{
|
||||||
|
"code": LDAPerr.ResultCode,
|
||||||
|
"result": ldap.LDAPResultCodeMap[LDAPerr.ResultCode],
|
||||||
|
"message": LDAPerr.Err.Error(),
|
||||||
|
}
|
||||||
|
return response
|
||||||
|
} else {
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
}
|
||||||
|
29
go.mod
29
go.mod
@@ -1,6 +1,8 @@
|
|||||||
module proxmoxaas-ldap
|
module proxmoxaas-ldap
|
||||||
|
|
||||||
go 1.22.4
|
go 1.23
|
||||||
|
|
||||||
|
toolchain go1.23.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gin-contrib/sessions v1.0.1
|
github.com/gin-contrib/sessions v1.0.1
|
||||||
@@ -11,35 +13,36 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
|
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
|
||||||
github.com/bytedance/sonic v1.11.8 // indirect
|
github.com/bytedance/sonic v1.12.3 // indirect
|
||||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
github.com/bytedance/sonic/loader v0.2.0 // indirect
|
||||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-asn1-ber/asn1-ber v1.5.7 // indirect
|
github.com/go-asn1-ber/asn1-ber v1.5.7 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.22.0 // indirect
|
github.com/go-playground/validator/v10 v10.22.1 // indirect
|
||||||
github.com/goccy/go-json v0.10.3 // indirect
|
github.com/goccy/go-json v0.10.3 // indirect
|
||||||
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/gorilla/context v1.1.2 // indirect
|
github.com/gorilla/context v1.1.2 // indirect
|
||||||
github.com/gorilla/securecookie v1.1.2 // indirect
|
github.com/gorilla/securecookie v1.1.2 // indirect
|
||||||
github.com/gorilla/sessions v1.3.0 // indirect
|
github.com/gorilla/sessions v1.4.0 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
|
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
|
||||||
github.com/leodido/go-urn v1.4.0 // indirect
|
github.com/leodido/go-urn v1.4.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||||
golang.org/x/arch v0.8.0 // indirect
|
golang.org/x/arch v0.11.0 // indirect
|
||||||
golang.org/x/crypto v0.24.0 // indirect
|
golang.org/x/crypto v0.28.0 // indirect
|
||||||
golang.org/x/net v0.26.0 // indirect
|
golang.org/x/net v0.30.0 // indirect
|
||||||
golang.org/x/sys v0.21.0 // indirect
|
golang.org/x/sys v0.26.0 // indirect
|
||||||
golang.org/x/text v0.16.0 // indirect
|
golang.org/x/text v0.19.0 // indirect
|
||||||
google.golang.org/protobuf v1.34.2 // indirect
|
google.golang.org/protobuf v1.35.1 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user