コード例 #1
0
        public async Task <Models.Community> Post([FromBody] Models.Community community)
        {
            User.EnsureCommunityAdmin(community.handle);

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                var orgOCUrl = _dbContext.Communities.AsNoTracking().Single(c => c.handle == community.handle).OCUrl;


                var attached = _dbContext.Communities.Attach(community);
                attached.State = EntityState.Modified;
                attached.Property(c => c.OCUrl).IsModified = false;


                await _dbContext.SaveChangesAsync();

                var ocs = new OpenChainServer(orgOCUrl);
                using (var ad = ocs.Login(TokenController.OCAdminpassPhrase))
                {
                    await updateLedgerInfo(ad, community);
                }

                transaction.Commit();
                return(community);
            }
        }
コード例 #2
0
ファイル: TokenController.cs プロジェクト: shreedee/sharonomy
        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);
        }
コード例 #3
0
ファイル: TokenController.cs プロジェクト: shreedee/sharonomy
        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));
        }
コード例 #4
0
ファイル: TokenController.cs プロジェクト: shreedee/sharonomy
        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));
        }
コード例 #5
0
        public async Task Run()
        {
            var ocs = new OpenChainServer("http://*****:*****@alice", 100, assetPath)}");
                        Console.WriteLine($"Transfert : {await ad.Transfert(assetPath, "@bob", 33, assetPath)}");

                        foreach (var r in await b.GetAccountRecords())
                        {
                            Console.WriteLine($"b  : {r}");
                        }
                        foreach (var r in await a.GetAccountRecords())
                        {
                            Console.WriteLine($"a  : {r}");
                        }
                    }
        }
コード例 #6
0
        public async Task <Models.Community> Put(String handle, [FromBody] Models.UpdateCommunityReq req)
        {
            using (var transaction = _dbContext.Database.BeginTransaction())
                using (var cli = new HttpClient())
                {
                    if (handle == UNKNOWN_COMMUNITY)
                    {
                        throw new Converters.DisplayableException("invalid handle");
                    }

                    //fix the admin user detail
                    var userHandle = this.User.Identity.Name;
                    var adminUser  = _dbContext.Users.Single(u =>
                                                             u.communityHandle == CommunityController.UNKNOWN_COMMUNITY &&
                                                             u.handle == userHandle);
                    adminUser.address = $"{handle}_admin";

                    var newCommunity = new Models.Community
                    {
                        handle      = handle,
                        OCUrl       = $"{_ocURLBase}{handle}/",
                        description = req.description,
                        full_name   = req.full_name
                    };

                    _dbContext.Communities.Add(newCommunity);
                    await _dbContext.SaveChangesAsync();

                    var query   = $"{_siteCreatorURL}?site={handle}";
                    var tresult = await cli.GetAsync(query);

                    if (tresult.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        throw new Converters.DisplayableException("Site creation error");
                    }

                    var s = await tresult.Content.ReadAsStringAsync();

                    var ocs = new OpenChainServer(newCommunity.OCUrl);
                    using (var ad = ocs.Login(TokenController.OCAdminpassPhrase))
                    {
                        await updateLedgerInfo(ad, req);

                        //create the asset defination
                        var assetPath = $"/treasury/{handle}_hours/";



                        var assetDef = await ad.GetData <Models.AssetDefination>(getTreasuryPath(handle), "asdef");

                        assetDef.Value = new Models.AssetDefination {
                            name = $"Hour exchange of {req.full_name}"
                        };
                        await ad.SetData(assetDef);

                        var assetsToCreate = new[] { $"/asset/{handle}_hours/", $"/{handle}_login/" };

                        //create the assets
                        var accs = await Task.WhenAll(assetsToCreate.Select(async a =>
                        {
                            var record = await ad.Api.GetValue(getTreasuryPath(handle), "ACC", a);
                            return(new AccountRecord(record).GetRecord());
                        }).ToArray());

                        var mutation = ad.Api.BuildMutation(Openchain.ByteString.Empty, accs);
                        await ad.PostMutation(mutation);

                        await SetAdminACL(ad, handle, req.adminPubKey);
                    }
                    transaction.Commit();
                    return(newCommunity);
                }
        }