Example #1
0
        public HomeController( )
        {
            var appFactory = new D2LAppContextFactory();

            m_valenceAppContext = appFactory.Create(APP_ID, APP_KEY);
            m_valenceHost       = new HostSpec("https", LMS_URL, 443);
        }
        public HomeController( )
        {
            var appFactory = new D2LAppContextFactory();

            m_valenceAppContext = appFactory.Create(m_appId, m_appKey);
            m_valenceHost       = new HostSpec("https", LMS_URL, 443);
        }
        private static ID2LUserContext InterceptUserTokens( HostSpec host, ID2LAppContext appContext )
        {
            // Start HTTP server and listen for the redirect after a successful auth
            var httpListener = new HttpListener();
            httpListener.Prefixes.Add("http://localhost:31337/result/");
            httpListener.Start();

            // This call blocks until we get a response
            var ctx = httpListener.GetContext();

            // The LMS returns the user tokens via query parameters to the value provided originally in x_target
            // TODO: deal with "failed to login" case
            var userContext = appContext.CreateUserContext( ctx.Request.Url, host );

            // Send some JavaScript to close the browser popup
            // This is not 100% effective: for example, Firefox will ignore this.
            const string RESPONSE = "<!doctype html><meta charset=\"utf-8\"><script>window.close();</script><h1>You may now close your window</h1><p>You may or may not see this message, depending on your browser</p>";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes( RESPONSE );
            ctx.Response.ContentType = "text/html";
            ctx.Response.ContentLength64 = buffer.Length;
            ctx.Response.OutputStream.Write( buffer, 0, buffer.Length );
            ctx.Response.OutputStream.Close();
            httpListener.Stop();

            return userContext;
        }
        private static ID2LUserContext  InterceptUserTokens(HostSpec host, ID2LAppContext appContext)
        {
            // Start HTTP server and listen for the redirect after a successful auth
            var httpListener = new HttpListener();

            httpListener.Prefixes.Add("http://localhost:31337/result/");
            httpListener.Start();

            // This call blocks until we get a response
            var ctx = httpListener.GetContext();

            // The LMS returns the user tokens via query parameters to the value provided originally in x_target
            // TODO: deal with "failed to login" case
            var userContext = appContext.CreateUserContext(ctx.Request.Url, host);

            // Send some JavaScript to close the browser popup
            // This is not 100% effective: for example, Firefox will ignore this.
            const string RESPONSE = "<!doctype html><meta charset=\"utf-8\"><script>window.close();</script><h1>You may now close your window</h1><p>You may or may not see this message, depending on your browser</p>";

            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(RESPONSE);
            ctx.Response.ContentType     = "text/html";
            ctx.Response.ContentLength64 = buffer.Length;
            ctx.Response.OutputStream.Write(buffer, 0, buffer.Length);
            ctx.Response.OutputStream.Close();
            httpListener.Stop();

            return(userContext);
        }
        public void SetUpAppContext()
        {
            var timestampProvider = TestUtils.CreateTimestampProviderStub(TestConstants.TIMESTAMP_MILLISECONDS);

            m_appContext  = TestUtils.CreateAppContextUnderTest(timestampProvider);
            m_userContext = CreateUserContextUnderTest();
            m_anonContext = CreateAnonymousContextUnderTest();
        }
Example #6
0
        public Valence()
        {
            var appFactory = new D2LAppContextFactory();
            m_valenceAppContext = appFactory.Create(m_appId, m_appKey);
            m_valenceHost = new HostSpec("https", LMS_URL, 443);

            // Note Using existing authenicated Tokens
            m_valenceUserContext = m_valenceAppContext.CreateUserContext(m_userId, m_userKey, m_valenceHost);
        }
Example #7
0
        /*
         * Creates a new ID2LUserContext object
         *
         * @return
         *      an instance of a ID2LUserContext
         */
        public static ID2LUserContext GetD2LUserContext()
        {
            D2LAppContextFactory factory     = new D2LAppContextFactory();
            ID2LAppContext       appContext  = factory.Create(Utility.GetAppIdValence(), Utility.GetAppKeyValence());
            HostSpec             hostInfo    = new HostSpec(Utility.GetSchemaValence(), Utility.GetHostValence(), Utility.GetPortValence());
            ID2LUserContext      userContext = appContext.CreateUserContext(Utility.GetUserIdValence(), Utility.GetUserKeyValence(), hostInfo);

            return(userContext);
        }
