summaryrefslogtreecommitdiff
path: root/pkg/gid/gid.go
blob: 7d1ea978c21e95102dca217f5f37a8af74947eef (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
package gid

import (
	"net/url"
	"strings"

	"github.com/cedar-policy/cedar-go"
)

func NewEntityUID(globalID string) cedar.EntityUID {
	if !strings.HasPrefix(globalID, "gid://") {
		return DefaultEntityUID(globalID)
	}

	url, err := url.Parse(globalID)
	if err != nil {
		return DefaultEntityUID(globalID)
	}
	items := strings.SplitN(url.Path, "/", 3)
	if len(items) != 3 {
		return DefaultEntityUID(globalID)
	}

	return cedar.NewEntityUID(
		cedar.EntityType(items[1]),
		cedar.String(items[2]),
	)
}

func DefaultEntityUID(id string) cedar.EntityUID {
	return cedar.NewEntityUID("User", cedar.String(id))
}

func ZeroEntityUID() cedar.EntityUID {
	return cedar.NewEntityUID("", "")
}