コード例 #1
0
ファイル: handler.go プロジェクト: zalando/planb-tokeninfo
// Checks if the Request contains a JWT that can be handled by this Handler
func (h *jwtHandler) Match(r *http.Request) bool {
	token := tokeninfo.AccessTokenFromRequest(r)
	if token == "" {
		return false
	}

	parts := strings.Split(token, ".")

	return len(parts) == 3
}
コード例 #2
0
ファイル: handler.go プロジェクト: zalando/planb-tokeninfo
// ServeHTTP proxies the Request with an Access Token to the upstream and sends back the response
// from the upstream
func (h *tokenInfoProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	token := tokeninfo.AccessTokenFromRequest(req)
	if token == "" {
		tokeninfo.ErrInvalidRequest.Write(w)
		return
	}
	start := time.Now()
	item := h.cache.Get(token)
	if item != nil {
		if !item.Expired() {
			incCounter("planb.tokeninfo.proxy.cache.hits")
			w.Header().Set("Content-Type", "application/json;charset=UTF-8")
			w.Header().Set("X-Cache", "HIT")
			w.Write(item.Value().([]byte))
			return
		} else {
			incCounter("planb.tokeninfo.proxy.cache.expirations")
		}
	}
	incCounter("planb.tokeninfo.proxy.cache.misses")
	err := hystrix.Do("proxy", func() error {
		upstreamStart := time.Now()
		rw := newResponseBuffer(w)
		rw.Header().Set("X-Cache", "MISS")
		h.upstream.ServeHTTP(rw, req)
		if rw.StatusCode == http.StatusOK && h.cacheTTL > 0 {
			h.cache.Set(token, rw.Buffer.Bytes(), h.cacheTTL)
		}
		upstreamTimer := metrics.DefaultRegistry.GetOrRegister("planb.tokeninfo.proxy.upstream", metrics.NewTimer).(metrics.Timer)
		upstreamTimer.UpdateSince(upstreamStart)
		return nil
	}, nil)

	if err != nil {
		status := http.StatusInternalServerError
		w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
		switch err {
		case hystrix.ErrTimeout:
			{
				status = http.StatusGatewayTimeout
				incCounter("planb.tokeninfo.proxy.upstream.timeouts")
			}
		case hystrix.ErrMaxConcurrency:
			{
				status = http.StatusTooManyRequests
				incCounter("planb.tokeninfo.proxy.upstream.overruns")
			}
		case hystrix.ErrCircuitOpen:
			{
				status = http.StatusBadGateway
				incCounter("planb.tokeninfo.proxy.upstream.openrequests")
			}
		}
		w.WriteHeader(status)
		w.Write([]byte(http.StatusText(status)))
		return
	}

	t := metrics.DefaultRegistry.GetOrRegister("planb.tokeninfo.proxy", metrics.NewTimer).(metrics.Timer)
	t.UpdateSince(start)
}