public async Task <Dictionary <String, String> > myHandles(String Community, String PubKey)
        {
            var toRet = new Dictionary <String, String>();

            var user = await _dbContext.Users.SingleOrDefaultAsync(
                u => u.communityHandle == Community &&
                u.pubKey == PubKey
                );

            if (null != user)
            {
                toRet["Handle"] = CommunityController.getUserPath(user.handle);
            }

            var communityObj = await _dbContext.Communities.SingleAsync(c => c.handle == Community);



            var ocs = new OpenChainServer(communityObj.OCUrl);

            using (var ad = ocs.Login(TokenController.OCAdminpassPhrase))
            {
                //check for admin
                var treasuryACL = await getACL(ad, CommunityController.getTreasuryPath(Community),
                                               new[] { new {
                                                           subjects = new [] { new {
                                                                                   addresses = new String[] { },
                                                                                   required  = 1
                                                                               } }
                                                       } }
                                               );

                var adminAddress = treasuryACL.SelectMany(t =>
                                                          t.subjects.SelectMany(s => s.addresses.Select(a => a)));

                if (adminAddress.Contains(PubKey))
                {
                    toRet["treasuryHandle"] = CommunityController.getTreasuryPath(Community);
                }
            }



            return(toRet);
        }
Exemple #2
0
        public async Task <Models.User> Post([FromBody] Models.updateUserRequest req)
        {
            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                _dbContext.Users.Add(req.user);
                await _dbContext.SaveChangesAsync();

                var community = _dbContext.Communities.Single(c => c.handle == req.user.communityHandle);

                await TokenController.TransactionVerifier(CommunityController.getUserPath(req.user.handle),
                                                          req.transaction, $"{community.OCUrl}");

                transaction.Commit();
            }


            return(req.user);
        }
        public async Task <dynamic> Post(String Community, [FromBody] AuthResetRequest req)
        {
            var user = await _dbContext.Users.
                       SingleOrDefaultAsync(u => u.communityHandle == Community && u.email == req.email);

            var Claims = new Dictionary <string, string> {
                { "Community", Community }
            };

            var CommunityObj = await _dbContext.Communities.SingleAsync(c => c.handle == Community);

            if (null == user)
            {
                //check if admin
                user = await _dbContext.Users.SingleOrDefaultAsync(u =>
                                                                   u.communityHandle == CommunityController.UNKNOWN_COMMUNITY &&
                                                                   u.email == req.email &&
                                                                   u.address == $"{Community}_admin");

                if (null == user)
                {
                    throw new Converters.DisplayableException("invalid email or pin");
                }
                else
                {
                    //we have admin
                    Claims[ADMINCLAIM] = "true";
                }
            }
            else
            {
                if (user.communityHandle == CommunityController.UNKNOWN_COMMUNITY &&
                    Community == CommunityController.UNKNOWN_COMMUNITY)
                {
                    Claims[ADMINCLAIM] = "true";
                }
                else
                {
                    Claims[ACCLAIM] = "true";
                    user.pubKey     = req.PubKey;
                }
            }

            if (user.ResetPin != req.pin)
            {
                throw new Converters.DisplayableException("email or pin are incorrect");
            }
            user.ResetPin = null;
            await _dbContext.SaveChangesAsync();

            var ocs = new OpenChainServer(CommunityObj.OCUrl);

            using (var ad = ocs.Login(TokenController.OCAdminpassPhrase))
            {
                if (Claims.ContainsKey(ADMINCLAIM) && CommunityController.UNKNOWN_COMMUNITY != Community)
                {
                    await CommunityController.SetAdminACL(ad, Community, req.PubKey);
                }

                if (Claims.ContainsKey(ACCLAIM))
                {
                    await CommunityController.setACL(ad, CommunityController.getUserPath(user.handle), new[] { new {
                                                                                                                   subjects = new [] { new {
                                                                                                                                           addresses = new[] { req.PubKey },
                                                                                                                                           required  = 1
                                                                                                                                       } },
                                                                                                                   permissions = new { account_spend = "Permit", account_modify = "Permit" }
                                                                                                               } });
                }
            }

            return(createToken(user, Claims));
        }
        public async Task <dynamic> Post(String Community, [FromBody] AuthRequest req)
        {
            var user = await _dbContext.Users.SingleOrDefaultAsync(
                u => u.communityHandle == Community &&
                u.pubKey == req.PubKey
                );

            var communityObj = await _dbContext.Communities.SingleAsync(c => c.handle == Community);

            var Claims = new Dictionary <string, string> ();

            if (null != user)
            {
                if (null == req.Handle_transaction)
                {
                    throw new Converters.DisplayableException("Handle transaction needed");
                }

                await TransactionVerifier(CommunityController.getUserPath(user.handle),
                                          req.Handle_transaction, communityObj.OCUrl);

                Claims[ACCLAIM] = "true";
            }

            if (null != req.treasuryHandle_transaction)
            {
                var ocs = new OpenChainServer(communityObj.OCUrl);
                using (var ad = ocs.Login(TokenController.OCAdminpassPhrase))
                {
                    //check for admin
                    var treasuryACL = await getACL(ad, CommunityController.getTreasuryPath(Community),
                                                   new[] { new {
                                                               subjects = new [] { new {
                                                                                       addresses = new String[] { },
                                                                                       required  = 1
                                                                                   } }
                                                           } }
                                                   );

                    var adminAddress = treasuryACL.SelectMany(t =>
                                                              t.subjects.SelectMany(s => s.addresses.Select(a => a)));

                    if (adminAddress.Contains(req.PubKey))
                    {
                        await TransactionVerifier(CommunityController.getTreasuryPath(Community),
                                                  req.treasuryHandle_transaction, communityObj.OCUrl);

                        //we have admin
                        Claims[ADMINCLAIM] = "true";

                        if (null == user)
                        {
                            user = _dbContext.Users.First(u =>
                                                          u.communityHandle == CommunityController.UNKNOWN_COMMUNITY &&
                                                          u.address == $"{Community}_admin");
                        }
                    }
                }
            }

            if (Claims.Count() == 0)
            {
                throw new Converters.DisplayableException("Failed to sign in");
            }

            Claims[COMMUNITYCLAIM] = Community;
            return(createToken(user, Claims));
        }