//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth.ChannelElements {
using System;
using System.Collections.Generic;
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 access token.
internal OAuthPrincipal(IServiceProviderAccessToken token)
: this(token.Username, token.Roles) {
Requires.NotNull(token, "token");
this.AccessToken = token.Token;
}
///
/// 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 the access token used to create this principal.
///
/// A non-empty string.
public string AccessToken { get; private set; }
#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
}
}