Ejemplo n.º 1
0
 /// <summary>
 /// Tries to find dragons by tags, if no dragons is found it will return empty list
 /// </summary>
 public async Task <List <Dragon> > FindDragonsAsync(List <string> stringTags)
 {
     using (var db = new DragonContext()) {
         var dragonsMatchTags = GetQueryableDragonThatMatchTags(db, stringTags);
         return(await dragonsMatchTags?.ToListAsync() ?? new List <Dragon>());
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Note: Does not check old password of oldpassword is set to null!
        /// </summary>
        public static void ChangePassword(this DragonContext ctx, string username, string oldpassword, string password)
        {
            if (!ctx.UserStore.HasUserByKey(SERVICE_ID, username))
            {
                throw new UserKeyDoesNotExistException();
            }
            else
            {
                var hashedSaltedSecret = HashUtil.ComputeHash(password);

                bool res;
                if (oldpassword == null)
                {
                    // do not verify oldpassword if set to null
                    ctx.UserStore.UpdateSecret(SERVICE_ID, username, hashedSaltedSecret);
                    res = true;
                }
                else
                {
                    res = ctx.UserStore.UpdateSecret(SERVICE_ID, username, (s) => HashUtil.VerifyHash(oldpassword, s), hashedSaltedSecret);
                }
                if (!res)
                {
                    throw new InvalidUserOrOldSecretException();
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// If Tags are in Database it will load them, if one does not exists, it will create it
        /// This one boii can modify Database, but doesn't save changes
        /// </summary>
        private async Task <List <Tag> > CreateTagListFromStringTagsAsync(DragonContext db, List <string> stringTags)
        {
            var lowerStringTags = stringTags.ConvertAll(st => st.ToLower());
            // Here we will have tags that exists in database
            List <Tag> tagsFound = await
                                   (
                from t in db.Tags
                where lowerStringTags.Contains(t.TagText)
                select t
                                   ).ToListAsync();

            if (tagsFound.Count == stringTags.Count)
            {
                return(tagsFound); // If we found all tags, we don't need to create missing
            }
            // Okey, we need to add some tags, let's go with this shit
            List <string> tagsTextFound = tagsFound.ConvertAll(t => t.TagText);
            List <string> missingTags   =
                (
                    from st in lowerStringTags
                    where tagsTextFound.Contains(st) == false
                    select st
                ).ToList();

            List <Tag> tagsToAdd = missingTags.ConvertAll(mt => new Tag(mt));
            await db.AddRangeAsync(tagsToAdd);

            return(tagsFound.Concat(tagsToAdd).ToList());
        }
Ejemplo n.º 4
0
 public static void EstablishExternallyValidatedAccount(this DragonContext ctx, string service, string identifier)
 {
     if (!ctx.UserStore.HasUserByKey(service, identifier))
     {
         // create account
         ctx.UserStore.Register(PrefixedService(service), identifier, string.Empty);
     }
 }
Ejemplo n.º 5
0
 public static bool Impersonate(
     this DragonContext ctx,
     Guid userID)
 {
     if (ctx.UserStore.Impersonate(userID))
     {
         return(true);
     }
     return(false);
 }
Ejemplo n.º 6
0
        public static void ChangePasswordForCurrentUser(this DragonContext ctx, string oldpassword, string password)
        {
            var hashedSaltedSecret = HashUtil.ComputeHash(password);

            var res = ctx.UserStore.UpdateSecret(ctx.CurrentUserID, (s) => HashUtil.VerifyHash(oldpassword, s), hashedSaltedSecret);

            if (!res)
            {
                throw new InvalidUserOrOldSecretException();
            }
        }
Ejemplo n.º 7
0
 public static void ChangeUsername(this DragonContext ctx, string username, string newusername)
 {
     if (!ctx.IsAuthenticated())
     {
         throw new Exception("User not authenticated");
     }
     else
     {
         ctx.UserStore.UpdateKey(SERVICE_ID, username, newusername);
     }
 }
Ejemplo n.º 8
0
 public static void RegisterUsernamePassword(this DragonContext ctx, string username, string password)
 {
     if (ctx.UserStore.HasUserByKey(SERVICE_ID, username))
     {
         throw new UserKeyAlreadyExistsForThisServiceException();
     }
     else
     {
         var hashedSaltedSecret = HashUtil.ComputeHash(password);
         ctx.UserStore.Register(SERVICE_ID, username, hashedSaltedSecret);
     }
 }
Ejemplo n.º 9
0
        public static bool ConnectWithFacebook(
            this DragonContext ctx,
            string key,
            string secret)
        {
            // check with facebook if these credentials are valid
            var     client = new FacebookClient(secret);
            dynamic me     = client.Get("me");

            if (me.id != key)
            {
                return(false);
            }

            if (ctx.CurrentUserID == Guid.Empty)
            {
                Guid?userID;

                if (ctx.UserStore.HasUserByKey(SERVICE_ID, key, out userID))
                {
                    ctx.UserStore.TryLogin(SERVICE_ID, key, (s) => true);
                    ctx.UserStore.UpdateSecret(SERVICE_ID, key, secret);
                }
                else
                {
                    ctx.UserStore.Register(SERVICE_ID, key, secret);
                }
                return(true);
            }
            else
            {
                Guid?userID;
                // user is logged-in, first check if this is registered
                if (ctx.UserStore.HasUserByKey(SERVICE_ID, key, out userID))
                {
                    if (userID.HasValue && userID.Value != ctx.CurrentUserID)
                    {
                        throw new UserKeyAlreadyExistsForThisServiceException();
                    }
                    else
                    {
                        // auth is already attached to this user
                        return(ctx.UserStore.TryLogin(SERVICE_ID, key, (s) => s.Equals(secret)));
                    }
                }
                else
                {
                    // attach auth to this user
                    ctx.UserStore.Register(SERVICE_ID, key, secret);
                    return(true);
                }
            }
        }
Ejemplo n.º 10
0
        public static bool TryLoginWithUsernamePassword(
            this DragonContext ctx,
            string username,
            string password)
        {
            var success = ctx.UserStore.TryLogin(SERVICE_ID, username, (s) => HashUtil.VerifyHash(password, s));

            if (success)
            {
                DragonContext.ProfileStore.SetProperty(ctx.CurrentUserID,
                                                       DragonContext.PROFILEKEY_SERVICE,
                                                       SERVICE_ID);
            }
            return(success);
        }
Ejemplo n.º 11
0
        private IQueryable <Dragon> GetQueryableDragonThatMatchTags(
            DragonContext db,
            List <string> stringTags
            )
        {
            var query =
                from dragon in db.Dragons
                where (
                    (from dt in dragon.DragonTags
                     where stringTags.Contains(dt.Tag.TagText)
                     select dt).Count() == stringTags.Count()
                    )
                select dragon;

            return(query);
        }
Ejemplo n.º 12
0
        public async Task AddDragonAsync(List <string> stringTags, string fileName)
        {
            using (var db = new DragonContext()) {
                List <Tag> tags = await CreateTagListFromStringTagsAsync(db, stringTags);

                var dragon = new Dragon()
                {
                    Filename = fileName
                };
                List <DragonTag> dragonTags = CreateDragonTags(tags, dragon);

                dragon.DragonTags = dragonTags;
                await db.AddAsync(dragon);

                await db.SaveChangesAsync();
            }
        }
Ejemplo n.º 13
0
        public static bool ExistsAccountForExternallyValidatedAccount(
            this DragonContext ctx, string service, string identifier)
        {
            if (service == null)
            {
                throw new Exception("Service is null");
            }
            if (identifier == null)
            {
                throw new Exception("Identifier is null");
            }

            var success = ctx.UserStore.TryLogin(PrefixedService(service), identifier, (s) => true);

            if (success)
            {
                DragonContext.ProfileStore.SetProperty(ctx.CurrentUserID,
                                                       DragonContext.PROFILEKEY_SERVICE,
                                                       SERVICE_ID);
            }
            return(success);
        }
Ejemplo n.º 14
0
        public async Task <Dragon> FindRandomDragonAsync(List <string> stringTags)
        {
            using (var db = new DragonContext()) {
                var dragonsMatchTags = GetQueryableDragonThatMatchTags(db, stringTags);

                if (dragonsMatchTags == null)
                {
                    return(null);
                }

                int dragonsFound = await dragonsMatchTags.CountAsync();

                if (dragonsFound == 0)
                {
                    return(null);
                }

                var rand   = new Random();
                int toSkip = rand.Next(0, dragonsFound);

                return(await dragonsMatchTags.Skip(toSkip).Take(1).FirstAsync());
            }
        }
Ejemplo n.º 15
0
 public DragonIdentity(DragonContext context):base(context.CurrentUserID.ToString(), "Dragon")
 {
 }
Ejemplo n.º 16
0
 public static void ChangePassword(this DragonContext ctx, string username, string password)
 {
     ChangePassword(ctx, username, null, password);
 }
Ejemplo n.º 17
0
 public static bool IsLocalAccountUser(this DragonContext ctx)
 {
     return(DragonContext.ProfileStore.GetProperty <string>(ctx.CurrentUserID, DragonContext.PROFILEKEY_SERVICE) == SERVICE_ID);
 }
Ejemplo n.º 18
0
 public DragonPrincipal(DragonContext ctx, string[] roles) : base(new DragonIdentity(ctx), roles)
 {
 }
Ejemplo n.º 19
0
 /*
  * Key and message independent initialization.
  */
 public Dragon(String key, String iv)
 {
     this.ctx = new DragonContext();
     keysetup(generateUserKeyFromCharKey(key));
     ivsetup(generateUserKeyFromCharKey(iv));
 }
Ejemplo n.º 20
0
 public DragonPrincipal(DragonContext ctx, string[] roles)
     : base(new DragonIdentity(ctx), roles)
 {
 }
Ejemplo n.º 21
0
 public static bool IsExternallyValidatedUser(this DragonContext ctx)
 {
     return(DragonContext.ProfileStore.GetProperty <string>(ctx.CurrentUserID, DragonContext.PROFILEKEY_SERVICE) == SERVICE_ID);
 }
Ejemplo n.º 22
0
 public DragonIdentity(DragonContext context) : base(context.CurrentUserID.ToString(), "Dragon")
 {
 }