~sircmpwn/core-go

bd40a71a935abdd736c33c175a8c69c58541a9fd — Drew DeVault 2 months ago 88dff0e
auth: add auth.IPAddress

With support for X-Forwarded-For
1 files changed, 18 insertions(+), 0 deletions(-)

M auth/middleware.go
M auth/middleware.go => auth/middleware.go +18 -0
@@ 60,6 60,7 @@ const (

type AuthContext struct {
	AuthMethod string
	IPAddress  string

	// Only filled out for non-anonymous authentication
	UserID           int


@@ 310,6 311,23 @@ func internalAuth(internalNet []*net.IPNet, payload []byte,

	auth.InternalAuth = internalAuth

	auth.IPAddress = r.RemoteAddr
	var route []string
	for _, val := range r.Header.Values("X-Forwarded-For") {
		route = append(route, strings.Split(val, ",")...)
	}
	for _, val := range route {
		ip := net.ParseIP(val)
		if ip == nil {
			continue
		}
		if ip.IsPrivate() {
			continue
		}
		auth.IPAddress = ip.String()
		break
	}

	ctx := context.WithValue(r.Context(), userCtxKey, auth)
	r = r.WithContext(ctx)
	next.ServeHTTP(w, r)