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-06 01:44:16 +00:00
|
|
|
type WebsocketPayload struct {
|
2020-04-06 01:00:33 +00:00
|
|
|
jwt.Payload
|
|
|
|
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 {
|
|
|
|
return &p.Payload
|
|
|
|
}
|
|
|
|
|
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-04-06 01:00:33 +00:00
|
|
|
for _, k := range p.Permissions {
|
|
|
|
if k == permission {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|