Exemple #1
0
// Do sends an HTTP request with the provided http.Client and returns an HTTP response.
// If the client is nil, http.DefaultClient is used.
// If the context is canceled or times out, ctx.Err() will be returned.
func Do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
	if client == nil {
		client = http.DefaultClient
	}

	// Request cancelation changed in Go 1.5, see cancelreq.go and cancelreq_go14.go.
	cancel := canceler(client, req)

	type responseAndError struct {
		resp *http.Response
		err  error
	}
	result := make(chan responseAndError, 1)

	go func() {
		resp, err := client.Do(req)
		result <- responseAndError{resp, err}
	}()

	select {
	case <-ctx.Done():
		cancel()
		return nil, ctx.Err()
	case r := <-result:
		return r.resp, r.err
	}
}
Exemple #2
0
func SubjectFromContext(ctx context.Context) (string, error) {
	args, ok := ctx.Value(authKey).(*authorization)
	if !ok {
		return "", errors.Errorf("Could not assert type for %v", ctx.Value(authKey))
	}
	return args.claims.GetSubject(), nil
}
Exemple #3
0
func TokenFromContext(ctx context.Context) (*jwt.Token, error) {
	args, ok := ctx.Value(authKey).(*authorization)
	if !ok {
		return nil, errors.Errorf("Could not assert type for %v", ctx.Value(authKey))
	}
	return args.token, nil
}
Exemple #4
0
func ContextClient(ctx context.Context) (*http.Client, error) {
	for _, fn := range contextClientFuncs {
		c, err := fn(ctx)
		if err != nil {
			return nil, err
		}
		if c != nil {
			return c, nil
		}
	}
	if hc, ok := ctx.Value(HTTPClient).(*http.Client); ok {
		return hc, nil
	}
	return http.DefaultClient, nil
}
Exemple #5
0
func PoliciesFromContext(ctx context.Context) ([]policy.Policy, error) {
	args, ok := ctx.Value(authKey).(*authorization)
	if !ok {
		return nil, errors.Errorf("Could not assert array type for %s", ctx.Value(authKey))
	}

	symbols := make([]policy.Policy, len(args.policies))
	for i, arg := range args.policies {
		symbols[i], ok = arg.(*policy.DefaultPolicy)
		if !ok {
			return nil, errors.Errorf("Could not assert policy type for %s", ctx.Value(authKey))
		}
	}

	return symbols, nil
}
Exemple #6
0
func IsAuthenticatedFromContext(ctx context.Context) bool {
	a, b := ctx.Value(authKey).(*authorization)
	return (b && a.token != nil && a.token.Valid)
}