コード例 #1
0
ファイル: Program.cs プロジェクト: amigin/my-secrets
        static void Main(string[] args)
        {
            StateKeeper.LoadState();


            var pageContent = new PageContentWindow();

            pageContent.ShowWindow();

            var pagesWindow = new PagesWindow();

            pagesWindow.ShowWindow();
            var noteBooksWindow = new NoteBooksWindow();

            noteBooksWindow.ShowWindow();

            Console.CancelKeyPress += OnExit;


            Task.Run(Resize);

            while (true)
            {
                UiView.Render();
                var key = Console.ReadKey(false);
                UiView.KeyPressed(key);
            }
        }
コード例 #2
0
        public override ADBState GetADBState()
        {
            try
            {
                Log.V(cLogTag, ">> Entering GetADBState()");

                lock (mSyncLock)
                {
                    if (mStateKeeper == null)
                    {
                        mStateKeeper = new StateKeeper();
                    }
                    else if (!mStateKeeper.IsExpired)
                    {
                        Log.V(cLogTag, "Returning stored state");
                        return(mStateKeeper.State);
                    }
                }

                Log.V(cLogTag, "Stored state not available or expired; establishing state");

                DeviceInfo[] devices = GetDevices();
                UpdateDeviceData(devices);

                if (devices.Length == 0)
                {
                    return(mStateKeeper.SetState(ADBState.NoDevices));
                }
                else if (devices.Length > 1)
                {
                    return(mStateKeeper.SetState(ADBState.MoreDevices));
                }

                if (!devices[0].Online)
                {
                    return(ADBState.InvalidState);
                }

                if (!GetDeviceRooted())
                {
                    return(mStateKeeper.SetState(ADBState.NoRoot));
                }

                return(mStateKeeper.SetState(ADBState.AllOK));
            }
            catch (AdbExeException adbExeEx)
            {
                return(adbExeEx.ADBState);
            }
            catch
            {
                return(mStateKeeper.SetState(ADBState.CannotRun));
            }
            finally
            {
                Log.V(cLogTag, "<< Exiting GetADBState()");
            }
        }
コード例 #3
0
        protected override Task ApplyResponseChallengeAsync()
        {
            if (Response.StatusCode != 401)
            {
                return(Task.FromResult <object>(null));
            }

            AuthenticationResponseChallenge challenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode);

            if (challenge != null)
            {
                string baseUri =
                    Request.Scheme +
                    Uri.SchemeDelimiter +
                    Request.Host +
                    Request.PathBase;

                string currentUri =
                    baseUri +
                    Request.Path +
                    Request.QueryString;

                string redirectUri = string.IsNullOrEmpty(Options.UnionCallbackPath) ? baseUri : Options.UnionCallbackPath
                                     + Options.CallbackPath;

                AuthenticationProperties properties = challenge.Properties;
                if (string.IsNullOrEmpty(properties.RedirectUri))
                {
                    properties.RedirectUri = currentUri;
                }

                // OAuth2 10.12 CSRF
                GenerateCorrelationId(properties);

                // comma separated
                string scope = string.Join(",", Options.Scope);

                string state = Options.StateDataFormat.Protect(properties);

                //store state
                var stateKey = StateKeeper.Put(state);

                string authorizationEndpoint =
                    Options.AuthorizationEndpoint +
                    "?appid=" + Uri.EscapeDataString(Options.AppId ?? string.Empty) +
                    "&redirect_uri=" + Uri.EscapeDataString(redirectUri) +
                    "&scope=" + Uri.EscapeDataString(scope) +
                    "&response_type=code&state=" + Uri.EscapeDataString(stateKey) +
                    "#wechat_redirect";

                var redirectContext = new WeChatApplyRedirectContext(
                    Context, Options,
                    properties, authorizationEndpoint);
                Options.Provider.ApplyRedirect(redirectContext);
            }

            return(Task.FromResult <object>(null));
        }
