Skip to content

Commit 2404127

Browse files
committed
Add callback for TURN authentication success
1 parent b44d85a commit 2404127

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

internal/server/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type Request struct {
2828

2929
// User Configuration
3030
AuthHandler func(username string, realm string, srcAddr net.Addr) (key []byte, ok bool)
31+
AuthSuccess func(username string, realm string, srcAddr net.Addr)
3132
Log logging.LeveledLogger
3233
Realm string
3334
ChannelBindTimeout time.Duration

internal/server/turn_test.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ func TestAllocationLifeTime(t *testing.T) {
8484
staticKey, err := nonceHash.Generate()
8585
assert.NoError(t, err)
8686

87+
authSuccessCallbackTimes := 0
88+
8789
r := Request{
8890
AllocationManager: allocationManager,
8991
NonceHash: nonceHash,
@@ -93,13 +95,16 @@ func TestAllocationLifeTime(t *testing.T) {
9395
AuthHandler: func(string, string, net.Addr) (key []byte, ok bool) {
9496
return []byte(staticKey), true
9597
},
98+
99+
AuthSuccess: func(username string, realm string, srcAddr net.Addr) {
100+
authSuccessCallbackTimes++
101+
},
96102
}
97103

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

100106
_, err = r.AllocationManager.CreateAllocation(fiveTuple, r.Conn, 0, time.Hour)
101107
assert.NoError(t, err)
102-
103108
assert.NotNil(t, r.AllocationManager.GetAllocation(fiveTuple))
104109

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

117+
assert.NoError(t, handleCreatePermissionRequest(r, m))
118+
assert.Equal(t, 1, authSuccessCallbackTimes)
119+
112120
assert.NoError(t, handleRefreshRequest(r, m))
121+
assert.Equal(t, 2, authSuccessCallbackTimes)
122+
113123
assert.Nil(t, r.AllocationManager.GetAllocation(fiveTuple))
114124
})
115125
}

internal/server/util.go

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func authenticateRequest(r Request, m *stun.Message, callingMethod stun.Method)
9797
return nil, false, buildAndSendErr(r.Conn, r.SrcAddr, err, badRequestMsg...)
9898
}
9999

100+
if r.AuthSuccess != nil {
101+
r.AuthSuccess(usernameAttr.String(), realmAttr.String(), r.SrcAddr)
102+
}
103+
100104
return stun.MessageIntegrity(ourKey), true, nil
101105
}
102106

server.go

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
type Server struct {
2525
log logging.LeveledLogger
2626
authHandler AuthHandler
27+
authSuccess AuthCallback
2728
realm string
2829
channelBindTimeout time.Duration
2930
nonceHash *server.NonceHash
@@ -60,6 +61,7 @@ func NewServer(config ServerConfig) (*Server, error) {
6061
s := &Server{
6162
log: loggerFactory.NewLogger("turn"),
6263
authHandler: config.AuthHandler,
64+
authSuccess: config.AuthSuccess,
6365
realm: config.Realm,
6466
channelBindTimeout: config.ChannelBindTimeout,
6567
packetConnConfigs: config.PacketConnConfigs,
@@ -221,6 +223,7 @@ func (s *Server) readLoop(p net.PacketConn, allocationManager *allocation.Manage
221223
Buff: buf[:n],
222224
Log: s.log,
223225
AuthHandler: s.authHandler,
226+
AuthSuccess: s.authSuccess,
224227
Realm: s.realm,
225228
AllocationManager: allocationManager,
226229
ChannelBindTimeout: s.channelBindTimeout,

server_config.go

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func (c *ListenerConfig) validate() error {
9696
// AuthHandler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior
9797
type AuthHandler func(username, realm string, srcAddr net.Addr) (key []byte, ok bool)
9898

99+
// AuthCallback is a callback used to inform users about the success of authentication events to the server
100+
type AuthCallback func(username, realm string, srcAddr net.Addr)
101+
99102
// GenerateAuthKey is a convenience function to easily generate keys in the format used by AuthHandler
100103
func GenerateAuthKey(username, realm, password string) []byte {
101104
// #nosec
@@ -120,6 +123,9 @@ type ServerConfig struct {
120123
// AuthHandler is a callback used to handle incoming auth requests, allowing users to customize Pion TURN with custom behavior
121124
AuthHandler AuthHandler
122125

126+
// AuthCallback is a callback used to notify users of successful authentication to the TURN server
127+
AuthSuccess AuthCallback
128+
123129
// ChannelBindTimeout sets the lifetime of channel binding. Defaults to 10 minutes.
124130
ChannelBindTimeout time.Duration
125131

0 commit comments

Comments
 (0)