summaryrefslogtreecommitdiffstats
path: root/samples/ServiceProvider/App_Code
diff options
context:
space:
mode:
Diffstat (limited to 'samples/ServiceProvider/App_Code')
-rw-r--r--samples/ServiceProvider/App_Code/Constants.cs32
-rw-r--r--samples/ServiceProvider/App_Code/DataApi.cs16
-rw-r--r--samples/ServiceProvider/App_Code/IDataApi.cs15
-rw-r--r--samples/ServiceProvider/App_Code/InMemoryTokenManager.cs91
-rw-r--r--samples/ServiceProvider/App_Code/Logging.cs20
-rw-r--r--samples/ServiceProvider/App_Code/OAuthAuthorizationManager.cs32
-rw-r--r--samples/ServiceProvider/App_Code/TracePageAppender.cs11
7 files changed, 217 insertions, 0 deletions
diff --git a/samples/ServiceProvider/App_Code/Constants.cs b/samples/ServiceProvider/App_Code/Constants.cs
new file mode 100644
index 0000000..7965dc0
--- /dev/null
+++ b/samples/ServiceProvider/App_Code/Constants.cs
@@ -0,0 +1,32 @@
+using System;
+using DotNetOAuth;
+using DotNetOAuth.ChannelElements;
+using DotNetOAuth.Messaging;
+
+/// <summary>
+/// Service Provider definitions.
+/// </summary>
+public static class Constants {
+ public static InMemoryTokenManager TokenManager { get; set; }
+
+ public static Uri WebRootUrl { get; set; }
+
+ public static ServiceProviderDescription SelfDescription {
+ get {
+ ServiceProviderDescription description = new ServiceProviderDescription {
+ AccessTokenEndpoint = new MessageReceivingEndpoint(new Uri(WebRootUrl, "/OAuth.ashx"), HttpDeliveryMethod.PostRequest),
+ RequestTokenEndpoint = new MessageReceivingEndpoint(new Uri(WebRootUrl, "/OAuth.ashx"), HttpDeliveryMethod.PostRequest),
+ UserAuthorizationEndpoint = new MessageReceivingEndpoint(new Uri(WebRootUrl, "/OAuth.ashx"), HttpDeliveryMethod.PostRequest),
+ TamperProtectionElements = new ITamperProtectionChannelBindingElement[] {
+ new HmacSha1SigningBindingElement(),
+ },
+ };
+
+ return description;
+ }
+ }
+
+ public static ServiceProvider CreateServiceProvider() {
+ return new ServiceProvider(SelfDescription, TokenManager);
+ }
+}
diff --git a/samples/ServiceProvider/App_Code/DataApi.cs b/samples/ServiceProvider/App_Code/DataApi.cs
new file mode 100644
index 0000000..9e679cd
--- /dev/null
+++ b/samples/ServiceProvider/App_Code/DataApi.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Serialization;
+using System.ServiceModel;
+using System.Text;
+
+public class DataApi : IDataApi {
+ public int GetAge() {
+ return 5;
+ }
+
+ public string GetName() {
+ return "Andrew";
+ }
+}
diff --git a/samples/ServiceProvider/App_Code/IDataApi.cs b/samples/ServiceProvider/App_Code/IDataApi.cs
new file mode 100644
index 0000000..895dda1
--- /dev/null
+++ b/samples/ServiceProvider/App_Code/IDataApi.cs
@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Runtime.Serialization;
+using System.ServiceModel;
+using System.Text;
+
+[ServiceContract]
+public interface IDataApi {
+ [OperationContract]
+ int GetAge();
+
+ [OperationContract]
+ string GetName();
+}
diff --git a/samples/ServiceProvider/App_Code/InMemoryTokenManager.cs b/samples/ServiceProvider/App_Code/InMemoryTokenManager.cs
new file mode 100644
index 0000000..540fd81
--- /dev/null
+++ b/samples/ServiceProvider/App_Code/InMemoryTokenManager.cs
@@ -0,0 +1,91 @@
+//-----------------------------------------------------------------------
+// <copyright file="InMemoryTokenManager.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using DotNetOAuth.ChannelElements;
+
+public class InMemoryTokenManager : ITokenManager {
+ private Dictionary<string, string> consumersAndSecrets = new Dictionary<string, string>();
+ private Dictionary<string, string> tokensAndSecrets = new Dictionary<string, string>();
+
+ /// <summary>
+ /// Request tokens that have been issued, and whether they have been authorized yet.
+ /// </summary>
+ private Dictionary<string, bool> requestTokens = new Dictionary<string, bool>();
+
+ /// <summary>
+ /// Access tokens that have been issued and have not yet expired.
+ /// </summary>
+ private List<string> accessTokens = new List<string>();
+
+ #region ITokenManager Members
+
+ public string GetConsumerSecret(string consumerKey) {
+ return this.consumersAndSecrets[consumerKey];
+ }
+
+ public string GetTokenSecret(string token) {
+ return this.tokensAndSecrets[token];
+ }
+
+ public void StoreNewRequestToken(string consumerKey, string requestToken, string requestTokenSecret, IDictionary<string, string> parameters) {
+ this.tokensAndSecrets[requestToken] = requestTokenSecret;
+ this.requestTokens.Add(requestToken, false);
+ }
+
+ /// <summary>
+ /// Checks whether a given request token has already been authorized
+ /// by some user for use by the Consumer that requested it.
+ /// </summary>
+ /// <param name="requestToken">The Consumer's request token.</param>
+ /// <returns>
+ /// True if the request token has already been fully authorized by the user
+ /// who owns the relevant protected resources. False if the token has not yet
+ /// been authorized, has expired or does not exist.
+ /// </returns>
+ public bool IsRequestTokenAuthorized(string requestToken) {
+ return this.requestTokens[requestToken];
+ }
+
+ public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret) {
+ Debug.Assert(this.requestTokens[requestToken], "Unauthorized token should not be exchanged for access token.");
+ this.requestTokens.Remove(requestToken);
+ this.accessTokens.Add(accessToken);
+ this.tokensAndSecrets.Remove(requestToken);
+ this.tokensAndSecrets[accessToken] = accessTokenSecret;
+ }
+
+ /// <summary>
+ /// Classifies a token as a request token or an access token.
+ /// </summary>
+ /// <param name="token">The token to classify.</param>
+ /// <returns>Request or Access token, or invalid if the token is not recognized.</returns>
+ public TokenType GetTokenType(string token) {
+ if (this.requestTokens.ContainsKey(token)) {
+ return TokenType.RequestToken;
+ } else if (this.accessTokens.Contains(token)) {
+ return TokenType.AccessToken;
+ } else {
+ return TokenType.InvalidToken;
+ }
+ }
+
+ #endregion
+
+ public void AddConsumer(string key, string secret) {
+ this.consumersAndSecrets.Add(key, secret);
+ }
+
+ public void AuthorizeRequestToken(string requestToken) {
+ if (requestToken == null) {
+ throw new ArgumentNullException("requestToken");
+ }
+
+ this.requestTokens[requestToken] = true;
+ }
+}
diff --git a/samples/ServiceProvider/App_Code/Logging.cs b/samples/ServiceProvider/App_Code/Logging.cs
new file mode 100644
index 0000000..cba9b4e
--- /dev/null
+++ b/samples/ServiceProvider/App_Code/Logging.cs
@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Web;
+
+/// <summary>
+/// Logging tools for this sample.
+/// </summary>
+public static class Logging {
+ /// <summary>
+ /// An application memory cache of recent log messages.
+ /// </summary>
+ public static StringBuilder LogMessages = new StringBuilder();
+
+ /// <summary>
+ /// The logger for this sample to use.
+ /// </summary>
+ public static log4net.ILog Logger = log4net.LogManager.GetLogger("DotNetOAuth.ConsumerSample");
+}
diff --git a/samples/ServiceProvider/App_Code/OAuthAuthorizationManager.cs b/samples/ServiceProvider/App_Code/OAuthAuthorizationManager.cs
new file mode 100644
index 0000000..20536d8
--- /dev/null
+++ b/samples/ServiceProvider/App_Code/OAuthAuthorizationManager.cs
@@ -0,0 +1,32 @@
+using System;
+using System.ServiceModel;
+using System.ServiceModel.Channels;
+using DotNetOAuth;
+
+/// <summary>
+/// A WCF extension to authenticate incoming messages using OAuth.
+/// </summary>
+public class OAuthAuthorizationManager : ServiceAuthorizationManager {
+ public OAuthAuthorizationManager() {
+ }
+
+ protected override bool CheckAccessCore(OperationContext operationContext) {
+ if (!base.CheckAccessCore(operationContext)) {
+ return false;
+ }
+
+ HttpRequestMessageProperty httpDetails = operationContext.RequestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
+ Uri requestUri = operationContext.RequestContext.RequestMessage.Properties["OriginalHttpRequestUri"] as Uri;
+ ServiceProvider sp = Constants.CreateServiceProvider();
+ var auth = sp.GetProtectedResourceAuthorization(httpDetails, requestUri);
+ if (auth != null) {
+ string consumer = auth.ConsumerKey;
+
+ //// TODO: pass the consumer along to the operation somehow
+
+ return true;
+ }
+
+ return false;
+ }
+}
diff --git a/samples/ServiceProvider/App_Code/TracePageAppender.cs b/samples/ServiceProvider/App_Code/TracePageAppender.cs
new file mode 100644
index 0000000..b3b539a
--- /dev/null
+++ b/samples/ServiceProvider/App_Code/TracePageAppender.cs
@@ -0,0 +1,11 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Web;
+
+public class TracePageAppender : log4net.Appender.AppenderSkeleton {
+ protected override void Append(log4net.Core.LoggingEvent loggingEvent) {
+ StringWriter sw = new StringWriter(Logging.LogMessages);
+ Layout.Format(sw, loggingEvent);
+ }
+}