-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 in AUTH/d2l.security.oauth2.webapi from ~JPARK…
…ER/d2l.security.oauth2.webapi:fixes to master * commit '2f8de21732ebdbc4ce157e4a89f743aa3efdf945': Check for AllowFrom attribute at controller level Make NoRequiredScopeAttribute public Restrict [NoRequiredScope] to methods
- Loading branch information
Showing
6 changed files
with
99 additions
and
6 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
D2L.Security.OAuth2.WebApi.IntegrationTests/Authorization/AllowFromControllerTests.cs
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,67 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using D2L.Services; | ||
|
||
namespace D2L.Security.OAuth2.Authorization { | ||
[TestFixture] | ||
internal sealed class AllowFromControllerTests { | ||
private const string SCOPE = "a:b:c"; | ||
|
||
[Test] | ||
public async Task Default_NoAuthentication_403() { | ||
await TestUtilities.RunBasicAuthTest( "/allowfrom/default", HttpStatusCode.Unauthorized ) | ||
.SafeAsync(); | ||
} | ||
|
||
[Test] | ||
public async Task Default_UserInvalidScope_401() { | ||
string jwt = await TestUtilities.GetAccessTokenValidForAMinute( | ||
userId: 123, | ||
scope: SCOPE + "foo" | ||
).SafeAsync(); | ||
|
||
await TestUtilities.RunBasicAuthTest( "/allowfrom/default", jwt, HttpStatusCode.Forbidden ) | ||
.SafeAsync(); | ||
} | ||
|
||
[Test] | ||
public async Task Default_UserValidScope_204() { | ||
string jwt = await TestUtilities.GetAccessTokenValidForAMinute( | ||
userId: 123, | ||
scope: SCOPE | ||
).SafeAsync(); | ||
|
||
await TestUtilities.RunBasicAuthTest( "/allowfrom/default", jwt, HttpStatusCode.NoContent ) | ||
.SafeAsync(); | ||
} | ||
|
||
[TestCase( 0, "wrong:scope:ok", HttpStatusCode.Unauthorized, TestName="A service with the wrong scope" )] | ||
[TestCase( 0, SCOPE, HttpStatusCode.Unauthorized, TestName= "A service with the right scope" )] | ||
[TestCase( 123, "wrong:scope:ok", HttpStatusCode.Forbidden, TestName = "A user with the wrong scope" )] | ||
[TestCase( 123, SCOPE, HttpStatusCode.NoContent, TestName = "A user with the right scope" )] | ||
public async Task Default_Service_403( long userId, string scope, HttpStatusCode expectedStatusCode ) { | ||
string jwt = await TestUtilities.GetAccessTokenValidForAMinute( | ||
userId: userId == 0 ? (long?)null : userId, | ||
scope: scope | ||
).SafeAsync(); | ||
|
||
await TestUtilities.RunBasicAuthTest( "/allowfrom/default", jwt, expectedStatusCode ) | ||
.SafeAsync(); | ||
} | ||
|
||
[TestCase( 0, "wrong:scope:ok", HttpStatusCode.Forbidden, TestName="A service with wrong scope fails authz" )] | ||
[TestCase( 0, SCOPE, HttpStatusCode.NoContent, TestName="A service with the right scope succeeds" )] | ||
[TestCase( 123, "wrong:scope:ok", HttpStatusCode.Forbidden, TestName="A user with the wrong scope fails authz (wrong kind of authn) TODO: does 401 instead of 403 (arguably) due to order of attributes" )] | ||
[TestCase( 123, SCOPE, HttpStatusCode.Unauthorized, TestName="A user with the right scope fails authz (wrong kind of authn)" )] | ||
public async Task ServicesOnly_AuthenticationOkCases( long userId, string scope, HttpStatusCode expectedStatusCode ) { | ||
string jwt = await TestUtilities.GetAccessTokenValidForAMinute( | ||
userId: userId == 0 ? (long?)null : userId, | ||
scope: scope | ||
).SafeAsync(); | ||
|
||
await TestUtilities.RunBasicAuthTest( "/allowfrom/servicesonly", jwt, expectedStatusCode ) | ||
.SafeAsync(); | ||
} | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
...Security.OAuth2.WebApi.IntegrationTests/TestWebService/Controllers/AllowFromController.cs
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,23 @@ | ||
using System.Web.Http; | ||
using D2L.Security.OAuth2.Authorization; | ||
|
||
namespace D2L.Security.OAuth2.TestWebService.Controllers { | ||
[DefaultAuthorization] | ||
[AllowFrom( users: true )] | ||
public sealed class AllowFromController : ApiController { | ||
[HttpGet] | ||
[RequireScope("a","b","c")] | ||
[Route("allowfrom/default")] | ||
public void Default() { | ||
|
||
} | ||
|
||
[HttpGet] | ||
[RequireScope("a","b","c")] | ||
[Route("allowfrom/servicesonly")] | ||
[AllowFrom( users: false, services: true )] | ||
public void ServicesOnly() { | ||
|
||
} | ||
} | ||
} |
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