Ejemplo n.º 1
0
        public void Send(NecPacket packet)
        {
            switch (packet.ServerType)
            {
            case ServerType.Area:
                AreaConnection.Send(packet);
                break;

            case ServerType.Msg:
                MsgConnection.Send(packet);
                break;

            case ServerType.Auth:
                AuthConnection.Send(packet);
                break;

            default:
                Logger.Error(this, "Invalid ServerType");
                break;
            }
        }
        public async Task <IActionResult> LoginUser(dynamic req)
        {
            JObject body = DeserializeRequest(req);

            string res;

            try {
                res = await AuthConnection.Instance.LoginUser(body.GetValue("email").ToString().ToLowerInvariant(),
                                                              body.GetValue("password").ToString());
            } catch (Exception e) {
                return(BadRequest($"Something went wrong: {e}"));
            }

            // If the user exists in Azure B2C but doesn't exist in the database, create the user's profile
            // First, get the user's claims from the generated JWT
            JObject tokenObject = DeserializeRequest(res);

            if (tokenObject.ContainsKey("error"))
            {
                return(Unauthorized(tokenObject.GetValue("error_description").ToString()));
            }

            JwtSecurityToken jwt = AuthConnection.DecodeToken(tokenObject.GetValue("access_token").ToString());

            Dictionary <string, string> claimsDictionary = new Dictionary <string, string>();

            foreach (Claim claim in jwt.Claims)
            {
                claimsDictionary[claim.Type] = claim.Value;
            }

            try {
                // See if the user exists in the database
                string queryString = ReadVertexQuery(claimsDictionary["emails"]);
                var    result      = await DatabaseConnection.Instance.ExecuteQuery(queryString);

                // If the user exists, return Ok()
                if (result.Count > 0)
                {
                    return(Ok(res));
                }

                // Else, create the user
                JObject user = new JObject(
                    new JProperty("firstName", claimsDictionary["given_name"]),
                    new JProperty("lastName", claimsDictionary["family_name"]),
                    new JProperty("email", claimsDictionary["emails"].ToLowerInvariant()));

                IActionResult createUserResult = await CreateUser(user.ToString()).ConfigureAwait(false);

                OkObjectResult okResult = createUserResult as OkObjectResult;

                if (okResult.StatusCode != 200)
                {
                    return(BadRequest("Error creating new user vertex when signing in user for the first time"));
                }

                return(Ok(res));
            } catch (Exception e) {
                return(BadRequest($"Unknown error signing user for the first time: {e}"));
            }
        }
        public async Task <IActionResult> Login(dynamic req)
        {
            JObject body = DeserializeRequest(req);

            if (!body.ContainsKey("email") || !body.ContainsKey("password"))
            {
                return(BadRequest("Request body must contain 'email' and 'password'"));
            }

            string res;

            try {
                res = await AuthConnection.Instance.LoginUser(body.GetValue("email").ToString().ToLowerInvariant(),
                                                              body.GetValue("password").ToString());
            } catch (Exception e) {
                return(BadRequest($"Something went wrong: {e}"));
            }

            // If the user exists in Azure B2C but doesn't exist in the database, create the user's profile
            // First, get the user's claims from the generated JWT
            JObject tokenObject = DeserializeRequest(res);

            if (tokenObject.ContainsKey("error"))
            {
                return(Unauthorized(tokenObject.GetValue("error_description").ToString()));
            }

            JwtSecurityToken            jwt = AuthConnection.DecodeToken(tokenObject.GetValue("access_token").ToString());
            Dictionary <string, string> claimsDictionary = AuthConnection.GetClaimsFromToken(jwt);

            try {
                // See if the user exists in the database
                string queryString = GetVertex(claimsDictionary["emails"]);
                var    result      = await DatabaseConnection.Instance.ExecuteQuery(queryString);

                // If the user exists, return Ok()
                if (result.Count > 0)
                {
                    return(Ok(res));
                }

                string firstName = claimsDictionary["given_name"];
                string lastName  = claimsDictionary["family_name"];
                string email     = claimsDictionary["emails"].ToLowerInvariant();

                // Else, create the user
                UserVertex u = new UserVertex(firstName, lastName);

                IActionResult  createUserResult = await new UsersController().CreateUser(email, u).ConfigureAwait(false);
                OkObjectResult okResult         = createUserResult as OkObjectResult;

                if (okResult.StatusCode != 200)
                {
                    return(BadRequest("Error creating new user vertex when signing in user for the first time"));
                }

                return(Ok(res));
            } catch (Exception e) {
                return(BadRequest($"Unknown error signing user for the first time: {e}"));
            }
        }
Ejemplo n.º 4
0
 public void Add(AuthConnection connection)
 {
     _connections.TryAdd(connection.SessionId, connection);
 }