コード例 #4
0
        public static void Add(string name)
        {
            var result = new List <Notebook>();

            result.AddRange(_items);
            result.Add(Notebook.Create(name));
            _items = result.ToArray();
            StateKeeper.StateHasToBeSynced();
        }
コード例 #5
0
        public static void Update(string id, string name)
        {
            var item = _items.FirstOrDefault(itm => itm.Id == id);

            if (item != null)
            {
                item.Name = name;
            }

            StateKeeper.StateHasToBeSynced();
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: amigin/my-secrets
        private static async Task Resize()
        {
            while (true)
            {
                UiView.CheckResize();
                StateKeeper.CheckIfStateHasToBeSaved();

                if (UiNotifications.HasItemToShow())
                {
                    UiView.Render();
                }

                await Task.Delay(1000);
            }
        }
コード例 #7
0
ファイル: Pages.cs プロジェクト: amigin/my-secrets
        public static void Add(string noteBookId, string pageName)
        {
            lock (_pages)
            {
                if (!_pages.ContainsKey(noteBookId))
                {
                    _pages.Add(noteBookId, new Page[0]);
                }

                var result = new List <Page>();
                result.AddRange(_pages[noteBookId]);
                result.Add(Page.Create(pageName));

                _pages[noteBookId] = result.ToArray();

                StateKeeper.StateHasToBeSynced();
            }
        }
コード例 #8
0
ファイル: Pages.cs プロジェクト: amigin/my-secrets
        public static void Update(string notebookId, string pageId, string name)
        {
            lock (_pages)
            {
                if (!_pages.ContainsKey(notebookId))
                {
                    return;
                }

                var page = _pages[notebookId].FirstOrDefault(itm => itm.Id == pageId);

                if (page != null)
                {
                    page.Name = name;
                }

                StateKeeper.StateHasToBeSynced();
            }
        }
コード例 #9
0
        public void KeyPressed(ConsoleKeyInfo key)
        {
            if (!IsActive)
            {
                return;
            }


            switch (key.Key)
            {
            case ConsoleKey.F2:
                StateKeeper.StateHasToBeSynced();
                return;

            case ConsoleKey.S:
                if ((key.Modifiers & ConsoleModifiers.Control) != 0)
                {
                    StateKeeper.StateHasToBeSynced();
                }
                break;

            case ConsoleKey.F8:
                Value.DeleteLine();
                return;


            case ConsoleKey.Tab:
                if (key.Modifiers == ConsoleModifiers.Shift)
                {
                    NavigateBack();
                }
                return;
            }

            Value.KeyPressed(key);
        }
コード例 #10
0
        public void KeyPressed(ConsoleKeyInfo key)
        {
            if (!IsActive)
            {
                return;
            }

            switch (key.Key)
            {
            case ConsoleKey.F7:
                new EnterNameWindow
                {
                    Header = "Enter notebook name"
                }
                .SubscribeOnEnter((ctx, value) =>
                {
                    NotebooksRepository.Add(value);
                    ActiveNotebookChanged();
                })
                .ShowModalWindow();

                return;

            case ConsoleKey.Enter:
            case ConsoleKey.Tab:
                var selectedNotebook = GetSelectedNotebook();
                if (selectedNotebook != null)
                {
                    UiView.SetActiveWindow(PagesWindow.MyId);
                }
                return;

            case ConsoleKey.F2:
                StateKeeper.StateHasToBeSynced();
                return;

            case ConsoleKey.F4:
                new EnterNameWindow
                {
                    Context = NotebooksRepository.Get(SelectedIndex),
                    Header  = "Edit notebook name",
                }
                .SetValue(NotebooksRepository.Get(SelectedIndex).Name)
                .SubscribeOnEnter((ctx, value) =>
                {
                    var noteBook = (Notebook)ctx;
                    NotebooksRepository.Update(noteBook.Id, value);
                })
                .ShowModalWindow();
                return;

            case ConsoleKey.DownArrow:
            {
                if (SelectedIndex < NotebooksRepository.Count - 1)
                {
                    SelectedIndex += 1;
                    ActiveNotebookChanged();
                }

                return;
            }

            case ConsoleKey.UpArrow:
            {
                if (SelectedIndex > 0)
                {
                    SelectedIndex--;
                    ActiveNotebookChanged();
                }

                break;
            }
            }
        }
コード例 #11
0
        public void KeyPressed(ConsoleKeyInfo key)
        {
            if (!IsActive)
            {
                return;
            }

            switch (key.Key)
            {
            case ConsoleKey.Escape:
                NavigateBack();
                return;

            case ConsoleKey.Tab:
                if (key.Modifiers == ConsoleModifiers.Shift)
                {
                    NavigateBack();
                }
                else
                {
                    NavigateForward();
                }
                return;

            case ConsoleKey.Enter:
                NavigateForward();
                return;



            case ConsoleKey.F7:
                new EnterNameWindow
                {
                    Header = "Enter page name"
                }
                .SubscribeOnEnter((_, value) =>
                {
                    PagesRepository.Add(_activeNoteBook.Id, value);
                    ActivePageChanged();
                })
                .ShowModalWindow();

                return;


            case ConsoleKey.F2:
                StateKeeper.StateHasToBeSynced();
                return;


            case ConsoleKey.F4:
                var selectedPage = GetSelectedPage();
                if (selectedPage != null)
                {
                    new EnterNameWindow
                    {
                        Context = selectedPage,
                        Header  = "Edit notebook name",
                    }
                }
                .SetValue(selectedPage.Name)
                .SubscribeOnEnter((ctx, value) =>
                {
                    var page = (Page)ctx;
                    PagesRepository.Update(_activeNoteBook.Id, page.Id, value);
                    ActivePageChanged();
                })
                .ShowModalWindow();
                return;
コード例 #12
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties properties = null;

            try
            {
                #region code + token
                string code  = null;
                string state = null;

                IReadableStringCollection query = Request.Query;

                IList <string> values = query.GetValues("code");
                if (values != null && values.Count == 1)
                {
                    code = values[0];
                }
                values = query.GetValues("state");
                if (values != null && values.Count == 1)
                {
                    var stateKey = values[0];
                    //load state
                    state = StateKeeper.Pop(stateKey);
                }

                properties = Options.StateDataFormat.Unprotect(state);
                if (properties == null)
                {
                    return(null);
                }

                // OAuth2 10.12 CSRF
                if (!ValidateCorrelationId(properties, _logger))
                {
                    return(new AuthenticationTicket(null, properties));
                }

                if (code == null)
                {
                    // Null if the remote server returns an error.
                    return(new AuthenticationTicket(null, properties));
                }

                var tokenResult = OAuthApi.GetAccessToken(Uri.EscapeDataString(Options.AppId),
                                                          Uri.EscapeDataString(Options.AppSecret),
                                                          Uri.EscapeDataString(code));
                if (tokenResult == null || string.IsNullOrEmpty(tokenResult.access_token))
                {
                    _logger.WriteWarning("Access token was not found");
                    return(new AuthenticationTicket(null, properties));
                }
                #endregion

                #region pull user info
                UserInfoJson  userInfo      = null;
                OAuthUserInfo oauthUserInfo = null;
                WeChatAuthenticatedContext context;

                if (Options.ScopeType == ScopeTypes.UserInfo)
                {
                    oauthUserInfo = OAuthApi.GetUserInfo(Options.AppId, tokenResult.openid, Senparc.Weixin.Language.zh_CN);
                }
                else if (Options.IsLoadUserInfo || Options.UseUnionIdAsIdentity)
                {
                    userInfo = UserApi.Info(Options.AppId, tokenResult.openid);
                }

                context = new WeChatAuthenticatedContext(Context, userInfo, oauthUserInfo,
                                                         tokenResult.access_token, tokenResult.expires_in);
                context.Identity = new ClaimsIdentity(Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType,
                                                      ClaimsIdentity.DefaultRoleClaimType);

                //identify by unionid or openid
                if (Options.UseUnionIdAsIdentity)
                {
                    //union id
                    context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userInfo?.unionid ?? oauthUserInfo?.unionid,
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }
                else
                {
                    //open id
                    context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, tokenResult.openid,
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }

                //open id
                context.Identity.AddClaim(new Claim(WeChatClaimTypes.OpenId, tokenResult.openid,
                                                    ClaimValueTypes.String, Options.AuthenticationType));

                //union id
                if (!string.IsNullOrEmpty(userInfo?.unionid ?? oauthUserInfo?.unionid))
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.UnionId, userInfo?.unionid ?? oauthUserInfo?.unionid,
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }

                //nickname
                if (!string.IsNullOrEmpty(userInfo?.nickname ?? oauthUserInfo?.nickname))
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.NickName, userInfo?.nickname ?? oauthUserInfo?.nickname,
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }

                //city
                if (!string.IsNullOrEmpty(userInfo?.city ?? oauthUserInfo?.city))
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.City, userInfo?.city ?? oauthUserInfo?.city,
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }

                //province
                if (!string.IsNullOrEmpty(userInfo?.province ?? oauthUserInfo?.province))
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.Province, userInfo.province,
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }

                //country
                if (!string.IsNullOrEmpty(userInfo?.country ?? oauthUserInfo?.country))
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.Country, userInfo?.country ?? oauthUserInfo?.country,
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }

                //headimage
                if (!string.IsNullOrEmpty(userInfo?.headimgurl ?? oauthUserInfo?.headimgurl))
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.HeadImageUrl, userInfo?.headimgurl ?? oauthUserInfo?.headimgurl,
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }

                //sex
                if ((userInfo?.sex ?? 0) > 0)
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.Sex, userInfo.sex == 1 ? "M" : "F",
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }
                else if ((oauthUserInfo?.sex ?? 0) > 0)
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.Sex, oauthUserInfo.sex == 1 ? "M" : "F",
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }

                //privilege
                if (oauthUserInfo?.privilege != null && oauthUserInfo.privilege.Length > 0)
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.privilege, string.Join(",", oauthUserInfo.privilege),
                                                        ClaimValueTypes.String, Options.AuthenticationType));
                }

                //group
                if (userInfo?.groupid != null)
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.GroupId, userInfo.groupid.ToString(),
                                                        ClaimValueTypes.Integer32, Options.AuthenticationType));
                }

                //subscribed
                if (userInfo?.subscribe != null)
                {
                    context.Identity.AddClaim(new Claim(WeChatClaimTypes.Subscribed, userInfo.subscribe == 0 ? "False" : "True",
                                                        ClaimValueTypes.Boolean, Options.AuthenticationType));

                    if (userInfo.subscribe == 1)
                    {
                        //subscribed time
                        if (userInfo?.subscribe_time != null)
                        {
                            context.Identity.AddClaim(new Claim(WeChatClaimTypes.SubscribedTime, DateTime.Now.AddMilliseconds(-userInfo.subscribe_time).ToString(),
                                                                ClaimValueTypes.DateTime, Options.AuthenticationType));
                        }
                    }
                }

                //loaded time
                context.Identity.AddClaim(new Claim(WeChatClaimTypes.LoadedTime, DateTime.Now.ToString(),
                                                    ClaimValueTypes.DateTime, Options.AuthenticationType));


                context.Properties = properties;
                #endregion

                await Options.Provider.Authenticated(context);

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                _logger.WriteError("Authentication failed", ex);
                return(new AuthenticationTicket(null, properties));
            }
        }