Skip to content

Commit 5f97c51

Browse files
Merge pull request #14 from shoy160/main
feat: 添加认证侧以及管理侧事件发布订阅相关方法
2 parents 1822226 + baefb1a commit 5f97c51

13 files changed

+469
-14
lines changed

Diff for: authentication/authentication_client.go

+62-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"strings"
9+
810
"github.com/Authing/authing-golang-sdk/v3/constant"
911
"github.com/Authing/authing-golang-sdk/v3/dto"
1012
"github.com/Authing/authing-golang-sdk/v3/util"
11-
"strings"
1213

1314
keyfunc "github.com/MicahParks/compatibility-keyfunc"
1415
"github.com/dgrijalva/jwt-go"
@@ -25,8 +26,9 @@ var commonHeaders = map[string]string{
2526
}
2627

2728
type AuthenticationClient struct {
28-
options *AuthenticationClientOptions
29-
jwks *keyfunc.JWKS
29+
options *AuthenticationClientOptions
30+
jwks *keyfunc.JWKS
31+
eventHub *util.WebSocketEventHub
3032
}
3133

3234
func NewAuthenticationClient(options *AuthenticationClientOptions) (*AuthenticationClient, error) {
@@ -39,6 +41,9 @@ func NewAuthenticationClient(options *AuthenticationClientOptions) (*Authenticat
3941
if options.AppHost == "" {
4042
return nil, errors.New("AppHost 不能为空")
4143
}
44+
if options.WssHost == "" {
45+
options.WssHost = constant.WebSocketHost
46+
}
4247
if options.RedirectUri == "" {
4348
return nil, errors.New("RedirectUri 不能为空")
4449
}
@@ -56,7 +61,8 @@ func NewAuthenticationClient(options *AuthenticationClientOptions) (*Authenticat
5661
}
5762

5863
client := &AuthenticationClient{
59-
options: options,
64+
options: options,
65+
eventHub: util.NewWebSocketEvent(),
6066
}
6167

6268
return client, nil
@@ -2204,3 +2210,55 @@ func (client *AuthenticationClient) getUserAuthResourceStruct(reqDto *dto.GetUse
22042210
}
22052211
return &response
22062212
}
2213+
2214+
/*
2215+
* @summary 事件发布
2216+
* @description 根据事件编码发布一个自定义事件
2217+
* @param eventCode 事件编码
2218+
* @param body 事件消息
2219+
* @returns IsSuccessRespDto
2220+
*/
2221+
func (client *AuthenticationClient) PubEvent(eventCode string, data interface{}) *dto.IsSuccessRespDto {
2222+
var reqDto = dto.NewEventReqDto(eventCode, data)
2223+
b, err := client.SendHttpRequest("/api/v3/pub-userEvent", fasthttp.MethodPost, reqDto)
2224+
var response dto.IsSuccessRespDto
2225+
if err != nil {
2226+
fmt.Println(err)
2227+
return nil
2228+
}
2229+
err = json.Unmarshal(b, &response)
2230+
if err != nil {
2231+
fmt.Println(err)
2232+
return nil
2233+
}
2234+
return &response
2235+
}
2236+
2237+
/*
2238+
* @summary 事件订阅
2239+
* @description 根据事件编码订阅一个自定义事件
2240+
* @param eventCode 事件编码
2241+
* @param onSuccess 成功的消息
2242+
* @param onError 异常处理
2243+
*/
2244+
func (client *AuthenticationClient) SubEvent(eventCode string, onSuccess func(msg []byte), onError func(err error)) {
2245+
var options = client.options
2246+
token := options.AccessToken
2247+
// fmt.Println(token)
2248+
if !client.eventHub.CreateAuthentication(eventCode, options.WssHost, token) {
2249+
return
2250+
}
2251+
client.eventHub.AddReceiver(eventCode, onSuccess, onError)
2252+
// recv message exec corresponding callback function
2253+
go client.eventHub.StartReceive(eventCode)
2254+
}
2255+
2256+
/*
2257+
* @summary 事件订阅
2258+
* @description 根据事件编码订阅一个自定义事件
2259+
* @param eventCode 事件编码
2260+
* @param receiver 消息处理器
2261+
*/
2262+
func (client *AuthenticationClient) SubEventByReceiver(eventCode string, receiver util.EventReceiver) {
2263+
client.SubEvent(eventCode, receiver.OnSuccess, receiver.OnError)
2264+
}

Diff for: authentication/authentication_client_test.go

+51-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package authentication
22

33
import (
44
"fmt"
5+
56
"github.com/Authing/authing-golang-sdk/v3/constant"
67
"github.com/Authing/authing-golang-sdk/v3/dto"
78

@@ -14,11 +15,12 @@ import (
1415

1516
var authenticationClient *AuthenticationClient
1617
var options = AuthenticationClientOptions{
17-
AppId: "",
18-
AppSecret: "",
19-
AppHost: "",
20-
RedirectUri: "http://localhost:8989",
21-
InsecureSkipVerify: true,
18+
AppId: "",
19+
AppSecret: "",
20+
AppHost: "",
21+
WssHost: "ws://localhost:88",
22+
RedirectUri: "http://localhost:8989",
23+
AccessToken: "",
2224
}
2325

2426
const idToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2M2IzZWE3Mzk0MDc0YWE1ZTE5YmIzMGMiLCJhdWQiOiI2M2IzZWE1YmM5NjY0YzlkMDhkMTkwYzgiLCJpYXQiOjE2NzI3MzY2NjcsImV4cCI6MTY3Mzk0NjI2NywiaXNzIjoiaHR0cHM6Ly9nby11c2VyLXBlcm1pc3Npb24tYXV0aC5hdXRoaW5nLmNuL29pZGMiLCJub25jZSI6InJtZ0pab3RxNGkiLCJuYW1lIjpudWxsLCJnaXZlbl9uYW1lIjpudWxsLCJtaWRkbGVfbmFtZSI6bnVsbCwiZmFtaWx5X25hbWUiOm51bGwsIm5pY2tuYW1lIjpudWxsLCJwcmVmZXJyZWRfdXNlcm5hbWUiOm51bGwsInByb2ZpbGUiOm51bGwsInBpY3R1cmUiOiJodHRwczovL2ZpbGVzLmF1dGhpbmcuY28vYXV0aGluZy1jb25zb2xlL2RlZmF1bHQtdXNlci1hdmF0YXIucG5nIiwid2Vic2l0ZSI6bnVsbCwiYmlydGhkYXRlIjpudWxsLCJnZW5kZXIiOiJVIiwiem9uZWluZm8iOm51bGwsImxvY2FsZSI6bnVsbCwidXBkYXRlZF9hdCI6IjIwMjMtMDEtMDNUMDg6NTQ6NDUuMzMwWiIsImVtYWlsIjoidGVzdEBleGFtcGxlLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZSwicGhvbmVfbnVtYmVyIjpudWxsLCJwaG9uZV9udW1iZXJfdmVyaWZpZWQiOmZhbHNlfQ.3awp567aJ3wBXR0mh0l1oBTugTNsDqYpJVIaDTeHXbI\n"
@@ -262,3 +264,47 @@ func TestAuthenticationClient_GetUserAuthResourceStruct(t *testing.T) {
262264
fmt.Println(response)
263265

264266
}
267+
268+
type UserEvent struct {
269+
Id string `json:"id"`
270+
Name string `json:"name"`
271+
}
272+
273+
func TestClient_PubEvent(t *testing.T) {
274+
var data = &UserEvent{
275+
Id: "232323232",
276+
Name: "golang-authentication-test",
277+
}
278+
var resp = authenticationClient.PubEvent("custom_xrgu.user_create", data)
279+
fmt.Println(resp)
280+
}
281+
282+
type Receiver1 struct{}
283+
284+
func (receiver *Receiver1) OnSuccess(msg []byte) {
285+
fmt.Println(string(msg))
286+
}
287+
func (receiver *Receiver1) OnError(err error) {
288+
fmt.Println(err)
289+
}
290+
291+
func TestClient_SubEvent(t *testing.T) {
292+
ErrChan := make(chan error, 1)
293+
receiver := &Receiver1{}
294+
authenticationClient.SubEventByReceiver("custom_xrgu.user_create", receiver)
295+
296+
authenticationClient.SubEvent("custom_xrgu.user_create", func(msg []byte) {
297+
fmt.Println(string(msg) + "222")
298+
}, func(err error) {
299+
fmt.Println(err)
300+
ErrChan <- err
301+
})
302+
303+
authenticationClient.SubEvent("custom_xrgu.user_create", func(msg []byte) {
304+
fmt.Println(string(msg) + "333")
305+
}, func(err error) {
306+
fmt.Println(err)
307+
ErrChan <- err
308+
})
309+
<-ErrChan
310+
}

Diff for: authentication/authentication_options.go

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ type AuthenticationClientOptions struct {
7777
是否跳过 HTTPS 证书检测,默认为 false;如果是私有化部署的场景且证书不被信任,可以设置为 true
7878
*/
7979
InsecureSkipVerify bool
80+
/**
81+
* 订阅事件 WebSocket 地址
82+
*/
83+
WssHost string
8084
}
8185

8286
type AuthUrlResult struct {

Diff for: constant/base_constant.go

+2
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ const (
1313
StringEmpty = ""
1414

1515
None = "none"
16+
17+
WebSocketHost = "wss://events.authing.com"
1618
)

Diff for: dto/EventReqDto.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package dto
2+
3+
import "encoding/json"
4+
5+
type EventReqDto struct {
6+
EventType string `json:"eventType"`
7+
EventData string `json:"eventData"`
8+
}
9+
10+
func NewEventReqDto(eventCode string, eventData interface{}) *EventReqDto {
11+
data, err := json.Marshal(eventData)
12+
if err != nil {
13+
panic(err)
14+
}
15+
return &EventReqDto{
16+
EventType: eventCode,
17+
EventData: string(data),
18+
}
19+
}

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.8
55
require (
66
github.com/MicahParks/compatibility-keyfunc v0.14.0
77
github.com/dgrijalva/jwt-go v3.2.0+incompatible
8+
github.com/gorilla/websocket v1.5.0 // indirect
89
github.com/klauspost/compress v1.15.2 // indirect
910
github.com/patrickmn/go-cache v2.1.0+incompatible
1011
github.com/valyala/fasthttp v1.36.0

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
66
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
77
github.com/form3tech-oss/jwt-go v3.2.5+incompatible h1:/l4kBbb4/vGSsdtB5nUe8L7B9mImVMaBPw9L/0TBHU8=
88
github.com/form3tech-oss/jwt-go v3.2.5+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
9+
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
10+
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
911
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
1012
github.com/klauspost/compress v1.15.2 h1:3WH+AG7s2+T8o3nrM/8u2rdqUEcQhmga7smjrT41nAw=
1113
github.com/klauspost/compress v1.15.2/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=

Diff for: management/management_client.go

+56
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package management
33
import (
44
"encoding/json"
55
"fmt"
6+
67
"github.com/Authing/authing-golang-sdk/v3/dto"
8+
"github.com/Authing/authing-golang-sdk/v3/util"
79
"github.com/valyala/fasthttp"
810
)
911

@@ -6547,3 +6549,57 @@ func (client *ManagementClient) UpdateAccessKey(reqDto *dto.UpdateAccessKeyDto)
65476549
}
65486550
return &response
65496551
}
6552+
6553+
/*
6554+
* @summary 事件发布
6555+
* @description 根据事件编码发布一个自定义事件
6556+
* @param eventCode 事件编码
6557+
* @param body 事件消息
6558+
* @returns IsSuccessRespDto
6559+
*/
6560+
func (client *ManagementClient) PubEvent(eventCode string, data interface{}) *dto.IsSuccessRespDto {
6561+
var reqDto = dto.NewEventReqDto(eventCode, data)
6562+
b, err := client.SendHttpRequest("/api/v3/pub-event", fasthttp.MethodPost, reqDto)
6563+
var response dto.IsSuccessRespDto
6564+
if err != nil {
6565+
fmt.Println(err)
6566+
return nil
6567+
}
6568+
err = json.Unmarshal(b, &response)
6569+
if err != nil {
6570+
fmt.Println(err)
6571+
return nil
6572+
}
6573+
return &response
6574+
}
6575+
6576+
/*
6577+
* @summary 事件订阅
6578+
* @description 根据事件编码订阅一个自定义事件
6579+
* @param eventCode 事件编码
6580+
* @param onSuccess 成功的消息
6581+
* @param onError 失败处理
6582+
*/
6583+
func (client *ManagementClient) SubEvent(eventCode string, onSuccess func(msg []byte), onError func(err error)) {
6584+
var options = client.options
6585+
defMap := make(map[string]string, 0)
6586+
stringToSign := util.ComposeStringToSign("websocket", "", defMap, defMap)
6587+
token := util.GetAuthorization(client.options.AccessKeyId, client.options.AccessKeySecret, stringToSign)
6588+
// fmt.Println(token)
6589+
if !client.eventHub.CreateManagement(eventCode, options.WssHost, token) {
6590+
return
6591+
}
6592+
client.eventHub.AddReceiver(eventCode, onSuccess, onError)
6593+
// recv message exec corresponding callback function
6594+
go client.eventHub.StartReceive(eventCode)
6595+
}
6596+
6597+
/*
6598+
* @summary 事件订阅
6599+
* @description 根据事件编码订阅一个自定义事件
6600+
* @param eventCode 事件编码
6601+
* @param receiver 消息处理器
6602+
*/
6603+
func (client *ManagementClient) SubEventByReceiver(eventCode string, receiver util.EventReceiver) {
6604+
client.SubEvent(eventCode, receiver.OnSuccess, receiver.OnError)
6605+
}

Diff for: management/management_client_options.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package management
22

33
import (
4-
"github.com/Authing/authing-golang-sdk/v3/constant"
54
"net/http"
5+
6+
"github.com/Authing/authing-golang-sdk/v3/constant"
7+
"github.com/Authing/authing-golang-sdk/v3/util"
68
)
79

810
type ManagementClient struct {
911
HttpClient *http.Client
1012
options *ManagementClientOptions
1113
userPoolId string
14+
eventHub *util.WebSocketEventHub
1215
}
1316

1417
type ManagementClientOptions struct {
@@ -22,12 +25,16 @@ type ManagementClientOptions struct {
2225
是否跳过 HTTPS 证书检测,默认为 false;如果是私有化部署的场景且证书不被信任,可以设置为 true
2326
*/
2427
InsecureSkipVerify bool
28+
WssHost string
2529
}
2630

2731
func NewManagementClient(options *ManagementClientOptions) (*ManagementClient, error) {
2832
if options.Host == "" {
2933
options.Host = constant.ApiServiceUrl
3034
}
35+
if options.WssHost == "" {
36+
options.WssHost = constant.WebSocketHost
37+
}
3138
c := &ManagementClient{
3239
options: options,
3340
}
@@ -42,5 +49,6 @@ func NewManagementClient(options *ManagementClientOptions) (*ManagementClient, e
4249
)
4350
c.HttpClient = oauth2.NewManagementClient(context.Background(), src)*/
4451
}
52+
c.eventHub = util.NewWebSocketEvent()
4553
return c, nil
4654
}

0 commit comments

Comments
 (0)