Example #1
0
        public static void OnLogin(Session session, JObject message)
        {
            string account_id = Utility.ReadStringFromJsonObject(message, "id");
            string type       = Utility.ReadStringFromJsonObject(message, "type");

            if (account_id == null || type == null)
            {
                Log.Warning("Wrong login request: sid={0}", session.Id);
                session.Close();
                return;
            }

            if (type == "fb")
            {
                // Facebook 인증을 먼저 합니다.
                string access_token = Utility.ReadStringFromJsonObject(message, "access_token");
                if (access_token == null)
                {
                    Log.Warning("Wrong login request. No access token: sid={0}, account_id={1}", session.Id, account_id);
                    session.Close();
                    return;
                }

                FacebookAuthentication.AuthenticationRequest request =
                    new FacebookAuthentication.AuthenticationRequest(access_token);
                FacebookAuthentication.Authenticate(request, (_1, _2, _3) => {
                    OnFacebookAuthenticated(_1, _2, _3, session, account_id, Session.EncodingScheme.kJsonEncoding);
                });
            }
            else
            {
                // Guest 는 별도의 인증 없이 로그인 합니다.
                AccountManager.CheckAndSetLoggedInAsync(account_id, session, (_1, _2, _3) => { OnLogin_Completed(_1, _2, _3, Session.EncodingScheme.kJsonEncoding); });
            }
        }
Example #2
0
        private static void OnFacebookAuthenticated(
            FacebookAuthentication.AuthenticationRequest param_request,
            FacebookAuthentication.AuthenticationResponse param_response,
            bool param_error, Session session, string account_id, Session.EncodingScheme encoding)
        {
            if (param_error)
            {
                // 인증에 오류가 있습니다. 장애 오류입니다.
                Log.Error("Failed to authenticate. Facebook authentication error: id={0}", account_id);
                if (encoding == Session.EncodingScheme.kJsonEncoding)
                {
                    session.SendMessage("login", Utility.MakeResponse("nop", "facebook authentication error"),
                                        Session.Encryption.kDefault);
                }
                else
                {
                    Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                    FunMessage      funmsg = new FunMessage();
                    LobbyLoginReply reply  = new LobbyLoginReply();
                    reply.result = "nop";
                    reply.msg    = "facebook authentication error";
                    funmsg.AppendExtension_lobby_login_repl(reply);
                    session.SendMessage("login", funmsg);
                }
                return;
            }

            if (!param_response.Success)
            {
                // 인증에 실패했습니다. 올바르지 않은 access token 입니다.
                Log.Info("Failed to authenticate. Wrong Facebook access token: id={0}", account_id);
                if (encoding == Session.EncodingScheme.kJsonEncoding)
                {
                    session.SendMessage("login",
                                        Utility.MakeResponse("nop", "facebook authentication fail: " + param_response.Error.Message),
                                        Session.Encryption.kDefault);
                }
                else
                {
                    Log.Assert(encoding == Session.EncodingScheme.kProtobufEncoding);
                    FunMessage      funmsg = new FunMessage();
                    LobbyLoginReply reply  = new LobbyLoginReply();
                    reply.result = "nop";
                    reply.msg    = "facebook authentication fail: " + param_response.Error.Message;
                    funmsg.AppendExtension_lobby_login_repl(reply);
                    session.SendMessage("login", funmsg);
                }
                return;
            }

            // 인증에 성공했습니다.
            Log.Info("Succeed to authenticate facebook account: id={0}", account_id);

            // 이어서 로그인 처리를 진행합니다.
            AccountManager.CheckAndSetLoggedInAsync(account_id, session, (_1, _2, _3) => { OnLogin_Completed(_1, _2, _3, encoding); });
        }
Example #3
0
        public static void OnLogin2(Session session, FunMessage message)
        {
            LobbyLoginRequest request = new LobbyLoginRequest();

            if (!message.TryGetExtension_lobby_login_req(
                    out request))
            {
                Log.Error("OnLogin2: Wrong message.");
                return;
            }

            string account_id = request.id;
            string type       = request.type;

            if (account_id == null || type == null)
            {
                Log.Warning("Wrong login request: sid={0}", session.Id);
                session.Close();
                return;
            }

            if (type == "fb")
            {
                // Facebook 인증을 먼저 합니다.
                string access_token = request.access_token;
                if (access_token == null)
                {
                    Log.Warning("Wrong login request. No access token: sid={0}, account_id={1}", session.Id, account_id);
                    session.Close();
                    return;
                }

                FacebookAuthentication.AuthenticationRequest fb_request =
                    new FacebookAuthentication.AuthenticationRequest(access_token);
                FacebookAuthentication.Authenticate(fb_request, (_1, _2, _3) => {
                    OnFacebookAuthenticated(_1, _2, _3, session, account_id, Session.EncodingScheme.kProtobufEncoding);
                });
            }
            else
            {
                // Guest 는 별도의 인증 없이 로그인 합니다.
                AccountManager.CheckAndSetLoggedInAsync(account_id, session, (_1, _2, _3) => { OnLogin_Completed(_1, _2, _3, Session.EncodingScheme.kProtobufEncoding); });
            }
        }
