Using one connection for multiple authenticated clients

This commit is contained in:
Brandon Watson 2023-09-19 20:40:52 -05:00
parent 832e02f42f
commit c4ebd5d6ff
3 changed files with 30 additions and 25 deletions

View File

@ -31,7 +31,7 @@ type client interface {
// Option set the Client options, such as Transport, message extensions,etc. // Option set the Client options, such as Transport, message extensions,etc.
type Option func(*options) type Option func(*options)
var _ client = (*Client)(nil) //var _ client = (*Client)(nil)
// Client represents a client connection to an faye server. // Client represents a client connection to an faye server.
type Client struct { type Client struct {
@ -58,14 +58,14 @@ func NewClient(url string, opts ...Option) (*Client, error) {
} }
// Subscribe informs the server that messages published to that channel are delivered to itself. // Subscribe informs the server that messages published to that channel are delivered to itself.
func (c *Client) Subscribe(channel string) (*subscription.Subscription, error) { func (c *Client) Subscribe(channel string, authToken string) (*subscription.Subscription, error) {
return c.dispatcher.Subscribe(channel) return c.dispatcher.Subscribe(channel, authToken)
} }
// Publish publishes events on a channel by sending event messages, the server MAY respond to a publish event // Publish publishes events on a channel by sending event messages, the server MAY respond to a publish event
// if this feature is supported by the server use the OnPublishResponse to get the publish status. // if this feature is supported by the server use the OnPublishResponse to get the publish status.
func (c *Client) Publish(channel string, data message.Data) (err error) { func (c *Client) Publish(channel string, authToken string, data message.Data) (err error) {
return c.dispatcher.Publish(channel, data) return c.dispatcher.Publish(channel, authToken, data)
} }
// Disconnect closes all subscriptions and inform the server to remove any client-related state. // Disconnect closes all subscriptions and inform the server to remove any client-related state.

View File

@ -10,6 +10,7 @@ import (
"strconv" "strconv"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time"
) )
type Dispatcher struct { type Dispatcher struct {
@ -116,7 +117,7 @@ func (d *Dispatcher) dispatchMessage(msg *message.Message) {
subsList := d.store.GetAll() subsList := d.store.GetAll()
for i := range subsList { for i := range subsList {
sub := subsList[i] sub := subsList[i]
d.Subscribe(sub.Name()) d.Subscribe(sub.Name(), sub.AuthToken())
} }
case message.MetaSubscribe: case message.MetaSubscribe:
//handle MetaSubscribe resp //handle MetaSubscribe resp
@ -187,13 +188,14 @@ func (d *Dispatcher) sendMessage(m *message.Message) error {
return d.transport.SendMessage(m) return d.transport.SendMessage(m)
} }
func (d *Dispatcher) Subscribe(channel string) (*subscription.Subscription, error) { func (d *Dispatcher) Subscribe(channel string, authToken string) (*subscription.Subscription, error) {
id := d.nextMsgID() id := d.nextMsgID()
m := &message.Message{ m := &message.Message{
Channel: message.MetaSubscribe, Channel: message.MetaSubscribe,
ClientId: d.clientID, ClientId: d.clientID,
Subscription: channel, Subscription: channel,
Id: id, Id: id,
Ext: map[string]string{"access_token": authToken, "timestamp": string(time.Now().Unix())},
} }
d.extensions.ApplyOutExtensions(m) d.extensions.ApplyOutExtensions(m)
@ -208,7 +210,7 @@ func (d *Dispatcher) Subscribe(channel string) (*subscription.Subscription, erro
d.pendingSubs[id] = subscriptionConfirmation d.pendingSubs[id] = subscriptionConfirmation
d.pendingSubsMu.Unlock() d.pendingSubsMu.Unlock()
sub, err := subscription.NewSubscription(channel, d.Unsubscribe, inMsgCh) sub, err := subscription.NewSubscription(channel, d.Unsubscribe, authToken, inMsgCh)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -245,7 +247,7 @@ func (d *Dispatcher) Unsubscribe(sub *subscription.Subscription) error {
return nil return nil
} }
func (d *Dispatcher) Publish(subscription string, data message.Data) (err error) { func (d *Dispatcher) Publish(subscription string, authToken string, data message.Data) (err error) {
id := d.nextMsgID() id := d.nextMsgID()
m := &message.Message{ m := &message.Message{
@ -253,6 +255,7 @@ func (d *Dispatcher) Publish(subscription string, data message.Data) (err error)
Data: data, Data: data,
ClientId: d.clientID, ClientId: d.clientID,
Id: id, Id: id,
Ext: map[string]string{"access_token": authToken, "timestamp": string(time.Now().Unix())},
} }
//ack from server //ack from server

View File

@ -11,35 +11,33 @@ var ErrInvalidChannelName = errors.New("invalid channel channel")
type Unsubscriber func(subscription *Subscription) error type Unsubscriber func(subscription *Subscription) error
type Subscription struct { type Subscription struct {
channel string channel string
unsub Unsubscriber authToken string
msgCh chan *message.Message unsub Unsubscriber
msgCh chan *message.Message
} }
// todo error // todo error
func NewSubscription(chanel string, unsub Unsubscriber, msgCh chan *message.Message) (*Subscription, error) { func NewSubscription(chanel string, unsub Unsubscriber, authToken string, msgCh chan *message.Message) (*Subscription, error) {
if !IsValidSubscriptionName(chanel) { if !IsValidSubscriptionName(chanel) {
return nil, ErrInvalidChannelName return nil, ErrInvalidChannelName
} }
return &Subscription{ return &Subscription{
channel: chanel, channel: chanel,
unsub: unsub, authToken: authToken,
msgCh: msgCh, unsub: unsub,
msgCh: msgCh,
}, nil }, nil
} }
func (s *Subscription) OnMessage(onMessage func(channel string, msg message.Data)) error { func (s *Subscription) OnMessage(onMessage func(channel string, msg message.Data)) error {
var inMsg *message.Message var inMsg *message.Message
go func() error { for inMsg = range s.msgCh {
for inMsg = range s.msgCh { if inMsg.GetError() != nil {
if inMsg.GetError() != nil { return inMsg.GetError()
return inMsg.GetError()
}
onMessage(inMsg.Channel, inMsg.Data)
} }
return nil onMessage(inMsg.Channel, inMsg.Data)
}() }
return nil return nil
} }
@ -56,6 +54,10 @@ func (s *Subscription) Unsubscribe() error {
return s.unsub(s) return s.unsub(s)
} }
func (s *Subscription) AuthToken() string {
return s.authToken
}
// validChannelName channel specifies is the channel is in the format /foo/432/bar // validChannelName channel specifies is the channel is in the format /foo/432/bar
var validChannelName = regexp.MustCompile(`^\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$`) var validChannelName = regexp.MustCompile(`^\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+(\/(((([a-z]|[A-Z])|[0-9])|(\-|\_|\!|\~|\(|\)|\$|\@)))+)*$`)