Skip to content

Commit 0adc0b3

Browse files
committed
Complete and fix SIF_HMACSHA256 authentication implementation.
1 parent 50f2b07 commit 0adc0b3

28 files changed

Lines changed: 777 additions & 328 deletions
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2017 Systemic Pty Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using Sif.Framework.Model.Authentication;
19+
using System;
20+
21+
namespace Sif.Framework.Service.Authentication
22+
{
23+
24+
/// <summary>
25+
/// Unit tests for AuthorisationTokenService.
26+
/// </summary>
27+
[TestClass]
28+
public class AuthorisationTokenServiceTest
29+
{
30+
31+
/// <summary>
32+
/// Use ClassInitialize to run code before running the first test in the class.
33+
/// </summary>
34+
/// <param name="testContext">Context information for the unit test.</param>
35+
[ClassInitialize()]
36+
public static void ClassInitialize(TestContext testContext)
37+
{
38+
}
39+
40+
/// <summary>
41+
/// Use ClassCleanup to run code after all tests in a class have run.
42+
/// </summary>
43+
[ClassCleanup()]
44+
public static void ClassCleanup()
45+
{
46+
}
47+
48+
/// <summary>
49+
/// Use TestInitialize to run code before running each test.
50+
/// </summary>
51+
[TestInitialize()]
52+
public void TestInitialize()
53+
{
54+
}
55+
56+
/// <summary>
57+
/// Use TestCleanup to run code after each test has run.
58+
/// </summary>
59+
[TestCleanup()]
60+
public void TestCleanup()
61+
{
62+
}
63+
64+
/// <summary>
65+
/// Delegate method for retrieving a shared secret.
66+
/// </summary>
67+
/// <param name="token">Token associated with the shared secret.</param>
68+
/// <returns>Shared secret.</returns>
69+
string SharedSecret(string token)
70+
{
71+
return "guest";
72+
}
73+
74+
/// <summary>
75+
/// Authentication test using a Basic authorisation token.
76+
/// </summary>
77+
[TestMethod]
78+
public void BasicAuthorisationTest()
79+
{
80+
IAuthorisationTokenService service = new BasicAuthorisationTokenService();
81+
AuthorisationToken authorisationToken = service.Generate("new", "guest");
82+
Console.WriteLine("Authorisation token is " + authorisationToken.Token + ".");
83+
Console.WriteLine("Generated UTC ISO 8601 date is " + authorisationToken.Timestamp + ".");
84+
GetSharedSecret sharedSecret = SharedSecret;
85+
string sessionToken;
86+
bool authorised = service.Verify(authorisationToken, sharedSecret, out sessionToken);
87+
Assert.AreEqual(sessionToken, "new");
88+
Assert.IsTrue(authorised);
89+
}
90+
91+
/// <summary>
92+
/// Authentication test using a HMAC-SHA256 authorisation token.
93+
/// </summary>
94+
[TestMethod]
95+
public void HMACSHA256AuthorisationTest()
96+
{
97+
IAuthorisationTokenService service = new HmacShaAuthorisationTokenService();
98+
AuthorisationToken authorisationToken = service.Generate("new", "guest");
99+
Console.WriteLine("Authorisation token is " + authorisationToken.Token + ".");
100+
Console.WriteLine("Generated UTC ISO 8601 date is " + authorisationToken.Timestamp + ".");
101+
GetSharedSecret sharedSecret = SharedSecret;
102+
string sessionToken;
103+
bool authorised = service.Verify(authorisationToken, sharedSecret, out sessionToken);
104+
Assert.AreEqual(sessionToken, "new");
105+
Assert.IsTrue(authorised);
106+
}
107+
108+
}
109+
110+
}

Code/Sif3Framework/Sif.Framework.Tests/Sif.Framework.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
<Compile Include="Service\Infrastructure\EnvironmentServiceTest.cs" />
7676
<Compile Include="Service\Mapper\MapperFactoryTest.cs" />
7777
<Compile Include="Utils\SerialisationUtilsTest.cs" />
78-
<Compile Include="Utils\AuthenticationUtilsTest.cs" />
78+
<Compile Include="Service\Authentication\AuthorisationTokenServiceTest.cs" />
7979
<Compile Include="Utils\SettingsManagerTest.cs" />
8080
</ItemGroup>
8181
<ItemGroup>

Code/Sif3Framework/Sif.Framework.Tests/Utils/AuthenticationUtilsTest.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.

Code/Sif3Framework/Sif.Framework/Consumers/Consumer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Consumer<TSingle, TMultiple, TPrimaryKey> : IConsumer<TSingle, TMul
4545
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
4646

4747
private Environment environmentTemplate;
48-
private RegistrationService registrationService;
48+
private IRegistrationService registrationService;
4949

5050
/// <summary>
5151
/// Consumer environment.
@@ -63,7 +63,7 @@ protected Environment EnvironmentTemplate
6363
/// <summary>
6464
/// Service for Consumer registration.
6565
/// </summary>
66-
protected RegistrationService RegistrationService
66+
protected IRegistrationService RegistrationService
6767
{
6868

6969
get

Code/Sif3Framework/Sif.Framework/Consumers/FunctionalServiceConsumer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Crown Copyright © Department for Education (UK) 2016
3+
* Copyright 2017 Systemic Pty Ltd
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
56
* you may not use this file except in compliance with the License.
@@ -26,13 +27,13 @@
2627
using System.Collections.Generic;
2728
using System.Linq;
2829
using System.Net;
29-
using System.Net.Http;
3030
using System.Reflection;
3131
using System.Web.Http;
3232
using Environment = Sif.Framework.Model.Infrastructure.Environment;
3333

3434
namespace Sif.Framework.Consumers
3535
{
36+
3637
/// <summary>
3738
/// The base class for all Functional Service consumers
3839
/// </summary>
@@ -43,7 +44,6 @@ public class FunctionalServiceConsumer
4344
private Environment environmentTemplate;
4445
private RegistrationService registrationService;
4546

46-
4747
/// <summary>
4848
/// Consumer environment template
4949
/// </summary>
@@ -55,7 +55,7 @@ protected Environment EnvironmentTemplate
5555
/// <summary>
5656
/// Service for Consumer registration.
5757
/// </summary>
58-
protected RegistrationService RegistrationService
58+
protected IRegistrationService RegistrationService
5959
{
6060
get { return registrationService; }
6161
}
@@ -610,5 +610,7 @@ private string checkJobs(IList<Job> jobs, RightType right, string zone = null)
610610
}
611611
return name;
612612
}
613+
613614
}
615+
614616
}

0 commit comments

Comments
 (0)