2020-07-04 18:41:58 +01:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2021-01-02 13:16:01 -08:00
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2020-09-30 01:44:42 +09:00
|
|
|
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/sessions"
|
2020-07-04 18:41:58 +01:00
|
|
|
)
|
|
|
|
|
2021-01-02 13:16:01 -08:00
|
|
|
type scopeKey string
|
|
|
|
|
|
|
|
// RequestScopeKey uses a typed string to reduce likelihood of clashing
|
|
|
|
// with other context keys
|
|
|
|
const RequestScopeKey scopeKey = "request-scope"
|
|
|
|
|
2020-07-04 18:41:58 +01:00
|
|
|
// RequestScope contains information regarding the request that is being made.
|
|
|
|
// The RequestScope is used to pass information between different middlewares
|
|
|
|
// within the chain.
|
|
|
|
type RequestScope struct {
|
2020-12-23 17:42:02 -08:00
|
|
|
// ReverseProxy tracks whether OAuth2-Proxy is operating in reverse proxy
|
|
|
|
// mode and if request `X-Forwarded-*` headers should be trusted
|
|
|
|
ReverseProxy bool
|
|
|
|
|
2021-03-21 11:20:57 -07:00
|
|
|
// RequestID is set to the request's `X-Request-Id` header if set.
|
|
|
|
// Otherwise a random UUID is set.
|
|
|
|
RequestID string
|
|
|
|
|
2020-07-04 18:41:58 +01:00
|
|
|
// Session details the authenticated users information (if it exists).
|
|
|
|
Session *sessions.SessionState
|
|
|
|
|
|
|
|
// SaveSession indicates whether the session storage should attempt to save
|
|
|
|
// the session or not.
|
|
|
|
SaveSession bool
|
|
|
|
|
|
|
|
// ClearSession indicates whether the user should be logged out or not.
|
|
|
|
ClearSession bool
|
|
|
|
|
|
|
|
// SessionRevalidated indicates whether the session has been revalidated since
|
|
|
|
// it was loaded or not.
|
|
|
|
SessionRevalidated bool
|
2021-03-06 09:27:16 -08:00
|
|
|
|
|
|
|
// Upstream tracks which upstream was used for this request
|
|
|
|
Upstream string
|
2020-07-04 18:41:58 +01:00
|
|
|
}
|
2021-01-02 13:16:01 -08:00
|
|
|
|
|
|
|
// GetRequestScope returns the current request scope from the given request
|
|
|
|
func GetRequestScope(req *http.Request) *RequestScope {
|
|
|
|
scope := req.Context().Value(RequestScopeKey)
|
|
|
|
if scope == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return scope.(*RequestScope)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRequestScope adds a RequestScope to a request
|
|
|
|
func AddRequestScope(req *http.Request, scope *RequestScope) *http.Request {
|
|
|
|
ctx := context.WithValue(req.Context(), RequestScopeKey, scope)
|
|
|
|
return req.WithContext(ctx)
|
|
|
|
}
|