2017-06-26 09:07:53 +00:00
|
|
|
package api
|
2017-07-06 18:49:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/Pterodactyl/wings/config"
|
|
|
|
"github.com/Pterodactyl/wings/control"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-08-02 21:47:09 +00:00
|
|
|
accessTokenHeader = "X-Access-Token"
|
|
|
|
|
|
|
|
contextVarServer = "server"
|
|
|
|
contextVarAuth = "auth"
|
2017-07-06 18:49:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type responseError struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:35:15 +00:00
|
|
|
// AuthorizationManager handles permission checks
|
|
|
|
type AuthorizationManager interface {
|
2017-08-02 21:47:09 +00:00
|
|
|
HasPermission(string) bool
|
2017-08-02 19:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type authorizationManager struct {
|
|
|
|
token string
|
|
|
|
server control.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ AuthorizationManager = &authorizationManager{}
|
|
|
|
|
|
|
|
func newAuthorizationManager(token string, server control.Server) *authorizationManager {
|
|
|
|
return &authorizationManager{
|
|
|
|
token: token,
|
|
|
|
server: server,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 21:47:09 +00:00
|
|
|
func (a *authorizationManager) HasPermission(permission string) bool {
|
2017-08-02 19:35:15 +00:00
|
|
|
if permission == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
prefix := permission[:1]
|
|
|
|
if prefix == "c" {
|
2017-08-31 18:13:01 +00:00
|
|
|
return config.ContainsAuthKey(a.token)
|
2017-08-02 19:35:15 +00:00
|
|
|
}
|
|
|
|
if a.server == nil {
|
2017-08-02 21:47:09 +00:00
|
|
|
log.WithField("permission", permission).Error("Auth: Server required but none found.")
|
2017-08-02 19:35:15 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if prefix == "g" {
|
2017-08-31 18:13:01 +00:00
|
|
|
return config.ContainsAuthKey(a.token)
|
2017-08-02 19:35:15 +00:00
|
|
|
}
|
|
|
|
if prefix == "s" {
|
2017-10-01 18:42:17 +00:00
|
|
|
return a.server.HasPermission(a.token, permission) || config.ContainsAuthKey(a.token)
|
2017-08-02 19:35:15 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-06 18:49:36 +00:00
|
|
|
// AuthHandler returns a HandlerFunc that checks request authentication
|
|
|
|
// permission is a permission string describing the required permission to access the route
|
|
|
|
func AuthHandler(permission string) gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
requestToken := c.Request.Header.Get(accessTokenHeader)
|
2017-08-02 21:47:09 +00:00
|
|
|
requestServer := c.Param("server")
|
2017-08-02 19:35:15 +00:00
|
|
|
var server control.Server
|
2017-08-02 21:47:09 +00:00
|
|
|
|
2017-08-02 20:02:34 +00:00
|
|
|
if requestToken == "" && permission != "" {
|
2017-07-06 18:49:36 +00:00
|
|
|
log.Debug("Token missing in request.")
|
2017-08-02 19:35:15 +00:00
|
|
|
c.JSON(http.StatusBadRequest, responseError{"Missing required " + accessTokenHeader + " header."})
|
|
|
|
c.Abort()
|
|
|
|
return
|
2017-07-06 18:49:36 +00:00
|
|
|
}
|
2017-08-02 19:35:15 +00:00
|
|
|
if requestServer != "" {
|
|
|
|
server = control.GetServer(requestServer)
|
|
|
|
//fmt.Println(server)
|
|
|
|
if server == nil {
|
|
|
|
log.WithField("serverUUID", requestServer).Error("Auth: Requested server not found.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := newAuthorizationManager(requestToken, server)
|
|
|
|
|
2017-08-02 21:47:09 +00:00
|
|
|
if auth.HasPermission(permission) {
|
|
|
|
c.Set(contextVarServer, server)
|
|
|
|
c.Set(contextVarAuth, auth)
|
2017-08-02 19:35:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusForbidden, responseError{"You do not have permission to perform this action."})
|
2017-07-06 18:49:36 +00:00
|
|
|
c.Abort()
|
|
|
|
}
|
|
|
|
}
|
2017-08-02 21:47:09 +00:00
|
|
|
|
|
|
|
// GetContextAuthManager returns a AuthorizationManager contained in
|
|
|
|
// a gin.Context or nil
|
|
|
|
func GetContextAuthManager(c *gin.Context) AuthorizationManager {
|
|
|
|
auth, exists := c.Get(contextVarAuth)
|
|
|
|
if !exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if auth, ok := auth.(AuthorizationManager); ok {
|
|
|
|
return auth
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContextServer returns a control.Server contained in a gin.Context
|
|
|
|
// or null
|
|
|
|
func GetContextServer(c *gin.Context) control.Server {
|
|
|
|
server, exists := c.Get(contextVarAuth)
|
|
|
|
if !exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if server, ok := server.(control.Server); ok {
|
|
|
|
return server
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|