2020-04-06 01:44:16 +00:00
|
|
|
package tokens
|
2020-04-06 01:00:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/gbrlsnchs/jwt/v3"
|
2020-04-07 04:03:39 +00:00
|
|
|
"strings"
|
2020-07-18 23:03:25 +00:00
|
|
|
"sync"
|
2020-04-06 01:00:33 +00:00
|
|
|
)
|
|
|
|
|
2020-04-06 01:44:16 +00:00
|
|
|
type WebsocketPayload struct {
|
2020-04-06 01:00:33 +00:00
|
|
|
jwt.Payload
|
2020-07-18 23:03:25 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
UserID json.Number `json:"user_id"`
|
|
|
|
ServerUUID string `json:"server_uuid"`
|
|
|
|
Permissions []string `json:"permissions"`
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:44:16 +00:00
|
|
|
// Returns the JWT payload.
|
|
|
|
func (p *WebsocketPayload) GetPayload() *jwt.Payload {
|
2020-07-18 23:03:25 +00:00
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
|
|
|
|
2020-04-06 01:44:16 +00:00
|
|
|
return &p.Payload
|
|
|
|
}
|
|
|
|
|
2020-07-18 23:03:25 +00:00
|
|
|
func (p *WebsocketPayload) GetServerUuid() string {
|
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
|
|
|
|
|
|
|
return p.ServerUUID
|
|
|
|
}
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
// Checks if the given token payload has a permission string.
|
2020-04-06 01:44:16 +00:00
|
|
|
func (p *WebsocketPayload) HasPermission(permission string) bool {
|
2020-07-18 23:03:25 +00:00
|
|
|
p.RLock()
|
|
|
|
defer p.RUnlock()
|
|
|
|
|
2020-04-06 01:00:33 +00:00
|
|
|
for _, k := range p.Permissions {
|
2020-04-07 04:03:39 +00:00
|
|
|
if k == permission || (!strings.HasPrefix(permission, "admin") && k == "*") {
|
2020-04-06 01:00:33 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|