mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
# Conflicts: # core/record_field_resolver_multi_match.go # ui/dist/assets/AuthMethodsDocs-ChShXLwE.js # ui/dist/assets/AuthMethodsDocs-Cqvl0T_l.js # ui/dist/assets/AuthMethodsDocs-DIqhS7qY.js # ui/dist/assets/AuthRefreshDocs-CHakkNnn.js # ui/dist/assets/AuthRefreshDocs-DLb1qree.js # ui/dist/assets/AuthRefreshDocs-DXgrlMdk.js # ui/dist/assets/AuthWithOAuth2Docs-Bl8VRxNv.js # ui/dist/assets/AuthWithOAuth2Docs-DVBOel22.js # ui/dist/assets/AuthWithOAuth2Docs-I213UiJ7.js # ui/dist/assets/AuthWithOtpDocs-B9tL6gIN.js # ui/dist/assets/AuthWithOtpDocs-C2PPm9-Z.js # ui/dist/assets/AuthWithOtpDocs-qEMoMG3D.js # ui/dist/assets/AuthWithPasswordDocs-B8JWDbPL.js # ui/dist/assets/AuthWithPasswordDocs-BTHeYJwE.js # ui/dist/assets/AuthWithPasswordDocs-D9rqgLpO.js # ui/dist/assets/BatchApiDocs-BV4PYYXn.js # ui/dist/assets/BatchApiDocs-DXa997am.js # ui/dist/assets/BatchApiDocs-Di2Qe2dV.js # ui/dist/assets/DeleteApiDocs-C3IINRwz.js # ui/dist/assets/DeleteApiDocs-LfmVp4Uf.js # ui/dist/assets/DeleteApiDocs-SU4XdGhJ.js # ui/dist/assets/EmailChangeDocs-COx1USU5.js # ui/dist/assets/EmailChangeDocs-D-qNHT_P.js # ui/dist/assets/EmailChangeDocs-DpfXIE_V.js # ui/dist/assets/FieldsQueryParam-BYe9ZK-G.js # ui/dist/assets/FieldsQueryParam-L8vpHeF4.js # ui/dist/assets/FieldsQueryParam-z0dGQ6uY.js # ui/dist/assets/ListApiDocs-BCTT-AOz.js # ui/dist/assets/ListApiDocs-C0_GwI7k.js # ui/dist/assets/ListApiDocs-T9_aO-pW.js # ui/dist/assets/PageInstaller-B22bivaN.js # ui/dist/assets/PageInstaller-C0BBhyBx.js # ui/dist/assets/PageInstaller-IYSwJhZW.js # ui/dist/assets/PageOAuth2RedirectFailure-BjbpqlFj.js # ui/dist/assets/PageOAuth2RedirectFailure-COyNF79R.js # ui/dist/assets/PageOAuth2RedirectFailure-DPmGG9zv.js # ui/dist/assets/PageOAuth2RedirectSuccess-6cMXLFAX.js # ui/dist/assets/PageOAuth2RedirectSuccess-CdkCarGH.js # ui/dist/assets/PageOAuth2RedirectSuccess-DSEIHeQL.js # ui/dist/assets/PageRecordConfirmEmailChange-BYW-6ASg.js # ui/dist/assets/PageRecordConfirmEmailChange-Bm1CIYvq.js # ui/dist/assets/PageRecordConfirmEmailChange-BpaqOT4u.js # ui/dist/assets/PageRecordConfirmPasswordReset--wx_u2lI.js # ui/dist/assets/PageRecordConfirmPasswordReset-CoeY2Z_C.js # ui/dist/assets/PageRecordConfirmPasswordReset-Ptya5bfe.js # ui/dist/assets/PageRecordConfirmVerification-6xjF3T0O.js # ui/dist/assets/PageRecordConfirmVerification-C1UTIIJh.js # ui/dist/assets/PageRecordConfirmVerification-C4LixXvA.js # ui/dist/assets/PageSuperuserConfirmPasswordReset-19z2IFg4.js # ui/dist/assets/PageSuperuserConfirmPasswordReset-DwNi0ugQ.js # ui/dist/assets/PageSuperuserConfirmPasswordReset-SMnDTRNi.js # ui/dist/assets/PageSuperuserRequestPasswordReset-BpDW217f.js # ui/dist/assets/PageSuperuserRequestPasswordReset-DK63qE6z.js # ui/dist/assets/PageSuperuserRequestPasswordReset-bG02qLIM.js # ui/dist/assets/PasswordResetDocs-CE67mLC0.js # ui/dist/assets/PasswordResetDocs-CHAK0vma.js # ui/dist/assets/PasswordResetDocs-Dap-N4rx.js # ui/dist/assets/RealtimeApiDocs-BooTSQCb.js # ui/dist/assets/RealtimeApiDocs-CoHmYVEx.js # ui/dist/assets/RealtimeApiDocs-D7ds3fHt.js # ui/dist/assets/VerificationDocs-B42AIoCQ.js # ui/dist/assets/VerificationDocs-BS7b11zH.js # ui/dist/assets/VerificationDocs-ZhkdFVIl.js # ui/dist/assets/ViewApiDocs-BXmRLHZ1.js # ui/dist/assets/ViewApiDocs-DM5JJQjh.js # ui/dist/assets/ViewApiDocs-DRpxcuJR.js # ui/public/fonts/source-sans-pro/source-sans-pro-v18-latin_cyrillic-700.woff2 # ui/public/fonts/ubuntu-mono/ubuntu-mono-v17-cyrillic_latin-700.woff2
198 lines
5.0 KiB
Go
198 lines
5.0 KiB
Go
package core
|
|
|
|
import (
|
|
"maps"
|
|
"net/netip"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/hanzoai/base/tools/inflector"
|
|
"github.com/hanzoai/base/tools/router"
|
|
)
|
|
|
|
// Common request store keys used by the middlewares and api handlers.
|
|
const (
|
|
RequestEventKeyInfoContext = "infoContext"
|
|
)
|
|
|
|
// RequestEvent defines the Base router handler event.
|
|
type RequestEvent struct {
|
|
App App
|
|
|
|
cachedRequestInfo *RequestInfo
|
|
|
|
Auth *Record
|
|
|
|
router.Event
|
|
|
|
mu sync.Mutex
|
|
}
|
|
|
|
// RealIP returns the "real" IP address from the configured trusted proxy headers.
|
|
//
|
|
// If Settings.TrustedProxy is not configured or the found IP is empty,
|
|
// it fallbacks to e.RemoteIP().
|
|
//
|
|
// NB!
|
|
// Be careful when used in a security critical context as it relies on
|
|
// the trusted proxy to be properly configured and your app to be accessible only through it.
|
|
// If you are not sure, use e.RemoteIP().
|
|
func (e *RequestEvent) RealIP() string {
|
|
settings := e.App.Settings()
|
|
|
|
for _, h := range settings.TrustedProxy.Headers {
|
|
headerValues := e.Request.Header.Values(h)
|
|
if len(headerValues) == 0 {
|
|
continue
|
|
}
|
|
|
|
// extract the last header value as it is expected to be the one controlled by the proxy
|
|
ipsList := headerValues[len(headerValues)-1]
|
|
if ipsList == "" {
|
|
continue
|
|
}
|
|
|
|
ips := strings.Split(ipsList, ",")
|
|
|
|
if settings.TrustedProxy.UseLeftmostIP {
|
|
for _, ip := range ips {
|
|
parsed, err := netip.ParseAddr(strings.TrimSpace(ip))
|
|
if err == nil {
|
|
return parsed.StringExpanded()
|
|
}
|
|
}
|
|
} else {
|
|
for i := len(ips) - 1; i >= 0; i-- {
|
|
parsed, err := netip.ParseAddr(strings.TrimSpace(ips[i]))
|
|
if err == nil {
|
|
return parsed.StringExpanded()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return e.RemoteIP()
|
|
}
|
|
|
|
// HasSuperuserAuth checks whether the current RequestEvent has superuser authentication loaded.
|
|
func (e *RequestEvent) HasSuperuserAuth() bool {
|
|
return e.Auth != nil && e.Auth.IsSuperuser()
|
|
}
|
|
|
|
// RequestInfo parses the current request into RequestInfo instance.
|
|
//
|
|
// Note that the returned result is cached to avoid copying the request data multiple times
|
|
// but the auth state and other common store items are always refreshed in case they were changed by another handler.
|
|
func (e *RequestEvent) RequestInfo() (*RequestInfo, error) {
|
|
e.mu.Lock()
|
|
defer e.mu.Unlock()
|
|
|
|
if e.cachedRequestInfo != nil {
|
|
e.cachedRequestInfo.Auth = e.Auth
|
|
|
|
infoCtx, _ := e.Get(RequestEventKeyInfoContext).(string)
|
|
if infoCtx != "" {
|
|
e.cachedRequestInfo.Context = infoCtx
|
|
} else {
|
|
e.cachedRequestInfo.Context = RequestInfoContextDefault
|
|
}
|
|
} else {
|
|
// (re)init e.cachedRequestInfo based on the current request event
|
|
if err := e.initRequestInfo(); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return e.cachedRequestInfo, nil
|
|
}
|
|
|
|
func (e *RequestEvent) initRequestInfo() error {
|
|
infoCtx, _ := e.Get(RequestEventKeyInfoContext).(string)
|
|
if infoCtx == "" {
|
|
infoCtx = RequestInfoContextDefault
|
|
}
|
|
|
|
info := &RequestInfo{
|
|
Context: infoCtx,
|
|
Method: e.Request.Method,
|
|
Query: map[string]string{},
|
|
Headers: map[string]string{},
|
|
Body: map[string]any{},
|
|
}
|
|
|
|
if err := e.BindBody(&info.Body); err != nil {
|
|
return err
|
|
}
|
|
|
|
// extract the first value of all query params
|
|
query := e.Request.URL.Query()
|
|
for k, v := range query {
|
|
if len(v) > 0 {
|
|
info.Query[k] = v[0]
|
|
}
|
|
}
|
|
|
|
// extract the first value of all headers and normalizes the keys
|
|
// ("X-Token" is converted to "x_token")
|
|
for k, v := range e.Request.Header {
|
|
if len(v) > 0 {
|
|
info.Headers[inflector.Snakecase(k)] = v[0]
|
|
}
|
|
}
|
|
|
|
info.Auth = e.Auth
|
|
|
|
e.cachedRequestInfo = info
|
|
|
|
return nil
|
|
}
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
const (
|
|
RequestInfoContextDefault = "default"
|
|
RequestInfoContextExpand = "expand"
|
|
RequestInfoContextRealtime = "realtime"
|
|
RequestInfoContextProtectedFile = "protectedFile"
|
|
RequestInfoContextBatch = "batch"
|
|
RequestInfoContextOAuth2 = "oauth2"
|
|
RequestInfoContextOTP = "otp"
|
|
RequestInfoContextPasswordAuth = "password"
|
|
)
|
|
|
|
// RequestInfo defines a HTTP request data struct, usually used
|
|
// as part of the `@request.*` filter resolver.
|
|
//
|
|
// The Query and Headers fields contains only the first value for each found entry.
|
|
type RequestInfo struct {
|
|
Query map[string]string `json:"query"`
|
|
Headers map[string]string `json:"headers"`
|
|
Body map[string]any `json:"body"`
|
|
Auth *Record `json:"auth"`
|
|
Method string `json:"method"`
|
|
Context string `json:"context"`
|
|
}
|
|
|
|
// HasSuperuserAuth checks whether the current RequestInfo instance
|
|
// has superuser authentication loaded.
|
|
func (info *RequestInfo) HasSuperuserAuth() bool {
|
|
return info.Auth != nil && info.Auth.IsSuperuser()
|
|
}
|
|
|
|
// Clone creates a new shallow copy of the current RequestInfo and its Auth record (if any).
|
|
func (info *RequestInfo) Clone() *RequestInfo {
|
|
clone := &RequestInfo{
|
|
Method: info.Method,
|
|
Context: info.Context,
|
|
Query: maps.Clone(info.Query),
|
|
Body: maps.Clone(info.Body),
|
|
Headers: maps.Clone(info.Headers),
|
|
}
|
|
|
|
if info.Auth != nil {
|
|
clone.Auth = info.Auth.Fresh()
|
|
}
|
|
|
|
return clone
|
|
}
|