getRequestMetadata

Available in a dev version: 0․11․0-grpc-186

How to configure

Retrieves authentication metadata for the gRPC call.

This method is invoked before each gRPC call to generate authentication headers or metadata. Implementations should return a GrpcMetadata object containing the necessary authentication information for the request.

The method is suspending to allow asynchronous operations such as token retrieval from secure storage.

Context Information

The Context receiver provides access to call-specific information:

Examples

Simple bearer token:

override suspend fun Context.getRequestMetadata(): GrpcMetadata {
return GrpcMetadata {
append("Authorization", "Bearer $token")
}
}

Throwing a kotlinx.rpc.grpc.GrpcStatusException to fail the call:

override suspend fun Context.getRequestMetadata(): GrpcMetadata {
val token = try {
refreshToken()
} catch (e: Exception) {
throw GrpcStatusException(GrpcStatus(GrpcStatusCode.UNAUTHENTICATED, "Token refresh failed"))
}

return GrpcMetadata {
append("Authorization", "Bearer $token")
}
}

Receiver

Context information about the call being authenticated.

Return

Metadata containing authentication information to attach to the request.

Throws

to abort the call with a specific gRPC status.