Example #1
0
		/// <summary>
		/// Records the feature and dependency use.
		/// </summary>
		/// <param name="value">The consumer or service provider.</param>
		/// <param name="service">The service.</param>
		/// <param name="tokenManager">The token manager.</param>
		/// <param name="nonceStore">The nonce store.</param>
		internal static void RecordFeatureAndDependencyUse(object value, ServiceProviderHostDescription service, ITokenManager tokenManager, INonceStore nonceStore) {
			Requires.NotNull(value, "value");
			Requires.NotNull(service, "service");
			Requires.NotNull(tokenManager, "tokenManager");

			// In release builds, just quietly return.
			if (value == null || service == null || tokenManager == null) {
				return;
			}

			if (Reporting.Enabled && Reporting.Configuration.IncludeFeatureUsage) {
				StringBuilder builder = new StringBuilder();
				builder.Append(value.GetType().Name);
				builder.Append(" ");
				builder.Append(tokenManager.GetType().Name);
				if (nonceStore != null) {
					builder.Append(" ");
					builder.Append(nonceStore.GetType().Name);
				}
				builder.Append(" ");
				builder.Append(service.UserAuthorizationEndpoint != null ? service.UserAuthorizationEndpoint.Location.AbsoluteUri : string.Empty);
				Reporting.ObservedFeatures.Add(builder.ToString());
				Reporting.Touch();
			}
		}
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceProvider"/> class.
 /// </summary>
 /// <param name="serviceDescription">The endpoints and behavior on the Service Provider.</param>
 /// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
 /// <param name="messageTypeProvider">An object that can figure out what type of message is being received for deserialization.</param>
 public ServiceProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, OAuthServiceProviderMessageFactory messageTypeProvider)
     : this(serviceDescription, tokenManager, OAuthElement.Configuration.ServiceProvider.ApplicationStore.CreateInstance(GetHttpApplicationStore(), null), messageTypeProvider)
 {
     Requires.NotNull(serviceDescription, "serviceDescription");
     Requires.NotNull(tokenManager, "tokenManager");
     Requires.NotNull(messageTypeProvider, "messageTypeProvider");
 }
		public void AccessTokenUriTest() {
			var target = new ServiceProviderHostDescription();
			MessageReceivingEndpoint expected = new MessageReceivingEndpoint("http://localhost/accesstoken", HttpDeliveryMethods.GetRequest);
			MessageReceivingEndpoint actual;
			target.AccessTokenEndpoint = expected;
			actual = target.AccessTokenEndpoint;
			Assert.AreEqual(expected, actual);

			target.AccessTokenEndpoint = null;
			Assert.IsNull(target.AccessTokenEndpoint);
		}
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceProvider"/> class.
        /// </summary>
        /// <param name="serviceDescription">The endpoints and behavior on the Service Provider.</param>
        /// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
        /// <param name="nonceStore">The nonce store.</param>
        /// <param name="messageTypeProvider">An object that can figure out what type of message is being received for deserialization.</param>
        public ServiceProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, INonceStore nonceStore, OAuthServiceProviderMessageFactory messageTypeProvider)
        {
            Requires.NotNull(serviceDescription, "serviceDescription");
            Requires.NotNull(tokenManager, "tokenManager");
            Requires.NotNull(nonceStore, "nonceStore");
            Requires.NotNull(messageTypeProvider, "messageTypeProvider");

            var signingElement = serviceDescription.CreateTamperProtectionElement();

            this.ServiceDescription = serviceDescription;
            this.SecuritySettings   = OAuthElement.Configuration.ServiceProvider.SecuritySettings.CreateSecuritySettings();
            this.OAuthChannel       = new OAuthServiceProviderChannel(signingElement, nonceStore, tokenManager, this.SecuritySettings, messageTypeProvider);
            this.TokenGenerator     = new StandardTokenGenerator();

            OAuthReporting.RecordFeatureAndDependencyUse(this, serviceDescription, tokenManager, nonceStore);
        }
Example #5
0
		/// <summary>
		/// Initializes a new instance of the <see cref="ServiceProvider"/> class.
		/// </summary>
		/// <param name="serviceDescription">The endpoints and behavior on the Service Provider.</param>
		/// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
		/// <param name="nonceStore">The nonce store.</param>
		/// <param name="messageTypeProvider">An object that can figure out what type of message is being received for deserialization.</param>
		public ServiceProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, INonceStore nonceStore, OAuthServiceProviderMessageFactory messageTypeProvider) {
			Requires.NotNull(serviceDescription, "serviceDescription");
			Requires.NotNull(tokenManager, "tokenManager");
			Requires.NotNull(nonceStore, "nonceStore");
			Requires.NotNull(messageTypeProvider, "messageTypeProvider");

			var signingElement = serviceDescription.CreateTamperProtectionElement();
			this.ServiceDescription = serviceDescription;
			this.SecuritySettings = OAuthElement.Configuration.ServiceProvider.SecuritySettings.CreateSecuritySettings();
			this.OAuthChannel = new OAuthServiceProviderChannel(signingElement, nonceStore, tokenManager, this.SecuritySettings, messageTypeProvider);
			this.TokenGenerator = new StandardTokenGenerator();

			OAuthReporting.RecordFeatureAndDependencyUse(this, serviceDescription, tokenManager, nonceStore);
		}
