diff options
Diffstat (limited to 'samples/DotNetOpenAuth.ApplicationBlock')
5 files changed, 160 insertions, 0 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj index 6739bf9..7e1a0dd 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj +++ b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj @@ -72,6 +72,8 @@ <Reference Include="System.Core"> <RequiredTargetFramework>3.5</RequiredTargetFramework> </Reference> + <Reference Include="System.Runtime.Serialization" /> + <Reference Include="System.ServiceModel.Web" /> <Reference Include="System.Web" /> <Reference Include="System.Xml.Linq"> <RequiredTargetFramework>3.5</RequiredTargetFramework> @@ -86,11 +88,15 @@ <Compile Include="CustomExtensions\Acme.cs" /> <Compile Include="CustomExtensions\AcmeRequest.cs" /> <Compile Include="CustomExtensions\AcmeResponse.cs" /> + <Compile Include="Facebook\FacebookClient.cs" /> + <Compile Include="Facebook\FacebookGraph.cs" /> <Compile Include="GoogleConsumer.cs" /> + <Compile Include="InMemoryClientTokenManager.cs" /> <Compile Include="InMemoryTokenManager.cs"> <SubType>Code</SubType> </Compile> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="TokenManager.cs" /> <Compile Include="TwitterConsumer.cs" /> <Compile Include="Util.cs" /> <Compile Include="YubikeyRelyingParty.cs" /> diff --git a/samples/DotNetOpenAuth.ApplicationBlock/Facebook/FacebookClient.cs b/samples/DotNetOpenAuth.ApplicationBlock/Facebook/FacebookClient.cs new file mode 100644 index 0000000..2f3840e --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/Facebook/FacebookClient.cs @@ -0,0 +1,29 @@ +//----------------------------------------------------------------------- +// <copyright file="FacebookClient.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.ApplicationBlock { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Web; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuthWrap; + + public class FacebookClient : WebServerClient { + private static readonly AuthorizationServerDescription FacebookDescription = new AuthorizationServerDescription { + TokenEndpoint = new Uri("https://graph.facebook.com/oauth/access_token"), + AuthorizationEndpoint = new Uri("https://graph.facebook.com/oauth/authorize"), + }; + + /// <summary> + /// Initializes a new instance of the <see cref="FacebookClient"/> class. + /// </summary> + public FacebookClient() : base(FacebookDescription) { + this.TokenManager = new TokenManager(); + } + } +} diff --git a/samples/DotNetOpenAuth.ApplicationBlock/Facebook/FacebookGraph.cs b/samples/DotNetOpenAuth.ApplicationBlock/Facebook/FacebookGraph.cs new file mode 100644 index 0000000..0e878c1 --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/Facebook/FacebookGraph.cs @@ -0,0 +1,54 @@ +//----------------------------------------------------------------------- +// <copyright file="FacebookGraph.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.ApplicationBlock.Facebook { + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Runtime.Serialization; + using System.Runtime.Serialization.Json; + using System.Text; + + [DataContract] + public class FacebookGraph { + private static DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(FacebookGraph)); + + [DataMember(Name = "id")] + public int Id { get; set; } + + [DataMember(Name = "name")] + public string Name { get; set; } + + [DataMember(Name = "first_name")] + public string FirstName { get; set; } + + [DataMember(Name = "last_name")] + public string LastName { get; set; } + + [DataMember(Name = "link")] + public Uri Link { get; set; } + + [DataMember(Name = "birthday")] + public string Birthday { get; set; } + + public static FacebookGraph Deserialize(string json) { + if (String.IsNullOrEmpty(json)) { + throw new ArgumentNullException("json"); + } + + return Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(json))); + } + + public static FacebookGraph Deserialize(Stream jsonStream) { + if (jsonStream == null) { + throw new ArgumentNullException("jsonStream"); + } + + return (FacebookGraph)jsonSerializer.ReadObject(jsonStream); + } + } +} diff --git a/samples/DotNetOpenAuth.ApplicationBlock/InMemoryClientTokenManager.cs b/samples/DotNetOpenAuth.ApplicationBlock/InMemoryClientTokenManager.cs new file mode 100644 index 0000000..4c86df7 --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/InMemoryClientTokenManager.cs @@ -0,0 +1,53 @@ +//----------------------------------------------------------------------- +// <copyright file="InMemoryClientTokenManager.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.ApplicationBlock { + using System; + using System.Collections.Generic; + using System.Globalization; + using System.Linq; + using System.ServiceModel; + using System.Text; + using System.Threading; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuthWrap; + +#if SAMPLESONLY + internal class InMemoryClientTokenManager : IClientTokenManager { + private readonly Dictionary<int, IAuthorizationState> savedStates = new Dictionary<int, IAuthorizationState>(); + private int stateCounter; + + #region Implementation of IClientTokenManager + + /// <summary> + /// Gets the state of the authorization for a given callback URL and client state. + /// </summary> + /// <param name="callbackUrl">The callback URL.</param> + /// <param name="clientState">State of the client stored at the beginning of an authorization request.</param> + /// <returns>The authorization state; may be <c>null</c> if no authorization state matches.</returns> + public IAuthorizationState GetAuthorizationState(Uri callbackUrl, string clientState) { + IAuthorizationState state; + if (this.savedStates.TryGetValue(int.Parse(clientState), out state)) { + if (state.Callback != callbackUrl) { + throw new DotNetOpenAuth.Messaging.ProtocolException("Client state and callback URL do not match."); + } + } + + return state; + } + + #endregion + + internal IAuthorizationState NewAuthorization(string scope, out string clientState) { + int counter = Interlocked.Increment(ref this.stateCounter); + clientState = counter.ToString(CultureInfo.InvariantCulture); + return this.savedStates[counter] = new AuthorizationState { + Scope = scope, + }; + } + } +#endif +} diff --git a/samples/DotNetOpenAuth.ApplicationBlock/TokenManager.cs b/samples/DotNetOpenAuth.ApplicationBlock/TokenManager.cs new file mode 100644 index 0000000..22099a3 --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/TokenManager.cs @@ -0,0 +1,18 @@ +//----------------------------------------------------------------------- +// <copyright file="TokenManager.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.ApplicationBlock { + using System; + using DotNetOpenAuth.OAuthWrap; + + public class TokenManager : IClientTokenManager { + public IAuthorizationState GetAuthorizationState(Uri callbackUrl, string clientState) { + return new AuthorizationState { + Callback = callbackUrl, + }; + } + } +} |