~bigbes/core-go

71b27871dc308a586f286d1454acb0fafea91a90 — Drew DeVault a month ago cc7fd1d
auth: add function to create ad-hoc authenticated contexts
1 files changed, 10 insertions(+), 7 deletions(-)

M auth/middleware.go
M auth/middleware.go => auth/middleware.go +10 -7
@@ 219,8 219,7 @@ func cookieAuth(cookie *http.Cookie, w http.ResponseWriter,

	auth.AuthMethod = AUTH_COOKIE

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



@@ 310,8 309,7 @@ func internalAuth(payload []byte, w http.ResponseWriter, r *http.Request, next h
		break
	}

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



@@ 558,8 556,7 @@ func OAuth2(token string, hash [64]byte, w http.ResponseWriter,
		panic(err) // unreachable
	}

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



@@ 592,7 589,7 @@ func WebhookAuth(ctx context.Context, auth *AuthContext,
		whAuth.BearerToken.ClientID = *clientID
	}

	return context.WithValue(ctx, userCtxKey, &whAuth), nil
	return Context(ctx, auth), nil
}

func Middleware(conf ini.File, apiconf string) func(http.Handler) http.Handler {


@@ 641,6 638,7 @@ func Middleware(conf ini.File, apiconf string) func(http.Handler) http.Handler {
	}
}

// Returns the authentication details associated with this context.
func ForContext(ctx context.Context) *AuthContext {
	raw, ok := ctx.Value(userCtxKey).(*AuthContext)
	if !ok {


@@ 648,3 646,8 @@ func ForContext(ctx context.Context) *AuthContext {
	}
	return raw
}

// Creates a new authenticated context.
func Context(base context.Context, auth *AuthContext) context.Context {
	return context.WithValue(base, userCtxKey, auth)
}