@@ -3,10 +3,7 @@ package authz
33import (
44 "context"
55 "net/http"
6- "slices"
7- "strings"
86
9- "github.com/obot-platform/nah/pkg/router"
107 "github.com/obot-platform/obot/pkg/alias"
118 v1 "github.com/obot-platform/obot/pkg/storage/apis/obot.obot.ai/v1"
129 "github.com/obot-platform/obot/pkg/system"
@@ -23,117 +20,42 @@ func getValidUserIDs(user user.Info) []string {
2320 return keys
2421}
2522
26- func (a * Authorizer ) assistantIsAuthorized (ctx context.Context , agentID string , validUserIDs []string ) bool {
27- for _ , userID := range validUserIDs {
28- var access v1.AgentAuthorizationList
29- err := a .storage .List (ctx , & access , kclient .InNamespace (system .DefaultNamespace ), kclient.MatchingFields {
30- "spec.userID" : userID ,
31- "spec.agentID" : agentID ,
32- })
33- if err == nil && len (access .Items ) == 1 {
34- return true
35- }
36- }
37- return false
38- }
39-
40- func (a * Authorizer ) threadIsAuthorized (ctx context.Context , agentID , projectID , threadID string , user user.Info ) bool {
41- var thread v1.Thread
42- if err := a .storage .Get (ctx , router .Key (system .DefaultNamespace , threadID ), & thread ); err != nil {
43- return false
44- }
45- if thread .Spec .AgentName != agentID {
46- return false
47- }
48- if thread .Spec .ParentThreadName != strings .Replace (projectID , system .ProjectPrefix , system .ThreadPrefix , 1 ) {
49- return false
50- }
51- if thread .Spec .UserUID != user .GetUID () {
52- return false
53- }
54- return true
55- }
56-
57- func (a * Authorizer ) projectIsAuthorized (ctx context.Context , agentID , projectID string , validUserIDs []string ) bool {
58- var (
59- thread v1.Thread
60- threadID = strings .Replace (projectID , system .ProjectPrefix , system .ThreadPrefix , 1 )
61- )
62- if err := a .storage .Get (ctx , router .Key (system .DefaultNamespace , threadID ), & thread ); err != nil {
63- return false
64- }
65- if ! thread .Spec .Project {
66- return false
67- }
68- if thread .Spec .AgentName != agentID {
69- return false
70- }
71- if slices .Contains (validUserIDs , thread .Spec .UserUID ) {
72- return true
73- }
74-
75- for _ , userID := range validUserIDs {
76- var access v1.ThreadAuthorizationList
77- err := a .storage .List (ctx , & access , kclient .InNamespace (system .DefaultNamespace ), kclient.MatchingFields {
78- "spec.userID" : userID ,
79- "spec.threadID" : threadID ,
80- "spec.accepted" : "true" ,
81- })
82- if err == nil && len (access .Items ) == 1 {
83- return true
84- }
85- }
86- return false
87- }
88-
89- func (a * Authorizer ) authorizeAssistant (req * http.Request , user user.Info ) bool {
90- if ! strings .HasPrefix (req .URL .Path , "/api/assistants/" ) {
91- return false
92- }
93-
94- paths := strings .Split (req .URL .Path , "/" )
95- if paths [3 ] == "" {
96- return false
97- }
98-
99- // Must be authenticated
100- if ! slices .Contains (user .GetGroups (), AuthenticatedGroup ) {
101- return false
23+ func (a * Authorizer ) checkAssistant (req * http.Request , resources * Resources , user user.Info ) (bool , error ) {
24+ if resources .AssistantID == "" {
25+ return true , nil
10226 }
10327
10428 var (
105- agentID = paths [ 3 ]
29+ agentID = resources . AssistantID
10630 validUserIDs = getValidUserIDs (user )
31+ agent v1.Agent
10732 )
10833
10934 if ! system .IsAgentID (agentID ) {
110- var agent v1.Agent
11135 if err := alias .Get (req .Context (), a .storage , & agent , "" , agentID ); err != nil {
112- return false
36+ return false , err
11337 }
11438 agentID = agent .Name
11539 }
11640
11741 if ! a .assistantIsAuthorized (req .Context (), agentID , validUserIDs ) {
118- return false
42+ return false , nil
11943 }
12044
121- if len (paths ) <= 5 || paths [4 ] != "projects" {
122- return true
123- }
124-
125- // Emails are authorized only here, so reverse the list
126- slices .Reverse (validUserIDs )
127-
128- var projectID = paths [5 ]
129- if ! a .projectIsAuthorized (req .Context (), agentID , projectID , validUserIDs ) {
130- return false
131- }
45+ resources .Authorizated .Assistant = & agent
46+ return true , nil
47+ }
13248
133- if len (paths ) <= 7 || paths [6 ] != "threads" {
134- return true
49+ func (a * Authorizer ) assistantIsAuthorized (ctx context.Context , agentID string , validUserIDs []string ) bool {
50+ for _ , userID := range validUserIDs {
51+ var access v1.AgentAuthorizationList
52+ err := a .storage .List (ctx , & access , kclient .InNamespace (system .DefaultNamespace ), kclient.MatchingFields {
53+ "spec.userID" : userID ,
54+ "spec.agentID" : agentID ,
55+ })
56+ if err == nil && len (access .Items ) == 1 {
57+ return true
58+ }
13559 }
136-
137- var threadID = paths [7 ]
138- return a .threadIsAuthorized (req .Context (), agentID , projectID , threadID , user )
60+ return false
13961}
0 commit comments