BlueGit

timfa bluegit

Files

bluegit / internal / access / scope.go

scope.go

feat: bied dezelfde acties als de UI aan via /api/v1
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
d89195e9 History
scope.go 2.8 KiB Download Raw
1package access
2
3import (
4 "sort"
5 "strings"
6
7 "gitlab.com/Timfa/bluegit/internal/i18n"
8)
9
10const (
11 ScopeAPI = "api"
12 ScopeReadAPI = "read_api"
13 ScopeReadRepo = "read_repository"
14 ScopeWriteRepo = "write_repository"
15)
16
17// ScopeClass is het soort aanroep dat een token moet toestaan.
18type ScopeClass int
19
20const (
21 ScopePublic ScopeClass = iota
22 ScopeRead
23 ScopeWrite
24 ScopeRepoRead
25 ScopeRepoWrite
26 ScopeAdmin
27)
28
29// Scopes is de verzameling rechten op een toegangstoken.
30// api dekt de hele API en git. read_api is alleen lezen.
31// read_repository en write_repository gelden voor git en voor
32// repository-inhoud (bestanden, commits, branches, tags).
33type Scopes struct {
34 set map[string]bool
35}
36
37// ParseScopes maakt een opgeslagen lijst. Leeg wordt api.
38func ParseScopes(in []string) (string, Scopes, error) {
39 if len(in) == 0 {
40 return ScopeAPI, Scopes{set: map[string]bool{ScopeAPI: true}}, nil
41 }
42 seen := map[string]bool{}
43 var list []string
44 for _, raw := range in {
45 name := strings.TrimSpace(raw)
46 if name == "" {
47 continue
48 }
49 switch name {
50 case ScopeAPI, ScopeReadAPI, ScopeReadRepo, ScopeWriteRepo:
51 default:
52 return "", Scopes{}, i18n.E("err.scopeName", name)
53 }
54 if seen[name] {
55 continue
56 }
57 seen[name] = true
58 list = append(list, name)
59 }
60 if len(list) == 0 {
61 return ScopeAPI, Scopes{set: map[string]bool{ScopeAPI: true}}, nil
62 }
63 sort.Strings(list)
64 return strings.Join(list, ","), Scopes{set: seen}, nil
65}
66
67// StoredScopes leest de kommagescheiden kolom. Leeg telt als api.
68func StoredScopes(raw string) Scopes {
69 raw = strings.TrimSpace(raw)
70 if raw == "" {
71 return Scopes{set: map[string]bool{ScopeAPI: true}}
72 }
73 set := map[string]bool{}
74 for _, part := range strings.Split(raw, ",") {
75 part = strings.TrimSpace(part)
76 if part != "" {
77 set[part] = true
78 }
79 }
80 if len(set) == 0 {
81 set[ScopeAPI] = true
82 }
83 return Scopes{set: set}
84}
85
86func (s Scopes) List() []string {
87 var out []string
88 for name := range s.set {
89 out = append(out, name)
90 }
91 sort.Strings(out)
92 return out
93}
94
95func (s Scopes) APIWrite() bool { return s.set[ScopeAPI] }
96func (s Scopes) APIRead() bool { return s.set[ScopeAPI] || s.set[ScopeReadAPI] }
97func (s Scopes) RepoWrite() bool {
98 return s.set[ScopeAPI] || s.set[ScopeWriteRepo]
99}
100func (s Scopes) RepoRead() bool {
101 return s.RepoWrite() || s.set[ScopeReadRepo]
102}
103
104// Allows meldt of deze scopes de klasse dekken.
105func (s Scopes) Allows(class ScopeClass) bool {
106 switch class {
107 case ScopePublic:
108 return true
109 case ScopeRead:
110 return s.APIRead()
111 case ScopeWrite, ScopeAdmin:
112 return s.APIWrite()
113 case ScopeRepoRead:
114 return s.APIRead() || s.RepoRead()
115 case ScopeRepoWrite:
116 return s.APIWrite() || s.RepoWrite()
117 default:
118 return false
119 }
120}
121
122// AllScopes is wat een SSH-sessie mag: dezelfde rechten als de UI.
123func AllScopes() Scopes {
124 return Scopes{set: map[string]bool{ScopeAPI: true}}
125}