Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Consolidate auth APIs #282

Merged
merged 6 commits into from
Apr 24, 2015
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ public class AuthenticationDescription
/// Initializes a new instance of the <see cref="AuthenticationDescription"/> class
/// </summary>
public AuthenticationDescription()
: this(items: null)
{
Dictionary = new Dictionary<string, object>(StringComparer.Ordinal);
}

/// <summary>
/// Initializes a new instance of the <see cref="AuthenticationDescription"/> class
/// </summary>
/// <param name="properties"></param>
public AuthenticationDescription([NotNull] IDictionary<string, object> properties)
/// <param name="items"></param>
public AuthenticationDescription(IDictionary<string, object> items)
{
Dictionary = properties;
Items = items ?? new Dictionary<string, object>(StringComparer.Ordinal); ;
}

/// <summary>
/// Contains metadata about the authentication provider.
/// </summary>
public IDictionary<string, object> Dictionary { get; private set; }
public IDictionary<string, object> Items { get; private set; }

/// <summary>
/// Gets or sets the name used to reference the authentication middleware instance.
/// </summary>
public string AuthenticationScheme
{
get { return GetString(AuthenticationSchemePropertyKey); }
set { Dictionary[AuthenticationSchemePropertyKey] = value; }
set { Items[AuthenticationSchemePropertyKey] = value; }
}

/// <summary>
Expand All @@ -53,13 +53,13 @@ public string AuthenticationScheme
public string Caption
{
get { return GetString(CaptionPropertyKey); }
set { Dictionary[CaptionPropertyKey] = value; }
set { Items[CaptionPropertyKey] = value; }
}

