#pragma warning restore 1591
        /// <summary>
        /// Populate the Authorization header with an access token from a principal (e.g. the current user being serviced by an API.)
        /// </summary>
        /// <param name="this"></param>
        /// <param name="principal">This principals access token will be used</param>
        public static void AuthenticateAs(this HttpRequestMessage @this, ID2LPrincipal principal)
        {
            @this.Headers.Authorization = new AuthenticationHeaderValue(
                scheme: "Bearer",
                parameter: principal.AccessToken.SensitiveRawAccessToken
                );
        }
        private async Task <ID2LPrincipal> AuthenticateAsync(HttpAuthenticationContext context)
        {
            ID2LPrincipal principal = await m_requestAuthenticator
                                      .AuthenticateAsync(context.Request)
                                      .SafeAsync();

            return(principal);
        }
        private async Task RunTest(
            string request_authorizationHeader,
            Type expectedExceptionType           = null,
            PrincipalType?expected_principalType = null
            )
        {
            IAccessToken token = AccessTokenMock.Create().Object;

            IAccessTokenValidator tokenValidator = AccessTokenValidatorMock.Create(
                accessToken: ACCESS_TOKEN,
                accessTokenAfterValidation: token,
                expectedExceptionType: expectedExceptionType
                ).Object;

            IRequestAuthenticator authenticator = new RequestAuthenticator(tokenValidator);

            var httpRequestMessage = new HttpRequestMessage()
                                     .WithAuthHeader(request_authorizationHeader);

            ID2LPrincipal principal = null;
            Exception     exception = null;

            try {
                principal = await authenticator.AuthenticateAsync(
                    httpRequestMessage
                    ).SafeAsync();
            } catch (Exception e) {
                exception = e;
            }

            CheckExpectations(
                principal,
                exception,
                expectedExceptionType,
                expected_principalType);

            exception = null;

            HttpRequest httpRequest = RequestBuilder
                                      .Create()
                                      .WithAuthHeader(request_authorizationHeader);

            try {
                principal = await authenticator.AuthenticateAsync(
                    httpRequest
                    ).SafeAsync();
            } catch (Exception e) {
                exception = e;
            }

            CheckExpectations(
                principal,
                exception,
                expectedExceptionType,
                expected_principalType);
        }
		public D2LPrincipalToIPrincipalAdaptor( ID2LPrincipal principal ) {
			m_principal = principal;

			// This is required for the IIS hosted services (WebHost)
			// We aren't honestly using this functionality at the moment.
			// TODO: validate that IIS uses this e.g. to fill out logs
			m_identity = new GenericIdentity(
				name: "D2LPrincipalToIPrincipalAdaptor_" + Guid.NewGuid().ToString()
			);
		}
        public D2LPrincipalToIPrincipalAdaptor(ID2LPrincipal principal)
        {
            m_principal = principal;

            // This is required for the IIS hosted services (WebHost)
            // We aren't honestly using this functionality at the moment.
            // TODO: validate that IIS uses this e.g. to fill out logs
            m_identity = new GenericIdentity(
                name: "D2LPrincipalToIPrincipalAdaptor_" + Guid.NewGuid().ToString()
                );
        }
Example #6
0
        void ID2LPrincipalDependencyRegistry.Register(
            HttpAuthenticationContext context,
            ID2LPrincipal principal
            )
        {
            // TODO: in D2L.Services.Core.Activation we should make IDependencyRegistry
            // injectible as an alternative to IUnityContainer (and block injection of that
            // if possible) so that we aren't coupling this code to unity.
            var container = context.Request.GetDependency <IUnityContainer>();

            container.RegisterInstance(
                name: D2LPrincipalWrapper.DI_INNER_NAME,
                instance: principal
                );
        }
Example #7
0
        private void Setup(
            long?userId       = null,
            long?actualUserId = null
            )
        {
            var  claims        = new List <Claim>();
            long?actualUserId2 = null;

            if (userId.HasValue)
            {
                claims.Add(
                    new Claim(
                        Constants.Claims.USER_ID,
                        userId.Value.ToString()
                        )
                    );

                actualUserId2 = userId;
            }

            if (actualUserId.HasValue)
            {
                actualUserId2 = actualUserId;
            }

            if (actualUserId2.HasValue)
            {
                claims.Add(
                    new Claim(
                        Constants.Claims.ACTUAL_USER_ID,
                        actualUserId2.Value.ToString()
                        )
                    );
            }

            var accessToken = new Mock <IAccessToken>(MockBehavior.Strict);

            accessToken
            .Setup(at => at.Claims)
            .Returns(claims);

            m_principal = new D2LPrincipal(accessToken.Object);
        }
		private void Setup(
			long? userId = null,
			long? actualUserId = null
		) {
			var claims = new List<Claim>();
			long? actualUserId2 = null;

			if ( userId.HasValue) {
				claims.Add(
					new Claim(
						Constants.Claims.USER_ID,
						userId.Value.ToString()
					)
				);

				actualUserId2 = userId;
			}

			if ( actualUserId.HasValue ) {
				actualUserId2 = actualUserId;
			}

			if ( actualUserId2.HasValue ) {
				claims.Add(
					new Claim(
						Constants.Claims.ACTUAL_USER_ID,
						actualUserId2.Value.ToString()
					)
				);
			}

			var accessToken = new Mock<IAccessToken>( MockBehavior.Strict );

			accessToken
				.Setup( at => at.Claims )
				.Returns( claims );

			m_principal = new D2LPrincipal( accessToken.Object );
		}
Example #9
0
        private void CheckExpectations(
            ID2LPrincipal principal,
            Exception exception,
            Type expectedExceptionType,
            PrincipalType?expected_principalType
            )
        {
            if (expectedExceptionType != null)
            {
                Assert.IsNull(principal);
                Assert.IsNotNull(exception);
                Assert.AreEqual(expectedExceptionType, exception.GetType());
                return;
            }

            Assert.IsNotNull(principal);
            Assert.IsNull(exception);

            if (expected_principalType.HasValue)
            {
                Assert.AreEqual(expected_principalType, principal.Type);
            }
        }
#pragma warning restore 1591
		/// <summary>
		/// Populate the Authorization header with an access token from a principal (e.g. the current user being serviced by an API.)	
		/// </summary>
		/// <param name="this"></param>
		/// <param name="principal">This principals access token will be used</param>
		public static void AuthenticateAs( this HttpClient @this, ID2LPrincipal principal ) {
			@this.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
				scheme: "Bearer",
				parameter: principal.AccessToken.SensitiveRawAccessToken
			);	
		}
		private void CheckExpectations(
			ID2LPrincipal principal,
			Exception exception,
			Type expectedExceptionType,
			PrincipalType? expected_principalType
		) {
			if( expectedExceptionType != null ) {
				Assert.IsNull( principal );
				Assert.IsNotNull( exception );
				Assert.AreEqual( expectedExceptionType, exception.GetType() );
				return;
			}

			Assert.IsNotNull( principal );
			Assert.IsNull( exception );

			if( expected_principalType.HasValue ) {
				Assert.AreEqual( expected_principalType, principal.Type );
			}
		}