Example #4
0
        public static void Login(
            Session session, JObject message,
            SessionResponse.SessionResponseHandler login_handler,
            SessionResponse.SessionResponseHandler logout_handler)
        {
            Log.Assert(session != null);
            Log.Assert(message != null);
            Log.Assert(login_handler != null);
            Log.Assert(logout_handler != null);

            //
            // 로그인 요청 예제
            //
            // 클라이언트는 다음 메시지 형태로 로그인을 요청해야 합니다.
            // {
            //   // Facebook ID 또는 구글+ ID 등 고유한 ID 를 사용해야 합니다.
            //   "account_id": "id",
            //   "platform": "facebook"
            //   "access_token": "account's access token"
            // }

            // 메시지 안에 필수 파라메터가 있는지 확인합니다.
            if (message[kAccounId] == null || message[kAccounId].Type != JTokenType.String ||
                message[kPlatformName] == null || message[kPlatformName].Type != JTokenType.String)
            {
                Log.Error("The message does not have '{0}' or '{1}': session={2}, message={3}",
                          kAccounId, kPlatformName, session.Id, message.ToString());
                login_handler(SessionResponse.ResponseResult.FAILED,
                              new SessionResponse(session, 400, "Missing required fields.", new JObject()));
                return;
            }

            string account_id = message[kAccounId].Value <string>();
            string platform   = message[kPlatformName].Value <string>();

            if (platform == "facebook")
            {
                // Facebook 플랫폼 사용자의 경우, 올바른 사용자인지 검증합니다.
                if (message[kPlatformAccessToken] == null || message[kPlatformAccessToken].Type != JTokenType.String)
                {
                    Log.Error("The message does not have {0}: session={1}, message={2}",
                              kPlatformAccessToken, session.Id, message.ToString());
                    login_handler(SessionResponse.ResponseResult.FAILED,
                                  new SessionResponse(session, 400, "Missing required fields.", new JObject()));
                    return;
                }

                string access_token = message[kPlatformAccessToken].Value <string>();
                FacebookAuthentication.AuthenticationRequest request =
                    new FacebookAuthentication.AuthenticationRequest(access_token);

                FacebookAuthentication.AuthenticationResponseHandler on_authenticated =
                    new FacebookAuthentication.AuthenticationResponseHandler(
                        (FacebookAuthentication.AuthenticationRequest request2,
                         FacebookAuthentication.AuthenticationResponse response2,
                         bool error) => {
                    if (error)
                    {
                        // Facebook 서버 오류 또는 올바르지 않은 사용자인 경우
                        Log.Warning("Failed to authenticate Facebook account: session={0}, code={1}, message={2}",
                                    session.Id, response2.Error.Code, response2.Error.Message);
                        login_handler(SessionResponse.ResponseResult.FAILED,
                                      new SessionResponse(session, 400, "Missing required fields.", new JObject()));
                        return;
                    }

                    Log.Info("Facebook authentication succeed: session={0}, account_id={1}", session.Id, account_id);

                    // 이 예제에서는 로그인 시도를 기록합니다.
                    long try_count = 0;

                    // 분산 환경이라면 CheckAndSetLoggedInGlobalAsync() 함수를 사용해주세요.
                    AccountManager.LoginCallback on_logged_in =
                        new AccountManager.LoginCallback((string account_id2, Session session2, bool logged_in2) => {
                        OnLoggedIn(account_id2, session2, logged_in2, platform, try_count, login_handler, logout_handler);
                    });
                    AccountManager.CheckAndSetLoggedInAsync(account_id, session, on_logged_in);
                });

                // Facebook 인증을 요청합니다.
                FacebookAuthentication.Authenticate(request, on_authenticated);
            }
            else
            {
                //
                // 로그인 시도
                //
                // 요청한 세션으로 로그인을 시도합니다. 이 서버의 로그인 정책은 로그인을 시도하되,
                // 이미 다른 곳에서 로그인한 경우, 로그아웃 후 재시도합니다.
                long try_count = 0;

                // 분산 환경이라면 CheckAndSetLoggedInGlobalAsync() 함수를 사용해주세요.
                AccountManager.LoginCallback on_logged_in =
                    new AccountManager.LoginCallback((string account_id2, Session session2, bool logged_in2) => {
                    OnLoggedIn(account_id2, session2, logged_in2, platform, try_count, login_handler, logout_handler);
                });
                AccountManager.CheckAndSetLoggedInAsync(account_id, session, on_logged_in);
            }
        }