//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth.ChannelElements {
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Principal;
///
/// Represents an OAuth consumer that is impersonating a known user on the system.
///
[SuppressMessage("Microsoft.Interoperability", "CA1409:ComVisibleTypesShouldBeCreatable", Justification = "Not cocreatable.")]
[Serializable]
[ComVisible(true)]
public class OAuthPrincipal : IPrincipal {
///
/// The roles this user belongs to.
///
private ICollection roles;
///
/// Initializes a new instance of the class.
///
/// The username.
/// The roles this user belongs to.
public OAuthPrincipal(string userName, string[] roles)
: this(new OAuthIdentity(userName), roles) {
}
///
/// Initializes a new instance of the class.
///
/// The identity.
/// The roles this user belongs to.
internal OAuthPrincipal(OAuthIdentity identity, string[] roles) {
this.Identity = identity;
this.roles = roles;
}
///
/// Gets or sets the access token used to create this principal.
///
/// A non-empty string.
public string AccessToken { get; protected set; }
///
/// Gets the roles that this principal has as a ReadOnlyCollection.
///
public ReadOnlyCollection Roles
{
get { return new ReadOnlyCollection(this.roles.ToList()); }
}
#region IPrincipal Members
///
/// Gets the identity of the current principal.
///
///
///
/// The object associated with the current principal.
///
public IIdentity Identity { get; private set; }
///
/// Determines whether the current principal belongs to the specified role.
///
/// The name of the role for which to check membership.
///
/// true if the current principal is a member of the specified role; otherwise, false.
///
///
/// The role membership check uses .
///
public bool IsInRole(string role) {
return this.roles.Contains(role, StringComparer.OrdinalIgnoreCase);
}
#endregion
///
/// Creates a new instance of GenericPrincipal based on this OAuthPrincipal.
///
/// A new instance of GenericPrincipal with a GenericIdentity, having the same username and roles as this OAuthPrincipal and OAuthIdentity
public GenericPrincipal CreateGenericPrincipal()
{
return new GenericPrincipal(new GenericIdentity(this.Identity.Name), this.roles.ToArray());
}
}
}