Skip to content

Commit

Permalink
alfajrimutawadhi/feature/telegram-userid-whitelist (#18)
Browse files Browse the repository at this point in the history
* fix(whitelist): add middleware for check whitelist

Closes 15

* feat(whitelist): add access whitelist by user id based on config

#15

Closes 15
  • Loading branch information
alfajrimutawadhi authored Dec 29, 2022
1 parent f9fe1cb commit 9dcd1f6
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
5 changes: 4 additions & 1 deletion empty/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
teleterm:
telegram_token: "xxxxxxxxx"
telegram_token: "xxxxxx"
shell_executor: "/bin/bash"
whitelist:
- <USER_ID>
- <USER_ID>
15 changes: 11 additions & 4 deletions teleterm/telegram_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/alfiankan/teleterm/v2/common"
"github.com/alfiankan/teleterm/v2/executor"
"github.com/spf13/viper"
"gopkg.in/telebot.v3"
tele "gopkg.in/telebot.v3"
"gopkg.in/telebot.v3/middleware"
)

const (
Expand All @@ -32,7 +32,7 @@ Err :

func createButtonReplay(ctx context.Context, persist Persistence, menu *tele.ReplyMarkup) (teleMenus []tele.Row, ere error) {

menus := []tele.Row{}
var menus []tele.Row

buttons, err := persist.GetAllButtons(ctx)
if err != nil {
Expand Down Expand Up @@ -66,6 +66,13 @@ func Start(ctx context.Context, db *sql.DB, telebotToken string) {
return
}

configWhitelist := viper.GetIntSlice("whitelist")
var whitelist []int64
for _, id := range configWhitelist {
whitelist = append(whitelist, int64(id))
}
b.Use(middleware.Whitelist(whitelist...))

menu := &tele.ReplyMarkup{ResizeKeyboard: true}

b.Handle("/refresh", func(c tele.Context) error {
Expand Down Expand Up @@ -185,8 +192,8 @@ func Start(ctx context.Context, db *sql.DB, telebotToken string) {

})

//receive document
b.Handle(telebot.OnDocument, func(c tele.Context) error {
// receive document
b.Handle(tele.OnDocument, func(c tele.Context) error {

log.Info().Str("state", "upload file").Msg(c.Message().Document.FileID)

Expand Down
25 changes: 25 additions & 0 deletions vendor/gopkg.in/telebot.v3/middleware/logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions vendor/gopkg.in/telebot.v3/middleware/misc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions vendor/gopkg.in/telebot.v3/middleware/restrict.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ gopkg.in/ini.v1
# gopkg.in/telebot.v3 v3.0.0
## explicit; go 1.13
gopkg.in/telebot.v3
gopkg.in/telebot.v3/middleware
# gopkg.in/yaml.v2 v2.4.0
## explicit; go 1.15
gopkg.in/yaml.v2
Expand Down

0 comments on commit 9dcd1f6

Please sign in to comment.