Skip to content

Commit

Permalink
construct messages with fields interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahad-10 committed May 29, 2024
1 parent b1c8bdd commit e0f8fe1
Show file tree
Hide file tree
Showing 25 changed files with 478 additions and 433 deletions.
2 changes: 1 addition & 1 deletion auth/anonymous.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func (a *anonymousAuthenticator) AuthExtra() map[string]any {
return a.authExtra
}

func (a *anonymousAuthenticator) Authenticate(_ messages.Challenge) (messages.Authenticate, error) {
func (a *anonymousAuthenticator) Authenticate(_ messages.Challenge) (*messages.Authenticate, error) {
return nil, errors.New("func Authenticate() must not be called for anonymous authentication")
}
2 changes: 1 addition & 1 deletion auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ type ClientAuthenticator interface {
AuthMethod() string
AuthID() string
AuthExtra() map[string]any
Authenticate(challenge messages.Challenge) (messages.Authenticate, error)
Authenticate(challenge messages.Challenge) (*messages.Authenticate, error)
}
4 changes: 2 additions & 2 deletions auth/cra.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (a *craAuthenticator) AuthExtra() map[string]any {
return a.authExtra
}

func (a *craAuthenticator) Authenticate(challenge messages.Challenge) (messages.Authenticate, error) {
func (a *craAuthenticator) Authenticate(challenge messages.Challenge) (*messages.Authenticate, error) {
ch, _ := challenge.Extra()["challenge"].(string)
// If the client needed to look up a user's key, this would require decoding
// the JSON-encoded challenge string and getting the authid. For this
Expand All @@ -60,7 +60,7 @@ func (a *craAuthenticator) Authenticate(challenge messages.Challenge) (messages.
}

challengeStr := SignCRAChallenge(ch, rawSecret)
return messages.NewAuthenticate(challengeStr, map[string]any{}), nil
return messages.NewAuthenticate(messages.NewAuthenticateFields(challengeStr, map[string]any{})), nil
}

// SignCRAChallengeBytes computes the HMAC-SHA256, using the given key, over the
Expand Down
4 changes: 2 additions & 2 deletions auth/cryptosign.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ func (a *cryptoSignAuthenticator) AuthExtra() map[string]any {
return a.authExtra
}

func (a *cryptoSignAuthenticator) Authenticate(challenge messages.Challenge) (messages.Authenticate, error) {
func (a *cryptoSignAuthenticator) Authenticate(challenge messages.Challenge) (*messages.Authenticate, error) {
challengeHex, _ := challenge.Extra()["challenge"].(string)
result, err := SignCryptoSignChallenge(challengeHex, a.privateKey)
if err != nil {
return nil, fmt.Errorf("failed to sign challenge")
}

return messages.NewAuthenticate(result, map[string]any{}), nil
return messages.NewAuthenticate(messages.NewAuthenticateFields(result, map[string]any{})), nil
}

func SignCryptoSignChallenge(challenge string, privateKey ed25519.PrivateKey) (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions auth/ticket.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func (a *ticketAuthenticator) AuthExtra() map[string]any {
return a.authExtra
}

func (a *ticketAuthenticator) Authenticate(_ messages.Challenge) (messages.Authenticate, error) {
return messages.NewAuthenticate(a.ticket, map[string]any{}), nil
func (a *ticketAuthenticator) Authenticate(_ messages.Challenge) (*messages.Authenticate, error) {
return messages.NewAuthenticate(messages.NewAuthenticateFields(a.ticket, map[string]any{})), nil
}
12 changes: 6 additions & 6 deletions joiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func NewJoiner(realm string, serializer serializers.Serializer, authenticator au
}

func (j *Joiner) SendHello() ([]byte, error) {
hello := messages.NewHello(
hello := messages.NewHello(messages.NewHelloFields(
j.realm,
j.authenticator.AuthID(),
j.authenticator.AuthExtra(),
ClientRoles,
[]any{j.authenticator.AuthMethod()},
)
[]string{j.authenticator.AuthMethod()},
))

rawBytes, err := j.serializer.Serialize(hello)
if err != nil {
Expand Down Expand Up @@ -110,7 +110,7 @@ func (j *Joiner) ReceiveMessage(msg messages.Message) (messages.Message, error)
return nil, errors.New("received WELCOME when it was not expected")
}

welcome := msg.(messages.Welcome)
welcome := msg.(*messages.Welcome)
j.sessionDetails = NewSessionDetails(welcome.SessionID(), j.realm, welcome.Details()["authid"].(string),
welcome.Details()["authrole"].(string))
j.state = joinerStateJoined
Expand All @@ -121,8 +121,8 @@ func (j *Joiner) ReceiveMessage(msg messages.Message) (messages.Message, error)
return nil, errors.New("received CHALLENGE when it was not expected")
}

challenge := msg.(messages.Challenge)
authenticate, err := j.authenticator.Authenticate(challenge)
challenge := msg.(*messages.Challenge)
authenticate, err := j.authenticator.Authenticate(*challenge)
if err != nil {
return nil, err
}
Expand Down
51 changes: 27 additions & 24 deletions messages/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,56 @@ var authenticateValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
},
}

type Authenticate interface {
Message

type AuthenticateFields interface {
Signature() string
Extra() map[string]any
}

type authenticate struct {
type authenticateFields struct {
signature string
extra map[string]any
}

func NewEmptyAuthenticate() Authenticate {
return &authenticate{}
}

func NewAuthenticate(signature string, extra map[string]any) Authenticate {
return &authenticate{
func NewAuthenticateFields(signature string, extra map[string]any) AuthenticateFields {
return &authenticateFields{
signature: signature,
extra: extra,
}
}

func (a *authenticate) Type() int {
func (a *authenticateFields) Signature() string {
return a.signature
}

func (a *authenticateFields) Extra() map[string]any {
return a.extra
}

type Authenticate struct {
AuthenticateFields
}

func NewAuthenticate(fields AuthenticateFields) *Authenticate {
return &Authenticate{
AuthenticateFields: fields,
}
}

func (a *Authenticate) Type() int {
return MessageTypeAuthenticate
}

func (a *authenticate) Parse(wampMsg []any) error {
func (a *Authenticate) Parse(wampMsg []any) error {
fields, err := ValidateMessage(wampMsg, authenticateValidationSpec)
if err != nil {
return fmt.Errorf("authenticate: failed to validate message %s: %w", MessageNameAuthenticate, err)
}

a.signature = fields.Signature
a.extra = fields.Extra
a.AuthenticateFields = NewAuthenticateFields(fields.Signature, fields.Extra)

return nil
}

func (a *authenticate) Marshal() []any {
return []any{MessageTypeAuthenticate, a.signature, a.extra}
}

func (a *authenticate) Signature() string {
return a.signature
}

func (a *authenticate) Extra() map[string]any {
return a.extra
func (a *Authenticate) Marshal() []any {
return []any{MessageTypeAuthenticate, a.Signature(), a.Extra()}
}
39 changes: 21 additions & 18 deletions messages/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,56 @@ var cancelValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
},
}

type Cancel interface {
Message

type CancelFields interface {
RequestID() int64
Options() map[string]any
}

type cancel struct {
type cancelFields struct {
requestID int64
options map[string]any
}

func NewEmptyCancel() Cancel {
return &cancel{}
}

func NewCancel(requestID int64, options map[string]any) Cancel {
return &cancel{
func NewCancelFields(requestID int64, options map[string]any) CancelFields {
return &cancelFields{
requestID: requestID,
options: options,
}
}

func (c *cancel) RequestID() int64 {
func (c *cancelFields) RequestID() int64 {
return c.requestID
}

func (c *cancel) Options() map[string]any {
func (c *cancelFields) Options() map[string]any {
return c.options
}

func (c *cancel) Type() int {
type Cancel struct {
CancelFields
}

func NewCancel(fields CancelFields) *Cancel {
return &Cancel{
CancelFields: fields,
}
}

func (c *Cancel) Type() int {
return MessageTypeCancel
}

func (c *cancel) Parse(wampMsg []any) error {
func (c *Cancel) Parse(wampMsg []any) error {
fields, err := ValidateMessage(wampMsg, cancelValidationSpec)
if err != nil {
return fmt.Errorf("cancel: failed to validate message %s: %w", MessageNameCancel, err)
}

c.requestID = fields.RequestID
c.options = fields.Options
c.CancelFields = NewCancelFields(fields.RequestID, fields.Options)

return nil
}

func (c *cancel) Marshal() []any {
return []any{MessageTypeCancel, c.requestID, c.options}
func (c *Cancel) Marshal() []any {
return []any{MessageTypeCancel, c.RequestID(), c.Options()}
}
51 changes: 27 additions & 24 deletions messages/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,56 @@ var challengeValidationSpec = ValidationSpec{ //nolint:gochecknoglobals
},
}

type Challenge interface {
Message

type ChallengeFields interface {
AuthMethod() string
Extra() map[string]any
}

type challenge struct {
type challengeFields struct {
authMethod string
extra map[string]any
}

func NewEmptyChallenge() Challenge {
return &challenge{}
}

func NewChallenge(authMethod string, extra map[string]any) Challenge {
return &challenge{
func NewChallengeFields(authMethod string, extra map[string]any) ChallengeFields {
return &challengeFields{
authMethod: authMethod,
extra: extra,
}
}

func (c *challenge) Type() int {
func (c *challengeFields) AuthMethod() string {
return c.authMethod
}

func (c *challengeFields) Extra() map[string]any {
return c.extra
}

type Challenge struct {
ChallengeFields
}

func NewChallenge(fields ChallengeFields) *Challenge {
return &Challenge{
ChallengeFields: fields,
}
}

func (c *Challenge) Type() int {
return MessageTypeChallenge
}

func (c *challenge) Parse(wampMsg []any) error {
func (c *Challenge) Parse(wampMsg []any) error {
fields, err := ValidateMessage(wampMsg, challengeValidationSpec)
if err != nil {
return fmt.Errorf("challenge: failed to validate message %s: %w", MessageNameChallenge, err)
}

c.authMethod = fields.AuthMethod
c.extra = fields.Extra
c.ChallengeFields = NewChallengeFields(fields.AuthMethod, fields.Extra)

return nil
}

func (c *challenge) Marshal() []any {
return []any{MessageTypeChallenge, c.authMethod, c.extra}
}

func (c *challenge) AuthMethod() string {
return c.authMethod
}

func (c *challenge) Extra() map[string]any {
return c.extra
func (c *Challenge) Marshal() []any {
return []any{MessageTypeChallenge, c.AuthMethod(), c.Extra()}
}
Loading

0 comments on commit e0f8fe1

Please sign in to comment.