Skip to content

Commit

Permalink
TrustedPersitant session and initial test project
Browse files Browse the repository at this point in the history
  • Loading branch information
edewit committed Dec 15, 2014
1 parent bd46c5a commit 6a826ab
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 9 deletions.
34 changes: 34 additions & 0 deletions aerogear-windows-oauth2.sln
Expand Up @@ -7,26 +7,34 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aerogear-windows-oauth2", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "demo", "demo\demo.csproj", "{34F226DC-15F0-495E-8721-D0F9B1EA29A0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tests", "tests\tests.csproj", "{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|ARM = Debug|ARM
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|ARM = Release|ARM
Release|Mixed Platforms = Release|Mixed Platforms
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Debug|ARM.ActiveCfg = Debug|ARM
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Debug|ARM.Build.0 = Debug|ARM
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Debug|Mixed Platforms.Build.0 = Debug|x86
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Debug|x86.ActiveCfg = Debug|x86
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Debug|x86.Build.0 = Debug|x86
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Release|Any CPU.Build.0 = Release|Any CPU
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Release|ARM.ActiveCfg = Release|ARM
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Release|ARM.Build.0 = Release|ARM
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Release|Mixed Platforms.ActiveCfg = Release|x86
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Release|Mixed Platforms.Build.0 = Release|x86
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Release|x86.ActiveCfg = Release|x86
{D70FE93A-FCE4-49FE-A356-05BC5A9CF19E}.Release|x86.Build.0 = Release|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand All @@ -35,6 +43,9 @@ Global
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|ARM.ActiveCfg = Debug|ARM
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|ARM.Build.0 = Debug|ARM
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|ARM.Deploy.0 = Debug|ARM
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|Mixed Platforms.Build.0 = Debug|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|Mixed Platforms.Deploy.0 = Debug|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|x86.ActiveCfg = Debug|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|x86.Build.0 = Debug|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Debug|x86.Deploy.0 = Debug|x86
Expand All @@ -44,9 +55,32 @@ Global
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Release|ARM.ActiveCfg = Release|ARM
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Release|ARM.Build.0 = Release|ARM
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Release|ARM.Deploy.0 = Release|ARM
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Release|Mixed Platforms.ActiveCfg = Release|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Release|Mixed Platforms.Build.0 = Release|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Release|Mixed Platforms.Deploy.0 = Release|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Release|x86.ActiveCfg = Release|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Release|x86.Build.0 = Release|x86
{34F226DC-15F0-495E-8721-D0F9B1EA29A0}.Release|x86.Deploy.0 = Release|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|Any CPU.ActiveCfg = Debug|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|ARM.ActiveCfg = Debug|ARM
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|ARM.Build.0 = Debug|ARM
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|ARM.Deploy.0 = Debug|ARM
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|Mixed Platforms.Build.0 = Debug|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|Mixed Platforms.Deploy.0 = Debug|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|x86.ActiveCfg = Debug|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|x86.Build.0 = Debug|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Debug|x86.Deploy.0 = Debug|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|Any CPU.ActiveCfg = Release|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|ARM.ActiveCfg = Release|ARM
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|ARM.Build.0 = Release|ARM
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|ARM.Deploy.0 = Release|ARM
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|Mixed Platforms.ActiveCfg = Release|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|Mixed Platforms.Build.0 = Release|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|Mixed Platforms.Deploy.0 = Release|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|x86.ActiveCfg = Release|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|x86.Build.0 = Release|x86
{8B5D0C53-FD6F-4CC7-B304-C1FD4A6DEB6E}.Release|x86.Deploy.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
13 changes: 12 additions & 1 deletion aerogear-windows-oauth2/OAuth2Session.cs
Expand Up @@ -6,8 +6,19 @@

namespace AeroGear.OAuth2
{
interface OAuth2Session
public interface OAuth2Session
{
void SaveAccessToken();
void SaveAccessToken(string accessToken, string refreshToken, string accessTokenExpiration, string refreshTokenExpiration);
void SaveAccessToken(Session session);
}

public class Session
{
public string accountId { get; set; }
public string accessToken { get; set; }
public DateTime accessTokenExpirationDate { get; set; }
public DateTime refreshTokenExpirationDate { get; set; }
public string refreshToken { get; set; }
}
}
62 changes: 57 additions & 5 deletions aerogear-windows-oauth2/TrustedPersistantOAuth2Session.cs
@@ -1,17 +1,69 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.DataProtection;
using Windows.Storage;
using Windows.Storage.Streams;

namespace AeroGear.OAuth2
{
class TrustedPersistantOAuth2Session: OAuth2Session
public class TrustedPersistantOAuth2Session : OAuth2Session
{
private const BinaryStringEncoding ENCODING = BinaryStringEncoding.Utf8;

public void SaveAccessToken()
public async void SaveAccessToken()
{
throw new NotImplementedException();
}

public async Task SaveAccessToken(string accessToken, string refreshToken, string accessTokenExpiration, string refreshTokenExpiration)
{
DateTime now = new DateTime();
await SaveAccessToken(new Session() {
accessToken = accessToken,
refreshToken = refreshToken,
accessTokenExpirationDate = now.AddMilliseconds(Double.Parse(accessTokenExpiration)),
refreshTokenExpirationDate = now.AddMilliseconds(Double.Parse(refreshTokenExpiration))
});
}

public async Task SaveAccessToken(Session session)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Session));
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, session);
var bytes = ms.ToArray();
await SaveAccessToken(Encoding.UTF8.GetString(bytes, 0, bytes.Length));
}
}

