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

MX-13884: native auth support legacy signable messages #125

Merged
merged 1 commit into from
Feb 21, 2023
Merged
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 authentication/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type AuthTokenHandler interface {
Encode(authToken AuthToken) (string, error)
GetUnsignedToken(authToken AuthToken) []byte
GetSignableMessage(address, unsignedToken []byte) []byte
GetSignableMessageLegacy(address, unsignedToken []byte) []byte
IsInterfaceNil() bool
}

Expand Down
17 changes: 13 additions & 4 deletions authentication/native/mock/authTokenHandlerStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import "github.com/multiversx/mx-sdk-go/authentication"

// AuthTokenHandlerStub -
type AuthTokenHandlerStub struct {
DecodeCalled func(accessToken string) (authentication.AuthToken, error)
EncodeCalled func(authToken authentication.AuthToken) (string, error)
GetUnsignedTokenCalled func(authToken authentication.AuthToken) []byte
GetSignableMessageCalled func(address, unsignedToken []byte) []byte
DecodeCalled func(accessToken string) (authentication.AuthToken, error)
EncodeCalled func(authToken authentication.AuthToken) (string, error)
GetUnsignedTokenCalled func(authToken authentication.AuthToken) []byte
GetSignableMessageCalled func(address, unsignedToken []byte) []byte
GetSignableMessageLegacyCalled func(address, unsignedToken []byte) []byte
}

// Decode -
Expand Down Expand Up @@ -42,6 +43,14 @@ func (stub *AuthTokenHandlerStub) GetSignableMessage(address, unsignedToken []by
return nil
}

// GetSignableMessageLegacy -
func (stub *AuthTokenHandlerStub) GetSignableMessageLegacy(address, unsignedToken []byte) []byte {
if stub.GetSignableMessageLegacyCalled != nil {
return stub.GetSignableMessageLegacyCalled(address, unsignedToken)
}
return nil
}

// IsInterfaceNil -
func (stub *AuthTokenHandlerStub) IsInterfaceNil() bool {
return stub == nil
Expand Down
8 changes: 7 additions & 1 deletion authentication/native/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ func (server *authServer) validateSignature(token authentication.AuthToken) erro

unsignedToken := server.tokenHandler.GetUnsignedToken(token)
signableMessage := server.tokenHandler.GetSignableMessage(token.GetAddress(), unsignedToken)
return server.signer.VerifyMessage(signableMessage, pubkey, token.GetSignature())

err = server.signer.VerifyMessage(signableMessage, pubkey, token.GetSignature())
if err != nil {
signableMessageLegacy := server.tokenHandler.GetSignableMessageLegacy(token.GetAddress(), unsignedToken)
return server.signer.VerifyMessage(signableMessageLegacy, pubkey, token.GetSignature())
}
return err
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
5 changes: 5 additions & 0 deletions authentication/native/tokenHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func (th *authTokenHandler) GetSignableMessage(address, unsignedToken []byte) []
return []byte(fmt.Sprintf("%s%s", address, unsignedToken))
}

// GetSignableMessageLegacy returns a message to be signed
func (th *authTokenHandler) GetSignableMessageLegacy(address, unsignedToken []byte) []byte {
return []byte(fmt.Sprintf("%s%s{}", address, unsignedToken))
}

func decodeHandler(source string) ([]byte, error) {
source = strings.ReplaceAll(source, "-", "+")
source = strings.ReplaceAll(source, "_", "/")
Expand Down