Skip to content

Commit

Permalink
fix auth add status code
Browse files Browse the repository at this point in the history
  • Loading branch information
kocherovv committed Oct 26, 2023
1 parent f248474 commit 2604c4a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ sudo ufw enable
#### How to initialize an object

Set server options in `application.yml`.
The WebhookToken attribute is optional.
The WebhookToken attribute is optional and does not need to be assigned a value, but it must be in `application.yml`.
If you don't want to set a password, you can simply leave the webhookToken parameter without a value.

```yaml
green-api:
webhookToken: 1a2b3c4d5e
webhookToken:
server:
port: 8080
```
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.green-api</groupId>
<artifactId>whatsapp-api-webhook-server-java</artifactId>
<version>0.0.2</version>
<version>0.0.3</version>
<name>whatsapp-api-webhook-server-java</name>
<description>whatsapp-api-webhook-server-java</description>
<licenses>
Expand All @@ -34,7 +34,7 @@
</developers>
<properties>
<java.version>17</java.version>
<whatsapp-api-client-java.version>0.0.7</whatsapp-api-client-java.version>
<whatsapp-api-client-java.version>0.0.8</whatsapp-api-client-java.version>
</properties>
<distributionManagement>
<snapshotRepository>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/greenapi/server/docs/README_RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ sudo ufw enable
#### Как инициализировать объект

Установите параметры сервера в `application.yml`.
Атрибут WebhookToken является опциональным.
Атрибут WebhookToken является опциональным, ему можно не присваивать значение, однако он должен быть в `application.yml`.
Если вы не хотите устанавливать пароль, вы можете просто оставить параметр webhookToken без значения.

```yaml
green-api:
webhookToken: 1a2b3c4d5e
webhookToken:
server:
port: 8080
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.greenapi.client.pkg.api.webhook.WebhookHandler;
import com.greenapi.client.pkg.models.notifications.Notification;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

//@Component(value = "whatsappWebhookHandler")
@Component(value = "whatsappWebhookHandler")
class WebhookHandlerExample implements WebhookHandler {
@SneakyThrows
@Override
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/com/greenapi/server/pkg/WebhookServer.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.greenapi.server.pkg;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.greenapi.client.pkg.api.exceptions.GreenApiClientException;
import com.greenapi.client.pkg.api.webhook.NotificationMapper;
import com.greenapi.client.pkg.api.webhook.WebhookHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;

@RestController
Expand All @@ -18,23 +18,21 @@
@Log4j2
public class WebhookServer {

private final NotificationMapper notificationMapper = new NotificationMapper(new ObjectMapper());
private final NotificationMapper notificationMapper = new NotificationMapper();
private final WebhookHandler whatsappWebhookHandler;
@Value("${green-api.webhookToken}")
private String webhookToken;

@PostMapping("/async/webhook")
@ResponseStatus(HttpStatus.OK)
public void receiveAsyncWebhook(@RequestBody String jsonString,
@RequestHeader(required = false) String Authorization) {
if (Authorization != null &&
!Authorization.replaceAll("Bearer ", "").equals(webhookToken)) {
throw new GreenApiClientException("Invalid webhookToken - request is ignored");
public ResponseEntity<Void> receiveAsyncWebhook(@RequestBody String jsonString,
@RequestHeader(required = false) String Authorization) {
if ((Authorization != null && Authorization.replaceAll("Bearer ", "").equals(webhookToken)) ||
Objects.equals(webhookToken, "")) {

CompletableFuture.runAsync(() -> whatsappWebhookHandler.handle(notificationMapper.get(jsonString)));
} else {
CompletableFuture.runAsync(() -> {
whatsappWebhookHandler.handle(notificationMapper.get(jsonString));
});
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
return ResponseEntity.status(HttpStatus.OK).build();
}
}

0 comments on commit 2604c4a

Please sign in to comment.