Credentials authenticate a channel's calls without becoming part of the
channel. The secret stays in this R session: printing, saving or sending a
channel or a credentials object to another process carries only a key, and
a copy that reaches another process refuses every call with
zugrpc_error_argument until it is created again there.
Arguments
- token
The token, one non-empty string of printable ASCII.
- provider
A function of one argument, the method being called (as
"/package.Service/Method"). It returns the metadata to send: a named character vector or a named list, asmetadataingrpc_call(). A provider that raises an error fails the call withzugrpc_error_unavailable, the error kept asparent, and nothing is sent.
Value
A zugrpc_credentials, for credentials in grpc_channel().
Details
grpc_bearer_token() sends a fixed token as authorization: Bearer <token>. grpc_call_credentials() calls provider before every call, so
a token that expires can be refreshed. Its result is sent and never
stored.
Credentials need TLS: grpc_channel() accepts them for an https:// URL,
or for an http:// URL whose host is localhost, a 127.x.x.x address
or [::1].
Examples
creds <- grpc_bearer_token("my-token")
creds
#> <zugrpc_credentials> bearer token
ch <- grpc_channel("https://grpc.example.org:443", credentials = creds)
ch
#> <zugrpc_channel> grpc.example.org:443
#> transport: TLS, ALPN h2
#> auth: bearer token
#> state: open
#> streams: 0 open
grpc_close(ch)
# A token read when each call is made, for one that expires:
refresh <- grpc_call_credentials(function(method) {
c(authorization = paste("Bearer", Sys.getenv("MY_TOKEN")))
})