Newer
Older
package routebuildermdl
import (
"io/ioutil"
"mime/multipart"
"net/http"
"strings"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/authmdl/roleenforcemdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/authmdl/jwtmdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/servicebuildermdl"
func Init(o, r, c *gin.RouterGroup, JWTKey string) {
o.POST("/mql", OpenHandler)
r.POST("/mql", RestrictedHandler)
c.POST("/mql", RoleBasedHandler)
jwtmdl.GlobalJWTKey = JWTKey
}
func setResponseHeader(serviceName string) responseData {
rd := responseData{}
val, ok := GetResponseHeader(serviceName)
if ok {
rd.ResponseHeader = val
}
return rd
}
func isMultipartRequest(header string) bool {
return strings.HasPrefix(header, "multipart/form-data")
}
func executeService(name string, data []byte, isForm bool, formData *multipart.Form, isRestricted, isRoleBased bool, principalObj servicebuildermdl.Principal) (interface{}, error) {
var service interface{}
var found bool
if isRestricted {
if isRoleBased {
service, found = roleBasedServices.Get(name)
} else {
service, found = restrictedServices.Get(name)
}
} else {
service, found = openServices.Get(name)
}
if !found {
return nil, errormdl.Wrap("Service Not Found: " + name)
}
if isForm {
serviceCache := service.(ServiceCache)
return serviceCache.FormService(formData, principalObj)
}
serviceCache := service.(ServiceCache)
if serviceCache.IsFormService {
return nil, errormdl.Wrap("Form_Header_Missing")
return serviceCache.Service(&rs, principalObj)
func commonHandler(c *gin.Context, isRestricted, isRoleBased bool, principalObj servicebuildermdl.Principal) {
service := c.Request.Header.Get("Service-Header")
header := c.Request.Header.Get("Content-Type")
if isMultipartRequest(header) {
form, multiPartError := c.MultipartForm()
if errormdl.CheckErr(multiPartError) != nil {
responseDataObj.Error = errormdl.CheckErr(multiPartError).Error()
c.JSON(http.StatusExpectationFailed, responseDataObj)
return
}
result, err := executeService(service, nil, true, form, isRestricted, isRoleBased, principalObj)
if errormdl.CheckErr1(err) != nil {
responseDataObj.Error = errormdl.CheckErr1(err).Error()
c.JSON(http.StatusExpectationFailed, responseDataObj)
return
}
responseDataObj.Result = result
c.JSON(http.StatusOK, responseDataObj)
return
}
var reqBody []byte
if c.Request.Body != nil {
var readError error
reqBody, readError = ioutil.ReadAll(c.Request.Body)
if errormdl.CheckErr2(readError) != nil {
responseDataObj.Error = errormdl.CheckErr2(readError).Error()
c.JSON(http.StatusExpectationFailed, responseDataObj)
return
}
result, err := executeService(service, reqBody, false, nil, isRestricted, isRoleBased, principalObj)
if errormdl.CheckErr3(err) != nil {
responseDataObj.Error = errormdl.CheckErr3(err).Error()
c.JSON(http.StatusExpectationFailed, responseDataObj)
return
}
responseDataObj.Result = result
c.JSON(http.StatusOK, responseDataObj)
return
}
commonHandler(c, false, false, servicebuildermdl.Principal{})
pricipalObj, extractError := extractPricipalObject(c)
if extractError != nil {
c.JSON(http.StatusExpectationFailed, extractError.Error())
return
}
commonHandler(c, true, false, pricipalObj)
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
pricipalObj, extractError := extractPricipalObject(c)
if extractError != nil {
c.JSON(http.StatusExpectationFailed, extractError.Error())
return
}
commonHandler(c, true, true, pricipalObj)
}
func extractPricipalObject(c *gin.Context) (servicebuildermdl.Principal, error) {
principal := servicebuildermdl.Principal{}
if jwtmdl.GlobalJWTKey == "" {
return principal, nil
}
claim, decodeError := jwtmdl.DecodeToken(c.Request)
if errormdl.CheckErr(decodeError) != nil {
return principal, errormdl.CheckErr(decodeError)
}
// ba, marshalError := ffjson.Marshal(claim)
// if errormdl.CheckErr(marshalError) != nil {
// return principal, errormdl.CheckErr(marshalError)
// }
// unmarshalError := ffjson.Unmarshal(ba, &principal)
// if errormdl.CheckErr(unmarshalError) != nil {
// return principal, errormdl.CheckErr(unmarshalError)
// }
// value, ok := gjson.ParseBytes(ba).Value().(servicebuildermdl.Principal)
// if !ok {
// return principal, errormdl.Wrap("Object is not of type principal")
// }
groups, grperr := roleenforcemdl.GetGroupNames(claim, "groups")
if errormdl.CheckErr(grperr) != nil {
return principal, errormdl.CheckErr(grperr)
}
userID, ok := claim["userId"].(string)
if !ok || len(userID) < 2 {
return principal, errormdl.Wrap("Unable to parse UserID from JWT Token")
}
principal.Groups = groups
principal.UserID = userID
return principal, nil