MCPcopy
hub / github.com/grpc/grpc-go / constructRequest

Function constructRequest

credentials/sts/sts.go:259–295  ·  view source on GitHub ↗

constructRequest creates the STS request body in JSON based on the provided options. - Contents of the subjectToken are read from the file specified in options. If we encounter an error here, we bail out. - Contents of the actorToken are read from the file specified in options. If we encounter an er

(ctx context.Context, opts Options)

Source from the content-addressed store, hash-verified

257// passing the provided context, thereby enforcing any timeouts specified in
258// the latter.
259func constructRequest(ctx context.Context, opts Options) (*http.Request, error) {
260 subToken, err := readSubjectTokenFrom(opts.SubjectTokenPath)
261 if err != nil {
262 return nil, err
263 }
264 reqScope := opts.Scope
265 if reqScope == "" {
266 reqScope = defaultCloudPlatformScope
267 }
268 reqParams := &requestParameters{
269 GrantType: tokenExchangeGrantType,
270 Resource: opts.Resource,
271 Audience: opts.Audience,
272 Scope: reqScope,
273 RequestedTokenType: opts.RequestedTokenType,
274 SubjectToken: string(subToken),
275 SubjectTokenType: opts.SubjectTokenType,
276 }
277 if opts.ActorTokenPath != "" {
278 actorToken, err := readActorTokenFrom(opts.ActorTokenPath)
279 if err != nil {
280 return nil, err
281 }
282 reqParams.ActorToken = string(actorToken)
283 reqParams.ActorTokenType = opts.ActorTokenType
284 }
285 jsonBody, err := json.Marshal(reqParams)
286 if err != nil {
287 return nil, err
288 }
289 req, err := http.NewRequestWithContext(ctx, "POST", opts.TokenExchangeServiceURI, bytes.NewBuffer(jsonBody))
290 if err != nil {
291 return nil, fmt.Errorf("failed to create http request: %v", err)
292 }
293 req.Header.Set("Content-Type", "application/json")
294 return req, nil
295}
296
297func sendRequest(client httpDoer, req *http.Request) ([]byte, error) {
298 // http.Client returns a non-nil error only if it encounters an error

Callers 3

GetRequestMetadataMethod · 0.85
TestConstructRequestMethod · 0.85
TestSendRequestMethod · 0.85

Calls 3

MarshalMethod · 0.65
ErrorfMethod · 0.65
SetMethod · 0.65

Tested by 2

TestConstructRequestMethod · 0.68
TestSendRequestMethod · 0.68