Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from johndpalm/VS2013RTM
Browse files Browse the repository at this point in the history
Updated for OWIN 2.0 and VS2013 RTM
  • Loading branch information
John Palmer committed Oct 17, 2013
2 parents 933df12 + fc84bec commit 7c8249f
Show file tree
Hide file tree
Showing 48 changed files with 23,612 additions and 32,022 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
<ItemGroup>
<Reference Include="Microsoft.Owin, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Owin.2.0.0-rc1\lib\net45\Microsoft.Owin.dll</HintPath>
<HintPath>..\packages\Microsoft.Owin.2.0.0\lib\net45\Microsoft.Owin.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Owin.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.Owin.Security.2.0.0-rc1\lib\net45\Microsoft.Owin.Security.dll</HintPath>
<HintPath>..\packages\Microsoft.Owin.Security.2.0.0\lib\net45\Microsoft.Owin.Security.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.5.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public static IAppBuilder UseFoursquareAuthentication(
new FoursquareAuthenticationOptions
{
ClientId = clientId,
ClientSecret = clientSecret,
SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType(),
ClientSecret = clientSecret
});
}
}
Expand Down
34 changes: 18 additions & 16 deletions Citrius.Owin.Security.Foursquare/FoursquareAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public FoursquareAuthenticationHandler(HttpClient httpClient, ILogger logger)
}
public override async Task<bool> InvokeAsync()
{
if (Options.ReturnEndpointPath != null &&
String.Equals(Options.ReturnEndpointPath, Request.Path, StringComparison.OrdinalIgnoreCase))
if (!String.IsNullOrEmpty(Options.CallbackPath) && Options.CallbackPath == Request.Path.ToString())
{
return await InvokeReturnPathAsync();
}
Expand Down Expand Up @@ -141,22 +140,20 @@ protected override Task ApplyResponseChallengeAsync()
return Task.FromResult<object>(null);
}

var challenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode);
AuthenticationResponseChallenge challenge = Helper.LookupChallenge(Options.AuthenticationType, Options.AuthenticationMode);

