Ejemplo n.º 1
0
        public BooleanResult AuthenticatedUserGateway(SessionProperties properties)
        {
            // this method shall perform some other tasks ...

            UserInformation userInfo = properties.GetTrackedSingle <UserInformation>();

            UInfo uinfo = HttpAccessor.getUserInfo(userInfo.Username);

            if (uinfo != null)
            {
                m_logger.DebugFormat("AuthenticatedUserGateway: Uinfo: {0}", uinfo.ToString());
                foreach (string group in uinfo.groups)
                {
                    userInfo.AddGroup(new GroupInformation()
                    {
                        Name = group
                    });
                }
                properties.AddTrackedSingle <UserInformation>(userInfo);

                // and what else ??? :)
            }

            return(new BooleanResult()
            {
                Success = true
            });
        }
Ejemplo n.º 2
0
        public static UInfo parseResponse(string res)
        {
            UInfo u = new UInfo();
            pGinaCbitsResponse jres = JToken.Parse(res).ToObject <pGinaCbitsResponse>();

            // reason why could not login (empty = can login)
            u.whyCannotLogin = jres.message;
            u.uname          = jres.username;
            if (u.uname == null)
            {
                throw new Exception("Bad response arrived: " + res);
            }
            u.fullName = jres.name;
            u.email    = jres.email;
            u.groups   = jres.groups.Split(';');
            if (u.groups.Length == 1 && u.groups[0].Contains(";"))
            {
                throw new Exception("Bad response arrived (groups wrong): " + res);
            }

            return(u);
        }
Ejemplo n.º 3
0
        public static BooleanResult getResponse(UserInformation uinfo)
        {
            try
            {
                // BEGIN TLS1.2 hack
                ServicePointManager.Expect100Continue      = true;
                ServicePointManager.SecurityProtocol       = (SecurityProtocolType)3072;
                ServicePointManager.DefaultConnectionLimit = 9999;
                // END TLS1.2 hack

                WebRequest request = WebRequest.Create(Settings.resolveSettings());
                request.Method  = "POST";
                request.Timeout = 5000;
                dynamic postObject = new JObject();
                postObject.username = uinfo.Username;
                postObject.password = uinfo.Password;
                string postData  = postObject.ToString();
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                request.ContentType   = "application/json";
                request.ContentLength = byteArray.Length;

                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                }

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            string responseFromServer = reader.ReadToEnd();
                            m_logger.InfoFormat("Response: {0}", responseFromServer);
                            UInfo proc_uinfo = UInfo.parseResponse(responseFromServer);
                            if (resps.ContainsKey(proc_uinfo.uname))
                            {
                                resps.Remove(proc_uinfo.uname);
                            }
                            resps.Add(proc_uinfo.uname, proc_uinfo);
                            uinfo.Username = proc_uinfo.uname;
                            uinfo.Fullname = proc_uinfo.fullName;
                        }
                    }
                }
                return(new BooleanResult()
                {
                    Success = true
                });
            }
            catch (WebException webx)
            {
                m_logger.ErrorFormat("Accessor.WebException: {0}", webx.Message);

                using (HttpWebResponse res = (HttpWebResponse)webx.Response)
                {
                    if (res != null)
                    {
                        using (StreamReader resReader = new StreamReader(res.GetResponseStream()))
                        {
                            string responseBody = resReader.ReadToEnd();
                            if (responseBody.Length > 0)
                            {
                                String message = ((JObject)JToken.Parse(responseBody)).Value <String>("message");
                                return(new BooleanResult()
                                {
                                    Success = false, Message = message
                                });
                            }
                        }
                    }
                }

                return(new BooleanResult()
                {
                    Success = false, Message = webx.Message
                });
            }
            catch (Exception e)
            {
                // very bad scenario
                m_logger.ErrorFormat("Accessor.Exception: {0}", e.StackTrace);
                return(new BooleanResult()
                {
                    Success = false, Message = e.Message
                });
            }
        }