Beispiel #1
0
        public void CreateAuthorizationUrl()
        {
            // ARRANGE
            string clientId         = "clientId1";
            string state            = "state1";
            string baseUri          = "https://dracoon.team";
            string deviceName       = "ImTheDeviceNo1324";
            string base64DeviceName = HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes(deviceName)));
            Uri    expected         = new Uri(baseUri + "/oauth/authorize?response_type=code&client_id=" + clientId + "&state=" + state + "&user_agent_info=" + base64DeviceName);

            Mock.Arrange(() => Arg.IsAny <Uri>().MustBeValid(Arg.AnyString)).DoNothing().Occurs(1);
            Mock.Arrange(() => Arg.AnyString.MustNotNullOrEmptyOrWhitespace(Arg.AnyString, false)).DoNothing().Occurs(2);

            // ACT
            Uri actual = OAuthHelper.CreateAuthorizationUrl(new Uri(baseUri), clientId, state, deviceName);

            // ASSERT
            Assert.Equal(expected.ToString(), actual.ToString());
            Mock.Assert(() => Arg.AnyString.MustNotNullOrEmptyOrWhitespace(Arg.AnyString, false));
            Mock.Assert(() => Arg.IsAny <Uri>().MustBeValid(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);
        }