summaryrefslogtreecommitdiffhomepage
path: root/internal/permission
diff options
context:
space:
mode:
authorKujtim Hoxha <[email protected]>2025-03-25 13:04:36 +0100
committerKujtim Hoxha <[email protected]>2025-03-26 01:12:30 +0100
commit904061c243f70696bfe781e97bf4e392e6954d07 (patch)
tree4428f96d09968ee0cde44e6ebbaee4757f80050e /internal/permission
parent005b8ac16776512b2d4b1f22bd989da162ca1bad (diff)
downloadopencode-904061c243f70696bfe781e97bf4e392e6954d07.tar.gz
opencode-904061c243f70696bfe781e97bf4e392e6954d07.zip
additional tools
Diffstat (limited to 'internal/permission')
-rw-r--r--internal/permission/permission.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/internal/permission/permission.go b/internal/permission/permission.go
new file mode 100644
index 000000000..5d3088d9f
--- /dev/null
+++ b/internal/permission/permission.go
@@ -0,0 +1,103 @@
+package permission
+
+import (
+ "sync"
+ "time"
+
+ "github.com/google/uuid"
+ "github.com/kujtimiihoxha/termai/internal/pubsub"
+)
+
+type CreatePermissionRequest struct {
+ ToolName string `json:"tool_name"`
+ Description string `json:"description"`
+ Action string `json:"action"`
+ Params any `json:"params"`
+ Path string `json:"path"`
+}
+type PermissionRequest struct {
+ ID string `json:"id"`
+ SessionID string `json:"session_id"`
+ ToolName string `json:"tool_name"`
+ Description string `json:"description"`
+ Action string `json:"action"`
+ Params any `json:"params"`
+ Path string `json:"path"`
+}
+
+type Service interface {
+ pubsub.Suscriber[PermissionRequest]
+ GrantPersistant(permission PermissionRequest)
+ Grant(permission PermissionRequest)
+ Deny(permission PermissionRequest)
+ Request(opts CreatePermissionRequest) bool
+}
+
+type permissionService struct {
+ *pubsub.Broker[PermissionRequest]
+
+ sessionPermissions []PermissionRequest
+ pendingRequests sync.Map
+}
+
+func (s *permissionService) GrantPersistant(permission PermissionRequest) {
+ respCh, ok := s.pendingRequests.Load(permission.ID)
+ if ok {
+ respCh.(chan bool) <- true
+ }
+ s.sessionPermissions = append(s.sessionPermissions, permission)
+}
+
+func (s *permissionService) Grant(permission PermissionRequest) {
+ respCh, ok := s.pendingRequests.Load(permission.ID)
+ if ok {
+ respCh.(chan bool) <- true
+ }
+}
+
+func (s *permissionService) Deny(permission PermissionRequest) {
+ respCh, ok := s.pendingRequests.Load(permission.ID)
+ if ok {
+ respCh.(chan bool) <- false
+ }
+}
+
+func (s *permissionService) Request(opts CreatePermissionRequest) bool {
+ permission := PermissionRequest{
+ ID: uuid.New().String(),
+ ToolName: opts.ToolName,
+ Description: opts.Description,
+ Action: opts.Action,
+ Params: opts.Params,
+ }
+
+ for _, p := range s.sessionPermissions {
+ if p.ToolName == permission.ToolName && p.Action == permission.Action {
+ return true
+ }
+ }
+
+ respCh := make(chan bool, 1)
+
+ s.pendingRequests.Store(permission.ID, respCh)
+ defer s.pendingRequests.Delete(permission.ID)
+
+ s.Publish(pubsub.CreatedEvent, permission)
+
+ // Wait for the response with a timeout
+ select {
+ case resp := <-respCh:
+ return resp
+ case <-time.After(10 * time.Minute):
+ return false
+ }
+}
+
+func NewPermissionService() Service {
+ return &permissionService{
+ Broker: pubsub.NewBroker[PermissionRequest](),
+ sessionPermissions: make([]PermissionRequest, 0),
+ }
+}
+
+var Default Service = NewPermissionService()