Documentation
¶
Index ¶
- Constants
- Variables
- func BearerAuth(r *http.Request) (bearerToken string, ok bool)
- func GetClientCredentialsToken(tokenEndpoint, client_id, client_secret string, scope []string) (token string, tokenExpires time.Time, err error)
- func GetPasswordToken(tokenEndpoint, client_id, client_secret, username, password string, ...) (token string, tokenExpires time.Time, refreshToken string, err error)
- func GetRefreshToken(tokenEndpoint, client_id, client_secret, refresh_token string, scope []string) (token string, tokenExpires time.Time, err error)
- func GetTokenInformation(tokenInfoEndpoint, token string) (tokenInfo map[string]interface{}, err error)
- func IsJSONAnAcceptableResponse(acceptHeader string) bool
- func LogApache(trw *ResponseWriter, r *http.Request) string
- func LogCommonExtended(trw *ResponseWriter, r *http.Request) string
- func LogCommonExtendedForwarded(trw *ResponseWriter, r *http.Request) string
- func NewCorsHandler(h http.Handler, cors *Cors) http.Handler
- func NewDefaultCorsHandler(h http.Handler) http.Handler
- func NewForwardedLoggingHandler(h http.Handler, log io.Writer) http.Handler
- func NewLoggingHandler(h http.Handler, log io.Writer) http.Handler
- func NewPanicRecoveryHandler(h http.Handler) http.Handler
- func NewReverseProxyHandler(destinationURL *url.URL, stripListenPath bool, listenPath string) http.Handler
- func ProcessCors(w http.ResponseWriter, r *http.Request)
- type Cors
- type GatewayReverseProxy
- type ResponseWriter
- func (trw *ResponseWriter) CloseNotify() <-chan bool
- func (trw *ResponseWriter) Flush()
- func (trw *ResponseWriter) GetCloseNotifier() (closeNotifier http.CloseNotifier, ok bool)
- func (trw *ResponseWriter) GetFlusher() (flusher http.Flusher, ok bool)
- func (trw *ResponseWriter) GetHijacker() (hijacker http.Hijacker, ok bool)
- func (trw *ResponseWriter) Header() http.Header
- func (trw *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (trw *ResponseWriter) Length() int64
- func (trw *ResponseWriter) LogCommonExtended(r *http.Request) string
- func (trw *ResponseWriter) LogCommonExtendedForwarded(r *http.Request) string
- func (trw *ResponseWriter) Status() int
- func (trw *ResponseWriter) Write(bytes []byte) (int, error)
- func (trw *ResponseWriter) WriteHeader(status int)
- type TokenTracker
Constants ¶
const ( CorsOrigin = "Origin" CorsAccessControlRequestMethod = "Access-Control-Request-Method" CorsAccessControlRequestHeader = "Access-Control-Request-Header" CorsAccessControlAllowOrigin = "Access-Control-Allow-Origin" CorsAccessControlAllowMethods = "Access-Control-Allow-Methods" CorsAccessControlAllowHeaders = "Access-Control-Allow-Headers" CorsAccessControlAllowCredentials = "Access-Control-Allow-Credentials" CorsAccessControlExposeHeaders = "Access-Control-Expose-Headers" CorsAccessControlMaxAge = "Access-Control-Max-Age" )
Variables ¶
var ( CorsDefaultAllowOrigin = "*" CorsDefaultAllowHeaders = []string{"Location", "Content-Type", "ETag", "Accept-Patch"} CorsDefaultAllowMethods = []string{"OPTIONS", "HEAD", "GET", "POST", "PUT", "PATCH", "DELETE"} CorsDefaultExposeHeaders = []string{"Location", "Content-Type", "ETag", "Accept-Patch"} CorsDefaultMaxAge = int64(1728000) CorsDefaultAllowCredentials = true )
Functions ¶
func BearerAuth ¶
BearerAuth is a function that will pull an access token out of the Authorization header it will return the bearer token if found, and ok will tell you whether it was able to find the token or not. This function will look for the token in the query params, as well as the headers.
func GetClientCredentialsToken ¶
func GetClientCredentialsToken(tokenEndpoint, client_id, client_secret string, scope []string) (token string, tokenExpires time.Time, err error)
This method uses the client_credentials grant_type of oauth2 to obtain a token from the token endpoint.
func GetPasswordToken ¶
func GetPasswordToken(tokenEndpoint, client_id, client_secret, username, password string, scope []string) (token string, tokenExpires time.Time, refreshToken string, err error)
This method uses the password grant type of oauth2 to get a token from the token endpoint.
func GetRefreshToken ¶
func GetRefreshToken(tokenEndpoint, client_id, client_secret, refresh_token string, scope []string) (token string, tokenExpires time.Time, err error)
This method uses the refresh_token grant type of oauth2 to obtain a token from the token endpoint
func GetTokenInformation ¶
func GetTokenInformation(tokenInfoEndpoint, token string) (tokenInfo map[string]interface{}, err error)
This method calls the token information endpoint and returns the json response as a map of string to interface{} values.
func IsJSONAnAcceptableResponse ¶
This function will take in the accept header string from a inbound request and determine if application/json is an acceptable response for the request. It ignores any priorities that the requester has, and if they don't include an accept header it will treat it as if they had just used Accept: */*
func LogCommonExtended ¶
func LogCommonExtended(trw *ResponseWriter, r *http.Request) string
func LogCommonExtendedForwarded ¶
func LogCommonExtendedForwarded(trw *ResponseWriter, r *http.Request) string
func NewReverseProxyHandler ¶
func ProcessCors ¶
func ProcessCors(w http.ResponseWriter, r *http.Request)
Types ¶
type Cors ¶
type Cors struct {
AllowOrigin string
AllowHeaders []string
AllowMethods []string
ExposeHeaders []string
MaxAge int64
AllowCredentials bool
}
var DefaultCors *Cors
func (*Cors) ProcessCors ¶
func (cors *Cors) ProcessCors(w http.ResponseWriter, r *http.Request)
This function will write out cross origin headers so that javascript clients can call apis.
type GatewayReverseProxy ¶
type GatewayReverseProxy struct {
Name string
StripListenPath bool
ListenPath string
// Director must be a function which modifies
// the request into a new request to be sent
// using Transport. Its response is then copied
// back to the original client unmodified.
Director func(*http.Request)
// The transport used to perform proxy requests.
// If nil, http.DefaultTransport is used.
Transport http.RoundTripper
// FlushInterval specifies the flush interval
// to flush to the client while copying the
// response body.
// If zero, no periodic flushing is done.
FlushInterval time.Duration
// ErrorLog specifies an optional logger for errors
// that occur when attempting to proxy the request.
// If nil, logging goes to os.Stderr via the log package's
// standard logger.
ErrorLog *log.Logger
}
ReverseProxy is an HTTP Handler that takes an incoming request and sends it to another server, proxying the response back to the client.
func NewGatewayReverseProxy ¶
func NewGatewayReverseProxy(target *url.URL, stripListenPath bool, listenPath string) *GatewayReverseProxy
NewGatewayReverseProxy returns a new GatewayReverseProxy that rewrites URLs to the scheme, host, and base path provided in target. If the target's path is "/base" and the incoming request was for "/dir", the target request will be for /base/dir.
func (*GatewayReverseProxy) ServeHTTP ¶
func (p *GatewayReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request)
type ResponseWriter ¶
type ResponseWriter struct {
// contains filtered or unexported fields
}
func NewResponseWriter ¶
func NewResponseWriter(w http.ResponseWriter) *ResponseWriter
func (*ResponseWriter) CloseNotify ¶
func (trw *ResponseWriter) CloseNotify() <-chan bool
func (*ResponseWriter) Flush ¶
func (trw *ResponseWriter) Flush()
func (*ResponseWriter) GetCloseNotifier ¶
func (trw *ResponseWriter) GetCloseNotifier() (closeNotifier http.CloseNotifier, ok bool)
func (*ResponseWriter) GetFlusher ¶
func (trw *ResponseWriter) GetFlusher() (flusher http.Flusher, ok bool)
func (*ResponseWriter) GetHijacker ¶
func (trw *ResponseWriter) GetHijacker() (hijacker http.Hijacker, ok bool)
func (*ResponseWriter) Header ¶
func (trw *ResponseWriter) Header() http.Header
func (*ResponseWriter) Hijack ¶
func (trw *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
func (*ResponseWriter) Length ¶
func (trw *ResponseWriter) Length() int64
func (*ResponseWriter) LogCommonExtended ¶
func (trw *ResponseWriter) LogCommonExtended(r *http.Request) string
func (*ResponseWriter) LogCommonExtendedForwarded ¶
func (trw *ResponseWriter) LogCommonExtendedForwarded(r *http.Request) string
func (*ResponseWriter) Status ¶
func (trw *ResponseWriter) Status() int
func (*ResponseWriter) WriteHeader ¶
func (trw *ResponseWriter) WriteHeader(status int)
type TokenTracker ¶
type TokenTracker struct {
// contains filtered or unexported fields
}
A structure to cache oauth2 state. Has helper methods which will allow users to pull cached tokens from the structure when needed without utilizing the network to obtain new tokens constantly
func NewClientCredentialsTokenTracker ¶
func NewClientCredentialsTokenTracker(tokenEndpoint, tokenInfoEndpoint, client_id, client_secret string, scope []string) (tokenTracker *TokenTracker, err error)
This method will return a new TokenTracker set up to cache and obtain new tokens in behalf of a oauth2 client. It will always use the client_credentials grant type to obtain the new tokens from the token endpoint.
func NewPasswordTokenTracker ¶
func NewPasswordTokenTracker(tokenEndpoint, tokenInfoEndpoint, client_id, client_secret, username, password string, scope []string) (tokenTracker *TokenTracker, err error)
This method will return a new TokenTracker set up to cache and obtain tokens on behalf of a particular user. It will use the password grant type to obtain the first token, and a refresh token. Subsequent tokens will be obtained by using the refresh_token grant type. It will not cache the username and password for the user. So if for any reason the refresh_token is invalidated or revoked special logic will need to be done (outside of this lib) to reset the TokenTracker so it doesn't forever remain in a bad state.
func (*TokenTracker) GetNewToken ¶
func (tt *TokenTracker) GetNewToken() (token string, err error)
This method will fetch a new token from the token endpoint. It will replace any cached tokens that the tracker has. It uses the client credentials to get a new token on behalf of a client, and uses the refresh token to get a token on behalf of a client and user combination.
func (*TokenTracker) GetSafeToken ¶
func (tt *TokenTracker) GetSafeToken() (token string, err error)
This method of the token tracker will verify the veracity of the token against the token info endpoint before it returns it to the user. If the token info endpoint returns anything other than a 200 status, this endpoint will then attempt to get a fresh token.
func (*TokenTracker) GetToken ¶
func (tt *TokenTracker) GetToken() (token string, err error)
The get token method will grab a cached token from it's store if it has not expired (using the expires_in value from the original token call. It is not guaranteed to be a valid token as it may have been invalidated or revoked before it expired. If you want to ensure the token is valid use the GetSafeToken() method.
func (*TokenTracker) GetTokenInformation ¶
func (tt *TokenTracker) GetTokenInformation() (tokenInfo map[string]interface{}, err error)
This method will return the cached token information if it is available, or call the token information endpoint if it doesn't have any.