//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth2 {
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2.ChannelElements;
using Validation;
///
/// 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.That(resourceServerPrivateEncryptionKey == null || !resourceServerPrivateEncryptionKey.PublicOnly, "resourceServerPrivateEncryptionKey", "Private key required when encrypting.");
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's serialized representation.
/// The deserialized, validated token.
/// Thrown if the access token is expired, invalid, or from an untrusted authorization server.
public virtual AccessToken DeserializeAccessToken(IDirectedProtocolMessage message, string accessToken) {
ErrorUtilities.VerifyProtocol(!string.IsNullOrEmpty(accessToken), ResourceServerStrings.MissingAccessToken);
var accessTokenFormatter = AccessToken.CreateFormatter(this.AuthorizationServerPublicSigningKey, this.ResourceServerPrivateEncryptionKey);
var token = new AccessToken();
try {
accessTokenFormatter.Deserialize(token, accessToken, message, Protocol.access_token);
} catch (IOException ex) {
throw new ProtocolException(ResourceServerStrings.InvalidAccessToken, ex);
}
return token;
}
}
}