Add /modify endpoint, GET returns 404

This commit is contained in:
yo 2022-11-13 16:49:23 +01:00
parent cb6a7ffaee
commit 5aee108f65

56
main.go
View File

@ -220,6 +220,38 @@ func initRouter(r *gin.Engine) {
})
// All following routes need authentication
/* panic: ':ou' in new path '/:ou/:cn/:class' conflicts with existing wildcard ':dn' in existing prefix '/:dn'
r.GET("/:dn", ldapBasicAuth, func(c *gin.Context) {
dn := c.Param("dn")
// Get user authenticated LDAP connection from context
ldapCon, err := getLdapConFromContext(c)
if err != nil {
log.Error(err)
c.AbortWithError(http.StatusInternalServerError, nil)
return
}
// json format is the default
format := c.DefaultQuery("format", "json")
res, err := searchByDn(ldapCon, dn, "ALL")
// If DN does not exist, we'll get err='LDAP Result Code 32 "No Such Object"'
if err != nil {
if strings.Contains(err.Error(), "LDAP Result Code 32") {
c.AbortWithError(http.StatusNotFound, err)
return
} else {
log.Errorf("Error searching %s: %v", dn, err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
sendResponse(c, res, format)
return
})*/
r.GET("/:ou/:cn/:class", ldapBasicAuth, func(c *gin.Context) {
ou := c.Param("ou")
cn := c.Param("cn")
@ -240,10 +272,15 @@ func initRouter(r *gin.Engine) {
// If OU does not exist, we'll get err='LDAP Result Code 32 "No Such Object"'
if err != nil {
if strings.Contains(err.Error(), "LDAP Result Code 32") {
c.AbortWithError(http.StatusNotFound, err)
return
} else {
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
sendResponse(c, res, format)
return
})
@ -301,10 +338,15 @@ func initRouter(r *gin.Engine) {
res, err := searchByCn(ldapCon, ou, cn, class, attr)
if err != nil {
if strings.Contains(err.Error(), "LDAP Result Code 32") {
c.AbortWithError(http.StatusNotFound, err)
return
} else {
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
sendResponse(c, res, format)
return
})
@ -412,6 +454,11 @@ func initRouter(r *gin.Engine) {
* curl -u "admin:admin" --header "Content-Type: application/json" -X PUT
* --data '{"objectClass":["person","top"],"cn":"newuser","sn":"New","description":"Test account"}' \
* https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org
* or
* curl -u "admin:admin" -H "Content-Type: application/json" -X PUT
* -d '{"dn":"cn=newuser,ou=users,dc=example,dc=org", \
* "objectClass":["person","top"],"cn":"newuser","sn":"New","description":"Test account"}' \
* https://localhost:8443/modify
*/
r.PUT("/:dn", ldapBasicAuth, func(c *gin.Context) {
dn := c.Param("dn")
@ -432,6 +479,15 @@ func initRouter(r *gin.Engine) {
return
}
// Get dn in body if called with "http://1.2.3.4/modify"
if strings.EqualFold(dn, "modify") {
dn = attributes["dn"].(string)
}
if len(dn) == 0 {
c.AbortWithError(http.StatusBadRequest, err)
return
}
err = updateEntry(ldapCon, dn, attributes)
if err != nil {
if strings.Contains(err.Error(), "LDAP Result Code 50") {