-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
adapter.go
46 lines (38 loc) · 1.28 KB
/
adapter.go
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 pgid
import (
"net/url"
"strings"
"time"
"exusiai.dev/gommon/constant"
"github.com/gofiber/fiber/v2"
)
func Extract(ctx *fiber.Ctx) string {
authorization := ctx.Get(fiber.HeaderAuthorization)
if authorization != "" && !strings.HasPrefix(authorization, constant.PenguinIDAuthorizationRealm) {
return ""
}
penguinId := strings.TrimSpace(strings.TrimPrefix(authorization, constant.PenguinIDAuthorizationRealm))
if penguinId == "" {
penguinId = ctx.Cookies(constant.PenguinIDCookieKey)
}
return penguinId
}
func Inject(ctx *fiber.Ctx, penguinId string) {
// we even got emojis in PenguinID for some internal testers :)
penguinId = url.QueryEscape(penguinId)
// Populate cookie
ctx.Cookie(&fiber.Cookie{
Name: constant.PenguinIDCookieKey,
Value: penguinId,
MaxAge: constant.PenguinIDAuthMaxCookieAgeSec,
Path: "/",
Expires: time.Now().Add(time.Second * constant.PenguinIDAuthMaxCookieAgeSec),
// TODO: make this configurable and use better source rather than Host header
Domain: "." + ctx.Get("Host", constant.SiteDefaultHost),
SameSite: "None",
Secure: true,
})
// Sets the PenguinID in response header, used for scenarios
// where cookie is not able to be used, such as in the Capacitor client.
ctx.Set(constant.PenguinIDSetHeader, penguinId)
}