if (challenge != null)
{
string requestPrefix = Request.Scheme + "://" + Request.Host;
string currentQueryString = Request.QueryString;
string currentUri = string.IsNullOrEmpty(currentQueryString)
? requestPrefix + Request.PathBase + Request.Path
: requestPrefix + Request.PathBase + Request.Path + "?" + currentQueryString;
string baseUri = Request.Scheme + Uri.SchemeDelimiter + Request.Host + Request.PathBase;

string redirectUri = requestPrefix + Request.PathBase + Options.ReturnEndpointPath;
string currentUri = baseUri + Request.Path + Request.QueryString;

var extra = challenge.Properties;
if (string.IsNullOrEmpty(extra.RedirectUrl))
string redirectUri = baseUri + Options.CallbackPath;

AuthenticationProperties extra = challenge.Properties;
if (string.IsNullOrEmpty(extra.RedirectUri))
{
extra.RedirectUrl = currentUri;
extra.RedirectUri = currentUri;
}

// OAuth2 10.12 CSRF
Expand Down Expand Up @@ -187,10 +184,10 @@ public async Task<bool> InvokeReturnPathAsync()

var model = await AuthenticateAsync();

var context = new FoursquareReturnEndpointContext(Context, model, ErrorDetails);
var context = new FoursquareReturnEndpointContext(Context, model);
context.SignInAsAuthenticationType = Options.SignInAsAuthenticationType;
context.RedirectUri = model.Properties.RedirectUrl;
model.Properties.RedirectUrl = null;
context.RedirectUri = model.Properties.RedirectUri;
model.Properties.RedirectUri = null;

await Options.Provider.ReturnEndpoint(context);

Expand All @@ -206,6 +203,11 @@ public async Task<bool> InvokeReturnPathAsync()

if (!context.IsRequestCompleted && context.RedirectUri != null)
{
if (context.Identity == null)
{
// add a redirect hint that sign-in failed in some way
context.RedirectUri = WebUtilities.AddQueryString(context.RedirectUri, "error", "access_denied");
}
Response.Redirect(context.RedirectUri);
context.RequestCompleted();
}
Expand All @@ -217,7 +219,7 @@ private string GenerateRedirectUri()
{
string requestPrefix = Request.Scheme + "://" + Request.Host;

string redirectUri = requestPrefix + RequestPathBase + Options.ReturnEndpointPath; // + "?state=" + Uri.EscapeDataString(Options.StateDataFormat.Protect(state));
string redirectUri = requestPrefix + RequestPathBase + Options.CallbackPath; // + "?state=" + Uri.EscapeDataString(Options.StateDataFormat.Protect(state));
return redirectUri;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public FoursquareAuthenticationMiddleware(
FoursquareAuthenticationOptions options)
: base(next, options)
{
if (string.IsNullOrWhiteSpace(Options.ClientId))
{
throw new ArgumentException("The 'ClientId' must be provided.");
}
if (string.IsNullOrWhiteSpace(Options.ClientSecret))
{
throw new ArgumentException("The 'ClientSecret' option must be provided.");
}

_logger = app.CreateLogger<FoursquareAuthenticationMiddleware>();

if (Options.Provider == null)
Expand All @@ -32,12 +41,17 @@ public FoursquareAuthenticationMiddleware(

if (Options.StateDataFormat == null)
{
var dataProtector = app.CreateDataProtector(
IDataProtector dataProtector = app.CreateDataProtector(
typeof(FoursquareAuthenticationMiddleware).FullName,
Options.AuthenticationType, "v1");
Options.StateDataFormat = new PropertiesDataFormat(dataProtector);
}

if (String.IsNullOrEmpty(Options.SignInAsAuthenticationType))
{
Options.SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType();
}

_httpClient = new HttpClient(ResolveHttpMessageHandler(Options));
_httpClient.Timeout = Options.BackchannelTimeout;
_httpClient.MaxResponseContentBufferSize = 1024 * 1024 * 10; // 10 MB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public FoursquareAuthenticationOptions()
: base(Scheme)
{
Caption = Scheme;
ReturnEndpointPath = "/signin-foursquare";
CallbackPath = "/signin-foursquare";
AuthenticationMode = AuthenticationMode.Passive;
BackchannelTimeout = TimeSpan.FromSeconds(60);
Scope = new List<string>();
Expand All @@ -35,7 +35,7 @@ public string Caption
set { Description.Caption = value; }
}

public string ReturnEndpointPath { get; set; }
public string CallbackPath { get; set; }

public string SignInAsAuthenticationType { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Owin.Security.Provider;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Security.Claims;

namespace Citrius.Owin.Security.Foursquare
Expand All @@ -12,10 +13,20 @@ public class FoursquareAuthenticatedContext : BaseContext
public FoursquareAuthenticatedContext(IOwinContext context, JObject user, string accessToken)
: base(context)
{
if (user == null)
{
throw new ArgumentNullException("user");
}

User = user;
AccessToken = accessToken;

JToken userId = User["id"];
if (userId == null)
{
throw new ArgumentException("The user does not have an id.", "user");
}

Id = TryGetValue(user, "id");
FirstName = TryGetValue(user, "firstName");
LastName = TryGetValue(user, "lastName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ public class FoursquareReturnEndpointContext : ReturnEndpointContext
{
public FoursquareReturnEndpointContext(
IOwinContext context,
AuthenticationTicket ticket,
IDictionary<string, string> errorDetails)
: base(context, ticket, errorDetails)
AuthenticationTicket ticket)
: base(context, ticket)
{
}
}
Expand Down
6 changes: 3 additions & 3 deletions Citrius.Owin.Security.Foursquare/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Owin" version="2.0.0-rc1" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="2.0.0-rc1" targetFramework="net45" />
<package id="Newtonsoft.Json" version="4.5.11" targetFramework="net45" />
<package id="Microsoft.Owin" version="2.0.0" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="2.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
</packages>
Binary file not shown.
18 changes: 0 additions & 18 deletions packages/Microsoft.Owin.2.0.0-rc1/Microsoft.Owin.2.0.0-rc1.nuspec

This file was deleted.

Loading

0 comments on commit 7c8249f

Please sign in to comment.