Example #6
0
		/// <summary>
		/// Initializes a new instance of the <see cref="ServiceProvider"/> class.
		/// </summary>
		/// <param name="serviceDescription">The endpoints and behavior on the Service Provider.</param>
		/// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
		/// <param name="nonceStore">The nonce store.</param>
		public ServiceProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, INonceStore nonceStore)
			: this(serviceDescription, tokenManager, nonceStore, new OAuthServiceProviderMessageFactory(tokenManager)) {
		}
Example #7
0
		/// <summary>
		/// Initializes a new instance of the <see cref="ServiceProvider"/> class.
		/// </summary>
		/// <param name="serviceDescription">The endpoints and behavior on the Service Provider.</param>
		/// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
		/// <param name="messageTypeProvider">An object that can figure out what type of message is being received for deserialization.</param>
		public ServiceProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, OAuthServiceProviderMessageFactory messageTypeProvider)
			: this(serviceDescription, tokenManager, OAuthElement.Configuration.ServiceProvider.ApplicationStore.CreateInstance(GetHttpApplicationStore(), null), messageTypeProvider) {
			Requires.NotNull(serviceDescription, "serviceDescription");
			Requires.NotNull(tokenManager, "tokenManager");
			Requires.NotNull(messageTypeProvider, "messageTypeProvider");
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="ServiceProviderOpenIdProvider"/> class.
		/// </summary>
		/// <param name="serviceDescription">The service description.</param>
		/// <param name="tokenManager">The token manager.</param>
		/// <param name="nonceStore">The nonce store.</param>
		/// <param name="messageTypeProvider">The message type provider.</param>
		public ServiceProviderOpenIdProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, INonceStore nonceStore, OAuthServiceProviderMessageFactory messageTypeProvider)
			: base(serviceDescription, tokenManager, nonceStore, messageTypeProvider) {
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="ServiceProviderOpenIdProvider"/> class.
		/// </summary>
		/// <param name="serviceDescription">The service description.</param>
		/// <param name="tokenManager">The token manager.</param>
		/// <param name="nonceStore">The nonce store.</param>
		public ServiceProviderOpenIdProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, INonceStore nonceStore)
			: base(serviceDescription, tokenManager, nonceStore) {
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="ServiceProviderOpenIdProvider"/> class.
		/// </summary>
		/// <param name="serviceDescription">The endpoints and behavior on the Service Provider.</param>
		/// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
		public ServiceProviderOpenIdProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager)
			: base(serviceDescription, tokenManager) {
		}
