-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(fix): pass IOContext from WebSocket
- Loading branch information
Showing
12 changed files
with
153 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package websocket | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/fogfish/swarm" | ||
) | ||
|
||
// Helper function to fetch principal identity from the context of websocket message | ||
func PrincipalOf[T any](msg swarm.Msg[T]) (string, error) { | ||
ctx, ok := msg.IOContext.(*events.APIGatewayWebsocketProxyRequestContext) | ||
if !ok { | ||
return "", fmt.Errorf("invalid message context: %T", msg.IOContext) | ||
} | ||
|
||
// Note: Authorizer context is defined by authorizer function (see auth lambda). | ||
// The context value depends on the auth type e.g. basic, jwt, etc). | ||
// There are mandatory fields exists in any context: `auth`, `principalId`. | ||
// In case of `jwt` the following fields exists: `iss`, `sub`, `exp`, `nbf`, `iat`, `scope`. | ||
// In case of `basic` the fields `sub`, `scope` is simulated. | ||
auth, ok := ctx.Authorizer.(map[string]any) | ||
if !ok { | ||
return "", fmt.Errorf("invalid authorizer context: %T", ctx.Authorizer) | ||
} | ||
|
||
value, has := auth["principalId"] | ||
if !has { | ||
return "", fmt.Errorf("unknown principal in the authorizer context") | ||
} | ||
|
||
principal, ok := value.(string) | ||
if !ok { | ||
return "", fmt.Errorf("invalid principal type: %T", value) | ||
} | ||
|
||
return principal, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package websocket | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/fogfish/it/v2" | ||
"github.com/fogfish/swarm" | ||
) | ||
|
||
func TestPrincipalOf(t *testing.T) { | ||
t.Run("IOContext-None", func(t *testing.T) { | ||
msg := swarm.Msg[string]{} | ||
it.Then(t).Should( | ||
it.Error(PrincipalOf(msg)).Contain("invalid message context"), | ||
) | ||
}) | ||
|
||
t.Run("IOContext-Bad", func(t *testing.T) { | ||
msg := swarm.Msg[string]{IOContext: "context"} | ||
it.Then(t).Should( | ||
it.Error(PrincipalOf(msg)).Contain("invalid message context"), | ||
) | ||
}) | ||
|
||
t.Run("AuthContext-Bad", func(t *testing.T) { | ||
msg := swarm.Msg[string]{IOContext: &events.APIGatewayWebsocketProxyRequestContext{ | ||
Authorizer: "authroizer", | ||
}} | ||
it.Then(t).Should( | ||
it.Error(PrincipalOf(msg)).Contain("invalid authorizer context"), | ||
) | ||
}) | ||
|
||
t.Run("Principal-None", func(t *testing.T) { | ||
msg := swarm.Msg[string]{IOContext: &events.APIGatewayWebsocketProxyRequestContext{ | ||
Authorizer: map[string]any{}, | ||
}} | ||
it.Then(t).Should( | ||
it.Error(PrincipalOf(msg)).Contain("unknown principal in the authorizer context"), | ||
) | ||
}) | ||
|
||
t.Run("Principal-Bad", func(t *testing.T) { | ||
msg := swarm.Msg[string]{IOContext: &events.APIGatewayWebsocketProxyRequestContext{ | ||
Authorizer: map[string]any{ | ||
"principalId": 1, | ||
}, | ||
}} | ||
it.Then(t).Should( | ||
it.Error(PrincipalOf(msg)).Contain("invalid principal type"), | ||
) | ||
}) | ||
|
||
t.Run("Principal", func(t *testing.T) { | ||
msg := swarm.Msg[string]{IOContext: &events.APIGatewayWebsocketProxyRequestContext{ | ||
Authorizer: map[string]any{ | ||
"principalId": "test", | ||
}, | ||
}} | ||
val, err := PrincipalOf(msg) | ||
it.Then(t).Should( | ||
it.Nil(err), | ||
it.Equal(val, "test"), | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
|
||
package websocket | ||
|
||
const Version = "broker/websocket/v0.20.0" | ||
const Version = "broker/websocket/v0.20.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters