Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use a common Err for permission denied #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ssh
import (
"context"
"errors"
"fmt"
"net"
"sync"
"time"
Expand All @@ -29,6 +28,8 @@ var DefaultChannelHandlers = map[string]ChannelHandler{
"session": DefaultSessionHandler,
}

var ErrPermissionDenied = errors.New("permission denied")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be in a single var statement with all error types.


// Server defines parameters for running an SSH server. The zero value for
// Server is a valid configuration. When both PasswordHandler and
// PublicKeyHandler are nil, no client authentication is performed.
Expand Down Expand Up @@ -136,7 +137,7 @@ func (srv *Server) config(ctx Context) *gossh.ServerConfig {
config.PasswordCallback = func(conn gossh.ConnMetadata, password []byte) (*gossh.Permissions, error) {
applyConnMetadata(ctx, conn)
if ok := srv.PasswordHandler(ctx, string(password)); !ok {
return ctx.Permissions().Permissions, fmt.Errorf("permission denied")
return ctx.Permissions().Permissions, ErrPermissionDenied
}
return ctx.Permissions().Permissions, nil
}
Expand All @@ -145,7 +146,7 @@ func (srv *Server) config(ctx Context) *gossh.ServerConfig {
config.PublicKeyCallback = func(conn gossh.ConnMetadata, key gossh.PublicKey) (*gossh.Permissions, error) {
applyConnMetadata(ctx, conn)
if ok := srv.PublicKeyHandler(ctx, key); !ok {
return ctx.Permissions().Permissions, fmt.Errorf("permission denied")
return ctx.Permissions().Permissions, ErrPermissionDenied
}
ctx.SetValue(ContextKeyPublicKey, key)
return ctx.Permissions().Permissions, nil
Expand All @@ -155,7 +156,7 @@ func (srv *Server) config(ctx Context) *gossh.ServerConfig {
config.KeyboardInteractiveCallback = func(conn gossh.ConnMetadata, challenger gossh.KeyboardInteractiveChallenge) (*gossh.Permissions, error) {
applyConnMetadata(ctx, conn)
if ok := srv.KeyboardInteractiveHandler(ctx, challenger); !ok {
return ctx.Permissions().Permissions, fmt.Errorf("permission denied")
return ctx.Permissions().Permissions, ErrPermissionDenied
}
return ctx.Permissions().Permissions, nil
}
Expand Down