Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscribing to authing events #46

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
<version>2.3.0</version>
</dependency>

<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.5.3</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -182,4 +187,4 @@
</repository>
</distributionManagement>

</project>
</project>
42 changes: 39 additions & 3 deletions src/main/java/cn/authing/sdk/java/client/AuthenticationClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import cn.authing.sdk.java.dto.authentication.*;
import cn.authing.sdk.java.enums.AuthMethodEnum;
import cn.authing.sdk.java.enums.ProtocolEnum;
import cn.authing.sdk.java.model.AuthenticationClientOptions;
import cn.authing.sdk.java.model.AuthingRequestConfig;
import cn.authing.sdk.java.model.ClientCredentialInput;
import cn.authing.sdk.java.model.*;
import cn.authing.sdk.java.util.CommonUtils;
import cn.authing.sdk.java.util.HttpUtils;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import com.nimbusds.jose.JWSAlgorithm;
Expand All @@ -21,6 +20,8 @@
import com.nimbusds.jose.jwk.RSAKey;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.MessageDigest;
Expand Down Expand Up @@ -2000,4 +2001,39 @@ public GetUserAuthResourceStructRespDto getUserAuthResourceStruct(

// ==== AUTO GENERATED AUTHENTICATION METHODS END ====


@Override
public void subEvent(String eventCode, Receiver receiver) {
if (StrUtil.isBlank(eventCode)) {
throw new IllegalArgumentException("eventCode is required");
}
if (receiver == null) {
throw new IllegalArgumentException("receiver is required");
}
Assert.notNull(this.options.getAccessToken());
AuthenticationClientOptions options = (AuthenticationClientOptions) this.options;
String eventUri = options.getWebSocketHost()+options.getWebSocketEndpoint()
+"?code="+eventCode
+"&token="+this.options.getAccessToken();
URI wssUri = null;
try {
wssUri = new URI(eventUri);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
HashMap<String,String> headers = new HashMap();
AuthingWebsocketClient client = new AuthingWebsocketClient(wssUri,headers,receiver);
client.connect();
}

public CommonResponseDto pubtEvent(String eventCode,Object data){
Assert.notNull(eventCode);
Assert.notNull(data);
AuthingRequestConfig config = new AuthingRequestConfig();
config.setUrl("/api/v3/pub-userEvent");
config.setBody(new EventDto(eventCode,data));
config.setMethod("POST");
String response = request(config);
return deserialize(response, CommonResponseDto.class);
}
}
17 changes: 10 additions & 7 deletions src/main/java/cn/authing/sdk/java/client/BaseClient.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
package cn.authing.sdk.java.client;

import cn.authing.sdk.java.model.Receiver;
import cn.authing.sdk.java.model.AuthingClientOptions;
import cn.authing.sdk.java.model.AuthingRequestConfig;
import cn.authing.sdk.java.util.JsonUtils;


/**
* @author luojielin
*/
public class BaseClient {

protected AuthingClientOptions options;

public BaseClient(AuthingClientOptions options) {
this.options = options;
}

public static <T> T deserialize(String content, Class<T> valueType) {
return JsonUtils.deserialize(content, valueType);
}

public static String serialize(Object value) {
return JsonUtils.serialize(value);
}

public String request(AuthingRequestConfig config) {
return options.doRequest(config.getUrl(), config.getMethod(), config.getHeaders(), config.getBody());
}



public void subEvent(String eventCode, Receiver receiver){
}
}
52 changes: 49 additions & 3 deletions src/main/java/cn/authing/sdk/java/client/ManagementClient.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package cn.authing.sdk.java.client;

import cn.authing.sdk.java.model.AuthingWebsocketClient;
import cn.authing.sdk.java.model.Receiver;
import cn.authing.sdk.java.util.signature.Impl.SignatureComposer;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.authing.sdk.java.dto.*;

import cn.authing.sdk.java.model.AuthingRequestConfig;
import cn.authing.sdk.java.model.ManagementClientOptions;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Collections;
import java.util.Map;


public class ManagementClient extends BaseClient {
Expand Down Expand Up @@ -3310,6 +3315,17 @@ public CostGetCurrentUsageRespDto getUsageInfo() {
return deserialize(response, CostGetCurrentUsageRespDto.class);
}

public CostGetAllRightItemRespDto pubtEvent(String eventCode,Object data){
Assert.notNull(eventCode);
Assert.notNull(data);
AuthingRequestConfig config = new AuthingRequestConfig();
config.setUrl("/api/v3/pub-event");
config.setBody(new EventDto(eventCode,data));
config.setMethod("POST");
String response = request(config);
return deserialize(response, CostGetAllRightItemRespDto.class);
}

/**
* @summary 获取 MAU 使用记录
* @description 获取当前用户池 MAU 使用记录
Expand Down Expand Up @@ -3651,4 +3667,34 @@ public IsSuccessRespDto updateAccessKey(UpdateAccessKeyDto reqDto) {
}


}

@Override
public void subEvent(String eventCode, Receiver receiver) {
if (StrUtil.isBlank(eventCode)) {
throw new IllegalArgumentException("eventCode is required");
}
if (receiver == null) {
throw new IllegalArgumentException("receiver is required");
}
ManagementClientOptions options = (ManagementClientOptions) this.options;
String eventUri = options.getWebsocketHost()+options.getWebsocketEndpoint()+"?code="+eventCode;
URI wssUri = null;
try {
wssUri = new URI(eventUri);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
// System.out.println("eventUri:"+eventUri);
SignatureComposer signatureComposer = new SignatureComposer();
HashMap<String,String> query = new HashMap<String, String>();
String signa = signatureComposer.composeStringToSign("websocket","",query,query);
// String signa = signatureComposer.composeStringToSign("websocket",eventUri,query,query); // server 端验证不用传 uri
// System.out.println("signa:"+signa);
String authorization = signatureComposer.getAuthorization(options.getAccessKeyId(),options.getAccessKeySecret(),signa);
// System.out.println(authorization);
HashMap<String,String> headers = new HashMap();
headers.put("Authorization",authorization);
AuthingWebsocketClient client = new AuthingWebsocketClient(wssUri,headers,receiver);
client.connect();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public void setData(RightItemRes data) {
this.data = data;
}



}
@Override
public String toString() {
return "CostGetAllRightItemRespDto{" +
"statusCode=" + statusCode +
", message='" + message + '\'' +
", apiCode=" + apiCode +
", requestId='" + requestId + '\'' +
", data=" + data +
'}';
}
}
37 changes: 37 additions & 0 deletions src/main/java/cn/authing/sdk/java/dto/EventDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cn.authing.sdk.java.dto;

/**
* @author songxueyan
* @date 2023/2/23
**/
public class EventDto {
private String eventType ;
private Object eventData ;

public EventDto() {
}

public EventDto(String eventType, Object eventData) {
this.eventType = eventType;
this.eventData = eventData;
}

public String getEventType() {
return eventType;
}

public void setEventType(String eventType) {
this.eventType = eventType;
}

public Object getEventData() {
return eventData;
}

public void setEventData(Object eventData) {
this.eventData = eventData;
}



}
Loading