Authentication Required for Flow gRPC (Access API)
To ensure secure access to Flow gRPC (Access API), users are required to authenticate themselves. This authentication process is necessary before utilizing any method. Quicknode endpoints consist of two crucial components: the endpoint 以及相应的 token. 用户需要在调用任何方法之前,使用这两个组件配置一个带有身份验证凭据的gRPC 。
Authentication for the Flow gRPC (Access API) can be handled in two ways:
- 基本身份验证
- x-token 身份验证
在本文档中,我们将把 getAccessClientWithBasicAuth 或 getAccessClientWithXToken 用于演示如何处理这些不同身份验证机制的函数。
基本身份验证
该 getAccessClientWithBasicAuth 该函数演示了如何使用基本身份验证(Basic Authentication)来处理身份验证,该方法会将凭据编码为 base64。以下是该函数的代码实现: getAccessClientWithBasicAuth 功能以及 basicAuth RPC 凭据的实现:
import (
"上下文"
"crypto/tls"
"encoding/base64"
"fmt"
"github.com/onflow/flow/protobuf/go/flow/access"
"google.golang.grpc"
"google.golang.grpc"
)
func getAccessClientWithBasicAuth(endpoint, token string) (access.AccessAPIClient, error) {
target := endpoint + ".flow-mainnet.quiknode.pro:8999" // for TLS connections
client, err := grpc.Dial(target,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(basicAuth{
username: endpoint,
password: token,
}),
)
if err != nil {
return nil, fmt.Errorf("Unable to dial endpoint %w", err)
}
return access.NewAccessAPIClient(client), nil
}
// basicAuth implements the credentials.PerRPCCredentials interface to support basic authentication for grpc requests.
type basicAuth struct {
username string
password string
}
func (b basicAuth) GetRequestMetadata(ctx context.Context, in ...string) (map[string]string, error) {
auth := b.username + ":" + b.password
enc := base64.StdEncoding.EncodeToString([]byte(auth))
return map[string]string{"authorization": "Basic " + enc}, nil
}
func (basicAuth) RequireTransportSecurity() bool {
return false
}
该 getAccessClientWithBasicAuth 该函数会使用必要的安全选项配置一个gRPC ,并连接到endpoint 8999. It takes the endpoint name and token as input parameters and returns an instance of the AccessAPIClient interface, which you can use to make authenticated API calls.
client, err := getAccessClientWithBasicAuth("ENDPOINT_NAME", "TOKEN")
if err != nil {
log.Fatalf("err: %v", err)
}
x-token 身份验证
该 getAccessClientWithXToken 该函数演示了如何使用 x-token 进行身份验证。此方法会将令牌附加到每个请求的 x-token 头中。
import (
"上下文"
"crypto/tls"
"fmt"
"github.com/onflow/flow/protobuf/go/flow/access"
"google.golang.grpc"
"google.golang.grpc"
)
func getAccessClientWithXToken(endpoint, token string) (access.AccessAPIClient, error) {
target := endpoint + ".flow-mainnet.quiknode.pro:8999" // for TLS connections
client, err := grpc.Dial(target,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(auth{
token: token,
}),
)
if err != nil {
return nil, fmt.Errorf("Unable to dial endpoint %w", err)
}
return access.NewAccessAPIClient(client), nil
}
// auth implements the credentials.PerRPCCredentials interface to support x-token authentication for grpc requests.
type auth struct {
token string
}
func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"x-token": a.token,
}, nil
}
func (auth) RequireTransportSecurity() bool {
return false
}
该方法的配置gRPC 与基本身份验证示例类似,但会将身份验证令牌附加到 x-token 头中。以下是使用此函数进行 API 调用的方法:
client, err := getAccessClientWithXToken("ENDPOINT_NAME", "TOKEN")
if err != nil {
log.Fatalf("err: %v", err)
}