go auth_error ai_generated true

rpc error: code = PermissionDenied desc = unauthenticated: request not authenticated

ID: go/grpc-permission-denied-auth-missing

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-06-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.1 active

Root Cause

The gRPC call lacks required authentication credentials, such as a missing or invalid token.

generic

中文

gRPC调用缺少必需的身份验证凭据,例如令牌缺失或无效。

Workarounds

  1. 90% success Attach a valid authentication token to the gRPC metadata.
    token := "valid-jwt-token"
    conn, err := grpc.Dial("localhost:8080", grpc.WithInsecure(), grpc.WithPerRPCCredentials(&authCreds{token: token}))
    // Define authCreds implementing credentials.PerRPCCredentials
    func (c *authCreds) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
        return map[string]string{"authorization": "Bearer " + c.token}, nil
    }
    func (c *authCreds) RequireTransportSecurity() bool { return false }
  2. 85% success Implement an interceptor to add credentials automatically.
    func authInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
        md := metadata.Pairs("authorization", "Bearer token")
        ctx = metadata.NewOutgoingContext(ctx, md)
        return invoker(ctx, method, req, reply, cc, opts...)
    }
    conn, err := grpc.Dial("localhost:8080", grpc.WithInsecure(), grpc.WithUnaryInterceptor(authInterceptor))

Dead Ends

Common approaches that don't work:

  1. Retry the request without adding any credentials. 100% fail

    The server will continue to reject unauthenticated requests.

  2. Use a hardcoded token that may be expired. 90% fail

    Expired tokens are also rejected by the server.