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

Provide a means of changing client username #106

Closed
Surgbc opened this issue Oct 24, 2022 · 3 comments
Closed

Provide a means of changing client username #106

Surgbc opened this issue Oct 24, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Surgbc
Copy link

Surgbc commented Oct 24, 2022

I have come across a situation where it is necessary to change the username used to authenticate the client. I am implementing a system where a user logs in using either (1) username and password (2) webtoken obtained from authentication done by the client itself. Since the username user and the webtoken user are one and the same, I require to change the client username to userId which is the same regardless of how the client authenticates.

I have so far managed to do it this way:

  1. Changing the auth interface
    Authenticate(user, password []byte) (interface{}, error)

  2. Obtaining new username and assigning to client during authentication


	// if !ac.Authenticate(pk.Username, pk.Password) {
	// 	if err := s.ackConnection(cl, packets.CodeConnectBadAuthValues, false); err != nil {
	// 		return s.onError(cl.Info(), fmt.Errorf("invalid connection send ack: %w", err))
	// 	}
	// 	return s.onError(cl.Info(), ErrConnectionFailed)
	// }
	username, err := ac.Authenticate(pk.Username, pk.Password)
	if err != nil {
		if err := s.ackConnection(cl, packets.CodeConnectBadAuthValues, false); err != nil {
			return s.onError(cl.Info(), fmt.Errorf("invalid connection send ack: %w", err))
		}
		return s.onError(cl.Info(), ErrConnectionFailed)
	}
	if username != nil {
		pk.Username = []byte(username.(string))
		cl.Identify(lid, pk, ac)
	}

Is this a feature you could consider?

@mochi-co
Copy link
Collaborator

Hi @Surgbc - this feature will be available in v2.0.0. V2 is nearly ready (just working out some of the finer details of mqtt v5) 👍🏻

@mochi-co mochi-co added the enhancement New feature or request label Oct 24, 2022
@mochi-co mochi-co self-assigned this Oct 24, 2022
@mochi-co
Copy link
Collaborator

This issue has been resolved in v2.0.0 through the implementation of universal hooks. You should be able to set the client.ID value in OnConnect or OnConnectAuthenticate as client is a pointer. Let me know if it doesn't behave as expected.

@Surgbc
Copy link
Author

Surgbc commented Dec 15, 2022

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants