//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OAuth2 { using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Security.Cryptography; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OAuth2.ChannelElements; /// /// An access token reader that understands DotNetOpenAuth authorization server issued tokens. /// public class StandardAccessTokenAnalyzer : IAccessTokenAnalyzer { /// /// Initializes a new instance of the class. /// /// The crypto service provider with the authorization server public signing key. /// The crypto service provider with the resource server private encryption key. public StandardAccessTokenAnalyzer(RSACryptoServiceProvider authorizationServerPublicSigningKey, RSACryptoServiceProvider resourceServerPrivateEncryptionKey) { Requires.NotNull(authorizationServerPublicSigningKey, "authorizationServerPublicSigningKey"); Requires.NotNull(resourceServerPrivateEncryptionKey, "resourceServerPrivateEncryptionKey"); Requires.True(!resourceServerPrivateEncryptionKey.PublicOnly, "resourceServerPrivateEncryptionKey"); this.AuthorizationServerPublicSigningKey = authorizationServerPublicSigningKey; this.ResourceServerPrivateEncryptionKey = resourceServerPrivateEncryptionKey; } /// /// Gets the authorization server public signing key. /// /// The authorization server public signing key. public RSACryptoServiceProvider AuthorizationServerPublicSigningKey { get; private set; } /// /// Gets the resource server private encryption key. /// /// The resource server private encryption key. public RSACryptoServiceProvider ResourceServerPrivateEncryptionKey { get; private set; } /// /// Reads an access token to find out what data it authorizes access to. /// /// The message carrying the access token. /// The access token. /// The user whose data is accessible with this access token. /// The scope of access authorized by this access token. /// /// A value indicating whether this access token is valid. /// /// /// This method also responsible to throw a or return /// false when the access token is expired, invalid, or from an untrusted authorization server. /// public virtual bool TryValidateAccessToken(IDirectedProtocolMessage message, string accessToken, out string user, out HashSet scope) { var accessTokenFormatter = AccessToken.CreateFormatter(this.AuthorizationServerPublicSigningKey, this.ResourceServerPrivateEncryptionKey); var token = accessTokenFormatter.Deserialize(message, accessToken); user = token.User; scope = new HashSet(token.Scope, OAuthUtilities.ScopeStringComparer); return true; } } }