-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update echo endpoint to support upcoming tutorial (#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
- Loading branch information
Showing
4 changed files
with
108 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,12 +18,18 @@ | |
|
||
import com.google.api.server.spi.auth.EspAuthenticator; | ||
import com.google.api.server.spi.auth.common.User; | ||
import com.google.api.server.spi.config.AnnotationBoolean; | ||
import com.google.api.server.spi.config.Api; | ||
import com.google.api.server.spi.config.ApiIssuer; | ||
import com.google.api.server.spi.config.ApiIssuerAudience; | ||
import com.google.api.server.spi.config.ApiMethod; | ||
import com.google.api.server.spi.config.ApiNamespace; | ||
import com.google.api.server.spi.config.AuthLevel; | ||
import com.google.api.server.spi.config.Named; | ||
import com.google.api.server.spi.config.Nullable; | ||
import com.google.api.server.spi.response.UnauthorizedException; | ||
|
||
/** The Echo API which Endpoints will be exposing. */ | ||
// [START echo_api_annotation] | ||
@Api( | ||
name = "echo", | ||
version = "v1", | ||
|
@@ -32,20 +38,80 @@ | |
ownerDomain = "echo.example.com", | ||
ownerName = "echo.example.com", | ||
packagePath = "" | ||
) | ||
), | ||
// [START_EXCLUDE] | ||
issuers = { | ||
@ApiIssuer( | ||
name = "firebase", | ||
issuer = "https://securetoken.google.com/YOUR-PROJECT-ID", | ||
jwksUri = "https://www.googleapis.com/robot/v1/metadata/x509/[email protected]") | ||
} | ||
// [END_EXCLUDE] | ||
) | ||
// [END echo_api_annotation] | ||
public class Echo { | ||
/** | ||
* Echoes the received message back. | ||
* Echoes the received message back. If n is a non-negative integer, the message is copied that | ||
* many times in the returned message. | ||
* | ||
* Note that name is specified and will override the default name of "{class name}.{method | ||
* name}". For example, the default is "echo.echo". | ||
* | ||
* Note that httpMethod is not specified. This will default to a reasonable HTTP method | ||
* depending on the API method name. In this case, the HTTP method will default to POST. | ||
*/ | ||
// [START echo_method] | ||
@ApiMethod(name = "echo") | ||
public Message echo(Message message) { | ||
public Message echo(Message message, @Named("n") @Nullable Integer n) { | ||
return doEcho(message, n); | ||
} | ||
// [END echo_method] | ||
|
||
/** | ||
* Echoes the received message back. If n is a non-negative integer, the message is copied that | ||
* many times in the returned message. | ||
* | ||
* Note that name is specified and will override the default name of "{class name}.{method | ||
* name}". For example, the default is "echo.echo". | ||
* | ||
* Note that httpMethod is not specified. This will default to a reasonable HTTP method | ||
* depending on the API method name. In this case, the HTTP method will default to POST. | ||
*/ | ||
// [START echo_path] | ||
@ApiMethod(name = "echo_path_parameter", path = "echo/{n}") | ||
public Message echoPathParameter(Message message, @Named("n") int n) { | ||
return doEcho(message, n); | ||
} | ||
// [END echo_path] | ||
|
||
/** | ||
* Echoes the received message back. If n is a non-negative integer, the message is copied that | ||
* many times in the returned message. | ||
* | ||
* Note that name is specified and will override the default name of "{class name}.{method | ||
* name}". For example, the default is "echo.echo". | ||
* | ||
* Note that httpMethod is not specified. This will default to a reasonable HTTP method | ||
* depending on the API method name. In this case, the HTTP method will default to POST. | ||
*/ | ||
// [START echo_api_key] | ||
@ApiMethod(name = "echo_api_key", path = "echo_api_key", apiKeyRequired = AnnotationBoolean.TRUE) | ||
public Message echoApiKey(Message message, @Named("n") @Nullable Integer n) { | ||
return doEcho(message, n); | ||
} | ||
// [END echo_api_key] | ||
|
||
private Message doEcho(Message message, Integer n) { | ||
if (n != null && n >= 0) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < n; i++) { | ||
if (i > 0) { | ||
sb.append(" "); | ||
} | ||
sb.append(message.getMessage()); | ||
} | ||
message.setMessage(sb.toString()); | ||
} | ||
return message; | ||
} | ||
|
||
|
@@ -59,19 +125,49 @@ public Message echo(Message message) { | |
* Note that httpMethod is not required here. Without httpMethod, this will default to GET due | ||
* to the API method name. httpMethod is added here for example purposes. | ||
*/ | ||
// [START google_id_token_auth] | ||
@ApiMethod( | ||
httpMethod = ApiMethod.HttpMethod.GET, | ||
authenticators = {EspAuthenticator.class}, | ||
audiences = {"YOUR_OAUTH_CLIENT_ID"}, | ||
authLevel = AuthLevel.REQUIRED | ||
clientIds = {"YOUR_OAUTH_CLIENT_ID"} | ||
) | ||
public Email getUserEmail(User user) throws UnauthorizedException { | ||
if (user == null) { | ||
throw new UnauthorizedException(); | ||
throw new UnauthorizedException("Invalid credentials"); | ||
} | ||
|
||
Email response = new Email(); | ||
response.setEmail(user.getEmail()); | ||
return response; | ||
} | ||
// [END google_id_token_auth] | ||
|
||
/** | ||
* Gets the authenticated user's email. If the user is not authenticated, this will return an HTTP | ||
* 401. | ||
* | ||
* Note that name is not specified. This will default to "{class name}.{method name}". For | ||
* example, the default is "echo.getUserEmail". | ||
* | ||
* Note that httpMethod is not required here. Without httpMethod, this will default to GET due | ||
* to the API method name. httpMethod is added here for example purposes. | ||
*/ | ||
// [START firebase_auth] | ||
@ApiMethod( | ||
path = "firebase_user", | ||
httpMethod = ApiMethod.HttpMethod.GET, | ||
authenticators = {EspAuthenticator.class}, | ||
issuerAudiences = {@ApiIssuerAudience(name = "firebase", audiences = {"YOUR-PROJECT-ID"})} | ||
) | ||
public Email getUserEmailFirebase(User user) throws UnauthorizedException { | ||
if (user == null) { | ||
throw new UnauthorizedException("Invalid credentials"); | ||
} | ||
|
||
Email response = new Email(); | ||
response.setEmail(user.getEmail()); | ||
return response; | ||
} | ||
// [END firebase_auth] | ||
} |
30 changes: 0 additions & 30 deletions
30
...endpoints-frameworks-v2/backend/src/main/java/com/example/echo/UnauthorizedException.java
This file was deleted.
Oops, something went wrong.