Skip to content

Commit

Permalink
fix(#14): use json as response
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-Tian committed Sep 7, 2023
1 parent 3b36359 commit 33abcdb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-services-social-weixin</artifactId>
<version>0.5.2</version>
<version>0.5.4</version>
<name>Keycloak Services Social WeiXin</name>
<description/>
<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.keycloak.social.weixin.resources;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import lombok.SneakyThrows;
import org.apache.commons.collections4.map.HashedMap;
import org.jboss.logging.Logger;
import org.keycloak.models.KeycloakSession;
import org.keycloak.services.resource.RealmResourceProvider;
Expand Down Expand Up @@ -67,7 +69,7 @@ public Response mpQrUrl(@QueryParam("ticket") String ticket, @QueryParam("qr-cod
<script type="text/javascript">
async function fetchQrScanStatus() {
const res = await fetch(`mp-qr-scan-status?ticket=${%s}`, {
const res = await fetch(`mp-qr-scan-status?ticket=%s`, {
headers: {
'Content-Type': 'application/json'
}
Expand All @@ -76,7 +78,7 @@ async function fetchQrScanStatus() {
const {status, openid} = await res.json()
if (openid) {
window.location.href = `%s?openid=${openid}&state=${%s}`
window.location.href = `%s?openid=${openid}&state=%s`
} else {
setTimeout(fetchQrScanStatus, 1000)
}
Expand All @@ -94,6 +96,7 @@ async function fetchQrScanStatus() {
return Response.ok(htmlContent, MediaType.TEXT_HTML_TYPE).build();
}

@SneakyThrows
@GET
@Path("mp-qr-scan-status")
@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -113,22 +116,25 @@ public Response mpQrScanStatus(@QueryParam("ticket") String ticket) {
var openid = ticketEntity.getOpenid();
var scannedAt = ticketEntity.getScannedAt();

if ((Long) expireSeconds < System.currentTimeMillis() / 1000 - (Long) ticketCreatedAt) {
if (expireSeconds.longValue() < (System.currentTimeMillis() / 1000 - ticketCreatedAt.longValue())) {
status = "expired";

ticketEntity.setStatus(status);
this.ticketStatusProvider.saveTicketStatus(ticketEntity);
}

logger.info(String.format("ticket is %s%n, status is %s%n", ticket, status));
return Response.ok(Map.of(
"ticket", ticket,
"expireSeconds", expireSeconds,
"ticketCreatedAt", ticketCreatedAt,
"status", status,
"openid", openid,
"scannedAt", scannedAt
)).build();
logger.info(String.format("ticket is %s%n, status is %s%n, openid is %s", ticket, status, openid));
Map<String, String> data = new HashedMap<>();
data.put("ticket", ticket);
data.put("expireSeconds", expireSeconds.toString());
data.put("ticketCreatedAt", ticketCreatedAt.toString());
data.put("status", status);
data.put("openid", openid);
data.put("scannedAt", scannedAt.toString());

var objectMapper = new ObjectMapper();
var json = objectMapper.writeValueAsString(data);
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}

@SneakyThrows
Expand Down

0 comments on commit 33abcdb

Please sign in to comment.