summaryrefslogtreecommitdiffstats
path: root/src/DotNetOAuth/OAuth/ChannelElements/StandardTokenGenerator.cs
blob: a1bc36ae2f505f064a0d5808fd92a76831d691e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//-----------------------------------------------------------------------
// <copyright file="StandardTokenGenerator.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOAuth.OAuth.ChannelElements {
	using System;
	using System.Security.Cryptography;

	/// <summary>
	/// A cryptographically strong random string generator for tokens and secrets.
	/// </summary>
	internal class StandardTokenGenerator : ITokenGenerator {
		/// <summary>
		/// The cryptographically strong random string generator for tokens and secrets.
		/// </summary>
		private RandomNumberGenerator cryptoProvider = new RNGCryptoServiceProvider();

		#region ITokenGenerator Members

		/// <summary>
		/// Generates a new token to represent a not-yet-authorized request to access protected resources.
		/// </summary>
		/// <param name="consumerKey">The consumer that requested this token.</param>
		/// <returns>The newly generated token.</returns>
		/// <remarks>
		/// This method should not store the newly generated token in any persistent store.
		/// This will be done in <see cref="ITokenManager.StoreNewRequestToken"/>.
		/// </remarks>
		public string GenerateRequestToken(string consumerKey) {
			return this.GenerateCryptographicallyStrongString();
		}

		/// <summary>
		/// Generates a new token to represent an authorized request to access protected resources.
		/// </summary>
		/// <param name="consumerKey">The consumer that requested this token.</param>
		/// <returns>The newly generated token.</returns>
		/// <remarks>
		/// This method should not store the newly generated token in any persistent store.
		/// This will be done in <see cref="ITokenManager.ExpireRequestTokenAndStoreNewAccessToken"/>.
		/// </remarks>
		public string GenerateAccessToken(string consumerKey) {
			return this.GenerateCryptographicallyStrongString();
		}

		/// <summary>
		/// Returns a cryptographically strong random string for use as a token secret.
		/// </summary>
		/// <returns>The generated string.</returns>
		public string GenerateSecret() {
			return this.GenerateCryptographicallyStrongString();
		}

		#endregion

		/// <summary>
		/// Returns a new random string.
		/// </summary>
		/// <returns>The new random string.</returns>
		private string GenerateCryptographicallyStrongString() {
			byte[] buffer = new byte[20];
			this.cryptoProvider.GetBytes(buffer);
			return Convert.ToBase64String(buffer);
		}
	}
}