Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TrustedPersitant session and initial test project
- Loading branch information
Showing
15 changed files
with
343 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.")); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.