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

Add callback for TURN authentication success #402

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Request struct {

// User Configuration
AuthHandler func(username string, realm string, srcAddr net.Addr) (key []byte, ok bool)
AuthSuccess func(username string, realm string, srcAddr net.Addr)
Log logging.LeveledLogger
Realm string
ChannelBindTimeout time.Duration
Expand Down
12 changes: 11 additions & 1 deletion internal/server/turn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func TestAllocationLifeTime(t *testing.T) {
staticKey, err := nonceHash.Generate()
assert.NoError(t, err)

authSuccessCallbackTimes := 0

r := Request{
AllocationManager: allocationManager,
NonceHash: nonceHash,
Expand All @@ -93,13 +95,16 @@ func TestAllocationLifeTime(t *testing.T) {
AuthHandler: func(string, string, net.Addr) (key []byte, ok bool) {
return []byte(staticKey), true
},

AuthSuccess: func(username string, realm string, srcAddr net.Addr) {
authSuccessCallbackTimes++
},
}

fiveTuple := &allocation.FiveTuple{SrcAddr: r.SrcAddr, DstAddr: r.Conn.LocalAddr(), Protocol: allocation.UDP}

_, err = r.AllocationManager.CreateAllocation(fiveTuple, r.Conn, 0, time.Hour)
assert.NoError(t, err)

assert.NotNil(t, r.AllocationManager.GetAllocation(fiveTuple))

m := &stun.Message{}
Expand All @@ -109,7 +114,12 @@ func TestAllocationLifeTime(t *testing.T) {
assert.NoError(t, (stun.Realm(staticKey)).AddTo(m))
assert.NoError(t, (stun.Username(staticKey)).AddTo(m))

assert.NoError(t, handleCreatePermissionRequest(r, m))
assert.Equal(t, 1, authSuccessCallbackTimes)

assert.NoError(t, handleRefreshRequest(r, m))
assert.Equal(t, 2, authSuccessCallbackTimes)

assert.Nil(t, r.AllocationManager.GetAllocation(fiveTuple))
})
}
4 changes: 4 additions & 0 deletions internal/server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ func authenticateRequest(r Request, m *stun.Message, callingMethod stun.Method)
return nil, false, buildAndSendErr(r.Conn, r.SrcAddr, err, badRequestMsg...)
}

if r.AuthSuccess != nil {
r.AuthSuccess(usernameAttr.String(), realmAttr.String(), r.SrcAddr)
}

return stun.MessageIntegrity(ourKey), true, nil
}

Expand Down
3 changes: 3 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
type Server struct {
log logging.LeveledLogger
authHandler AuthHandler
authSuccess AuthCallback
realm string
channelBindTimeout time.Duration
nonceHash *server.NonceHash
Expand Down Expand Up @@ -60,6 +61,7 @@ func NewServer(config ServerConfig) (*Server, error) {
s := &Server{
log: loggerFactory.NewLogger("turn"),
authHandler: config.AuthHandler,
authSuccess: config.AuthSuccess,
realm: config.Realm,
channelBindTimeout: config.ChannelBindTimeout,
packetConnConfigs: config.PacketConnConfigs,
Expand Down Expand Up @@ -221,6 +223,7 @@ func (s *Server) readLoop(p net.PacketConn, allocationManager *allocation.Manage
Buff: buf[:n],
Log: s.log,
AuthHandler: s.authHandler,
AuthSuccess: s.authSuccess,
Realm: s.realm,
AllocationManager: allocationManager,
ChannelBindTimeout: s.channelBindTimeout,
Expand Down
6 changes: 6 additions & 0 deletions server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func (c *ListenerConfig) validate() error {
// AuthHandler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior
type AuthHandler func(username, realm string, srcAddr net.Addr) (key []byte, ok bool)

// AuthCallback is a callback used to inform users about the success of authentication events to the server
type AuthCallback func(username, realm string, srcAddr net.Addr)

// GenerateAuthKey is a convenience function to easily generate keys in the format used by AuthHandler
func GenerateAuthKey(username, realm, password string) []byte {
// #nosec
Expand All @@ -120,6 +123,9 @@ type ServerConfig struct {
// AuthHandler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior
AuthHandler AuthHandler

// AuthCallback is a callback used to notify users of successful authentication to the TURN server
AuthSuccess AuthCallback

// ChannelBindTimeout sets the lifetime of channel binding. Defaults to 10 minutes.
ChannelBindTimeout time.Duration

Expand Down