This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Micah Martin
committed
May 23, 2012
1 parent
e856aa1
commit 1d9ea2b
Showing
95 changed files
with
2,985 additions
and
124 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
using System.Web.Http; | ||
using System.Web.Mvc; | ||
using CrackerJack.OAuth; | ||
using CrackerJack.OAuth.Request; | ||
using CrackerJack.OAuth.Response; | ||
|
||
namespace WebApiSample.Controllers | ||
{ | ||
public class OAuthController : ApiController | ||
{ | ||
|
||
public ActionResult Token() | ||
{ | ||
try | ||
{ | ||
var oauthRequest = new TokenRequest(Request, MvcApplication.ServiceLocator); | ||
|
||
var token = oauthRequest.Authorize(); | ||
|
||
if (token.RedirectsUri.HasValue()) | ||
{ | ||
|
||
var redirectUri = OAuthResponse | ||
.TokenResponse(token.AccessToken, token.ExpiresIn, token.RefreshToken) | ||
.SetLocation(token.RedirectsUri) | ||
.BuildQueryMessage().LocationUri; | ||
|
||
return Redirect(redirectUri); | ||
} | ||
|
||
var response = OAuthResponse | ||
.TokenResponse(token.AccessToken, token.ExpiresIn, token.RefreshToken) | ||
.BuildJsonMessage(); | ||
|
||
return this.OAuth(response); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
var response = new ErrorResponseBuilder(ex).BuildJsonMessage(); | ||
return this.OAuth(response); | ||
} | ||
} | ||
} | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
203 changes: 203 additions & 0 deletions
203
src/OAuth2Provider.Tests/Authorization/AuthorizationCodeAuthorizerTest.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,203 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using OAuth2Provider.Authorization; | ||
using OAuth2Provider.Issuer; | ||
using OAuth2Provider.Request; | ||
using AutoMoq; | ||
using NUnit.Framework; | ||
|
||
namespace OAuth2Provider.Tests.Authorization | ||
{ | ||
[TestFixture] | ||
public class AuthorizationCodeAuthorizerTest | ||
{ | ||
[Test] | ||
public void RequiresAuthorizationCodeGrantType() | ||
{ | ||
var mocker = new AutoMoqer(); | ||
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns<string>(null); | ||
|
||
var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>(); | ||
|
||
try | ||
{ | ||
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
Assert.Fail("Exception not thrown."); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode); | ||
Assert.IsTrue(ex.ErrorDescription.HasValue()); | ||
} | ||
|
||
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(""); | ||
|
||
try | ||
{ | ||
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
Assert.Fail("Exception not thrown."); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode); | ||
Assert.IsTrue(ex.ErrorDescription.HasValue()); | ||
} | ||
|
||
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(" "); | ||
|
||
try | ||
{ | ||
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
Assert.Fail("Exception not thrown."); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode); | ||
Assert.IsTrue(ex.ErrorDescription.HasValue()); | ||
} | ||
|
||
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns("asdf"); | ||
|
||
try | ||
{ | ||
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
Assert.Fail("Exception not thrown."); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
Assert.AreEqual(ErrorCode.InvalidGrant, ex.ErrorCode); | ||
Assert.IsTrue(ex.ErrorDescription.HasValue()); | ||
} | ||
} | ||
|
||
[Test] | ||
public void RequiresAuthorizationCode() | ||
{ | ||
var mocker = new AutoMoqer(); | ||
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode); | ||
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns<string>(null); | ||
|
||
var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>(); | ||
|
||
try | ||
{ | ||
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
Assert.Fail("Exception not thrown."); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode); | ||
Assert.IsTrue(ex.ErrorDescription.HasValue()); | ||
} | ||
|
||
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(""); | ||
|
||
try | ||
{ | ||
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
Assert.Fail("Exception not thrown."); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode); | ||
Assert.IsTrue(ex.ErrorDescription.HasValue()); | ||
} | ||
|
||
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(" "); | ||
|
||
try | ||
{ | ||
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
Assert.Fail("Exception not thrown."); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode); | ||
Assert.IsTrue(ex.ErrorDescription.HasValue()); | ||
} | ||
} | ||
|
||
[Test] | ||
public void WhenAuthorizationCodeHasExpired_ThenThrowException() | ||
{ | ||
var mocker = new AutoMoqer(); | ||
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode); | ||
mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300); | ||
mocker.SetInstance<IOAuthIssuer>(new OAuthIssuer()); | ||
var issuer = new OAuthIssuer(); | ||
var token = issuer.GenerateAuthorizationToken(new TokenData { ConsumerId = 1, Timestamp = DateTime.UtcNow.AddHours(-1).Ticks }); | ||
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token); | ||
|
||
var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>(); | ||
|
||
try | ||
{ | ||
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
Assert.Fail("Exception not thrown"); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode); | ||
Assert.IsTrue(ex.ErrorDescription.HasValue()); | ||
} | ||
} | ||
|
||
[Test] | ||
public void WhenRedirectUriDoesNotMatch_ThenExceptionIsThrown() | ||
{ | ||
var mocker = new AutoMoqer(); | ||
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode); | ||
mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300); | ||
mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(500); | ||
var issuer = new OAuthIssuer(); | ||
mocker.SetInstance<IOAuthIssuer>(issuer); | ||
var token = issuer.GenerateAuthorizationToken(new TokenData { ConsumerId = 1, Timestamp = DateTime.UtcNow.Ticks, RedirectUri = "http://test.com" }); | ||
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token); | ||
|
||
var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>(); | ||
|
||
|
||
try | ||
{ | ||
authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
Assert.Fail("Exception not thrown"); | ||
} | ||
catch (OAuthException ex) | ||
{ | ||
Assert.AreEqual(ErrorCode.InvalidRequest, ex.ErrorCode); | ||
Assert.IsTrue(ex.ErrorDescription.HasValue()); | ||
} | ||
|
||
mocker.GetMock<IOAuthRequest>().Setup(x => x.RedirectUri).Returns("http://test.com"); | ||
var result = authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsTrue(result.AccessToken.HasValue()); | ||
Assert.AreEqual(500, result.ExpiresIn); | ||
Assert.IsTrue(result.RefreshToken.HasValue()); | ||
} | ||
|
||
[Test] | ||
public void ReturnsAccessToken() | ||
{ | ||
var mocker = new AutoMoqer(); | ||
mocker.GetMock<IOAuthRequest>().Setup(x => x.GrantType).Returns(GrantType.AuthorizationCode); | ||
mocker.GetMock<IConfiguration>().Setup(x => x.AuthorizationTokenExpirationLength).Returns(300); | ||
mocker.GetMock<IConfiguration>().Setup(x => x.AccessTokenExpirationLength).Returns(500); | ||
var issuer = new OAuthIssuer(); | ||
mocker.SetInstance<IOAuthIssuer>(issuer); | ||
var token = issuer.GenerateAuthorizationToken(new TokenData {ConsumerId = 1, Timestamp = DateTime.UtcNow.Ticks}); | ||
mocker.GetMock<IOAuthRequest>().Setup(x => x.AuthorizationCode).Returns(token); | ||
|
||
var authorizer = mocker.Resolve<AuthorizationCodeAuthorizer>(); | ||
var result = authorizer.Authorize(mocker.GetMock<IOAuthRequest>().Object); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsTrue(result.AccessToken.HasValue()); | ||
Assert.AreEqual(500, result.ExpiresIn); | ||
Assert.IsTrue(result.RefreshToken.HasValue()); | ||
} | ||
} | ||
} |
Oops, something went wrong.