Example #8
0
        public ValenceAuthenticator CreateAuthenticator()
        {
            D2LAppContextFactory contextFactory = new D2LAppContextFactory();
            ID2LAppContext       appContext     = contextFactory.Create(m_appId, m_appKey);
            HostSpec             valenceHost    = new HostSpec("https", m_lmsUrl, 443);
            ID2LUserContext      userContext    = appContext.CreateUserContext(m_userId, m_userKey, valenceHost);

            return(new ValenceAuthenticator(userContext));
        }
        public void SendAuthenticatedRequestWithDelayedTimestamp_ResponseMustContainTimeOffset()
        {
            ID2LAppContext  appContext  = CreateAppContextWithDelay(ConfigHelper.AppId, ConfigHelper.AppKey, TEST_TIME_DELAY);
            ID2LUserContext userContext = ContextProvider.UserContext(appContext);
            Uri             uri         = userContext.CreateAuthenticatedUri(RouteProvider.OrganizationInfoRoute, "GET");
            HttpWebRequest  request     = (HttpWebRequest)WebRequest.Create(uri);

            request.Method = "GET";

            try {
                using (request.GetResponse() as HttpWebResponse) { }
            } catch (WebException ex) {
                string responseContents = StringHelper.ReadResponseContents(ex.Response as HttpWebResponse);

                StringAssert.IsMatch("Timestamp out of range\\s?(\\d+)", responseContents);
                return;
            }
            Assert.Fail("Expected WebException was not thrown");
        }
        public void AnonymousUserContext_CreateAuthUri_ResultQueryParam_x_t_MatchesAdjustedTimestampInSeconds()
        {
            const string httpMethod = "PUT";
            const long   serverClockSkewMilliseconds = 213000;

            var timestampProvider = TestUtils.CreateTimestampProviderStub(
                TestConstants.TIMESTAMP_MILLISECONDS - serverClockSkewMilliseconds);

            m_appContext = TestUtils.CreateAppContextUnderTest(timestampProvider);

            var anonContext = CreateAnonymousContextUnderTest();

            anonContext.ServerSkewMillis = serverClockSkewMilliseconds;

            Uri authUri = anonContext.CreateAuthenticatedUri(TestConstants.API_PATH, httpMethod);

            string expectedTimestampParameter =
                TestConstants.TIMESTAMP_SECONDS.ToString(CultureInfo.InvariantCulture);
            string actualTimestampParameter = TestUtils.GetUriQueryParameter(authUri, "x_t");

            Assert.AreEqual(expectedTimestampParameter, actualTimestampParameter);
        }
        public void UserContext_CreateAuthUri_IfThereIsClockSkew_SignatureUsesAdjustedTimestamp()
        {
            const string httpMethod = "POST";
            const long   serverClockSkewMilliseconds = 343000;
            string       expectedParameter           = TestUtils.CalculateParameterExpectation(
                TestConstants.APP_KEY, httpMethod,
                TestConstants.API_PATH, TestConstants.TIMESTAMP_SECONDS);

            var timestampProvider = TestUtils.CreateTimestampProviderStub(
                TestConstants.TIMESTAMP_MILLISECONDS - serverClockSkewMilliseconds);

            m_appContext = TestUtils.CreateAppContextUnderTest(timestampProvider);

            var userContext = CreateUserContextUnderTest();

            userContext.ServerSkewMillis = serverClockSkewMilliseconds;

            Uri authUri = userContext.CreateAuthenticatedUri(TestConstants.API_PATH, httpMethod);

            string parameter = TestUtils.GetUriQueryParameter(authUri, "x_c");

            Assert.AreEqual(expectedParameter, parameter);
        }
        public void RetryAuthenticatedRequestWithCorrectedTimestamp_ResultCodeIs200()
        {
            ID2LAppContext  appContext  = CreateAppContextWithDelay(ConfigHelper.AppId, ConfigHelper.AppKey, TEST_TIME_DELAY);
            ID2LUserContext userContext = ContextProvider.UserContext(appContext);
            Uri             uri         = userContext.CreateAuthenticatedUri(RouteProvider.OrganizationInfoRoute, "GET");
            HttpWebRequest  request     = (HttpWebRequest)WebRequest.Create(uri);

            request.Method = "GET";

            try {
                using (request.GetResponse() as HttpWebResponse) { }
            } catch (WebException ex) {
                var exWrapper = new D2LWebException(ex);
                userContext.InterpretResult(exWrapper);
            }

            Uri            retryUri     = userContext.CreateAuthenticatedUri(RouteProvider.OrganizationInfoRoute, "GET");
            HttpWebRequest retryRequest = (HttpWebRequest)WebRequest.Create(retryUri);

            retryRequest.Method = "GET";

            Assert.DoesNotThrow(() => { using (HttpWebResponse response = retryRequest.GetResponse() as HttpWebResponse) { } });
        }
 static ContextProvider()
 {
     m_appContextFactory = new D2LAppContextFactory();
     m_appContext = m_appContextFactory.Create( ConfigHelper.AppId, ConfigHelper.AppKey );
 }
        public void UserContext_CreateAuthUri_IfThereIsClockSkew_SignatureUsesAdjustedTimestamp()
        {
            const string httpMethod = "POST";
            const long serverClockSkewMilliseconds = 343000;
            string expectedParameter = TestUtils.CalculateParameterExpectation(
                TestConstants.APP_KEY, httpMethod,
                TestConstants.API_PATH, TestConstants.TIMESTAMP_SECONDS );

            var timestampProvider = TestUtils.CreateTimestampProviderStub(
                TestConstants.TIMESTAMP_MILLISECONDS - serverClockSkewMilliseconds );

            m_appContext = TestUtils.CreateAppContextUnderTest( timestampProvider );

            var userContext = CreateUserContextUnderTest();
            userContext.ServerSkewMillis = serverClockSkewMilliseconds;

            Uri authUri = userContext.CreateAuthenticatedUri( TestConstants.API_PATH, httpMethod );

            string parameter = TestUtils.GetUriQueryParameter( authUri, "x_c" );
            Assert.AreEqual( expectedParameter, parameter );
        }
		public void SetUpAppContext() {
			m_appContext = TestUtils.CreateAppContextUnderTest();
			TestUtils.SetUpTimestampProviderStub( TestConstants.TIMESTAMP_MILLISECONDS );
			m_userContext = CreateUserContextUnderTest();
			m_anonContext = CreateAnonymousContextUnderTest();
		}
        public void AnonymousUserContext_CreateAuthUri_ResultQueryParam_x_t_MatchesAdjustedTimestampInSeconds()
        {
            const string httpMethod = "PUT";
            const long serverClockSkewMilliseconds = 213000;

            var timestampProvider = TestUtils.CreateTimestampProviderStub(
                TestConstants.TIMESTAMP_MILLISECONDS - serverClockSkewMilliseconds );

            m_appContext = TestUtils.CreateAppContextUnderTest( timestampProvider );

            var anonContext = CreateAnonymousContextUnderTest();

            anonContext.ServerSkewMillis = serverClockSkewMilliseconds;

            Uri authUri = anonContext.CreateAuthenticatedUri( TestConstants.API_PATH, httpMethod );

            string expectedTimestampParameter =
                TestConstants.TIMESTAMP_SECONDS.ToString( CultureInfo.InvariantCulture );
            string actualTimestampParameter = TestUtils.GetUriQueryParameter( authUri, "x_t" );
            Assert.AreEqual( expectedTimestampParameter, actualTimestampParameter );
        }
 static ContextProvider()
 {
     m_appContextFactory = new D2LAppContextFactory();
     m_appContext        = m_appContextFactory.Create(ConfigHelper.AppId, ConfigHelper.AppKey);
 }
Example #18
0
 public void SetUpAppContext()
 {
     m_appContext = TestUtils.CreateAppContextUnderTest();
 }
 public HomeController( )
 {
     var appFactory = new D2LAppContextFactory();
     m_valenceAppContext = appFactory.Create( m_appId, m_appKey );
     m_valenceHost = new HostSpec( "https", LMS_URL, 443 );
 }
 internal static ID2LUserContext UserContext( ID2LAppContext appContext )
 {
     return appContext.CreateUserContext( ConfigHelper.UserId, ConfigHelper.UserKey, CreateHostSpec() );
 }
 public void SetUpAppContext()
 {
     m_appContext = TestUtils.CreateAppContextUnderTest();
 }
 internal static ID2LUserContext UserContext(ID2LAppContext appContext)
 {
     return(appContext.CreateUserContext(ConfigHelper.UserId, ConfigHelper.UserKey, CreateHostSpec()));
 }