private string GetString(string name)
{
object value;
if (Dictionary.TryGetValue(name, out value))
if (Items.TryGetValue(name, out value))
{
return Convert.ToString(value, CultureInfo.InvariantCulture);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Microsoft.AspNet.Http.Authentication
{
public abstract class AuthenticationManager
{
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();

public abstract AuthenticationResult Authenticate(string authenticationScheme);

public abstract Task<AuthenticationResult> AuthenticateAsync(string authenticationScheme);

public virtual void Challenge()
{
Challenge(properties: null, authenticationScheme: null);
}

public virtual void Challenge(AuthenticationProperties properties)
{
Challenge(properties, "");
}

public virtual void Challenge(string authenticationScheme)
{
Challenge(properties: null, authenticationScheme: authenticationScheme);
}

public abstract void Challenge(AuthenticationProperties properties, string authenticationScheme);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Brent's issue, we should probably switch scheme to be the first parameter here to be consistent with sign in/signout


public abstract void SignIn(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties = null);

public virtual void SignOut()
{
SignOut(authenticationScheme: null, properties: null);
}

public abstract void SignOut(string authenticationScheme);

public abstract void SignOut(string authenticationScheme, AuthenticationProperties properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,71 +24,71 @@ public class AuthenticationProperties
/// Initializes a new instance of the <see cref="AuthenticationProperties"/> class
/// </summary>
public AuthenticationProperties()
: this(dictionary: null)
: this(items: null)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AuthenticationProperties"/> class
/// </summary>
/// <param name="dictionary"></param>
public AuthenticationProperties(IDictionary<string, string> dictionary)
/// <param name="items"></param>
public AuthenticationProperties(IDictionary<string, string> items)
{
Dictionary = dictionary ?? new Dictionary<string, string>(StringComparer.Ordinal);
Items = items ?? new Dictionary<string, string>(StringComparer.Ordinal);
}

/// <summary>
/// State values about the authentication session.
/// </summary>
public IDictionary<string, string> Dictionary { get; private set; }
public IDictionary<string, string> Items { get; private set; }

/// <summary>
/// Gets or sets whether the authentication session is persisted across multiple requests.
/// </summary>
public bool IsPersistent
{
get { return Dictionary.ContainsKey(IsPersistentKey); }
get { return Items.ContainsKey(IsPersistentKey); }
set
{
if (Dictionary.ContainsKey(IsPersistentKey))
if (Items.ContainsKey(IsPersistentKey))
{
if (!value)
{
Dictionary.Remove(IsPersistentKey);
Items.Remove(IsPersistentKey);
}
}
else
{
if (value)
{
Dictionary.Add(IsPersistentKey, string.Empty);
Items.Add(IsPersistentKey, string.Empty);
}
}
}
}

/// <summary>
/// Gets or sets the full path or absolute URI to be used as an http redirect response value.
/// Gets or sets the full path or absolute URI to be used as an http redirect response value.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "By design")]
public string RedirectUri
{
get
{
string value;
return Dictionary.TryGetValue(RedirectUriKey, out value) ? value : null;
return Items.TryGetValue(RedirectUriKey, out value) ? value : null;
}
set
{
if (value != null)
{
Dictionary[RedirectUriKey] = value;
Items[RedirectUriKey] = value;
}
else
{
if (Dictionary.ContainsKey(RedirectUriKey))
if (Items.ContainsKey(RedirectUriKey))
{
Dictionary.Remove(RedirectUriKey);
Items.Remove(RedirectUriKey);
}
}
}
Expand All @@ -102,7 +102,7 @@ public DateTimeOffset? IssuedUtc
get
{
string value;
if (Dictionary.TryGetValue(IssuedUtcKey, out value))
if (Items.TryGetValue(IssuedUtcKey, out value))
{
DateTimeOffset dateTimeOffset;
if (DateTimeOffset.TryParseExact(value, UtcDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset))
Expand All @@ -116,13 +116,13 @@ public DateTimeOffset? IssuedUtc
{
if (value.HasValue)
{
Dictionary[IssuedUtcKey] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture);
Items[IssuedUtcKey] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture);
}
else
{
if (Dictionary.ContainsKey(IssuedUtcKey))
if (Items.ContainsKey(IssuedUtcKey))
{
Dictionary.Remove(IssuedUtcKey);
Items.Remove(IssuedUtcKey);
}
}
}
Expand All @@ -136,7 +136,7 @@ public DateTimeOffset? ExpiresUtc
get
{
string value;
if (Dictionary.TryGetValue(ExpiresUtcKey, out value))
if (Items.TryGetValue(ExpiresUtcKey, out value))
{
DateTimeOffset dateTimeOffset;
if (DateTimeOffset.TryParseExact(value, UtcDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset))
Expand All @@ -150,13 +150,13 @@ public DateTimeOffset? ExpiresUtc
{
if (value.HasValue)
{
Dictionary[ExpiresUtcKey] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture);
Items[ExpiresUtcKey] = value.Value.ToString(UtcDateTimeFormat, CultureInfo.InvariantCulture);
}
else
{
if (Dictionary.ContainsKey(ExpiresUtcKey))
if (Items.ContainsKey(ExpiresUtcKey))
{
Dictionary.Remove(ExpiresUtcKey);
Items.Remove(ExpiresUtcKey);
}
}
}
Expand All @@ -170,7 +170,7 @@ public bool? AllowRefresh
get
{
string value;
if (Dictionary.TryGetValue(RefreshKey, out value))
if (Items.TryGetValue(RefreshKey, out value))
{
bool refresh;
if (bool.TryParse(value, out refresh))
Expand All @@ -184,13 +184,13 @@ public bool? AllowRefresh
{
if (value.HasValue)
{
Dictionary[RefreshKey] = value.Value.ToString();
Items[RefreshKey] = value.Value.ToString();
}
else
{
if (Dictionary.ContainsKey(RefreshKey))
if (Items.ContainsKey(RefreshKey))
{
Dictionary.Remove(RefreshKey);
Items.Remove(RefreshKey);
}
}
}
Expand Down
22 changes: 3 additions & 19 deletions src/Microsoft.AspNet.Http.Core/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.Http.Authentication;

namespace Microsoft.AspNet.Http
Expand All @@ -20,6 +17,8 @@ public abstract class HttpContext : IDisposable

public abstract ConnectionInfo Connection { get; }

public abstract AuthenticationManager Authentication { get; }

public abstract ClaimsPrincipal User { get; set; }

public abstract IDictionary<object, object> Items { get; }
Expand All @@ -32,9 +31,7 @@ public abstract class HttpContext : IDisposable

public abstract ISessionCollection Session { get; }

public abstract bool IsWebSocketRequest { get; }

public abstract IList<string> WebSocketRequestedProtocols { get; }
public abstract WebSocketManager WebSockets { get; }

public abstract void Abort();

Expand All @@ -53,18 +50,5 @@ public virtual void SetFeature<T>(T instance)
{
SetFeature(typeof(T), instance);
}

public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();

public abstract AuthenticationResult Authenticate(string authenticationScheme);

public abstract Task<AuthenticationResult> AuthenticateAsync(string authenticationScheme);

public virtual Task<WebSocket> AcceptWebSocketAsync()
{
return AcceptWebSocketAsync(subProtocol: null);
}

public abstract Task<WebSocket> AcceptWebSocketAsync(string subProtocol);
}
}
5 changes: 0 additions & 5 deletions src/Microsoft.AspNet.Http.Core/HttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -12,8 +9,6 @@ namespace Microsoft.AspNet.Http
{
public abstract class HttpRequest
{
// TODO - review IOwinRequest for properties

public abstract HttpContext HttpContext { get; }

/// <summary>
Expand Down
37 changes: 4 additions & 33 deletions src/Microsoft.AspNet.Http.Core/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Claims;
using Microsoft.AspNet.Http.Authentication;

namespace Microsoft.AspNet.Http
{
public abstract class HttpResponse
{
// TODO - review IOwinResponse for completeness

public abstract HttpContext HttpContext { get; }

public abstract int StatusCode { get; set; }

public abstract IHeaderDictionary Headers { get; }

public abstract Stream Body { get; set; }

public abstract long? ContentLength { get; set; }

public abstract string ContentType { get; set; }

public abstract IResponseCookies Cookies { get; }
Expand All @@ -35,33 +34,5 @@ public virtual void Redirect(string location)
}

public abstract void Redirect(string location, bool permanent);

public virtual void Challenge()
{
Challenge(properties: null, authenticationScheme: null);
}

public virtual void Challenge(AuthenticationProperties properties)
{
Challenge(properties, "");
}

public virtual void Challenge(string authenticationScheme)
{
Challenge(properties: null, authenticationScheme: authenticationScheme);
}

public abstract void Challenge(AuthenticationProperties properties, string authenticationScheme);

public abstract void SignIn(string authenticationScheme, ClaimsPrincipal principal, AuthenticationProperties properties = null);

public virtual void SignOut()
{
SignOut(authenticationScheme: null, properties: null);
}

public abstract void SignOut(string authenticationScheme);

public abstract void SignOut(string authenticationScheme, AuthenticationProperties properties);
}
}
Loading