예제 #1
0
        private Dictionary <string, string> GetSamlTokenProfileData(SamlTokenData samlTokenData)
        {
            var extractedProfileData = new Dictionary <string, string>();

            foreach (var claim in samlTokenData.Attributes)
            {
                try
                {
                    var key = string.Concat(ProfileFieldPrefix, Regex.Replace(claim.ClaimType, @"[^\w\-]", string.Empty)).ToLower();
                    if (!extractedProfileData.ContainsKey(key))
                    {
                        extractedProfileData.Add(key, claim.Value);
                    }
                    else
                    {
                        extractedProfileData[key] = extractedProfileData[key] + "," + claim.Value;
                    }
                }
                catch (Exception ex)
                {
                    _eventLogApi.Write("ProfileAttributeManager Error GetSamlTokenProfileData: " + ex.Message + " : " + ex.StackTrace, new EventLogEntryWriteOptions()
                    {
                        Category = "SAML", EventId = 1, EventType = "Error"
                    });
                }
            }

            return(extractedProfileData);
        }
예제 #2
0
        private void Events_AfterUserCreate(UserAfterCreateEventArgs e)
        {
            var afterCreatedCookie = CookieHelper.GetCookie(SamlCookieName);

            if (afterCreatedCookie == null)
            {
                return;
            }

            var samlTokenData = SamlTokenData.GetTokenDataFromDatabase(afterCreatedCookie.Value);

            if (samlTokenData == null)
            {
                return;
            }

            //destroy secure cookie for new user if cookie is still present
            CookieHelper.DeleteCookie(afterCreatedCookie.Value);
            //also cleanup our afterCreatedCookie
            CookieHelper.DeleteCookie(afterCreatedCookie.Name);

            //update the samlTokenData now that we know the user ID and cleanup the cookie used by the login
            samlTokenData.UserId = e.Id.Value;

            //Update the cookie SAMLToken Data to have the UserId now that its an existing user to fire the after authenticated events (which also removes the cookie)
            var tokenKey = samlTokenData.SaveTokenDataToDatabase();
            var afterAuthenticatedCookie = new HttpCookie(clientType, tokenKey)
            {
                Expires  = DateTime.Now.AddHours(8),
                HttpOnly = true
            };

            CookieHelper.AddCookie(afterAuthenticatedCookie);

            if (PersistClaims)
            {
                SqlData.SaveSamlToken(samlTokenData);
            }

            var apiUser = _usersApi.Get(new UsersGetOptions()
            {
                Id = e.Id.Value
            });

            //raise new SamlUserCreated Event
            try
            {
                SamlEvents.Instance.OnAfterUserCreate(apiUser, samlTokenData);
            }
            catch (Exception ex)
            {
                _eventLogApi.Write("SamlOAuthClient Error OnAfterUserCreate: " + ex.Message + " : " + ex.StackTrace, new EventLogEntryWriteOptions()
                {
                    Category = "SAML", EventId = 1, EventType = "Error"
                });
            }
        }
