// 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 } }
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 }
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 }
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 }
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 }
func IsAuthenticatedFromContext(ctx context.Context) bool { a, b := ctx.Value(authKey).(*authorization) return (b && a.token != nil && a.token.Valid) }