DoPerRPCCreds performs a unary RPC with per RPC OAUTH2 token.
(ctx context.Context, tc testgrpc.TestServiceClient, serviceAccountKeyFile, oauthScope string)
| 375 | |
| 376 | // DoPerRPCCreds performs a unary RPC with per RPC OAUTH2 token. |
| 377 | func DoPerRPCCreds(ctx context.Context, tc testgrpc.TestServiceClient, serviceAccountKeyFile, oauthScope string) { |
| 378 | jsonKey := getServiceAccountJSONKey(serviceAccountKeyFile) |
| 379 | pl := ClientNewPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize) |
| 380 | req := &testpb.SimpleRequest{ |
| 381 | ResponseType: testpb.PayloadType_COMPRESSABLE, |
| 382 | ResponseSize: int32(largeRespSize), |
| 383 | Payload: pl, |
| 384 | FillUsername: true, |
| 385 | FillOauthScope: true, |
| 386 | } |
| 387 | token := GetToken(ctx, serviceAccountKeyFile, oauthScope) |
| 388 | kv := map[string]string{"authorization": token.Type() + " " + token.AccessToken} |
| 389 | ctx = metadata.NewOutgoingContext(ctx, metadata.MD{"authorization": []string{kv["authorization"]}}) |
| 390 | reply, err := tc.UnaryCall(ctx, req) |
| 391 | if err != nil { |
| 392 | logger.Fatal("/TestService/UnaryCall RPC failed: ", err) |
| 393 | } |
| 394 | user := reply.GetUsername() |
| 395 | scope := reply.GetOauthScope() |
| 396 | if !strings.Contains(string(jsonKey), user) { |
| 397 | logger.Fatalf("Got user name %q which is NOT a substring of %q.", user, jsonKey) |
| 398 | } |
| 399 | if !strings.Contains(oauthScope, scope) { |
| 400 | logger.Fatalf("Got OAuth scope %q which is NOT a substring of %q.", scope, oauthScope) |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // DoGoogleDefaultCredentials performs a unary RPC with google default credentials |
| 405 | func DoGoogleDefaultCredentials(ctx context.Context, tc testgrpc.TestServiceClient, defaultServiceAccount string) { |
no test coverage detected