services

package
v0.0.0-...-df1273a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FilterUser = func(ctx *beegoCtx.Context) {
	unprotectedRoutes := []map[string]string{
		{"method": http.MethodGet, "url": "/v1/user/login"},
	}

	for _, route := range unprotectedRoutes {
		if strings.HasPrefix(ctx.Input.URL(), route["url"]) && route["method"] == ctx.Request.Method {
			return
		}
	}

	authorization := ctx.Input.Header("Authorization")
	re := regexp.MustCompilePOSIX(`^Bearer (.+)$`)
	if !re.MatchString(authorization) {
		ctx.Output.Status = http.StatusUnauthorized
		ctx.Output.JSON(map[string]string{"message": "Unauthorized"}, false, false)
		return
	}

	tokenString := strings.Split(authorization, " ")[1]

	claims := &models.Claims{}
	token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
		return []byte(beego.AppConfig.String("jwtsecret")), nil
	})

	if err != nil {
		ctx.Output.Status = http.StatusUnauthorized
		ctx.Output.JSON(map[string]string{"message": "Unauthorized"}, false, false)
		return
	}

	if !token.Valid {
		ctx.Output.Status = http.StatusUnauthorized
		ctx.Output.JSON(map[string]string{"message": "Unauthorized"}, false, false)
		return
	}

	ctxWithUser := context.WithValue(ctx.Request.Context(), interface{}("claims"), claims)
	ctx.Request = ctx.Request.WithContext(ctxWithUser)
}

FilterUser verifies if the `User` is authenticated

Functions

func FilterAdmin

func FilterAdmin(c *beego.Controller)

FilterAdmin checks if the `User` has an administrator `Role`. If not, return an Unauthorized `JSONError`.

func FilterMeOrAdmin

func FilterMeOrAdmin(c *beego.Controller, uid int64)

FilterMeOrAdmin checks if the `User` is the same as the given `uid` or and admin. If not, return an Unauthorized `JSONError`.

func FilterOwnerOrAdmin

func FilterOwnerOrAdmin(c *beego.Controller, owner int64)

FilterOwnerOrAdmin checks id the `User` is the same as the given `uid`

func GetAllDevices

func GetAllDevices() (*[]models.Device, *models.JSONError)

GetAllDevices add all the Rubus `Device` into the database

func GetDevice

func GetDevice(port string) (*models.Device, *models.JSONError)

GetDevice adds a new Rubus `Device` into the database

func PowerDeviceOff

func PowerDeviceOff(port string) *models.JSONError

PowerDeviceOff shuts down the `Device` on the given `port`

func PowerDeviceOn

func PowerDeviceOn(port string) *models.JSONError

PowerDeviceOn boots the `Device` on the given `port`

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL