Пример #1
0
        private void CheckIfEnterpriseAdmin()
        {
            if (_auth.AuthContext.IsEnterpriseAdmin)
            {
                Enterprise = new EnterpriseData(_auth);

                _auth.PushNotifications?.RegisterCallback(EnterpriseNotificationCallback);
                Task.Run(async() =>
                {
                    try
                    {
                        await Enterprise.PopulateEnterprise();

                        await this.AppendEnterpriseCommands(this);

                        var entRq = new GetEnterpriseDataCommand
                        {
                            include = new[] { "licenses", "managed_companies" }
                        };
                        var entRs = await _auth.ExecuteAuthCommand <GetEnterpriseDataCommand, GetEnterpriseDataResponse>(entRq);

                        if (entRs.ManagedCompanies?.Count > 0)
                        {
                            _managedCompanies.AddRange(entRs.ManagedCompanies);
                            Commands.Add("mc-list",
                                         new SimpleCommand
                            {
                                Order       = 70,
                                Description = "List managed companies",
                                Action      = ListManagedCompanies,
                            });
                            Commands.Add("mc-login",
                                         new ParsableCommand <EnterpriseMcLoginOptions>
                            {
                                Order       = 71,
                                Description = "Login to managed company",
                                Action      = LoginToManagedCompany,
                            });
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e);
                    }
                });
            }
        }
Пример #2
0
 public McEnterpriseContext(ManagedCompanyAuth auth)
 {
     if (auth.AuthContext.IsEnterpriseAdmin)
     {
         Enterprise = new EnterpriseData(auth, auth.TreeKey);
         Task.Run(async() =>
         {
             try
             {
                 await Enterprise.PopulateEnterprise();
                 await this.AppendEnterpriseCommands(this);
             }
             catch (Exception e)
             {
                 Debug.WriteLine(e);
             }
         });
     }
 }
        public async Task LoginToManagedCompany(EnterpriseData enterprise, int mcEnterpriseId)
        {
            Endpoint    = enterprise.Auth.Endpoint;
            DeviceToken = enterprise.Auth.DeviceToken;
            Username    = enterprise.Auth.Username;
            var mcRq = new LoginToMcRequest
            {
                McEnterpriseId = mcEnterpriseId,
            };
            var mcRs = await enterprise.Auth.ExecuteAuthRest <LoginToMcRequest, LoginToMcResponse>(
                "authentication/login_to_mc", mcRq);

            authContext = new AuthContext
            {
                DataKey         = enterprise.Auth.AuthContext.DataKey,
                SessionToken    = mcRs.EncryptedSessionToken.ToByteArray(),
                AccountAuthType = AccountAuthType.ManagedCompany,
            };

            TreeKey = CryptoUtils.DecryptAesV2(mcRs.EncryptedTreeKey.Base64UrlDecode(), enterprise.TreeKey);
            await PostLogin();
        }
        public static async Task PopulateUserPublicKeys(this EnterpriseData enterprise, IDictionary <string, byte[]> publicKeys, Action <string> warnings = null)
        {
            var toLoad = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);

            toLoad.UnionWith(publicKeys.Keys);
            toLoad.ExceptWith(enterprise.UserPublicKeyCache.Keys);
            if (toLoad.Count > 0)
            {
                var publicKeyRq = new PublicKeysCommand
                {
                    keyOwners = toLoad.ToArray()
                };
                var publicKeyRs = await enterprise.Auth.ExecuteAuthCommand <PublicKeysCommand, PublicKeysResponse>(publicKeyRq);

                if (publicKeyRs.publicKeys != null)
                {
                    foreach (var key in publicKeyRs.publicKeys)
                    {
                        if (!string.IsNullOrEmpty(key.publicKey))
                        {
                            enterprise.UserPublicKeyCache[key.keyOwner] = key.publicKey.Base64UrlDecode();
                        }
                        else
                        {
                            warnings?.Invoke($"User \'{key.keyOwner}\': Public key error ({key.resultCode}): {key.message}");
                            enterprise.UserPublicKeyCache[key.keyOwner] = null;
                        }
                    }
                }
            }

            foreach (var email in publicKeys.Keys.ToArray())
            {
                if (enterprise.UserPublicKeyCache.TryGetValue(email, out var pk))
                {
                    publicKeys[email] = pk;
                }
            }
        }
        public static async Task PopulateTeamKeys(this EnterpriseData enterprise, IDictionary <string, byte[]> teamKeys, Action <string> warnings = null)
        {
            var toLoad = new HashSet <string>();

            foreach (var teamUid in teamKeys.Keys.ToArray())
            {
                if (enterprise.TryGetTeam(teamUid, out var team))
                {
                    if (team.TeamKey != null)
                    {
                        teamKeys[teamUid] = team.TeamKey;
                    }
                    else
                    {
                        toLoad.Add(teamUid);
                    }
                }
            }

            if (toLoad.Count > 0)
            {
                var teamKeyRq = new TeamGetKeysCommand
                {
                    teams = toLoad.ToArray()
                };
                var teamKeyRs = await enterprise.Auth.ExecuteAuthCommand <TeamGetKeysCommand, TeamGetKeysResponse>(teamKeyRq);

                if (teamKeyRs.keys != null)
                {
                    foreach (var tk in teamKeyRs.keys)
                    {
                        byte[] key = null;
                        if (!string.IsNullOrEmpty(tk.key))
                        {
                            try
                            {
                                switch (tk.keyType)
                                {
                                case 1:
                                    key = CryptoUtils.DecryptAesV1(tk.key.Base64UrlDecode(), enterprise.Auth.AuthContext.DataKey);
                                    break;

                                case 2:
                                    key = CryptoUtils.DecryptRsa(tk.key.Base64UrlDecode(), enterprise.Auth.AuthContext.PrivateKey);
                                    break;

                                default:
                                    warnings?.Invoke($"Team \'{tk.teamUid}\' unsupported key type: {tk.keyType}");
                                    break;
                                }
                            }
                            catch (Exception e)
                            {
                                warnings?.Invoke(e.Message);
                            }
                        }

                        if (key == null)
                        {
                            continue;
                        }

                        if (enterprise.TryGetTeam(tk.teamUid, out var team))
                        {
                            team.TeamKey = key;
                        }

                        teamKeys[tk.teamUid] = key;
                    }
                }
            }
        }