|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * <p>http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * <p>Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | + * express or implied. See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.google.chat.app.secured; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.logging.Logger; |
| 18 | + |
| 19 | +import org.springframework.boot.SpringApplication; |
| 20 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 21 | +import org.springframework.web.bind.annotation.PostMapping; |
| 22 | +import org.springframework.web.bind.annotation.RequestBody; |
| 23 | +import org.springframework.web.bind.annotation.RequestHeader; |
| 24 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 25 | +import org.springframework.web.bind.annotation.RestController; |
| 26 | + |
| 27 | +import com.fasterxml.jackson.databind.JsonNode; |
| 28 | +import com.google.api.client.json.GenericJson; |
| 29 | +import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken; |
| 30 | +import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier; |
| 31 | +import com.google.api.client.http.apache.ApacheHttpTransport; |
| 32 | +import com.google.api.client.json.JsonFactory; |
| 33 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 34 | +import com.google.api.services.chat.v1.model.Message; |
| 35 | + |
| 36 | +@SpringBootApplication |
| 37 | +@RestController |
| 38 | +public class App { |
| 39 | + |
| 40 | + // Service account email to verify requests from |
| 41 | + static String SERVICE_ACCOUNT_EMAIL = "your-add-on-service-account-email"; |
| 42 | + |
| 43 | + // Endpoint URL of the add-on |
| 44 | + static String HTTP_ENDPOINT = "your-add-on-endpoint-url"; |
| 45 | + |
| 46 | + private static final Logger logger = Logger.getLogger(App.class.getName()); |
| 47 | + |
| 48 | + public static void main(String[] args) { |
| 49 | + SpringApplication.run(App.class, args); |
| 50 | + } |
| 51 | + |
| 52 | + // Returns a simple text message app response based on whether the request is verified or not. |
| 53 | + @PostMapping("/") |
| 54 | + @ResponseBody |
| 55 | + public GenericJson onEvent( |
| 56 | + @RequestBody JsonNode event, @RequestHeader("Authorization") String authorization) |
| 57 | + throws Exception { |
| 58 | + return new GenericJson() {{ |
| 59 | + put("hostAppDataAction", new GenericJson() {{ |
| 60 | + put("chatDataAction", new GenericJson() {{ |
| 61 | + put("createMessageAction", new GenericJson() {{ |
| 62 | + put("message", new Message() |
| 63 | + .setText(verifyAddOnRequest(event, authorization) ? "Successful verification!" : "Failed verification!")); |
| 64 | + }}); |
| 65 | + }}); |
| 66 | + }}); |
| 67 | + }}; |
| 68 | + } |
| 69 | + |
| 70 | + // [START verify_add_on_request] |
| 71 | + /** |
| 72 | + * Determine whether a Google Workspace add-on request is legitimate. |
| 73 | + * |
| 74 | + * @param event Event sent from Google Workspace add-on |
| 75 | + * @param authorization Authorization header from the request |
| 76 | + * @return {boolean} Whether the request is legitimate |
| 77 | + */ |
| 78 | + private boolean verifyAddOnRequest(JsonNode event, String authorization) throws Exception { |
| 79 | + JsonFactory factory = JacksonFactory.getDefaultInstance(); |
| 80 | + |
| 81 | + GoogleIdTokenVerifier verifier = |
| 82 | + new GoogleIdTokenVerifier.Builder(new ApacheHttpTransport(), factory) |
| 83 | + .setAudience(Collections.singletonList(HTTP_ENDPOINT)) |
| 84 | + .build(); |
| 85 | + |
| 86 | + String bearer = authorization.substring("Bearer ".length(), authorization.length()); |
| 87 | + GoogleIdToken idToken = GoogleIdToken.parse(factory, bearer); |
| 88 | + return idToken != null |
| 89 | + && verifier.verify(idToken) |
| 90 | + && idToken.getPayload().getEmailVerified() |
| 91 | + && idToken.getPayload().getEmail().equals(SERVICE_ACCOUNT_EMAIL); |
| 92 | + } |
| 93 | + // [END verify_add_on_request] |
| 94 | +} |
0 commit comments