go auth_error ai_generated true

rpc error: code = PermissionDenied desc = missing or invalid authorization token

ID: go/grpc-permission-denied-token

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-09-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.45+ active — — —

Root Cause

The server's auth interceptor rejected the request because the metadata did not contain a valid bearer token, or the token was expired.

generic

中文

服务端的认证拦截器拒绝了请求,因为 metadata 中不含有效的 bearer 令牌,或令牌已过期。

Workarounds

  1. 92% success
    md := metadata.Pairs("authorization", "Bearer "+token)
    ctx = metadata.NewOutgoingContext(ctx, md)
    resp, err := client.Get(ctx, req)
    // or as a unary interceptor:
    func auth(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, inv grpc.UnaryInvoker, opts ...grpc.CallOption) error {
        ctx = metadata.AppendToOutgoingContext(ctx, "authorization", "Bearer "+getToken())
        return inv(ctx, method, req, reply, cc, opts...)
    }
  2. 90% success
    type tokenAuth struct{ token string }
    func (t tokenAuth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
        return map[string]string{"authorization": "Bearer " + t.token}, nil
    }
    func (t tokenAuth) RequireTransportSecurity() bool { return true }
    conn, _ := grpc.Dial(addr, grpc.WithPerRPCCredentials(tokenAuth{token: tok}))

Dead Ends

Common approaches that don't work:

  1. 92% fail

    An expired or malformed token is rejected deterministically on every attempt.

  2. 95% fail

    Removes the security boundary entirely and lets unauthenticated clients reach protected handlers.