//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth.ChannelElements {
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Claims;
using System.Security.Principal;
using Validation;
///
/// Utilities for dealing with OAuth claims and principals.
///
internal static class OAuthPrincipal {
///
/// Creates a new instance of ClaimsPrincipal.
///
/// Name of the user.
/// The roles.
///
/// A new instance of GenericPrincipal with a GenericIdentity, having the same username and roles as this OAuthPrincipal and OAuthIdentity
///
internal static ClaimsPrincipal CreatePrincipal(string userName, IEnumerable roles = null) {
Requires.NotNullOrEmpty(userName, "userName");
var claims = new List();
claims.Add(new Claim(ClaimsIdentity.DefaultNameClaimType, userName));
if (roles != null) {
claims.AddRange(roles.Select(scope => new Claim(ClaimsIdentity.DefaultRoleClaimType, scope)));
}
var claimsIdentity = new ClaimsIdentity(claims, "OAuth 2 Bearer");
var principal = new ClaimsPrincipal(claimsIdentity);
return principal;
}
}
}