Skip to content

Commit bccaf07

Browse files
committed
Use real authority parsing in ClientAuthInterceptor
1 parent 5b2a03a commit bccaf07

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

auth/src/main/java/io/grpc/auth/ClientAuthInterceptor.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,31 @@ private URI serviceUri(Channel channel, MethodDescriptor<?, ?> method) throws St
116116
}
117117
// Always use HTTPS, by definition.
118118
final String scheme = "https";
119-
// The default port must not be present. Alternative ports should be present.
120-
final String suffixToStrip = ":443";
121-
if (authority.endsWith(suffixToStrip)) {
122-
authority = authority.substring(0, authority.length() - suffixToStrip.length());
123-
}
119+
final int defaultPort = 443;
124120
String path = "/" + MethodDescriptor.extractFullServiceName(method.getFullMethodName());
121+
URI uri;
125122
try {
126-
return new URI(scheme, authority, path, null, null);
123+
uri = new URI(scheme, authority, path, null, null);
127124
} catch (URISyntaxException e) {
128125
throw Status.UNAUTHENTICATED.withDescription("Unable to construct service URI for auth")
129126
.withCause(e).asException();
130127
}
128+
// The default port must not be present. Alternative ports should be present.
129+
if (uri.getPort() == defaultPort) {
130+
uri = removePort(uri);
131+
}
132+
return uri;
133+
}
134+
135+
private URI removePort(URI uri) throws StatusException {
136+
try {
137+
return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), -1 /* port */,
138+
uri.getPath(), uri.getQuery(), uri.getFragment());
139+
} catch (URISyntaxException e) {
140+
throw Status.UNAUTHENTICATED.withDescription(
141+
"Unable to construct service URI after removing port")
142+
.withCause(e).asException();
143+
}
131144
}
132145

133146
private Map<String, List<String>> getRequestMetadata(URI uri) throws StatusException {

0 commit comments

Comments
 (0)