Example #11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceProvider"/> class.
 /// </summary>
 /// <param name="serviceDescription">The endpoints and behavior on the Service Provider.</param>
 /// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
 /// <param name="nonceStore">The nonce store.</param>
 public ServiceProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, INonceStore nonceStore)
     : this(serviceDescription, tokenManager, nonceStore, new OAuthServiceProviderMessageFactory(tokenManager))
 {
 }
		public void RequestTokenUriWithOAuthParametersTest() {
			var target = new ServiceProviderHostDescription();
			target.RequestTokenEndpoint = new MessageReceivingEndpoint("http://localhost/requesttoken?oauth_token=something", HttpDeliveryMethods.GetRequest);
		}
		public async Task SpecAppendixAExample() {
			var serviceDescription = new ServiceProviderDescription(
				"https://photos.example.net/request_token",
				"http://photos.example.net/authorize",
				"https://photos.example.net/access_token");
			var serviceHostDescription = new ServiceProviderHostDescription {
				RequestTokenEndpoint = new MessageReceivingEndpoint(serviceDescription.TemporaryCredentialsRequestEndpoint, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
				UserAuthorizationEndpoint = new MessageReceivingEndpoint(serviceDescription.ResourceOwnerAuthorizationEndpoint, HttpDeliveryMethods.GetRequest),
				AccessTokenEndpoint = new MessageReceivingEndpoint(serviceDescription.TokenRequestEndpoint, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
				TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement(), },
			};
			var accessPhotoEndpoint = new Uri("http://photos.example.net/photos?file=vacation.jpg&size=original");
			var consumerDescription = new ConsumerDescription("dpf43f3p2l4k3l03", "kd94hf93k423kf44");

			var tokenManager = new InMemoryTokenManager();
			tokenManager.AddConsumer(consumerDescription);
			var sp = new ServiceProvider(serviceHostDescription, tokenManager);

			Handle(serviceDescription.TemporaryCredentialsRequestEndpoint).By(
				async (request, ct) => {
					var requestTokenMessage = await sp.ReadTokenRequestAsync(request, ct);
					return await sp.Channel.PrepareResponseAsync(sp.PrepareUnauthorizedTokenMessage(requestTokenMessage));
				});
			Handle(serviceDescription.ResourceOwnerAuthorizationEndpoint).By(
				async (request, ct) => {
					var authRequest = await sp.ReadAuthorizationRequestAsync(request, ct);
					((InMemoryTokenManager)sp.TokenManager).AuthorizeRequestToken(authRequest.RequestToken);
					return await sp.Channel.PrepareResponseAsync(sp.PrepareAuthorizationResponse(authRequest));
				});
			Handle(serviceDescription.TokenRequestEndpoint).By(
				async (request, ct) => {
					var accessRequest = await sp.ReadAccessTokenRequestAsync(request, ct);
					return await sp.Channel.PrepareResponseAsync(sp.PrepareAccessTokenMessage(accessRequest), ct);
				});
			Handle(accessPhotoEndpoint).By(
				async (request, ct) => {
					string accessToken = (await sp.ReadProtectedResourceAuthorizationAsync(request)).AccessToken;
					Assert.That(accessToken, Is.Not.Null.And.Not.Empty);
					var responseMessage = new HttpResponseMessage { Content = new ByteArrayContent(new byte[] { 0x33, 0x66 }), };
					responseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
					return responseMessage;
				});

			var consumer = new Consumer(
				consumerDescription.ConsumerKey,
				consumerDescription.ConsumerSecret,
				serviceDescription,
				new MemoryTemporaryCredentialStorage());
			consumer.HostFactories = this.HostFactories;
			var authorizeUrl = await consumer.RequestUserAuthorizationAsync(new Uri("http://printer.example.com/request_token_ready"));
			Uri authorizeResponseUri;
			this.HostFactories.AllowAutoRedirects = false;
			using (var httpClient = this.HostFactories.CreateHttpClient()) {
				using (var response = await httpClient.GetAsync(authorizeUrl)) {
					Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.Redirect));
					authorizeResponseUri = response.Headers.Location;
				}
			}

			var accessTokenResponse = await consumer.ProcessUserAuthorizationAsync(authorizeResponseUri);
			Assert.That(accessTokenResponse, Is.Not.Null);

			using (var authorizingClient = consumer.CreateHttpClient(accessTokenResponse.AccessToken)) {
				using (var protectedPhoto = await authorizingClient.GetAsync(accessPhotoEndpoint)) {
					Assert.That(protectedPhoto, Is.Not.Null);
					protectedPhoto.EnsureSuccessStatusCode();
					Assert.That("image/jpeg", Is.EqualTo(protectedPhoto.Content.Headers.ContentType.MediaType));
					Assert.That(protectedPhoto.Content.Headers.ContentLength, Is.Not.EqualTo(0));
				}
			}
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceProviderOpenIdProvider"/> class.
 /// </summary>
 /// <param name="serviceDescription">The service description.</param>
 /// <param name="tokenManager">The token manager.</param>
 /// <param name="nonceStore">The nonce store.</param>
 /// <param name="messageTypeProvider">The message type provider.</param>
 public ServiceProviderOpenIdProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, INonceStore nonceStore, OAuthServiceProviderMessageFactory messageTypeProvider)
     : base(serviceDescription, tokenManager, nonceStore, messageTypeProvider)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceProviderOpenIdProvider"/> class.
 /// </summary>
 /// <param name="serviceDescription">The service description.</param>
 /// <param name="tokenManager">The token manager.</param>
 /// <param name="nonceStore">The nonce store.</param>
 public ServiceProviderOpenIdProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager, INonceStore nonceStore)
     : base(serviceDescription, tokenManager, nonceStore)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceProviderOpenIdProvider"/> class.
 /// </summary>
 /// <param name="serviceDescription">The endpoints and behavior on the Service Provider.</param>
 /// <param name="tokenManager">The host's method of storing and recalling tokens and secrets.</param>
 public ServiceProviderOpenIdProvider(ServiceProviderHostDescription serviceDescription, IServiceProviderTokenManager tokenManager)
     : base(serviceDescription, tokenManager)
 {
 }