Skip to content

Commit

Permalink
added keyban
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed May 25, 2020
1 parent df7bdf8 commit 0ec2fa7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
40 changes: 40 additions & 0 deletions v2/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ func (c *Client) onMessage(_ mqtt.Client, m mqtt.Message) {
case strings.HasPrefix(m.Topic(), "emitter/keygen/"):
c.onResponse(m, new(keyGenResponse))

// Dispatch keyban handler
case strings.HasPrefix(m.Topic(), "emitter/keyban/"):
c.onResponse(m, new(keyBanResponse))

// Dispatch link handler
case strings.HasPrefix(m.Topic(), "emitter/link/"):
c.onResponse(m, new(Link))
Expand Down Expand Up @@ -360,6 +364,42 @@ func (c *Client) GenerateKey(key, channel, permissions string, ttl int) (string,
return "", ErrUnmarshal
}

// BlockKey sends a request to block a key.
func (c *Client) BlockKey(secretKey, targetKey string) (bool, error) {
resp, err := c.request("keyban", &keybanRequest{
Secret: secretKey,
Target: targetKey,
Banned: true,
})
if err != nil {
return false, err
}

// Cast the response and return it
if result, ok := resp.(*keyBanResponse); ok {
return result.Banned == true, nil
}
return false, ErrUnmarshal
}

// AllowKey sends a request to allow a previously blocked key.
func (c *Client) AllowKey(secretKey, targetKey string) (bool, error) {
resp, err := c.request("keyban", &keybanRequest{
Secret: secretKey,
Target: targetKey,
Banned: false,
})
if err != nil {
return false, err
}

// Cast the response and return it
if result, ok := resp.(*keyBanResponse); ok {
return result.Banned == false, nil
}
return false, ErrUnmarshal
}

// CreateLink sends a request to create a default link.
func (c *Client) CreateLink(key, channel, name string, optionalHandler MessageHandler, options ...Option) (*Link, error) {
resp, err := c.request("link", &linkRequest{
Expand Down
48 changes: 41 additions & 7 deletions v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (e *Error) RequestID() uint16 {
return e.Request
}

// ------------------------------------------------------------------------------------

// KeyGenRequest represents a request that can be sent to emitter broker
// in order to generate a new channel key.
type keygenRequest struct {
Expand All @@ -80,6 +82,8 @@ func (r *keyGenResponse) RequestID() uint16 {
return r.Request
}

// ------------------------------------------------------------------------------------

// PresenceRequest represents a request that can be sent to emitter broker
// in order to request presence information.
type presenceRequest struct {
Expand Down Expand Up @@ -117,13 +121,7 @@ type PresenceInfo struct {
Username string `json:"username"`
}

// linkRequest represents a request to create a link.
type linkRequest struct {
Name string `json:"name"` // The name of the shortcut, max 2 characters.
Key string `json:"key"` // The key for the channel.
Channel string `json:"channel"` // The channel name for the shortcut.
Subscribe bool `json:"subscribe"` // Specifies whether the broker should auto-subscribe.
}
// ------------------------------------------------------------------------------------

// meResponse represents information about the client.
type meResponse struct {
Expand All @@ -137,6 +135,16 @@ func (r *meResponse) RequestID() uint16 {
return r.Request
}

// ------------------------------------------------------------------------------------

// linkRequest represents a request to create a link.
type linkRequest struct {
Name string `json:"name"` // The name of the shortcut, max 2 characters.
Key string `json:"key"` // The key for the channel.
Channel string `json:"channel"` // The channel name for the shortcut.
Subscribe bool `json:"subscribe"` // Specifies whether the broker should auto-subscribe.
}

// Link represents a response for the link creation.
type Link struct {
Request uint16 `json:"req,omitempty"`
Expand All @@ -149,6 +157,32 @@ func (r *Link) RequestID() uint16 {
return r.Request
}

// ------------------------------------------------------------------------------------

// KeyBanRequest represents a request that can be sent to emitter broker
// in order to ban/blacklist a channel key.
type keybanRequest struct {
Secret string `json:"secret"` // The master key to use.
Target string `json:"target"` // The target key to ban.
Banned bool `json:"banned"` // Whether the target should be banned or not.
}

// keyBanResponse represents a response from emitter broker which contains
// the response to the key ban request.
type keyBanResponse struct {
Request uint16 `json:"req,omitempty"`
Status int `json:"status"` // The status of the response
Banned bool `json:"banned"` // Whether the target should be banned or not.
ErrorMessage string `json:"message"`
}

// RequestID returns the request ID for the response.
func (r *keyBanResponse) RequestID() uint16 {
return r.Request
}

// ------------------------------------------------------------------------------------

// uuid generates a simple UUID
func uuid() string {
b := make([]byte, 16)
Expand Down

0 comments on commit 0ec2fa7

Please sign in to comment.