public void LiveAuthUtility_ParseScopeString_Empty()
        {
            object        scopes    = LiveAuthUtility.ParseScopeString("");
            List <string> scopeList = (List <string>)scopes;

            Assert.AreEqual(0, scopeList.Count);
        }
        public void LiveAuthUtility_BuildScopeString_SingleScope()
        {
            string[] scopes   = new string[] { "wl.signin" };
            string   scopeStr = LiveAuthUtility.BuildScopeString(scopes);

            Assert.AreEqual("wl.signin", scopeStr);
        }
        public void LiveAuthUtility_BuildLogoutUrl_WithHostOverride()
        {
            LiveAuthClient.AuthEndpointOverride = "https://login.live-int.com";
            string url = LiveAuthUtility.BuildLogoutUrl("000000004802B729", "http://www.foo.com/callback.aspx");

            Assert.AreEqual("https://login.live-int.com/oauth20_logout.srf?client_id=000000004802B729&redirect_uri=http%3A%2F%2Fwww.foo.com%2Fcallback.aspx", url);
        }
        public void LiveAuthUtility_BuildScopeString_EmptyScopes()
        {
            string[] scopes   = new string[] {  };
            string   scopeStr = LiveAuthUtility.BuildScopeString(scopes);

            Assert.AreEqual(string.Empty, scopeStr);
        }
 private void RefreshToken(Action <LiveLoginResult> completionCallback)
 {
     if (this.refreshTokenInfo != null)
     {
         LiveAuthRequestUtility.RefreshTokenAsync(
             this.clientId,
             null,
             LiveAuthUtility.BuildDesktopRedirectUrl(),
             this.refreshTokenInfo.RefreshToken,
             null     /*scopes*/
             ).ContinueWith(t =>
         {
             if (t.Exception != null)
             {
                 throw t.Exception;
             }
             this.OnRefreshTokenCompleted(t.Result, completionCallback);
         });
     }
     else
     {
         LiveLoginResult result = new LiveLoginResult(LiveConnectSessionStatus.Unknown, null);
         this.OnRefreshTokenCompleted(result, completionCallback);
     }
 }
        public void LiveAuthUtility_BuildScopeString_MultipleScopes()
        {
            string[] scopes   = new string[] { "wl.signin", "wl.basic", "wl.contacts" };
            string   scopeStr = LiveAuthUtility.BuildScopeString(scopes);

            Assert.AreEqual("wl.signin wl.basic wl.contacts", scopeStr);
        }
        public void LiveAuthUtility_ParseScopeString_CommaDelimited()
        {
            object        scopes    = LiveAuthUtility.ParseScopeString("wl.signin,wl.basic,wl.skydrive ");
            List <string> scopeList = (List <string>)scopes;

            Assert.AreEqual(3, scopeList.Count);
            Assert.AreEqual("wl.signin", scopeList[0]);
            Assert.AreEqual("wl.basic", scopeList[1]);
            Assert.AreEqual("wl.skydrive", scopeList[2]);
        }
        public void LiveAuthUtility_IsSubSetScopesRange()
        {
            Assert.IsTrue(LiveAuthUtility.IsSubsetOfScopeRange(new string[] { "wl.signin", "wl.basic" }, new string[] { "wl.signin", "wl.basic" }));
            Assert.IsTrue(LiveAuthUtility.IsSubsetOfScopeRange(new string[] { "wl.signin", "wl.basic" }, new string[] { "wl.basic", "wl.signin" }));
            Assert.IsTrue(LiveAuthUtility.IsSubsetOfScopeRange(new string[] { "wl.signin", "wl.basic" }, new string[] { "wl.basic", "wl.signin", "wl.skydrive" }));
            Assert.IsTrue(LiveAuthUtility.IsSubsetOfScopeRange(new string[] { "wl.signin", "wl.basic" }, new string[] { "wl.skydrive", "wl.basic", "wl.signin" }));
            Assert.IsTrue(LiveAuthUtility.IsSubsetOfScopeRange(null, new string[] { "wl.basic", "wl.signin", "wl.skydrive" }));

            Assert.IsFalse(LiveAuthUtility.IsSubsetOfScopeRange(new string[] { "wl.calendar" }, new string[] { "wl.basic", "wl.signin", "wl.skydrive" }));
            Assert.IsFalse(LiveAuthUtility.IsSubsetOfScopeRange(new string[] { "wl.signin", "wl.basic", "wl.calendar" }, new string[] { "wl.basic", "wl.signin", "wl.skydrive" }));
        }
        public void LiveAuthUtility_BuildWebAuthorizeUrl()
        {
            string url = LiveAuthUtility.BuildWebAuthorizeUrl(
                "000000004802B729",
                "http://www.foo.com/callback.aspx",
                new string[] { "wl.signin", "wl.basic" },
                DisplayType.Page,
                "zh-hans",
                "login page");

            Assert.AreEqual("https://login.live.com/oauth20_authorize.srf?client_id=000000004802B729&redirect_uri=http%3A%2F%2Fwww.foo.com%2Fcallback.aspx&scope=wl.signin%20wl.basic&response_type=code&display=page&locale=zh-hans&state=appstate%3Dlogin%2520page", url);
        }
        private void ExchangeCodeForToken(string authorizationCode)
        {
            Task <LiveLoginResult> task = LiveAuthRequestUtility.ExchangeCodeForTokenAsync(
                this.clientId,
                null,
                LiveAuthUtility.BuildDesktopRedirectUrl(),
                authorizationCode);

            task.ContinueWith((Task <LiveLoginResult> t) =>
            {
                this.OnExchangeCodeCompleted(t.Result);
            });
        }
        private LiveLoginResult ValidateSessionInitScopes(LiveLoginResult loginResult)
        {
            if (loginResult.Session != null && this.initScopes != null)
            {
                if (!LiveAuthUtility.IsSubsetOfScopeRange(this.initScopes, loginResult.Session.Scopes))
                {
                    loginResult = new LiveLoginResult(LiveConnectSessionStatus.NotConnected, null);
                }

                this.initScopes = null;
            }

            return(loginResult);
        }
        public void LiveAuthUtility_BuildAuthorizeUrl_WithEmptyState()
        {
            string url = LiveAuthUtility.BuildAuthorizeUrl(
                "000000004802B729",
                "http://www.foo.com/callback.aspx",
                new string[] { "wl.signin", "wl.basic" },
                ResponseType.Code,
                DisplayType.WinDesktop,
                ThemeType.Win7,
                "en-US",
                string.Empty);

            Assert.AreEqual("https://login.live.com/oauth20_authorize.srf?client_id=000000004802B729&redirect_uri=http%3A%2F%2Fwww.foo.com%2Fcallback.aspx&scope=wl.signin%20wl.basic&response_type=code&display=windesktop&locale=en-US&state=appstate%3D&theme=win7", url);
        }
        /// <summary>
        /// Generates a consent URL that includes a set of provided  parameters.
        /// </summary>
        /// <param name="scopes">A list of scope values that the user will need to authorize.</param>
        /// <param name="options">A table of optional authorization parameters to be encoded into the URL.</param>
        /// <returns>The generated login URL value.</returns>
        public string GetLoginUrl(IEnumerable <string> scopes, IDictionary <string, string> options)
        {
            LiveUtility.ValidateNotEmptyStringEnumeratorArguement(scopes, "scopes");

            string      locale      = null;
            string      state       = null;
            DisplayType display     = DisplayType.WinDesktop;
            ThemeType   theme       = ThemeType.Win7;
            string      redirectUrl = LiveAuthUtility.BuildDesktopRedirectUrl();

            if (options != null)
            {
                if (options.ContainsKey(AuthConstants.Locale))
                {
                    locale = options[AuthConstants.Locale];
                }

                if (options.ContainsKey(AuthConstants.ClientState))
                {
                    state = options[AuthConstants.ClientState];
                }

                if (options.ContainsKey(AuthConstants.Display))
                {
                    string displayStr = options[AuthConstants.Display];
                    if (!Enum.TryParse <DisplayType>(displayStr, true, out display))
                    {
                        throw new ArgumentException(ErrorText.ParameterInvalidDisplayValue, "display");
                    }
                }

                if (options.ContainsKey(AuthConstants.Theme))
                {
                    string themeStr = options[AuthConstants.Theme];
                    if (!Enum.TryParse <ThemeType>(themeStr, true, out theme))
                    {
                        throw new ArgumentException(ErrorText.ParameterInvalidDisplayValue, "theme");
                    }
                }
            }

            if (locale == null)
            {
                locale = CultureInfo.CurrentUICulture.ToString();
            }

            return(this.authClient.GetLoginUrl(scopes, redirectUrl, display, theme, locale, state));
        }
        public void LiveAuthUtility_GetCurrentRedirectUrl_NoQeury()
        {
            Uri uri = new Uri("http://www.foo.com/callback.aspx");

            Assert.AreEqual("http://www.foo.com/callback.aspx", LiveAuthUtility.GetCurrentRedirectUrl(uri));
        }
 /// <summary>
 /// Generates a consent URL that includes a set of provided parameters.
 /// </summary>
 public string GetLoginUrl(IEnumerable <string> scopes, string redirectUrl, DisplayType display, ThemeType theme, string locale, string state)
 {
     return(LiveAuthUtility.BuildAuthorizeUrl(this.clientId, redirectUrl, scopes, ResponseType.Code, display, theme, locale, state));
 }
        public void LiveAuthUtility_GetCurrentRedirectUrl_QueryWithMixedAuthAndOtherParams()
        {
            Uri uri = new Uri("http://www.foo.com/callback.aspx?a=b&c=d&code=4342332334&state=appstate%25hellome");

            Assert.AreEqual("http://www.foo.com/callback.aspx?a=b&c=d", LiveAuthUtility.GetCurrentRedirectUrl(uri));
        }
        public void LiveAuthUtility_GetCurrentRedirectUrl_QueryWithoutAuthParams()
        {
            Uri uri = new Uri("http://www.foo.com/callback.aspx?a=b&c=d");

            Assert.AreEqual("http://www.foo.com/callback.aspx?a=b&c=d", LiveAuthUtility.GetCurrentRedirectUrl(uri));
        }
 public void LiveAuthUtility_EncodeAppRequestState()
 {
     Assert.AreEqual("appstate=hello%20me", LiveAuthUtility.EncodeAppRequestState("hello me"));
 }
        public void LiveAuthUtility_BuildLogoutUrl()
        {
            string url = LiveAuthUtility.BuildLogoutUrl("000000004802B729", "http://www.foo.com/callback.aspx");

            Assert.AreEqual("https://login.live.com/oauth20_logout.srf?client_id=000000004802B729&redirect_uri=http%3A%2F%2Fwww.foo.com%2Fcallback.aspx", url);
        }
 public void LiveAuthUtility_DecodeAppRequestState()
 {
     Assert.AreEqual("hello me", LiveAuthUtility.DecodeAppRequestStates("appstate=hello%20me")[AuthConstants.AppState]);
 }
 /// <summary>
 /// Gets the logout URL.
 /// </summary>
 /// <returns>The logout URL.</returns>
 public string GetLogoutUrl()
 {
     return(LiveAuthUtility.BuildLogoutUrl());
 }
        public void LiveAuthUtility_BuildScopeString_NullScopes()
        {
            string scopeStr = LiveAuthUtility.BuildScopeString(null);

            Assert.AreEqual(string.Empty, scopeStr);
        }