예제 #1
0
        public async Task <AuthenticationResult> AuthenticateCardAsync(string Code, List <string> ImprivataServers)
        {
            ImprivataSettings settings = await GetSettings();

            if (manager == null)
            {
                manager = new ImprivataManager(_core, ImprivataServers[0], productId);
            }
            return(await manager.AuthenticateWithCardCodeAsync(Code, settings));
        }
예제 #2
0
        private async Task <ImprivataSettings> GetSettings()
        {
            if (Settings != null)
            {
                return(Settings);
            }
            try
            {
                var res = await _core.HttpClient.GetObjectAsync <ImprivataSettings>("imprivata/get_settings/");

                Settings = res.Result;
                return(Settings);
            }
            catch (Exception e)
            {
                _core.Logger.Error(this, "error while getting imprivata settings: " + e.Message);
                return(null);
            }
        }
        public async Task <AuthenticationResult> AuthenticateWithCardCodeAsync(string code, ImprivataSettings settings)
        {
            try
            {
                //change card depending on imprivata installation
                var           results   = new List <KeyValuePair <int, XElement> >();
                Exception     exception = null;
                List <string> codes     = new List <string>()
                {
                    code
                };
                if (!string.IsNullOrEmpty(settings?.TransformScript))
                {
                    try
                    {
                        var codes2 = Compiler.GetNumbers(settings?.TransformScript, code);
                        codes = codes.Concat(codes2).ToList();
                    }
                    catch (Exception ex)
                    {
                        _core.Logger.Error(this, ex.Message);
                    }
                }
                var tasks = new List <Task>();
                foreach (var str in codes)
                {
                    var t = new Task(() =>
                    {
                        try
                        {
                            var res   = RequestCard(str);
                            var dispp = int.Parse(res.Element("AuthState").Attribute("disp").Value);
                            results.Add(new KeyValuePair <int, XElement>(dispp, res));
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                        }
                    });
                    t.Start();
                    tasks.Add(t);
                }

                Console.WriteLine(@"waiting for tasks to finish");
                Task.WaitAll(tasks.ToArray(), 20000);

                if (exception != null)
                {
                    _core.Logger.Error(this, exception?.StackTrace);
                    throw exception;
                }
                Console.WriteLine(@"----  all tasks finished");
                int disp = 2;
                if (results.Any(r => r.Key == 0))
                {
                    //yay
                    disp         = 0;
                    _latestReply = results.First(r => r.Key == 0).Value;
                }
                else if (results.Any(r => r.Key == 1))
                {
                    //pass requested from 1 task
                    disp         = 1;
                    _latestReply = results.First(r => r.Key == 1).Value;
                }
                else if (results.Any(r => r.Key == 2))
                {
                    //pass requested from 1 task
                    disp         = 2;
                    _latestReply = results.First(r => r.Key == 2).Value;
                }
                switch (disp)
                {
                case 0:
                    var authTicket = _latestReply.Element("AuthTicket").Value;
                    var user       =
                        _latestReply.Element("Principal")
                        .Element("UserIdentity")
                        .Element("Username")
                        .Value;
                    var domain =
                        _latestReply.Element("Principal")
                        .Element("UserIdentity")
                        .Element("Domain")
                        .Value;
                    string usern = _latestReply.Element("Principal").Attribute("displayName").Value;
                    var    pass  = GetCredsFromAuthTicket(authTicket);
                    return(new AuthenticationResult()
                    {
                        AuthenticationTicket = authTicket,
                        Domain = domain,
                        Password = pass,
                        Username = user,
                        Name = usern
                    });

                case 1:
                    string state   = _latestReply.Element("ServerState").Value;
                    var    domain1 =
                        _latestReply.Element("Principal")
                        .Element("UserIdentity")
                        .Element("Domain")
                        .Value;
                    string modality =
                        _latestReply.Element("RemainingAuthPolicy")
                        .Element("AuthPolicyOption")
                        .Element("AuthPolicyItem")
                        .Attribute("modalityID")
                        .Value;
                    var user1 =
                        _latestReply.Element("Principal")
                        .Element("UserIdentity")
                        .Element("Username")
                        .Value;

                    string username            = _latestReply.Element("Principal").Attribute("displayName").Value;
                    PasswordRequestEventArgs p = new PasswordRequestEventArgs()
                    {
                        ServerState = state,
                        Name        = username,
                        Modality    = modality,
                        Username    = user1,
                        Domain      = domain1
                    };
                    string password = await ShowPasswordPopupAsync(username, modality);

                    return(await AuthenticateWithPasswordAsync(p, password));

                default:
                    throw new AuthenticationException("Account does not exist");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }