File: accesscontrol.go

package info (click to toggle)
golang-github-charmbracelet-wish 0.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 164 kB
  • sloc: makefile: 3
file content (27 lines) | stat: -rw-r--r-- 660 bytes parent folder | download
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
// Package accesscontrol provides a middleware that allows you to restrict the commands the user can execute.
package accesscontrol

import (
	"github.com/charmbracelet/wish"
	"github.com/gliderlabs/ssh"
)

// Middleware will exit 1 connections trying to execute commands that are not allowed.
// If no allowed commands are provided, no commands will be allowed.
func Middleware(cmds ...string) wish.Middleware {
	return func(sh ssh.Handler) ssh.Handler {
		return func(s ssh.Session) {
			if len(s.Command()) == 0 {
				sh(s)
				return
			}
			for _, cmd := range cmds {
				if s.Command()[0] == cmd {
					sh(s)
					return
				}
			}
			s.Exit(1)
		}
	}
}