public async Task<string> ReadAccessToken()
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var file = await local.GetFileAsync("token-file.txt");

var text = await FileIO.ReadBufferAsync(file);

DataProtectionProvider Provider = new DataProtectionProvider();
IBuffer buffUnprotected = await Provider.UnprotectAsync(text);

return CryptographicBuffer.ConvertBinaryToString(ENCODING, buffUnprotected);
}

public async Task<IStorageFile> SaveAccessToken(string token)
{
DataProtectionProvider Provider = new DataProtectionProvider("LOCAL=user");
IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(token, ENCODING);
IBuffer buffProtected = await Provider.ProtectAsync(buffMsg);

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var file = await local.CreateFileAsync("token-file.txt", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBufferAsync(file, buffProtected);
return file;
}

}
}
3 changes: 0 additions & 3 deletions aerogear-windows-oauth2/aerogear-windows-oauth2.csproj
Expand Up @@ -78,9 +78,6 @@
<ErrorReport>prompt</ErrorReport>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
</ItemGroup>
<ItemGroup>
<Compile Include="AccountManager.cs" />
<Compile Include="AuthzModule.cs" />
Expand Down
Binary file added tests/Assets/Logo.scale-240.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/Assets/SmallLogo.scale-240.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/Assets/SplashScreen.scale-240.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/Assets/Square71x71Logo.scale-240.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/Assets/StoreLogo.scale-240.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/Assets/WideLogo.scale-240.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions tests/Package.appxmanifest
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest" xmlns:m3="http://schemas.microsoft.com/appx/2014/manifest" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest">

<Identity Name="1b76b2e3-a730-4d7c-8306-ad14f52f5fb1"
Publisher="CN=edewit"
Version="1.0.0.0" />

<mp:PhoneIdentity PhoneProductId="1b76b2e3-a730-4d7c-8306-ad14f52f5fb1" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

<Properties>
<DisplayName>tests</DisplayName>
<PublisherDisplayName>edewit</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>

<Prerequisites>
<OSMinVersion>6.3.1</OSMinVersion>
<OSMaxVersionTested>6.3.1</OSMaxVersionTested>
</Prerequisites>
<Resources>
<Resource Language="x-generate" />
</Resources>
<Applications>
<Application Id="App"
Executable="vstest.executionengine.appcontainer.exe"
EntryPoint="Microsoft.VisualStudio.TestPlatform.TestExecutor.AppContainer.App">
<m3:VisualElements
DisplayName="tests"
Square150x150Logo="Assets\Logo.png"
Square44x44Logo="Assets\SmallLogo.png"
Description="tests"
ForegroundText="light"
BackgroundColor="#464646">
<m3:DefaultTile Wide310x150Logo="Assets\WideLogo.png" Square71x71Logo="Assets\Square71x71Logo.png"/>
<m3:SplashScreen Image="Assets\SplashScreen.png"/>
<m3:ApplicationView MinWidth="width320"/> <!--Used in XAML Designer. DO NOT REMOVE-->
</m3:VisualElements>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClientServer" />
</Capabilities>
</Package>
29 changes: 29 additions & 0 deletions tests/Properties/AssemblyInfo.cs
@@ -0,0 +1,29 @@
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("tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("tests")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 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")]
[assembly: ComVisible(false)]
52 changes: 52 additions & 0 deletions tests/TrustedPersistantOauth2SessionTest.cs
@@ -0,0 +1,52 @@
using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using AeroGear.OAuth2;
using Windows.Storage;
using System.Threading.Tasks;
using Windows.Storage.Streams;

namespace tests
{
[TestClass]
public class TrustedPersistantOauth2SessionTest
{
[TestMethod]
public async Task SaveAndRead()
{
//given
string token = "test token";
TrustedPersistantOAuth2Session session = new TrustedPersistantOAuth2Session();

//when
var file = await session.SaveAccessToken(token);

//then
Assert.IsNotNull(file);
var readToken = await session.ReadAccessToken();
Assert.AreEqual(token, readToken);
}

[TestMethod]
public async Task Read()
{
//given
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
var file = await local.CreateFileAsync("token-file.txt", CreationCollisionOption.OpenIfExists);
await file.DeleteAsync();

TrustedPersistantOAuth2Session session = new TrustedPersistantOAuth2Session();

//when
try
{
await session.ReadAccessToken();
Assert.Fail("excption should have been thrown as there is no token saved yet");
}
catch (Exception e)
{
//then
Assert.IsTrue(e.Message.Contains("The system cannot find the file specified."));
}
}
}
}

0 comments on commit 6a826ab

Please sign in to comment.