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

accept or reject an user at server side #20

Closed
nikolaykuz opened this issue Oct 23, 2012 · 13 comments
Closed

accept or reject an user at server side #20

nikolaykuz opened this issue Oct 23, 2012 · 13 comments
Assignees

Comments

@nikolaykuz
Copy link

Hello again,

I am back to implement authentication of client at server.
At client side I use this code

Where userId, accessToken, firstName and lastName come from Facebook authentication of the client. So then client provides this data to server who will check validity of the data and then accept or the reject the user.

public static void connect (String userId, String accessToken, String firstName, String lastName) {
        //disable previous listener
        if (future != null) {
            future.cancel();
            future.removeListener(futureListener);
        }

        //connect to server
        future = bootstrap.connect(new InetSocketAddress(host, port));

        final ChannelBuffer loginBuffer = getLoginBuffer("CONNECT_ROOM_REF_KEY", userId, accessToken, firstName, lastName);
        futureListener = new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception
            {
                Channel channel = future.getChannel();
                channel.write(loginBuffer);
            }
        };

        future.addListener(futureListener);
    }

    private static ChannelBuffer getLoginBuffer(String refKey, String userId, String accessToken, String firstName, String lastName) {
        //1 stands for capacity - number of bytes, big Endian
        ChannelBuffer opCode = ChannelBuffers.buffer(1);

        opCode.writeByte(Events.LOG_IN);
        //Creates a new composite buffer which wraps the readable bytes of the specified buffers without copying them. A modification on the content of the specified buffers will be visible to the returned buffer.
        ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(opCode, NettyUtils.writeStrings(refKey, userId, accessToken, firstName, lastName));

        return buffer;
    }

The problem is that at server side I see LOG_IN event only inside compiled library jetserver-0.1.jar.

So how am I supposed to implement this kind of feature without touching the library?

@menacher
Copy link
Owner

Here is the link to the Events class in the jetclient side which has LOG_IN event. Mostly you will find all event related stuff in the event package.
In one way jetclient having similar class as jetserver is misleading. I should actually have a common project which both jetclient and jetserver can use. But I created jetclient project so that it has minimum jar dependencies and size at client side.

@ghost ghost assigned menacher Oct 23, 2012
@nikolaykuz
Copy link
Author

Sorry, I do not understand how it would help me.

 ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(opCode, NettyUtils.writeStrings(refKey, userId, accessToken, firstName, lastName));

On LOG_IN attempt I send 5 parameters to the server. Server has to parse this message and accept or reject the client. Now reject is not possible since the server always accepts every client. And LOG_IN handler is implemented inside the library.

@menacher
Copy link
Owner

Sorry for closing issue, tried to reopen it and could not...:( I will look into and answer.

@menacher
Copy link
Owner

The LoginHandler has the following code

    public Player lookupPlayer(final ChannelBuffer buffer, final Channel channel)
    {
        Credentials credentials = new SimpleCredentials(buffer);
        Player player = lookupService.playerLookup(credentials);
        if(null == player){
            LOG.error("Invalid credentials provided by user: {}",credentials);
        }
        return player;
    }
    public void handleLogin(Player player,Channel channel)
    {
        if (null != player)
        {
            channel.write(NettyUtils
                    .createBufferForOpcode(Events.LOG_IN_SUCCESS));
        }
        else
        {
            // Write future and close channel
            closeChannelWithLoginFailure(channel);
        }
    }

So if you return a null from the lookup service its going to invalidate the player and close the connection. In the ZombieSpringConfig class you can see the LookupService being injected. Please use your own implementation here, the default SimpleLookupService does not care for credentials.

@menacher menacher reopened this Oct 23, 2012
@nikolaykuz
Copy link
Author

This is the message I have written before you last comment but did not post solving some other issue.

"No problem, you help me a lot!

I guess these lines of code are important to this task from LoginHandler.java

public Player lookupPlayer(final ChannelBuffer buffer, final Channel channel)
     {
          Credentials credentials = new SimpleCredentials(buffer);
          Player player = lookupService.playerLookup(credentials);

I need to override lookupService.playerLookup to return null if authorization fails.

BUT, I think it is much better to have this function
public abstract Player playerLookup(final ChannelBuffer buffer);
NOT
public abstract Player playerLookup(Credentials loginDetail);

so I will be able to parse ChannelBuffer by myself and not being stuck to SimpleCredentials."

@nikolaykuz
Copy link
Author

So my advice is to add following method
public abstract Player playerLookup(final ChannelBuffer buffer)

My application would be authenticated with social network and client would submit 3 parameters to the server.

  1. social network id (facebook, vkontakte, twitter)
  2. user id
  3. access_token

Of course, I can combine 1) and 2) in one String and then parse it to make it work with currently library build.

@menacher
Copy link
Owner

Credentials ,interface was very simplistic hence you are running into this problem. Passing ChannelBuffer to a service is not a great idea, first of all it leaks and second the new netty version Netty 4 changes this and other interfaces drastically, so when jetserver updates to new version it will end up breaking the service interface also. It will be good if you can stick to overriding the lookupservice for the moment. I will log a feature request for revamping the credentials interface from the very simplistic one we have now.

@nikolaykuz
Copy link
Author

Yes, this makes sense.
I will combine 1) and 2) in one string them.

@nikolaykuz
Copy link
Author

In here why player is not used to create a playerSession?

public void handleGameRoomJoin(Player player, Channel channel, ChannelBuffer buffer)
    {
        String refKey = NettyUtils.readString(buffer);

        GameRoom gameRoom = lookupService.gameRoomLookup(refKey);
        if(null != gameRoom)
        {
            PlayerSession playerSession = gameRoom.createPlayerSession();

@menacher
Copy link
Owner

I have added an issue for this. I will update and provide code.

@menacher
Copy link
Owner

I have committed the change for this. Binaries are NOT updated, please build and use latest jetserver jar file.

@nikolaykuz
Copy link
Author

I have imported the jetserver folder to Eclipse.

And these are two major types of errors I am getting.

Description Resource    Path    Location    Type
ArtifactDescriptorException: Failed to read artifact descriptor for cglib:cglib-nodep:jar:2.1_3: ArtifactResolutionException: Failure to transfer cglib:cglib-nodep:pom:2.1_3 from http://repo1.maven.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact cglib:cglib-nodep:pom:2.1_3 from/to central (http://repo1.maven.org/maven2): Connect times out  pom.xml /jetserver  line 1  Maven Dependency Problem
Description Resource    Path    Location    Type
Project 'jetserver' is missing required library: 'C:\Users\Nikolay\.m2\repository\backport-util-concurrent\backport-util-concurrent\3.1\backport-util-concurrent-3.1.jar'   jetserver       Build path  Build Path Problem

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

No branches or pull requests

2 participants