Skip to content

Commit 621706a

Browse files
committed
Add basic OAuth support to gRPC.
The primary functionality is an interceptor that authenticates calls using oauth. However, most of the changes are for an integration test to make sure the code works with ESF. This is based on work by lryan and simonma. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=72567773
1 parent 88efcaf commit 621706a

File tree

6 files changed

+34
-6
lines changed

6 files changed

+34
-6
lines changed

core/src/main/java/com/google/net/stubby/newtransport/HttpUtil.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.net.stubby.transport.Transport;
44

5+
import java.net.HttpURLConnection;
6+
57
/**
68
* Constants for GRPC-over-HTTP (or HTTP/2)
79
*/
@@ -32,6 +34,15 @@ public final class HttpUtil {
3234
* Maps HTTP error response status codes to transport codes.
3335
*/
3436
public static Transport.Code httpStatusToTransportCode(int httpStatusCode) {
37+
// Specific HTTP code handling.
38+
switch (httpStatusCode) {
39+
case HttpURLConnection.HTTP_UNAUTHORIZED: // 401
40+
return Transport.Code.UNAUTHENTICATED;
41+
case HttpURLConnection.HTTP_FORBIDDEN: // 403
42+
return Transport.Code.PERMISSION_DENIED;
43+
default:
44+
}
45+
// Generic HTTP code handling.
3546
if (httpStatusCode < 300) {
3647
return Transport.Code.OK;
3748
}

core/src/main/java/com/google/net/stubby/newtransport/netty/NettyClientHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void onHeadersRead(ChannelHandlerContext ctx,
125125
boolean endSegment) throws Http2Exception {
126126
// TODO(user): Assuming that all headers fit in a single HEADERS frame.
127127
NettyClientStream stream = clientStream(connection().requireStream(streamId));
128-
stream.inboundHeadersRecieved(headers);
128+
stream.inboundHeadersRecieved(headers, endStream);
129129
}
130130

131131
/**

core/src/main/java/com/google/net/stubby/newtransport/netty/NettyClientStream.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ public void cancel() {
6161
/**
6262
* Called in the channel thread to process headers received from the server.
6363
*/
64-
public void inboundHeadersRecieved(Http2Headers headers) {
64+
public void inboundHeadersRecieved(Http2Headers headers, boolean endOfStream) {
6565
responseCode = responseCode(headers);
6666
isGrpcResponse = isGrpcResponse(headers, responseCode);
67+
if (!isGrpcResponse && endOfStream) {
68+
setStatus(new Status(responseCode));
69+
}
6770
}
6871

6972
/**

core/src/test/java/com/google/net/stubby/newtransport/netty/NettyClientHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public void inboundHeadersShouldForwardToStream() throws Exception {
187187
.set(HttpUtil.CONTENT_TYPE_HEADER, HttpUtil.CONTENT_TYPE_PROTORPC).build();
188188
ByteBuf headersFrame = headersFrame(3, headers);
189189
handler.channelRead(this.ctx, headersFrame);
190-
verify(stream).inboundHeadersRecieved(headers);
190+
verify(stream).inboundHeadersRecieved(headers, false);
191191
}
192192

193193
@Test

core/src/test/java/com/google/net/stubby/newtransport/netty/NettyClientStreamTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void setStatusWithErrorShouldNotOverridePreviousError() {
171171
@Test
172172
public void inboundContextShouldCallListener() throws Exception {
173173
// Receive headers first so that it's a valid GRPC response.
174-
stream.inboundHeadersRecieved(grpcResponseHeaders());
174+
stream.inboundHeadersRecieved(grpcResponseHeaders(), false);
175175

176176
stream.inboundDataReceived(contextFrame(), false, promise);
177177
ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
@@ -183,7 +183,7 @@ public void inboundContextShouldCallListener() throws Exception {
183183
@Test
184184
public void inboundMessageShouldCallListener() throws Exception {
185185
// Receive headers first so that it's a valid GRPC response.
186-
stream.inboundHeadersRecieved(grpcResponseHeaders());
186+
stream.inboundHeadersRecieved(grpcResponseHeaders(), false);
187187

188188
stream.inboundDataReceived(messageFrame(), false, promise);
189189
ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
@@ -197,7 +197,7 @@ public void inboundStatusShouldSetStatus() throws Exception {
197197
stream.id(1);
198198

199199
// Receive headers first so that it's a valid GRPC response.
200-
stream.inboundHeadersRecieved(grpcResponseHeaders());
200+
stream.inboundHeadersRecieved(grpcResponseHeaders(), false);
201201

202202
stream.inboundDataReceived(statusFrame(), false, promise);
203203
ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);

stub/src/main/java/com/google/net/stubby/stub/AbstractStub.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public StubConfigBuilder configureNewStub() {
3535
return new StubConfigBuilder();
3636
}
3737

38+
public Channel getChannel() {
39+
return channel;
40+
}
41+
3842
/**
3943
* Returns a new stub configuration for the provided method configurations.
4044
*/
@@ -46,8 +50,10 @@ public StubConfigBuilder configureNewStub() {
4650
public class StubConfigBuilder {
4751

4852
private final Map<String, MethodDescriptor> methodMap;
53+
private Channel channel;
4954

5055
private StubConfigBuilder() {
56+
this.channel = AbstractStub.this.channel;
5157
methodMap = Maps.newHashMapWithExpectedSize(config.methods().size());
5258
for (MethodDescriptor method : AbstractStub.this.config.methods()) {
5359
methodMap.put(method.getName(), method);
@@ -74,6 +80,14 @@ public StubConfigBuilder setTimeout(long timeout, TimeUnit unit) {
7480
return this;
7581
}
7682

83+
/**
84+
* Set the channel to be used by the stub.
85+
*/
86+
public StubConfigBuilder setChannel(Channel channel) {
87+
this.channel = channel;
88+
return this;
89+
}
90+
7791
/**
7892
* Create a new stub configuration
7993
*/

0 commit comments

Comments
 (0)