aboutsummaryrefslogtreecommitdiff
path: root/utils/auth/auth.go
blob: 976d9035f5ad4a288a1163de28139035ff370ddb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package auth

import (
	"dove/config"
	"dove/session"

	"github.com/gofiber/fiber/v2"
)

func IsAuthenticated(context *fiber.Ctx) bool {
	activeSession, sessionError := session.Store.Get(context)
	if sessionError != nil {
		return false
	}

	return activeSession.Get(AuthenticatedKey) != nil
}

func RequireAuthentication(handler fiber.Handler) fiber.Handler {
	return func(context *fiber.Ctx) error {
		if !config.AuthEnabled || IsAuthenticated(context) {
			return handler(context)
		}

		return fiber.ErrUnauthorized
	}
}

func Authenticate(context *fiber.Ctx) error {
	activeSession, sessionError := session.Store.Get(context)
	if sessionError != nil {
		return sessionError
	}

	activeSession.Set(AuthenticatedKey, true)
	return activeSession.Save()
}

func Deauthenticate(context *fiber.Ctx) error {
	activeSession, sessionError := session.Store.Get(context)
	if sessionError != nil {
		return sessionError
	}

	return activeSession.Destroy()
}