Add /add and /delete endpoints, with dn in json body
This commit is contained in:
parent
a02b8b9359
commit
d557521a5c
75
main.go
75
main.go
@ -18,10 +18,11 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/go-ldap/ldap/v3"
|
"github.com/go-ldap/ldap/v3"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
//"github.com/gin-gonic/gin/render"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gVersion = "0.5.3"
|
gVersion = "0.5.4"
|
||||||
|
|
||||||
gRoLdap *MyLdap
|
gRoLdap *MyLdap
|
||||||
)
|
)
|
||||||
@ -344,14 +345,18 @@ func initRouter(r *gin.Engine) {
|
|||||||
return
|
return
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
/* 2 call methods : Either DN in url, or DN in body using /add :
|
||||||
* curl -u "admin:admin" --header "Content-Type: application/json" -X POST
|
* * curl -u "admin:admin" -H "Content-Type: application/json" -X POST
|
||||||
* --data '{"objectClass":["person","top"],"cn":"newuser","sn":"New"}' \
|
* --data '{"objectClass":["person","top"],"cn":"newuser","sn":"New"}' \
|
||||||
* https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org
|
* https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org
|
||||||
|
*
|
||||||
|
* curl -u "admin:admin" -H "Content-Type: application/json" -X POST
|
||||||
|
* --data '{"dn":"cn=newuser,ou=users,dc=example,dc=org","objectClass":["person","top"],"cn":"newuser","sn":"New"}' \
|
||||||
|
* https://localhost:8443/add
|
||||||
*/
|
*/
|
||||||
r.POST("/:dn", ldapBasicAuth, func(c *gin.Context) {
|
r.POST("/:dn", ldapBasicAuth, func(c *gin.Context) {
|
||||||
dn := c.Param("dn")
|
dn := c.Param("dn")
|
||||||
|
|
||||||
// Get user authenticated LDAP connection from context
|
// Get user authenticated LDAP connection from context
|
||||||
ldapCon, err := getLdapConFromContext(c)
|
ldapCon, err := getLdapConFromContext(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -369,11 +374,30 @@ func initRouter(r *gin.Engine) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get dn in body if called with "http://1.2.3.4/add"
|
||||||
|
if strings.EqualFold(dn, "add") {
|
||||||
|
dn = attributes["dn"].(string)
|
||||||
|
}
|
||||||
|
if len(dn) == 0 {
|
||||||
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
err = createEntry(ldapCon, dn, attributes)
|
err = createEntry(ldapCon, dn, attributes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if strings.Contains(err.Error(), "LDAP Result Code 50") {
|
if strings.Contains(err.Error(), "LDAP Result Code 50") {
|
||||||
c.AbortWithStatus(http.StatusUnauthorized)
|
c.AbortWithStatus(http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
// "Entry Already Exists"
|
||||||
|
} else if strings.Contains(err.Error(), "LDAP Result Code 68") {
|
||||||
|
c.JSON(http.StatusCreated, gin.H{"message": "Entry already exists"})
|
||||||
|
/* This returns 201/Created with Location header, although 303/SeeOther is specified
|
||||||
|
* c.Render(http.StatusSeeOther, render.Redirect{
|
||||||
|
Code: 303,
|
||||||
|
Location: fmt.Sprintf("http://1.2.3.4/%s", dn),
|
||||||
|
Request: c.Request,
|
||||||
|
})*/
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
c.AbortWithError(http.StatusBadRequest, err)
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
return
|
return
|
||||||
@ -426,8 +450,10 @@ func initRouter(r *gin.Engine) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
/* 2 call methods : Either DN in url, or DN in body using /delete :
|
||||||
* curl -i -u "admin:admin" -X DELETE https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org
|
* curl -i -u "admin:admin" -X DELETE https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org
|
||||||
|
* or
|
||||||
|
* curl -i -u "admin:admin" -X DELETE -H "Content-Type: application/json" -d '{"dn":"cn=newuser,ou=users,dc=example,dc=org"}' https://localhost:8443/delete
|
||||||
*
|
*
|
||||||
* Each leaf have to be deleted (cannot delete if subordinates)
|
* Each leaf have to be deleted (cannot delete if subordinates)
|
||||||
*/
|
*/
|
||||||
@ -441,14 +467,39 @@ func initRouter(r *gin.Engine) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = deleteEntry(ldapCon, dn)
|
// Unmarshall json body to a map
|
||||||
if err != nil {
|
if c.Request.Header.Get("Content-Type") == "application/json" {
|
||||||
//log.Errorf("Error creating %s: %v", dn, err)
|
var attributes map[string]interface{}
|
||||||
c.AbortWithError(http.StatusBadRequest, err)
|
err := c.ShouldBindJSON(&attributes)
|
||||||
return
|
if err != nil {
|
||||||
}
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get dn in body if called with "http://1.2.3.4/delete"
|
||||||
|
if strings.EqualFold(dn, "delete") {
|
||||||
|
dn = attributes["dn"].(string)
|
||||||
|
}
|
||||||
|
if len(dn) == 0 {
|
||||||
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusCreated, gin.H{"message": "Successfully deleted"})
|
err = deleteEntry(ldapCon, dn)
|
||||||
|
if err != nil {
|
||||||
|
//log.Errorf("Error creating %s: %v", dn, err)
|
||||||
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Successfully deleted"})
|
||||||
|
} else {
|
||||||
|
err = deleteEntry(ldapCon, dn)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusBadRequest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user