diff --git a/pom.xml b/pom.xml index 5d5a693a..6e22eb9f 100644 --- a/pom.xml +++ b/pom.xml @@ -97,6 +97,11 @@ 2.3.0 + + org.java-websocket + Java-WebSocket + 1.5.3 + @@ -182,4 +187,4 @@ - \ No newline at end of file + diff --git a/src/main/java/cn/authing/sdk/java/client/AuthenticationClient.java b/src/main/java/cn/authing/sdk/java/client/AuthenticationClient.java index 7871f63e..dfb8bc9f 100644 --- a/src/main/java/cn/authing/sdk/java/client/AuthenticationClient.java +++ b/src/main/java/cn/authing/sdk/java/client/AuthenticationClient.java @@ -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; @@ -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; @@ -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 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); + } } diff --git a/src/main/java/cn/authing/sdk/java/client/BaseClient.java b/src/main/java/cn/authing/sdk/java/client/BaseClient.java index 3a434465..171ad636 100644 --- a/src/main/java/cn/authing/sdk/java/client/BaseClient.java +++ b/src/main/java/cn/authing/sdk/java/client/BaseClient.java @@ -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 deserialize(String content, Class 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){ + } } diff --git a/src/main/java/cn/authing/sdk/java/client/ManagementClient.java b/src/main/java/cn/authing/sdk/java/client/ManagementClient.java index dbeabc5e..6de78619 100644 --- a/src/main/java/cn/authing/sdk/java/client/ManagementClient.java +++ b/src/main/java/cn/authing/sdk/java/client/ManagementClient.java @@ -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 { @@ -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 使用记录 @@ -3651,4 +3667,34 @@ public IsSuccessRespDto updateAccessKey(UpdateAccessKeyDto reqDto) { } -} \ No newline at end of file + + @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 query = new HashMap(); + 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 headers = new HashMap(); + headers.put("Authorization",authorization); + AuthingWebsocketClient client = new AuthingWebsocketClient(wssUri,headers,receiver); + client.connect(); + } +} diff --git a/src/main/java/cn/authing/sdk/java/dto/CostGetAllRightItemRespDto.java b/src/main/java/cn/authing/sdk/java/dto/CostGetAllRightItemRespDto.java index b341deee..03072f61 100644 --- a/src/main/java/cn/authing/sdk/java/dto/CostGetAllRightItemRespDto.java +++ b/src/main/java/cn/authing/sdk/java/dto/CostGetAllRightItemRespDto.java @@ -67,6 +67,14 @@ public void setData(RightItemRes data) { this.data = data; } - - -} \ No newline at end of file + @Override + public String toString() { + return "CostGetAllRightItemRespDto{" + + "statusCode=" + statusCode + + ", message='" + message + '\'' + + ", apiCode=" + apiCode + + ", requestId='" + requestId + '\'' + + ", data=" + data + + '}'; + } +} diff --git a/src/main/java/cn/authing/sdk/java/dto/EventDto.java b/src/main/java/cn/authing/sdk/java/dto/EventDto.java new file mode 100644 index 00000000..e2879d6c --- /dev/null +++ b/src/main/java/cn/authing/sdk/java/dto/EventDto.java @@ -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; + } + + + +} diff --git a/src/main/java/cn/authing/sdk/java/model/AuthenticationClientOptions.java b/src/main/java/cn/authing/sdk/java/model/AuthenticationClientOptions.java index 44da9cfb..88e01adb 100644 --- a/src/main/java/cn/authing/sdk/java/model/AuthenticationClientOptions.java +++ b/src/main/java/cn/authing/sdk/java/model/AuthenticationClientOptions.java @@ -1,214 +1,233 @@ -package cn.authing.sdk.java.model; - -import cn.authing.sdk.java.enums.AuthMethodEnum; -import cn.authing.sdk.java.enums.ProtocolEnum; -import cn.authing.sdk.java.util.HttpUtils; -import cn.hutool.core.util.StrUtil; - -import java.util.*; - -/** - * @author ZKB - */ -public class AuthenticationClientOptions extends AuthingClientOptions { - /** - * Authing 应用 ID,必填。 - */ - private String appId; - - /** - * Authing 应用密钥,必填。 - */ - private String appSecret; - - /** - * Authing 应用域名,如 https://example.authing.cn,必填。 - */ - private String appHost; - - /** - * 认证完成后的重定向目标 URL,可选。Authing 服务器会对此链接进行校验,需要和控制台的设置保持一致。 - */ - private String redirectUri; - - /** - * 登出完成后的重定向目标 URL,可选。Authing 服务器会对此链接进行校验,需要和控制台的设置保持一致。 - */ - private String logoutRedirectUri; - - /** - * 应用侧向 Authing 请求的权限,以空格分隔,默认为 'openid profile' - * 成功获取的权限会出现在 Access Token 的 scope 字段中 - * 一些示例: - * - openid: OIDC 标准规定的权限,必须包含 - * - profile: 获取用户的基本身份信息 - * - offline_access: 获取用户的 Refresh Token,可用于调用 refreshLoginState 刷新用户的登录态 - */ - private String scope = "openid profile offline_access"; - - /** - * 存储认证上下文的 Cookie 名称 - */ - private String cookieKey; - - /** - * 超时时间 - */ - private int timeout = 10000; - - /** - * 获取 token 端点认证方式 - */ - private String tokenEndPointAuthMethod = AuthMethodEnum.CLIENT_SECRET_POST.getValue(); - - /** - * 获取 token 端点认证方式 - */ - private String protocol = ProtocolEnum.OIDC.getValue(); - - /** - * 检查 token 端点认证方式 - */ - private String introspectionEndPointAuthMethod = AuthMethodEnum.CLIENT_SECRET_POST.getValue(); - - /** - * 检查 token 端点认证方式 - */ - private String revocationEndPointAuthMethod = AuthMethodEnum.CLIENT_SECRET_POST.getValue(); - - /** - * 用户的 access_token,可以通过登录接口获取 - */ - private String AccessToken; - - @Override - public String doRequest(String url, String method, Map headers, Object body) { - if (headers == null) { - headers = new HashMap<>(); - } - headers.put("x-authing-request-from", AuthingClientOptions.REQUEST_FROM); - headers.put("x-authing-sdk-version", AuthingClientOptions.SDK_VERSION); - headers.put("x-authing-app-id", this.appId); - - // 如果设置的 tokenEndPointAuthMethod 为 client_secret_basic 并且调用的是 /oidc 相关接口: - // 1. 获取 token: /oidc(oauth)/token - // 2. 撤销 token: /oidc(oauth)/token/revocation - // 3. 检查 token: /oidc(oauth)/token/introspection - // 4. 其他登录获取 token 接口 - String[] endpointsToSendBasicHeader = { - "/oidc/token", - "/oidc/token/revocation", - "/oidc/token/introspection", - "/oauth/token", - "/oauth/token/revocation", - "/oauth/token/introspection", - "/api/v3/signin", - "/api/v3/signin-by-mobile", - "/api/v3/exchange-tokenset-with-qrcode-ticket" - }; - - // 请求的是上述几个地址 - if (Arrays.asList(endpointsToSendBasicHeader).contains(url)) { - if (Objects.equals(this.getTokenEndPointAuthMethod(), AuthMethodEnum.CLIENT_SECRET_BASIC.getValue())) { - headers.put("authorization", "Basic " + Base64.getEncoder().encodeToString((this.getAppId() + ":" + this.getAppSecret()).getBytes())); - } - } else if (StrUtil.isNotBlank(this.getAccessToken())){ - headers.put("authorization", "Bearer " + this.getAccessToken()); - } - - return HttpUtils.request(getAppHost() + url, method, body, headers, getTimeout()); - } - - public int getTimeout() { - return timeout; - } - - public String getAppId() { - return appId; - } - - public void setAppId(String appId) { - this.appId = appId; - } - - public String getAppSecret() { - return appSecret; - } - - public void setAppSecret(String appSecret) { - this.appSecret = appSecret; - } - - public String getAppHost() { - return appHost; - } - - public void setAppHost(String appHost) { - this.appHost = appHost; - } - - public String getRedirectUri() { - return redirectUri; - } - - public void setRedirectUri(String redirectUri) { - this.redirectUri = redirectUri; - } - - public String getLogoutRedirectUri() { - return logoutRedirectUri; - } - - public void setLogoutRedirectUri(String logoutRedirectUri) { - this.logoutRedirectUri = logoutRedirectUri; - } - - public String getScope() { - return scope; - } - - public void setScope(String scope) { - this.scope = scope; - } - - public String getTokenEndPointAuthMethod() { - return tokenEndPointAuthMethod; - } - - public void setTokenEndPointAuthMethod(String tokenEndPointAuthMethod) { - this.tokenEndPointAuthMethod = tokenEndPointAuthMethod; - } - - public String getProtocol() { - return protocol; - } - - public void setProtocol(String protocol) { - this.protocol = protocol; - } - - public String getIntrospectionEndPointAuthMethod() { - return introspectionEndPointAuthMethod; - } - - public void setIntrospectionEndPointAuthMethod(String introspectionEndPointAuthMethod) { - this.introspectionEndPointAuthMethod = introspectionEndPointAuthMethod; - } - - public String getAccessToken() { - return AccessToken; - } - - public void setAccessToken(String accessToken) { - AccessToken = accessToken; - } - - public String getRevocationEndPointAuthMethod() { - return revocationEndPointAuthMethod; - } - - public void setRevocationEndPointAuthMethod(String revocationEndPointAuthMethod) { - this.revocationEndPointAuthMethod = revocationEndPointAuthMethod; - } -} - +package cn.authing.sdk.java.model; + +import cn.authing.sdk.java.enums.AuthMethodEnum; +import cn.authing.sdk.java.enums.ProtocolEnum; +import cn.authing.sdk.java.util.HttpUtils; +import cn.hutool.core.util.StrUtil; + +import java.util.*; + +/** + * @author ZKB + */ +public class AuthenticationClientOptions extends AuthingClientOptions { + /** + * Authing 应用 ID,必填。 + */ + private String appId; + + /** + * Authing 应用密钥,必填。 + */ + private String appSecret; + + /** + * Authing 应用域名,如 https://example.authing.cn,必填。 + */ + private String appHost; + + private String webSocketHost = "wss://openevent.authing.cn"; + private String webSocketEndpoint = "/events/v1/authentication/sub"; + + /** + * 认证完成后的重定向目标 URL,可选。Authing 服务器会对此链接进行校验,需要和控制台的设置保持一致。 + */ + private String redirectUri; + + /** + * 登出完成后的重定向目标 URL,可选。Authing 服务器会对此链接进行校验,需要和控制台的设置保持一致。 + */ + private String logoutRedirectUri; + + /** + * 应用侧向 Authing 请求的权限,以空格分隔,默认为 'openid profile' + * 成功获取的权限会出现在 Access Token 的 scope 字段中 + * 一些示例: + * - openid: OIDC 标准规定的权限,必须包含 + * - profile: 获取用户的基本身份信息 + * - offline_access: 获取用户的 Refresh Token,可用于调用 refreshLoginState 刷新用户的登录态 + */ + private String scope = "openid profile offline_access"; + + /** + * 存储认证上下文的 Cookie 名称 + */ + private String cookieKey; + + /** + * 超时时间 + */ + private int timeout = 10000; + + /** + * 获取 token 端点认证方式 + */ + private String tokenEndPointAuthMethod = AuthMethodEnum.CLIENT_SECRET_POST.getValue(); + + /** + * 获取 token 端点认证方式 + */ + private String protocol = ProtocolEnum.OIDC.getValue(); + + /** + * 检查 token 端点认证方式 + */ + private String introspectionEndPointAuthMethod = AuthMethodEnum.CLIENT_SECRET_POST.getValue(); + + /** + * 检查 token 端点认证方式 + */ + private String revocationEndPointAuthMethod = AuthMethodEnum.CLIENT_SECRET_POST.getValue(); + + /** + * 用户的 access_token,可以通过登录接口获取 + */ + private String AccessToken; + + @Override + public String doRequest(String url, String method, Map headers, Object body) { + if (headers == null) { + headers = new HashMap<>(); + } + headers.put("x-authing-request-from", AuthingClientOptions.REQUEST_FROM); + headers.put("x-authing-sdk-version", AuthingClientOptions.SDK_VERSION); + headers.put("x-authing-app-id", this.appId); + + // 如果设置的 tokenEndPointAuthMethod 为 client_secret_basic 并且调用的是 /oidc 相关接口: + // 1. 获取 token: /oidc(oauth)/token + // 2. 撤销 token: /oidc(oauth)/token/revocation + // 3. 检查 token: /oidc(oauth)/token/introspection + // 4. 其他登录获取 token 接口 + String[] endpointsToSendBasicHeader = { + "/oidc/token", + "/oidc/token/revocation", + "/oidc/token/introspection", + "/oauth/token", + "/oauth/token/revocation", + "/oauth/token/introspection", + "/api/v3/signin", + "/api/v3/signin-by-mobile", + "/api/v3/exchange-tokenset-with-qrcode-ticket" + }; + + // 请求的是上述几个地址 + if (Arrays.asList(endpointsToSendBasicHeader).contains(url)) { + if (Objects.equals(this.getTokenEndPointAuthMethod(), AuthMethodEnum.CLIENT_SECRET_BASIC.getValue())) { + headers.put("authorization", "Basic " + Base64.getEncoder().encodeToString((this.getAppId() + ":" + this.getAppSecret()).getBytes())); + } + } else if (StrUtil.isNotBlank(this.getAccessToken())){ + headers.put("authorization", "Bearer " + this.getAccessToken()); + } + + return HttpUtils.request(getAppHost() + url, method, body, headers, getTimeout()); + } + + public String getWebSocketHost() { + return webSocketHost; + } + + public void setWebSocketHost(String webSocketHost) { + this.webSocketHost = webSocketHost; + } + + public String getWebSocketEndpoint() { + return webSocketEndpoint; + } + + public void setWebSocketEndpoint(String webSocketEndpoint) { + this.webSocketEndpoint = webSocketEndpoint; + } + + public int getTimeout() { + return timeout; + } + + public String getAppId() { + return appId; + } + + public void setAppId(String appId) { + this.appId = appId; + } + + public String getAppSecret() { + return appSecret; + } + + public void setAppSecret(String appSecret) { + this.appSecret = appSecret; + } + + public String getAppHost() { + return appHost; + } + + public void setAppHost(String appHost) { + this.appHost = appHost; + } + + public String getRedirectUri() { + return redirectUri; + } + + public void setRedirectUri(String redirectUri) { + this.redirectUri = redirectUri; + } + + public String getLogoutRedirectUri() { + return logoutRedirectUri; + } + + public void setLogoutRedirectUri(String logoutRedirectUri) { + this.logoutRedirectUri = logoutRedirectUri; + } + + public String getScope() { + return scope; + } + + public void setScope(String scope) { + this.scope = scope; + } + + public String getTokenEndPointAuthMethod() { + return tokenEndPointAuthMethod; + } + + public void setTokenEndPointAuthMethod(String tokenEndPointAuthMethod) { + this.tokenEndPointAuthMethod = tokenEndPointAuthMethod; + } + + public String getProtocol() { + return protocol; + } + + public void setProtocol(String protocol) { + this.protocol = protocol; + } + + public String getIntrospectionEndPointAuthMethod() { + return introspectionEndPointAuthMethod; + } + + public void setIntrospectionEndPointAuthMethod(String introspectionEndPointAuthMethod) { + this.introspectionEndPointAuthMethod = introspectionEndPointAuthMethod; + } + + public String getAccessToken() { + return AccessToken; + } + + public void setAccessToken(String accessToken) { + AccessToken = accessToken; + } + + public String getRevocationEndPointAuthMethod() { + return revocationEndPointAuthMethod; + } + + public void setRevocationEndPointAuthMethod(String revocationEndPointAuthMethod) { + this.revocationEndPointAuthMethod = revocationEndPointAuthMethod; + } +} + diff --git a/src/main/java/cn/authing/sdk/java/model/AuthingClientOptions.java b/src/main/java/cn/authing/sdk/java/model/AuthingClientOptions.java index d94c48ef..9e973b1c 100644 --- a/src/main/java/cn/authing/sdk/java/model/AuthingClientOptions.java +++ b/src/main/java/cn/authing/sdk/java/model/AuthingClientOptions.java @@ -1,5 +1,7 @@ package cn.authing.sdk.java.model; +import org.java_websocket.client.WebSocketClient; + import java.util.Map; /** @@ -28,4 +30,5 @@ public abstract class AuthingClientOptions { * @return 响应 */ public abstract String doRequest(String url, String method, Map headers, Object body); + } diff --git a/src/main/java/cn/authing/sdk/java/model/AuthingWebsocketClient.java b/src/main/java/cn/authing/sdk/java/model/AuthingWebsocketClient.java new file mode 100644 index 00000000..e55484b7 --- /dev/null +++ b/src/main/java/cn/authing/sdk/java/model/AuthingWebsocketClient.java @@ -0,0 +1,42 @@ +package cn.authing.sdk.java.model; + +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.handshake.ServerHandshake; + +import java.net.URI; +import java.util.Map; + +/** + * @author songxueyan + * @date 2023/2/21 + **/ +public class AuthingWebsocketClient extends WebSocketClient { + + private final Receiver receiver; + + public AuthingWebsocketClient(URI serverUri, Map httpHeaders, Receiver receiver) { + super(serverUri, httpHeaders); + this.receiver = receiver; + } + + @Override + public void onOpen(ServerHandshake serverHandshake) { + System.out.println("onOpen"); + } + + @Override + public void onMessage(String s) { + // System.out.println("onMessage:"+s); + this.receiver.onReceiverMessage(s); + } + + @Override + public void onClose(int i, String s, boolean b) { + System.out.println("onClose,i="+i+",s="+s+",b="+b); + } + + @Override + public void onError(Exception e) { + System.out.println(e); + } +} diff --git a/src/main/java/cn/authing/sdk/java/model/ManagementClientOptions.java b/src/main/java/cn/authing/sdk/java/model/ManagementClientOptions.java index e2e468ce..fc1ead85 100644 --- a/src/main/java/cn/authing/sdk/java/model/ManagementClientOptions.java +++ b/src/main/java/cn/authing/sdk/java/model/ManagementClientOptions.java @@ -49,6 +49,24 @@ public class ManagementClientOptions extends AuthingClientOptions { * Authing 服务器地址 */ private String host = "https://core.authing.cn"; + private String websocketHost = "wss://openevent.authing.cn"; + private String websocketEndpoint = "/events/v1/management/sub"; + + public String getWebsocketHost() { + return websocketHost; + } + + public void setWebsocketHost(String websocketHost) { + this.websocketHost = websocketHost; + } + + public String getWebsocketEndpoint() { + return websocketEndpoint; + } + + public void setWebsocketEndpoint(String websocketEndpoint) { + this.websocketEndpoint = websocketEndpoint; + } /** * 请求头 key,适用于去 Authing 品牌化场景 @@ -317,4 +335,5 @@ public void setExpires_in(Long expires_in) { } } + } diff --git a/src/main/java/cn/authing/sdk/java/model/Receiver.java b/src/main/java/cn/authing/sdk/java/model/Receiver.java new file mode 100644 index 00000000..e5c2da3b --- /dev/null +++ b/src/main/java/cn/authing/sdk/java/model/Receiver.java @@ -0,0 +1,5 @@ +package cn.authing.sdk.java.model; + +public interface Receiver { + void onReceiverMessage(String msg); +} diff --git a/src/main/java/cn/authing/sdk/java/util/signature/Impl/SignatureComposer.java b/src/main/java/cn/authing/sdk/java/util/signature/Impl/SignatureComposer.java index 9192064d..b5f7ad29 100644 --- a/src/main/java/cn/authing/sdk/java/util/signature/Impl/SignatureComposer.java +++ b/src/main/java/cn/authing/sdk/java/util/signature/Impl/SignatureComposer.java @@ -92,6 +92,8 @@ public String getAuthorization(String accessKeyId, String accessKeySecret, Strin private String signString(String stringToSign, String accessKeySecret) { try { + // System.out.println("stringToSign:"+stringToSign); + // System.out.println("accessKeySecret:"+accessKeySecret); Mac mac = Mac.getInstance(ALGORITHM_NAME); mac.init(new SecretKeySpec(accessKeySecret.getBytes(ENCODING), ALGORITHM_NAME)); byte[] signData = mac.doFinal(stringToSign.getBytes(ENCODING)); diff --git a/src/test/java/test/authentication/EventClientPubTest.java b/src/test/java/test/authentication/EventClientPubTest.java new file mode 100644 index 00000000..411391c5 --- /dev/null +++ b/src/test/java/test/authentication/EventClientPubTest.java @@ -0,0 +1,30 @@ +package test.authentication; + +import cn.authing.sdk.java.client.AuthenticationClient; +import cn.authing.sdk.java.dto.CommonResponseDto; +import cn.authing.sdk.java.model.AuthenticationClientOptions; + +import java.io.IOException; +import java.text.ParseException; + +/** + * @author songxueyan + * @date 2023/2/24 + **/ +public class EventClientPubTest { + // 需要替换成你的 Authing App ID + private static final String APP_ID = "AUTHING_APP_ID"; + // 需要替换成你的 Authing App Secret + private static final String APP_SECRET = "AUTHING_APP_SECRET"; + + public static void main(String[] args) throws IOException, ParseException { + AuthenticationClientOptions authenOptions = new AuthenticationClientOptions(); + authenOptions.setAppId(APP_ID); // Authing 应用 ID + authenOptions.setAppSecret(APP_SECRET); // Authing 应用密钥 + + AuthenticationClient authenticationClient = new AuthenticationClient(authenOptions); + String eventData = "{\"id\":\"q\",\"createdAt\":1675579686427,\"updatedAt\":1675579686427}"; + CommonResponseDto result = authenticationClient.pubtEvent("yourapp.event.code", eventData); + System.out.println(result); + } +} diff --git a/src/test/java/test/authentication/EventClientSubTest.java b/src/test/java/test/authentication/EventClientSubTest.java new file mode 100644 index 00000000..8575dfe4 --- /dev/null +++ b/src/test/java/test/authentication/EventClientSubTest.java @@ -0,0 +1,34 @@ +package test.authentication; + +import cn.authing.sdk.java.client.AuthenticationClient; +import cn.authing.sdk.java.dto.CommonResponseDto; +import cn.authing.sdk.java.model.AuthenticationClientOptions; +import cn.authing.sdk.java.model.Receiver; + +import java.io.IOException; +import java.text.ParseException; + +/** + * @author songxueyan + * @date 2023/2/24 + **/ +public class EventClientSubTest { + // 需要替换成你的 Authing App ID + private static final String APP_ID = "AUTHING_APP_ID"; + // 需要替换成你的 Authing App Secret + private static final String APP_SECRET = "AUTHING_APP_SECRET"; + + public static void main(String[] args) throws IOException, ParseException { + AuthenticationClientOptions authenOptions = new AuthenticationClientOptions(); + authenOptions.setAppId(APP_ID); // Authing 应用 ID + authenOptions.setAppSecret(APP_SECRET); // Authing 应用密钥 + + AuthenticationClient authenticationClient = new AuthenticationClient(authenOptions); + authenticationClient.subEvent("yourapp.event.code", new Receiver() { + @Override + public void onReceiverMessage(String msg) { + System.out.println(msg); + } + }); + } +} diff --git a/src/test/java/test/management/EventPubTest.java b/src/test/java/test/management/EventPubTest.java new file mode 100644 index 00000000..8b924d06 --- /dev/null +++ b/src/test/java/test/management/EventPubTest.java @@ -0,0 +1,29 @@ +package test.management; + +import cn.authing.sdk.java.client.ManagementClient; +import cn.authing.sdk.java.dto.CostGetAllRightItemRespDto; +import cn.authing.sdk.java.model.ManagementClientOptions; + +/** + * @author songxueyan + * @date 2023/2/23 + **/ +public class EventPubTest { + // 需要替换成你的 Authing Access Key ID + private static final String ACCESS_KEY_ID = "AUTHING_ACCESS_KEY_ID"; + // 需要替换成你的 Authing Access Key Secret + private static final String ACCESS_KEY_SECRET = "AUTHING_ACCESS_KEY_SECRET"; + + public static void main(String[] args) { + + ManagementClientOptions clientOptions = new ManagementClientOptions(); + clientOptions.setAccessKeyId(ACCESS_KEY_ID); + clientOptions.setAccessKeySecret(ACCESS_KEY_SECRET); + ManagementClient client = new ManagementClient(clientOptions); + + String eventData = "{\"id\":\"q\",\"createdAt\":1675579686427,\"updatedAt\":1675579686427}"; + CostGetAllRightItemRespDto result = client.pubtEvent("yourapp.event.code", eventData); + System.out.println(result); + + } +} diff --git a/src/test/java/test/management/EventSubTest.java b/src/test/java/test/management/EventSubTest.java new file mode 100644 index 00000000..19f7309c --- /dev/null +++ b/src/test/java/test/management/EventSubTest.java @@ -0,0 +1,31 @@ +package test.management; + +import cn.authing.sdk.java.client.ManagementClient; +import cn.authing.sdk.java.model.ManagementClientOptions; +import cn.authing.sdk.java.model.Receiver; + +/** + * @author songxueyan + * @date 2023/2/21 + **/ +public class EventSubTest { + // 需要替换成你的 Authing Access Key ID + private static final String ACCESS_KEY_ID = "AUTHING_ACCESS_KEY_ID"; + // 需要替换成你的 Authing Access Key Secret + private static final String ACCESS_KEY_SECRET = "AUTHING_ACCESS_KEY_SECRET"; + + public static void main(String[] args) { + ManagementClientOptions clientOptions = new ManagementClientOptions(); + clientOptions.setAccessKeyId(ACCESS_KEY_ID); + clientOptions.setAccessKeySecret(ACCESS_KEY_SECRET); + clientOptions.setWebsocketHost("ws://192.168.0.145:8866"); + + ManagementClient client = new ManagementClient(clientOptions); + client.subEvent("authing.user.login", new Receiver() { + @Override + public void onReceiverMessage(String msg) { + System.out.println(msg); + } + }); + } +}