Skip to content

Commit 1cd88bc

Browse files
tangiellesv
authored andcommitted
update echo endpoint to support upcoming tutorial (GoogleCloudPlatform#436)
* update echo endpoint to support upcoming tutorial * remove unused import * genericize firebase auth configuration * add clientIds to google auth example * update README for editing Echo
1 parent 0b578f6 commit 1cd88bc

File tree

4 files changed

+108
-38
lines changed

4 files changed

+108
-38
lines changed

appengine/endpoints-frameworks-v2/backend/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ To add the project ID:
1515
0. For `<endpoints.project.id>`, replace the value `YOUR_PROJECT_ID` with
1616
your project ID.
1717

18+
0. Edit the file `src/main/java/com/example/echo/Echo.java`.
19+
20+
0. Replace the value `YOUR-PROJECT-ID` with your project ID.
21+
1822
0. Save your changes.
1923

2024
## Building the sample project

appengine/endpoints-frameworks-v2/backend/pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
<properties>
3232
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3333

34-
<endpoints.framework.version>2.0.0-beta.8</endpoints.framework.version>
35-
<endpoints.management.version>1.0.0-beta.10</endpoints.management.version>
34+
<endpoints.framework.version>2.0.0-beta.9</endpoints.framework.version>
35+
<endpoints.management.version>1.0.0-beta.11</endpoints.management.version>
3636

3737
<endpoints.project.id>YOUR_PROJECT_ID</endpoints.project.id>
3838
</properties>

appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/Echo.java

+102-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818

1919
import com.google.api.server.spi.auth.EspAuthenticator;
2020
import com.google.api.server.spi.auth.common.User;
21+
import com.google.api.server.spi.config.AnnotationBoolean;
2122
import com.google.api.server.spi.config.Api;
23+
import com.google.api.server.spi.config.ApiIssuer;
24+
import com.google.api.server.spi.config.ApiIssuerAudience;
2225
import com.google.api.server.spi.config.ApiMethod;
2326
import com.google.api.server.spi.config.ApiNamespace;
24-
import com.google.api.server.spi.config.AuthLevel;
27+
import com.google.api.server.spi.config.Named;
28+
import com.google.api.server.spi.config.Nullable;
29+
import com.google.api.server.spi.response.UnauthorizedException;
2530

2631
/** The Echo API which Endpoints will be exposing. */
32+
// [START echo_api_annotation]
2733
@Api(
2834
name = "echo",
2935
version = "v1",
@@ -32,20 +38,80 @@
3238
ownerDomain = "echo.example.com",
3339
ownerName = "echo.example.com",
3440
packagePath = ""
35-
)
41+
),
42+
// [START_EXCLUDE]
43+
issuers = {
44+
@ApiIssuer(
45+
name = "firebase",
46+
issuer = "https://securetoken.google.com/YOUR-PROJECT-ID",
47+
jwksUri = "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com")
48+
}
49+
// [END_EXCLUDE]
3650
)
51+
// [END echo_api_annotation]
3752
public class Echo {
3853
/**
39-
* Echoes the received message back.
54+
* Echoes the received message back. If n is a non-negative integer, the message is copied that
55+
* many times in the returned message.
4056
*
4157
* Note that name is specified and will override the default name of "{class name}.{method
4258
* name}". For example, the default is "echo.echo".
4359
*
4460
* Note that httpMethod is not specified. This will default to a reasonable HTTP method
4561
* depending on the API method name. In this case, the HTTP method will default to POST.
4662
*/
63+
// [START echo_method]
4764
@ApiMethod(name = "echo")
48-
public Message echo(Message message) {
65+
public Message echo(Message message, @Named("n") @Nullable Integer n) {
66+
return doEcho(message, n);
67+
}
68+
// [END echo_method]
69+
70+
/**
71+
* Echoes the received message back. If n is a non-negative integer, the message is copied that
72+
* many times in the returned message.
73+
*
74+
* Note that name is specified and will override the default name of "{class name}.{method
75+
* name}". For example, the default is "echo.echo".
76+
*
77+
* Note that httpMethod is not specified. This will default to a reasonable HTTP method
78+
* depending on the API method name. In this case, the HTTP method will default to POST.
79+
*/
80+
// [START echo_path]
81+
@ApiMethod(name = "echo_path_parameter", path = "echo/{n}")
82+
public Message echoPathParameter(Message message, @Named("n") int n) {
83+
return doEcho(message, n);
84+
}
85+
// [END echo_path]
86+
87+
/**
88+
* Echoes the received message back. If n is a non-negative integer, the message is copied that
89+
* many times in the returned message.
90+
*
91+
* Note that name is specified and will override the default name of "{class name}.{method
92+
* name}". For example, the default is "echo.echo".
93+
*
94+
* Note that httpMethod is not specified. This will default to a reasonable HTTP method
95+
* depending on the API method name. In this case, the HTTP method will default to POST.
96+
*/
97+
// [START echo_api_key]
98+
@ApiMethod(name = "echo_api_key", path = "echo_api_key", apiKeyRequired = AnnotationBoolean.TRUE)
99+
public Message echoApiKey(Message message, @Named("n") @Nullable Integer n) {
100+
return doEcho(message, n);
101+
}
102+
// [END echo_api_key]
103+
104+
private Message doEcho(Message message, Integer n) {
105+
if (n != null && n >= 0) {
106+
StringBuilder sb = new StringBuilder();
107+
for (int i = 0; i < n; i++) {
108+
if (i > 0) {
109+
sb.append(" ");
110+
}
111+
sb.append(message.getMessage());
112+
}
113+
message.setMessage(sb.toString());
114+
}
49115
return message;
50116
}
51117

@@ -59,19 +125,49 @@ public Message echo(Message message) {
59125
* Note that httpMethod is not required here. Without httpMethod, this will default to GET due
60126
* to the API method name. httpMethod is added here for example purposes.
61127
*/
128+
// [START google_id_token_auth]
62129
@ApiMethod(
63130
httpMethod = ApiMethod.HttpMethod.GET,
64131
authenticators = {EspAuthenticator.class},
65132
audiences = {"YOUR_OAUTH_CLIENT_ID"},
66-
authLevel = AuthLevel.REQUIRED
133+
clientIds = {"YOUR_OAUTH_CLIENT_ID"}
67134
)
68135
public Email getUserEmail(User user) throws UnauthorizedException {
69136
if (user == null) {
70-
throw new UnauthorizedException();
137+
throw new UnauthorizedException("Invalid credentials");
138+
}
139+
140+
Email response = new Email();
141+
response.setEmail(user.getEmail());
142+
return response;
143+
}
144+
// [END google_id_token_auth]
145+
146+
/**
147+
* Gets the authenticated user's email. If the user is not authenticated, this will return an HTTP
148+
* 401.
149+
*
150+
* Note that name is not specified. This will default to "{class name}.{method name}". For
151+
* example, the default is "echo.getUserEmail".
152+
*
153+
* Note that httpMethod is not required here. Without httpMethod, this will default to GET due
154+
* to the API method name. httpMethod is added here for example purposes.
155+
*/
156+
// [START firebase_auth]
157+
@ApiMethod(
158+
path = "firebase_user",
159+
httpMethod = ApiMethod.HttpMethod.GET,
160+
authenticators = {EspAuthenticator.class},
161+
issuerAudiences = {@ApiIssuerAudience(name = "firebase", audiences = {"YOUR-PROJECT-ID"})}
162+
)
163+
public Email getUserEmailFirebase(User user) throws UnauthorizedException {
164+
if (user == null) {
165+
throw new UnauthorizedException("Invalid credentials");
71166
}
72167

73168
Email response = new Email();
74169
response.setEmail(user.getEmail());
75170
return response;
76171
}
172+
// [END firebase_auth]
77173
}

appengine/endpoints-frameworks-v2/backend/src/main/java/com/example/echo/UnauthorizedException.java

-30
This file was deleted.

0 commit comments

Comments
 (0)