Use builtin status codes, change HTTPS option
This commit is contained in:
parent
bdda2de936
commit
5e363df9b0
39
main.go
39
main.go
@ -10,6 +10,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"time"
|
"time"
|
||||||
"strings"
|
"strings"
|
||||||
|
"net/http"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
@ -19,7 +20,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gVersion = "0.5"
|
gVersion = "0.5.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func marshalResultToText(res *ldap.SearchResult, delimiter string, showValueName, showDN bool) string {
|
func marshalResultToText(res *ldap.SearchResult, delimiter string, showValueName, showDN bool) string {
|
||||||
@ -54,9 +55,9 @@ func sendResponse(c *gin.Context, res *ldap.SearchResult, format string) {
|
|||||||
// 404 Not found
|
// 404 Not found
|
||||||
if len(res.Entries) == 0 {
|
if len(res.Entries) == 0 {
|
||||||
if strings.EqualFold(format, "json") {
|
if strings.EqualFold(format, "json") {
|
||||||
c.JSON(404, gin.H{"error": "No result"})
|
c.JSON(http.StatusNotFound, gin.H{"error": "No result"})
|
||||||
} else {
|
} else {
|
||||||
c.String(404, "No result")
|
c.String(http.StatusNotFound, "No result")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -69,27 +70,27 @@ func sendResponse(c *gin.Context, res *ldap.SearchResult, format string) {
|
|||||||
log.Errorf("Error marshalling result to json: %v", err)
|
log.Errorf("Error marshalling result to json: %v", err)
|
||||||
}
|
}
|
||||||
log.Debugf("%v\n", string(jsonRes))
|
log.Debugf("%v\n", string(jsonRes))
|
||||||
c.String(200, string(jsonRes))
|
c.String(http.StatusOK, string(jsonRes))
|
||||||
|
|
||||||
} else if strings.EqualFold(format, "text") {
|
} else if strings.EqualFold(format, "text") {
|
||||||
txtRes := marshalResultToText(res, "=", false, true)
|
txtRes := marshalResultToText(res, "=", false, true)
|
||||||
log.Debugf("%v\n", string(txtRes))
|
log.Debugf("%v\n", string(txtRes))
|
||||||
c.String(200, string(txtRes))
|
c.String(http.StatusOK, string(txtRes))
|
||||||
|
|
||||||
} else if strings.EqualFold(format, "ldif") {
|
} else if strings.EqualFold(format, "ldif") {
|
||||||
txtRes := marshalResultToText(res, ": ", false, true)
|
txtRes := marshalResultToText(res, ": ", false, true)
|
||||||
log.Debugf("%v\n", string(txtRes))
|
log.Debugf("%v\n", string(txtRes))
|
||||||
c.String(200, string(txtRes))
|
c.String(http.StatusOK, string(txtRes))
|
||||||
|
|
||||||
} else if strings.EqualFold(format, "textvalue") {
|
} else if strings.EqualFold(format, "textvalue") {
|
||||||
txtRes := marshalResultToText(res, "", true, true)
|
txtRes := marshalResultToText(res, "", true, true)
|
||||||
log.Debugf("%v\n", string(txtRes))
|
log.Debugf("%v\n", string(txtRes))
|
||||||
c.String(200, string(txtRes))
|
c.String(http.StatusOK, string(txtRes))
|
||||||
|
|
||||||
} else if strings.EqualFold(format, "textvalue-nodn") {
|
} else if strings.EqualFold(format, "textvalue-nodn") {
|
||||||
txtRes := marshalResultToText(res, "", true, false)
|
txtRes := marshalResultToText(res, "", true, false)
|
||||||
log.Debugf("%v\n", string(txtRes))
|
log.Debugf("%v\n", string(txtRes))
|
||||||
c.String(200, string(txtRes))
|
c.String(http.StatusOK, string(txtRes))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,7 +150,7 @@ func basicAuth(c *gin.Context) {
|
|||||||
if hasAuth && user == "admin" && password == "admin" {
|
if hasAuth && user == "admin" && password == "admin" {
|
||||||
log.Infof("[%s]: User %s successfully authenticated", c.Request.RemoteAddr, user)
|
log.Infof("[%s]: User %s successfully authenticated", c.Request.RemoteAddr, user)
|
||||||
} else {
|
} else {
|
||||||
c.AbortWithStatus(401)
|
c.AbortWithStatus(http.StatusUnauthorized)
|
||||||
c.Writer.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
|
c.Writer.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -158,7 +159,7 @@ func basicAuth(c *gin.Context) {
|
|||||||
|
|
||||||
func initRouter(r *gin.Engine, myldap *MyLdap) {
|
func initRouter(r *gin.Engine, myldap *MyLdap) {
|
||||||
r.GET("/ping", func(c *gin.Context) {
|
r.GET("/ping", func(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "pong",
|
"message": "pong",
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -178,7 +179,7 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
|||||||
// If OU does not exist, we'll get err='LDAP Result Code 32 "No Such Object"'
|
// If OU does not exist, we'll get err='LDAP Result Code 32 "No Such Object"'
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
||||||
c.AbortWithError(500, err)
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sendResponse(c, res, format)
|
sendResponse(c, res, format)
|
||||||
@ -195,7 +196,7 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
|||||||
|
|
||||||
modified, err := checkIfModifiedSince(c, myldap, ou, cn, class, "ALL")
|
modified, err := checkIfModifiedSince(c, myldap, ou, cn, class, "ALL")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(500, err)
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -203,12 +204,12 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
|||||||
res, err := doLdapSearch(myldap, ou, cn, class, "ALL")
|
res, err := doLdapSearch(myldap, ou, cn, class, "ALL")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
||||||
c.AbortWithError(500, err)
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sendResponse(c, res, format)
|
sendResponse(c, res, format)
|
||||||
} else {
|
} else {
|
||||||
c.String(304, "")
|
c.String(http.StatusNotModified, "")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
@ -225,7 +226,7 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
|||||||
res, err := doLdapSearch(myldap, ou, cn, class, attr)
|
res, err := doLdapSearch(myldap, ou, cn, class, attr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
||||||
c.AbortWithError(500, err)
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sendResponse(c, res, format)
|
sendResponse(c, res, format)
|
||||||
@ -243,7 +244,7 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
|||||||
|
|
||||||
modified, err := checkIfModifiedSince(c, myldap, ou, cn, class, attr)
|
modified, err := checkIfModifiedSince(c, myldap, ou, cn, class, attr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(500, err)
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -251,12 +252,12 @@ func initRouter(r *gin.Engine, myldap *MyLdap) {
|
|||||||
res, err := doLdapSearch(myldap, ou, cn, class, attr)
|
res, err := doLdapSearch(myldap, ou, cn, class, attr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
log.Errorf("Error searching %s in %s : %v", cn, ou, err)
|
||||||
c.AbortWithError(500, err)
|
c.AbortWithError(http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sendResponse(c, res, format)
|
sendResponse(c, res, format)
|
||||||
} else {
|
} else {
|
||||||
c.String(304, "")
|
c.String(http.StatusNotModified, "")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
@ -335,7 +336,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if false == doTls {
|
if false == doTls {
|
||||||
doTls = viper.GetBool("SERVE_HTTPS")
|
doTls = viper.GetBool("HTTPS")
|
||||||
}
|
}
|
||||||
if doTls && len(tlsCert) == 0 {
|
if doTls && len(tlsCert) == 0 {
|
||||||
l := viper.GetString("SSL_CERTIFICATE")
|
l := viper.GetString("SSL_CERTIFICATE")
|
||||||
|
Loading…
Reference in New Issue
Block a user