Skip to content

Commit

Permalink
Add callback for TURN authentication success
Browse files Browse the repository at this point in the history
  • Loading branch information
renandincer committed Jul 16, 2024
1 parent b44d85a commit 3144403
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
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)
OnAuthSuccess 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
},

OnAuthSuccess: func(username string, realm string, srcAddr net.Addr) {

Check warning on line 99 in internal/server/turn_test.go

View workflow job for this annotation

GitHub Actions / lint / Go

unused-parameter: parameter 'username' seems to be unused, consider removing or renaming it as _ (revive)
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.OnAuthSuccess != nil {
r.OnAuthSuccess(usernameAttr.String(), realmAttr.String(), r.SrcAddr)
}

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

Expand Down
1 change: 1 addition & 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

Check failure on line 27 in server.go

View workflow job for this annotation

GitHub Actions / lint / Go

field `authSuccess` is unused (unused)
realm string
channelBindTimeout time.Duration
nonceHash *server.NonceHash
Expand Down
3 changes: 3 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 Down

0 comments on commit 3144403

Please sign in to comment.