예제 #3
0
        public static void SaveEncryptedSamlToken(Guid tokenKey, string encryptedData)
        {
            try
            {
                using (var conn = GetSqlConnection())
                {
                    var sql = $"INSERT INTO [{databaseOwner}].[db_SamlTempTokenData]" +
                              "(TokenKey" +
                              ", EncryptedData)" +
                              "VALUES" +
                              "(@TokenKey" +
                              ",@EncryptedData)";

                    var myCommand = new SqlCommand(sql, conn)
                    {
                        CommandType = CommandType.Text
                    };

                    myCommand.Parameters.Add("@TokenKey", SqlDbType.UniqueIdentifier).Value = tokenKey;
                    myCommand.Parameters.Add("@EncryptedData", SqlDbType.NVarChar).Value    = encryptedData;

                    conn.Open();
                    myCommand.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                _apiEventLog.Write("Error inserting token into the db_SamlTempTokenData table. " + ex, new EventLogEntryWriteOptions()
                {
                    Category = "SAML", EventId = 6023, EventType = "Error"
                });
            }
        }
예제 #4
0
 private void CreateMissingRoles(List <string> samlRoleNames)
 {
     foreach (string samlRole in samlRoleNames)
     {
         try
         {
             if (!_rolesApi.Find(samlRole).Any())
             {
                 _rolesApi.Create(samlRole, "Auto Created SAML based Role");
             }
         }
         catch (Exception ex)
         {
             _eventLogApi.Write("RoleMembershipManager Error CreateMissingRoles: " + ex.Message + " : " + ex.StackTrace, new EventLogEntryWriteOptions()
             {
                 Category = "SAML", EventId = 1, EventType = "Error"
             });
         }
     }
 }
예제 #5
0
 private void ManageUser(User user, SamlTokenData samlTokenData)
 {
     _usersApi.RunAsUser("admin", () =>
     {
         if (user.PrivateEmail.ToLower() != samlTokenData.Email.ToLower())
         {
             try
             {
                 _usersApi.Update(new UsersUpdateOptions {
                     PrivateEmail = samlTokenData.Email, Id = user.Id
                 });
             }
             catch (Exception ex)
             {
                 _eventLogApi.Write("UserEmailManager Error ManageUser: "******" : " + ex.StackTrace, new EventLogEntryWriteOptions()
                 {
                     Category = "SAML", EventId = 1, EventType = "Error"
                 });
             }
         }
     });
 }
예제 #6
0
        internal static SamlTokenData GetTokenDataFromDatabase(string tokenKey)
        {
            try
            {
                var encryptedData = SqlData.GetTokenData(tokenKey);
                if (!string.IsNullOrEmpty(encryptedData))
                {
                    var samlXml       = SamlHelpers.Unprotect(encryptedData, typeof(SamlTokenData).Name);
                    var samlTokenData = SamlHelpers.Deserialize <SamlTokenData>(samlXml);
                    return(samlTokenData);
                }
            }
            catch (Exception ex)
            {
                ApiEventLog.Write("Error Extracting SAML token from database:" + ex, new EventLogEntryWriteOptions()
                {
                    Category = "SAML", EventType = "Error", EventId = 1001
                });
            }

            return(null);
        }
예제 #7
0
 void Log()
 {
     log.Write(ev, name, DateTime.Now);
 }
예제 #8
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="collection">Collection of components</param>
        /// <param name="reason">Reason</param>
        /// <param name="priority">Priority</param>
        /// <param name="dataConsumer">Data consumer</param>
        /// <param name="realtimeStep">Realtime step</param>
        /// <param name="realtime">Realtime provider</param>
        public DataRuntime(IComponentCollection collection, string reason, int priority,
                           IDataConsumer dataConsumer            = null,
                           IAsynchronousCalculation realtimeStep = null,
                           ITimeMeasureProvider realtime         = null)
        {
            this.collection   = collection;
            this.priority     = priority;
            this.reason       = reason;
            this.realtimeStep = realtimeStep;
            Prepare();
            if (realtime != null & priority == 0)
            {
                if (reason == StaticExtensionEventInterfaces.Realtime |
                    reason.IsRealtimeAnalysis())
                {
                    List <IRealtimeUpdate> lr = new List <IRealtimeUpdate>();
                    realTimeData =
                        new Dictionary <IComponentCollection, Tuple <IDataRuntime, double[],
                                                                     IDifferentialEquationProcessor, Action> >();
                    this.realtime = realtime as RealtimeProvider;
                    provider      = realtime;
                    dCollection   = new Dictionary <object, IComponentCollection>();

                    /*!!!! * DECOMPOSITION THINK AFTER
                     * collections = collection.Decompose(dCollection);
                     */
                    //========== REPLACED========
                    collections = new List <IComponentCollection>()
                    {
                        collection
                    };
                    collection.ForEach((IEvent ev) => { dCollection[ev] = collection; });
                    //===========================
                    IEventLog log = null;
                    if (reason.Equals(StaticExtensionEventInterfaces.Realtime))
                    {
                        log = StaticExtensionEventInterfaces.NewLog;
                    }
                    Action logupd = null;
                    if (log != null)
                    {
                        Dictionary <IMeasurement, string> namem = new Dictionary <IMeasurement, string>();
                        List <IMeasurement> lim = new List <IMeasurement>();
                        collection.ForEach((IMeasurements mm) =>
                        {
                            string name = (dataConsumer).GetMeasurementsName(mm) + ".";
                            for (int i = 0; i < mm.Count; i++)
                            {
                                IMeasurement mea = mm[i];
                                if (mea is IReplacedMeasurementParameter)
                                {
                                    lim.Add(mea);
                                    namem[mea] = name + mea.Name;
                                }
                            }
                        });
                        if (lim.Count > 0)
                        {
                            if (reason == "Realtime")
                            {
                                logupd = () =>
                                {
                                    logDictionary = new Dictionary <string, object>();
                                    foreach (IMeasurement mea in namem.Keys)
                                    {
                                        string n = namem[mea];
                                        logDictionary[n] = mea.Parameter();
                                    }
                                    log.Write(logDictionary, DateTime.Now);
                                };
                            }
                        }
                    }

                    if (collections.Count == 1)
                    {
                        IDifferentialEquationProcessor pr = CreateProcessor(collection);
                        double[] dt   = new double[1];
                        bool     find = (reason.Equals(StaticExtensionEventInterfaces.Realtime) |
                                         reason.Equals(StaticExtensionEventInterfaces.RealtimeLogAnalysis));
                        collection.ForEach((IRealtimeUpdate ru) => { lr.Add(ru); }, find);
                        collection.ForEach((IEvent ev) =>
                        {
                            IComponentCollection cc = CreateCollection(ev);
                            if (!realTimeData.ContainsKey(cc))
                            {
                                List <IRealtimeUpdate> lru = new List <IRealtimeUpdate>();
                                cc.ForEach((IRealtimeUpdate rud) => { lru.Add(rud); }, find);
                                IDataRuntime drc = Copy(cc);
                                double[] dtt     = new double[1];
                                Action actp      = CreateAction(drc, dtt, pr, logupd, lru.ToArray());
                                realTimeData[cc] =
                                    new Tuple <IDataRuntime, double[], IDifferentialEquationProcessor, Action>
                                        (drc, dtt, pr, actp);
                            }
                        });
                    }
                    else
                    {
                        foreach (IComponentCollection coll in collections)
                        {
                            List <IRealtimeUpdate> lu = new List <IRealtimeUpdate>();
                            foreach (object o in coll.AllComponents)
                            {
                                if (o is IObjectLabel)
                                {
                                    IRealtimeUpdate ru = (o as IObjectLabel).Object.GetObject <IRealtimeUpdate>();
                                    if (ru != null)
                                    {
                                        lu.Add(ru);
                                    }
                                }
                            }
                            IDataRuntime rt = Copy(coll);
                            IDifferentialEquationProcessor pr = CreateProcessor(collection);
                            double[] dt  = new double[1];
                            Action   act = CreateAction(rt, dt, pr, null, lr.ToArray());
                            realTimeData[coll] = new Tuple <IDataRuntime, double[], IDifferentialEquationProcessor, Action>
                                                     (rt, dt, pr, act);
                        }
                    }
                    (realtime as RealtimeProvider).Update();
                    startRuntime = provider.Time;
                    foreach (Tuple <IDataRuntime, double[], IDifferentialEquationProcessor, Action> t in realTimeData.Values)
                    {
                        t.Item2[0] = startRuntime;
                    }
                    collection.ForEach <IStarted>((IStarted st) => { st.Start(startRuntime); });
                }
            }
        }
예제 #9
0
 void Log(object[] output)
 {
     log.Write(reader, name, output, DateTime.Now);
 }