feat: validate personal and enterprise auth tenants
This commit is contained in:
+63
-5
@@ -63,6 +63,12 @@ const (
|
||||
imReasonReviewUnavailable = "CONTENT_REVIEW_UNAVAILABLE"
|
||||
)
|
||||
|
||||
const (
|
||||
imDefaultTenantCode = "default"
|
||||
registrationTypeEnterprise = 0
|
||||
registrationTypePersonal = 1
|
||||
)
|
||||
|
||||
func reviewIMHTTPText(wrt http.ResponseWriter, req *http.Request, principal *imPrincipal, scene, topic, text string) bool {
|
||||
text = strings.TrimSpace(text)
|
||||
if text == "" {
|
||||
@@ -291,10 +297,11 @@ type imTenantEntryFeedbackRequest struct {
|
||||
}
|
||||
|
||||
type imVerificationCreateRequest struct {
|
||||
TenantCode string `json:"tenant_code,omitempty"`
|
||||
Scene string `json:"scene"`
|
||||
Phone string `json:"phone"`
|
||||
Country string `json:"country,omitempty"`
|
||||
TenantCode string `json:"tenant_code,omitempty"`
|
||||
RegistrationType *int `json:"registration_type,omitempty"`
|
||||
Scene string `json:"scene"`
|
||||
Phone string `json:"phone"`
|
||||
Country string `json:"country,omitempty"`
|
||||
}
|
||||
|
||||
type imVerificationVerifyRequest struct {
|
||||
@@ -979,7 +986,13 @@ func serveIMVerificationCodes(wrt http.ResponseWriter, req *http.Request) {
|
||||
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "phone"})
|
||||
return
|
||||
}
|
||||
tenant, ok := resolveIMHTTPTenant(wrt, req, body.TenantCode)
|
||||
var tenant *types.Tenant
|
||||
var ok bool
|
||||
if scene == "register" {
|
||||
tenant, ok = resolveIMHTTPRegistrationTenant(wrt, req, body)
|
||||
} else {
|
||||
tenant, ok = resolveIMHTTPTenant(wrt, req, body.TenantCode)
|
||||
}
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -9891,6 +9904,51 @@ func resolveIMHTTPTenant(wrt http.ResponseWriter, req *http.Request, fallbackCod
|
||||
return tenant, true
|
||||
}
|
||||
|
||||
func resolveIMHTTPRegistrationTenant(wrt http.ResponseWriter, req *http.Request, body imVerificationCreateRequest) (*types.Tenant, bool) {
|
||||
if body.RegistrationType == nil {
|
||||
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "registration_type"})
|
||||
return nil, false
|
||||
}
|
||||
|
||||
headerCode := strings.TrimSpace(req.Header.Get("X-Tenant-Code"))
|
||||
bodyCode := strings.TrimSpace(body.TenantCode)
|
||||
var tenantCode string
|
||||
|
||||
switch *body.RegistrationType {
|
||||
case registrationTypePersonal:
|
||||
if bodyCode != "" {
|
||||
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "tenant_code"})
|
||||
return nil, false
|
||||
}
|
||||
if headerCode != "" && !strings.EqualFold(headerCode, imDefaultTenantCode) {
|
||||
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "tenant_code"})
|
||||
return nil, false
|
||||
}
|
||||
tenantCode = imDefaultTenantCode
|
||||
case registrationTypeEnterprise:
|
||||
if bodyCode == "" {
|
||||
bodyCode = headerCode
|
||||
} else if headerCode != "" && !strings.EqualFold(headerCode, bodyCode) {
|
||||
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "tenant_code"})
|
||||
return nil, false
|
||||
}
|
||||
if bodyCode == "" || len(bodyCode) > 64 {
|
||||
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "tenant_code"})
|
||||
return nil, false
|
||||
}
|
||||
if strings.EqualFold(bodyCode, imDefaultTenantCode) {
|
||||
writeIMError(wrt, req, http.StatusNotFound, 30010, imReasonTenantUnavailable, "tenant unavailable", nil)
|
||||
return nil, false
|
||||
}
|
||||
tenantCode = bodyCode
|
||||
default:
|
||||
writeIMError(wrt, req, http.StatusBadRequest, 30000, imReasonMalformedRequest, "malformed request", map[string]any{"field": "registration_type"})
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return resolveIMHTTPTenant(wrt, req, tenantCode)
|
||||
}
|
||||
|
||||
func writeIMSuccess(wrt http.ResponseWriter, req *http.Request, data any) {
|
||||
writeIMJSON(wrt, http.StatusOK, imHTTPResponse{
|
||||
Code: 0,
|
||||
|
||||
Reference in New Issue
Block a user