Beispiel #1
0
        public void ExtractAuthorizationCodeFromUri()
        {
            // ARRANGE
            string expectedCode  = "code1";
            string expectedState = "state1";

            Mock.Arrange(() => Arg.IsAny <Uri>().MustNotNull(Arg.AnyString)).DoNothing().Occurs(1);
            Uri param = new Uri("https://dracoon.team/oauth/callback?code=" + expectedCode + "&state=" + expectedState);

            // ACT
            string actual = OAuthHelper.ExtractAuthorizationCodeFromUri(param);

            // ASSERT
            Assert.Equal(expectedCode, actual);
            Mock.Assert(() => Arg.IsAny <Uri>().MustNotNull(Arg.AnyString));
        }
        private static string AuthorizeClient()
        {
            string state = GenerateRandomBase64(32);

            // Create authorization uri
            Uri authUrl = OAuthHelper.CreateAuthorizationUrl(SERVER_URI, CLIENT_ID, state);

            // Open authorization URL in user's browser and wait for callback
            Uri loginResultUri = Login(authUrl).Result;

            // Extract the state and code from callback uri
            string callbackState = OAuthHelper.ExtractAuthorizationStateFromUri(loginResultUri);
            string callbackCode  = OAuthHelper.ExtractAuthorizationCodeFromUri(loginResultUri);

            // Check state
            if (!state.Equals(callbackState))
            {
                throw new Exception("Received OAuth state is not the same as expected!");
            }
            return(callbackCode);
        }