//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth.ChannelElements {
using System;
using System.Security.Cryptography;
using DotNetOpenAuth.Messaging;
///
/// A cryptographically strong random string generator for tokens and secrets.
///
internal class StandardTokenGenerator : ITokenGenerator {
#region ITokenGenerator Members
///
/// Generates a new token to represent a not-yet-authorized request to access protected resources.
///
/// The consumer that requested this token.
/// The newly generated token.
///
/// This method should not store the newly generated token in any persistent store.
/// This will be done in .
///
public string GenerateRequestToken(string consumerKey) {
return GenerateCryptographicallyStrongString();
}
///
/// Generates a new token to represent an authorized request to access protected resources.
///
/// The consumer that requested this token.
/// The newly generated token.
///
/// This method should not store the newly generated token in any persistent store.
/// This will be done in .
///
public string GenerateAccessToken(string consumerKey) {
return GenerateCryptographicallyStrongString();
}
///
/// Returns a cryptographically strong random string for use as a token secret.
///
/// The generated string.
public string GenerateSecret() {
return GenerateCryptographicallyStrongString();
}
#endregion
///
/// Returns a new random string.
///
/// The new random string.
private static string GenerateCryptographicallyStrongString() {
byte[] buffer = new byte[20];
MessagingUtilities.CryptoRandomDataGenerator.GetBytes(buffer);
return Convert.ToBase64String(buffer);
}
}
}