summaryrefslogtreecommitdiffstats
path: root/samples/OAuthServiceProvider/Code/OAuth2AuthorizationServer.cs
blob: 75778f595c5f5df0521cf10794e951d620608dc2 (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
namespace OAuthServiceProvider.Code {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Security.Cryptography;
	using System.Web;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.Messaging.Bindings;
	using DotNetOpenAuth.OAuth.ChannelElements;
	using DotNetOpenAuth.OAuthWrap;
	using DotNetOpenAuth.OAuthWrap.ChannelElements;

	internal class OAuth2AuthorizationServer : IAuthorizationServer {
		private static readonly byte[] secret;

		internal static readonly RSAParameters asymmetricKey;

		private readonly INonceStore nonceStore = new DatabaseNonceStore();

		static OAuth2AuthorizationServer() {
			// For this sample, we just generate random secrets.
			RandomNumberGenerator crypto = new RNGCryptoServiceProvider();
			secret = new byte[16];
			crypto.GetBytes(secret);

			asymmetricKey = new RSACryptoServiceProvider().ExportParameters(true);
		}

		#region Implementation of IAuthorizationServer

		public byte[] Secret {
			get { return secret; }
		}

		public DotNetOpenAuth.Messaging.Bindings.INonceStore VerificationCodeNonceStore {
			get { return this.nonceStore; }
		}

		public RSAParameters AccessTokenSigningPrivateKey {
			get { return asymmetricKey; }
		}

		public IConsumerDescription GetClient(string clientIdentifier) {
			var consumerRow = Global.DataContext.OAuthConsumers.SingleOrDefault(
				consumerCandidate => consumerCandidate.ConsumerKey == clientIdentifier);
			if (consumerRow == null) {
				throw new ArgumentOutOfRangeException("clientIdentifier");
			}

			return consumerRow;
		}

		#endregion

		public bool IsAuthorizationValid(IAuthorizationDescription authorization) {
			// We don't support revoking tokens yet.
			return true;
		}
	}
}