diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-11-03 17:22:00 -0800 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-11-04 08:12:52 -0800 |
commit | 462e19abd9034c11a12cad30e9899740f2bef8ff (patch) | |
tree | e08667f1d69249f8daa6c348a919bd0fd5434415 /samples/DotNetOpenAuth.ApplicationBlock | |
parent | 6a79be0eca3929d8fb4e797799dac8d6f7875475 (diff) | |
download | DotNetOpenAuth-462e19abd9034c11a12cad30e9899740f2bef8ff.zip DotNetOpenAuth-462e19abd9034c11a12cad30e9899740f2bef8ff.tar.gz DotNetOpenAuth-462e19abd9034c11a12cad30e9899740f2bef8ff.tar.bz2 |
Changed namepace and project names in preparation for merge with DotNetOpenId.
Diffstat (limited to 'samples/DotNetOpenAuth.ApplicationBlock')
4 files changed, 276 insertions, 0 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj new file mode 100644 index 0000000..9ebe394 --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj @@ -0,0 +1,66 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProductVersion>9.0.30729</ProductVersion> + <SchemaVersion>2.0</SchemaVersion> + <ProjectGuid>{AA78D112-D889-414B-A7D4-467B34C7B663}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>DotNetOAuth.ApplicationBlock</RootNamespace> + <AssemblyName>DotNetOAuth.ApplicationBlock</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Core"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + <Reference Include="System.Xml.Linq"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + <Reference Include="System.Data.DataSetExtensions"> + <RequiredTargetFramework>3.5</RequiredTargetFramework> + </Reference> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="GoogleConsumer.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="Util.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\src\DotNetOpenAuth\DotNetOpenAuth.csproj"> + <Project>{3191B653-F76D-4C1A-9A5A-347BC3AAAAB7}</Project> + <Name>DotNetOpenAuth</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project>
\ No newline at end of file diff --git a/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs b/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs new file mode 100644 index 0000000..f08b9ba --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs @@ -0,0 +1,145 @@ +//----------------------------------------------------------------------- +// <copyright file="GoogleConsumer.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.Xml.Linq; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth; + using DotNetOpenAuth.OAuth.ChannelElements; + + /// <summary> + /// A consumer capable of communicating with Google Data APIs. + /// </summary> + public static class GoogleConsumer { + /// <summary> + /// The Consumer to use for accessing Google data APIs. + /// </summary> + private static readonly ServiceProviderDescription GoogleDescription = new ServiceProviderDescription { + RequestTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetRequestToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest), + UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthAuthorizeToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest), + AccessTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetAccessToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest), + TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }, + }; + + /// <summary> + /// A mapping between Google's applications and their URI scope values. + /// </summary> + private static readonly Dictionary<Applications, string> DataScopeUris = new Dictionary<Applications, string> { + { Applications.Contacts, "http://www.google.com/m8/feeds/" }, + { Applications.Calendar, "http://www.google.com/calendar/feeds/" }, + }; + + /// <summary> + /// The URI to get contacts once authorization is granted. + /// </summary> + private static readonly MessageReceivingEndpoint GetContactsEndpoint = new MessageReceivingEndpoint("http://www.google.com/m8/feeds/contacts/default/full/", HttpDeliveryMethods.GetRequest); + + /// <summary> + /// The many specific authorization scopes Google offers. + /// </summary> + [Flags] + public enum Applications : long { + /// <summary> + /// The Gmail address book. + /// </summary> + Contacts = 0x1, + + /// <summary> + /// Appointments in Google Calendar. + /// </summary> + Calendar = 0x2, + } + + /// <summary> + /// Initializes a new instance of the <see cref="WebConsumer"/> class that is prepared to communicate with Google. + /// </summary> + /// <param name="tokenManager">The token manager.</param> + /// <param name="consumerKey">The consumer key.</param> + /// <returns>The newly instantiated <see cref="WebConsumer"/>.</returns> + public static WebConsumer CreateWebConsumer(ITokenManager tokenManager, string consumerKey) { + return new WebConsumer(GoogleDescription, tokenManager) { + ConsumerKey = consumerKey, + }; + } + + /// <summary> + /// Initializes a new instance of the <see cref="DesktopConsumer"/> class that is prepared to communicate with Google. + /// </summary> + /// <param name="tokenManager">The token manager.</param> + /// <param name="consumerKey">The consumer key.</param> + /// <returns>The newly instantiated <see cref="DesktopConsumer"/>.</returns> + public static DesktopConsumer CreateDesktopConsumer(ITokenManager tokenManager, string consumerKey) { + return new DesktopConsumer(GoogleDescription, tokenManager) { + ConsumerKey = consumerKey, + }; + } + + /// <summary> + /// Requests authorization from Google to access data from a set of Google applications. + /// </summary> + /// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param> + /// <param name="requestedAccessScope">The requested access scope.</param> + public static void RequestAuthorization(WebConsumer consumer, Applications requestedAccessScope) { + if (consumer == null) { + throw new ArgumentNullException("consumer"); + } + + var extraParameters = new Dictionary<string, string> { + { "scope", GetScopeUri(requestedAccessScope) }, + }; + Uri callback = Util.GetCallbackUrlFromContext(); + var request = consumer.PrepareRequestUserAuthorization(callback, extraParameters, null); + consumer.Channel.Send(request).Send(); + } + + /// <summary> + /// Requests authorization from Google to access data from a set of Google applications. + /// </summary> + /// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param> + /// <param name="requestedAccessScope">The requested access scope.</param> + /// <param name="requestToken">The unauthorized request token assigned by Google.</param> + /// <returns>The request token</returns> + public static Uri RequestAuthorization(DesktopConsumer consumer, Applications requestedAccessScope, out string requestToken) { + if (consumer == null) { + throw new ArgumentNullException("consumer"); + } + + var extraParameters = new Dictionary<string, string> { + { "scope", GetScopeUri(requestedAccessScope) }, + }; + + return consumer.RequestUserAuthorization(extraParameters, null, out requestToken); + } + + /// <summary> + /// Gets the Gmail address book's contents. + /// </summary> + /// <param name="consumer">The Google consumer previously constructed using <see cref="CreateWebConsumer"/> or <see cref="CreateDesktopConsumer"/>.</param> + /// <param name="accessToken">The access token previously retrieved.</param> + /// <returns>An XML document returned by Google.</returns> + public static XDocument GetContacts(ConsumerBase consumer, string accessToken) { + if (consumer == null) { + throw new ArgumentNullException("consumer"); + } + + var response = consumer.PrepareAuthorizedRequestAndSend(GetContactsEndpoint, accessToken); + XDocument result = XDocument.Parse(response.Body); + return result; + } + + /// <summary> + /// Gets the scope URI in Google's format. + /// </summary> + /// <param name="scope">The scope, which may include one or several Google applications.</param> + /// <returns>A space-delimited list of URIs for the requested Google applications.</returns> + private static string GetScopeUri(Applications scope) { + return string.Join(" ", Util.GetIndividualFlags(scope).Select(app => DataScopeUris[(Applications)app]).ToArray()); + } + } +} diff --git a/samples/DotNetOpenAuth.ApplicationBlock/Properties/AssemblyInfo.cs b/samples/DotNetOpenAuth.ApplicationBlock/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..876eb89 --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("DotNetOpenAuth.ApplicationBlock")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("DotNetOpenAuth.ApplicationBlock")] +[assembly: AssemblyCopyright("Copyright © 2008")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("6db0c62e-2f06-46fa-acfe-4d737467c8c7")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/samples/DotNetOpenAuth.ApplicationBlock/Util.cs b/samples/DotNetOpenAuth.ApplicationBlock/Util.cs new file mode 100644 index 0000000..5001e2e --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/Util.cs @@ -0,0 +1,29 @@ +namespace DotNetOpenAuth.ApplicationBlock { + using System; + using System.Collections.Generic; + using DotNetOpenAuth.Messaging; + + internal class Util { + /// <summary> + /// Enumerates through the individual set bits in a flag enum. + /// </summary> + /// <param name="flags">The flags enum value.</param> + /// <returns>An enumeration of just the <i>set</i> bits in the flags enum.</returns> + internal static IEnumerable<long> GetIndividualFlags(Enum flags) { + long flagsLong = Convert.ToInt64(flags); + for (int i = 0; i < sizeof(long) * 8; i++) { // long is the type behind the largest enum + // Select an individual application from the scopes. + long individualFlagPosition = (long)Math.Pow(2, i); + long individualFlag = flagsLong & individualFlagPosition; + if (individualFlag == individualFlagPosition) { + yield return individualFlag; + } + } + } + + internal static Uri GetCallbackUrlFromContext() { + Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix("oauth_"); + return callback; + } + } +} |