summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Alexander <jason.alexander@gmail.com>2007-02-25 23:57:24 +0000
committerJason <jason.alexander@gmail.com>2007-02-25 23:57:24 +0000
commitb83c4503b0fc1603c8d21a20b4ab9a47f0adea90 (patch)
tree94eb381b407383ad9e811185414348b521170bba
parente0a192f30895b7c1371d20c6a7e1ca824749e72d (diff)
downloadDotNetOpenAuth-b83c4503b0fc1603c8d21a20b4ab9a47f0adea90.zip
DotNetOpenAuth-b83c4503b0fc1603c8d21a20b4ab9a47f0adea90.tar.gz
DotNetOpenAuth-b83c4503b0fc1603c8d21a20b4ab9a47f0adea90.tar.bz2
git-svn-id: https://dotnetopenid.googlecode.com/svn/trunk@15 01efa1a6-402a-0410-b0ae-47b76eba00f0
-rw-r--r--source/Janrain.OpenId/Janrain.OpenId.csproj5
-rw-r--r--source/Janrain.OpenId/Server/AssociateRequest.cs74
-rw-r--r--source/Janrain.OpenId/Server/Codec.cs187
-rw-r--r--source/Janrain.OpenId/Server/Server.cs47
-rw-r--r--source/Janrain.OpenId/Server/ServerSession.cs147
-rw-r--r--source/Janrain.OpenId/Server/WebResponse.cs62
-rw-r--r--source/Janrain.OpenId/bin/Debug/Janrain.OpenId.dllbin86016 -> 94208 bytes
-rw-r--r--source/Janrain.OpenId/bin/Debug/Janrain.OpenId.pdbbin216576 -> 241152 bytes
-rw-r--r--source/Janrain.OpenId/obj/Debug/Janrain.OpenId.dllbin86016 -> 94208 bytes
-rw-r--r--source/Janrain.OpenId/obj/Debug/Janrain.OpenId.pdbbin216576 -> 241152 bytes
10 files changed, 522 insertions, 0 deletions
diff --git a/source/Janrain.OpenId/Janrain.OpenId.csproj b/source/Janrain.OpenId/Janrain.OpenId.csproj
index 7a230d9..9b89767 100644
--- a/source/Janrain.OpenId/Janrain.OpenId.csproj
+++ b/source/Janrain.OpenId/Janrain.OpenId.csproj
@@ -51,14 +51,19 @@
<Compile Include="DiffieHellman\mono\SequentialSearchPrimeGeneratorBase.cs" />
<Compile Include="KVUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Server\AssociateRequest.cs" />
<Compile Include="Server\CheckAuthRequest.cs" />
<Compile Include="Server\CheckIdRequest.cs" />
+ <Compile Include="Server\Codec.cs" />
<Compile Include="Server\IEncodable.cs" />
<Compile Include="Server\ProtocolException.cs" />
<Compile Include="Server\Request.cs" />
<Compile Include="Server\Response.cs" />
+ <Compile Include="Server\Server.cs" />
+ <Compile Include="Server\ServerSession.cs" />
<Compile Include="Server\Signatory.cs" />
<Compile Include="Server\TrustRoot.cs" />
+ <Compile Include="Server\WebResponse.cs" />
<Compile Include="Store\IAssociationStore.cs" />
<Compile Include="Store\MemoryStore.cs" />
<Compile Include="Util.cs" />
diff --git a/source/Janrain.OpenId/Server/AssociateRequest.cs b/source/Janrain.OpenId/Server/AssociateRequest.cs
new file mode 100644
index 0000000..432be23
--- /dev/null
+++ b/source/Janrain.OpenId/Server/AssociateRequest.cs
@@ -0,0 +1,74 @@
+using System;
+using System.Collections.Specialized;
+using System.Text;
+
+namespace Janrain.OpenId.Server
+{
+ public class AssociateRequest : Request
+ {
+
+ #region Private Members
+
+ private string _assoc_type = "HMAC-SHA1";
+ private ServerSession _session;
+
+ #endregion
+
+ #region Constructor(s)
+
+ public AssociateRequest(NameValueCollection query)
+ : base()
+ {
+ string session_type = query.Get("openid.session_type");
+
+ if (session_type == null)
+ {
+ _session = new PlainTextServerSession();
+ }
+ else if (session_type == "DH-SHA1")
+ {
+ _session = new DiffieHellmanServerSession(query);
+ }
+ else
+ {
+ throw new ProtocolException(query, "Unknown sessoin type " + session_type);
+ }
+ }
+
+ #endregion
+
+ #region Properties
+
+ public override string Mode
+ {
+ get { return "associate"; }
+ }
+
+ #endregion
+
+ #region Methods
+
+ public Response Answer(Association assoc)
+ {
+ Response response = new Response(this);
+
+ response.Fields["expires_in"] = assoc.ExpiresIn;
+ response.Fields["assoc_type"] = "HMAC-SHA1";
+ response.Fields["assoc_handle"] = assoc.Handle;
+
+ NameValueCollection nvc = _session.Answer(assoc.Secret);
+ foreach (string key in nvc)
+ {
+ response.Fields[key] = nvc[key];
+ }
+
+ if (_session.SessionType != "plaintext")
+ response.Fields["session_type"] = _session.SessionType;
+
+ return response;
+ }
+
+ #endregion
+
+ }
+}
diff --git a/source/Janrain.OpenId/Server/Codec.cs b/source/Janrain.OpenId/Server/Codec.cs
new file mode 100644
index 0000000..f363dbd
--- /dev/null
+++ b/source/Janrain.OpenId/Server/Codec.cs
@@ -0,0 +1,187 @@
+using System;
+using System.Collections.Specialized;
+using System.Text;
+
+namespace Janrain.OpenId.Server
+{
+ public class EncodingException : ApplicationException
+ {
+
+ #region Private Members
+
+ private IEncodable _response;
+
+ #endregion
+
+ #region Constructor(s)
+
+ public EncodingException(IEncodable response)
+ {
+ _response = response;
+ }
+
+ #endregion
+
+ #region Properties
+
+ public IEncodable Response
+ {
+ get { return _response; }
+ }
+
+ #endregion
+
+ }
+
+ public class AlreadySignedException : EncodingException
+ {
+
+ public AlreadySignedException(IEncodable response)
+ : base(response)
+ {
+ }
+
+ }
+
+ public class Encoder
+ {
+
+ #region Constructor(s)
+
+ public Encoder() { }
+
+ #endregion
+
+ #region Methods
+
+ public virtual WebResponse Encode(IEncodable response)
+ {
+ EncodingType encode_as = response.WhichEncoding;
+ WebResponse wr;
+
+
+ if (encode_as == EncodingType.ENCODE_KVFORM)
+ {
+ HttpCode code;
+
+ if (response is Exception)
+ code = HttpCode.HTTP_ERROR;
+ else
+ code = HttpCode.HTTP_OK;
+
+ wr = new WebResponse(code, null, response.EncodeToKVForm());
+
+ }
+ else if (encode_as == EncodingType.ENCODE_URL)
+ {
+ NameValueCollection headers = new NameValueCollection();
+
+ headers.Add("Location", response.EncodeToUrl().AbsoluteUri);
+
+ wr = new WebResponse(HttpCode.HTTP_REDIRECT, headers, new byte[0]);
+ }
+ else
+ {
+ throw new EncodingException(response);
+ }
+
+ return wr;
+ }
+
+ #endregion
+
+ }
+
+ public class SigningEncoder : Encoder
+ {
+
+ #region Private Members
+
+ private Signatory _signatory;
+
+ #endregion
+
+ #region Constructor(s)
+
+ public SigningEncoder(Signatory signatory)
+ {
+ _signatory = signatory;
+ }
+
+ #endregion
+
+ #region Methods
+
+ public override WebResponse Encode(IEncodable encodable)
+ {
+ if (!(encodable is Exception))
+ {
+ Response response = (Response)encodable;
+
+ if (response.NeedsSigning)
+ {
+ if (_signatory == null)
+ throw new ArgumentException("Must have a store to sign this request");
+
+ if (response.Fields.Contains("sig"))
+ throw new AlreadySignedException(encodable);
+
+ _signatory.Sign(response);
+ }
+
+ }
+
+ return base.Encode(encodable);
+ }
+
+ #endregion
+
+ }
+
+ public class Decoder
+ {
+
+ #region Private Members
+
+ private static string[] _handlers = { };
+
+ #endregion
+
+ #region Methods
+
+ public static Request Decode(NameValueCollection query)
+ {
+ if (query == null) return null;
+
+ NameValueCollection myquery = new NameValueCollection();
+ foreach (string key in query)
+ {
+ if (key.StartsWith("openid."))
+ myquery[key] = query[key];
+ }
+
+ if (myquery.Count == 0) return null;
+
+ string mode = myquery.Get("openid.mode");
+ if (mode == null)
+ throw new ProtocolException(query, "No openid.mode value in query");
+
+ if (mode == "checkid_setup")
+ return new CheckIdRequest(query);
+ else if (mode == "checkid_immediate")
+ return new CheckIdRequest(query);
+ else if (mode == "check_authentication")
+ return new CheckAuthRequest(query);
+ else if (mode == "associate")
+ return new AssociateRequest(query);
+
+ throw new ProtocolException(query, "No decoder for openid.mode=" + mode);
+
+ }
+
+ #endregion
+
+ }
+
+
+}
diff --git a/source/Janrain.OpenId/Server/Server.cs b/source/Janrain.OpenId/Server/Server.cs
new file mode 100644
index 0000000..937efeb
--- /dev/null
+++ b/source/Janrain.OpenId/Server/Server.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Specialized;
+using System.Text;
+using Janrain.OpenId.Store;
+
+
+namespace Janrain.OpenId.Server
+{
+ public class Server
+ {
+
+ #region Private Members
+
+ private IAssociationStore _store;
+ private Signatory _signatory;
+ private Encoder _encoder;
+
+ #endregion
+
+ #region Constructor(s)
+
+ public Server(IAssociationStore store)
+ {
+ _store = store;
+ _signatory = new Signatory(store);
+ _encoder = new SigningEncoder(_signatory);
+ }
+
+ #endregion
+
+ #region Methods
+
+ public Response HandleRequest(CheckAuthRequest request)
+ {
+ return request.Answer(_signatory);
+ }
+
+ public Response HandleRequest(AssociateRequest request)
+ {
+ Association assoc = _signatory.CreateAssociation(false);
+ return request.Answer(assoc);
+ }
+
+ #endregion
+
+ }
+}
diff --git a/source/Janrain.OpenId/Server/ServerSession.cs b/source/Janrain.OpenId/Server/ServerSession.cs
new file mode 100644
index 0000000..ec905b5
--- /dev/null
+++ b/source/Janrain.OpenId/Server/ServerSession.cs
@@ -0,0 +1,147 @@
+using System;
+using System.Collections.Specialized;
+using Org.Mentalis.Security.Cryptography;
+using System.Text;
+
+namespace Janrain.OpenId.Server
+{
+ public abstract class ServerSession
+ {
+
+ #region Private Members
+
+ private string _session_type;
+
+ #endregion
+
+ #region Properties
+
+ public string SessionType
+ {
+ get { return _session_type; }
+ set { _session_type = value; }
+ }
+
+ #endregion
+
+ #region Methods
+
+ public abstract NameValueCollection Answer(byte[] secret);
+
+ #endregion
+
+ }
+
+ public class PlainTextServerSession : ServerSession
+ {
+
+ #region Constructor(s)
+
+ public PlainTextServerSession()
+ {
+ this.SessionType = "plaintext";
+ }
+
+ #endregion
+
+ #region Methods
+
+ public override NameValueCollection Answer(byte[] secret)
+ {
+ NameValueCollection nvc = new NameValueCollection();
+
+ nvc.Add("mac_key", CryptUtil.ToBase64String(secret));
+
+ return nvc;
+ }
+
+ #endregion
+
+ }
+
+ public class DiffieHellmanServerSession : ServerSession
+ {
+
+ #region Private Members
+
+ private byte[] _consumer_pubkey;
+ private DiffieHellman _dh;
+
+ #endregion
+
+ #region Constructor(s)
+
+ public DiffieHellmanServerSession(NameValueCollection query)
+ {
+ string missing;
+ string dh_modulus = query.Get("openid.dh_modulus");
+ string dh_gen = query.Get("openid.dh_gen");
+ byte[] dh_modulus_bytes = new byte[0];
+ byte[] dh_gen_bytes = new byte[0];
+
+ this.SessionType = "DH-SHA1";
+
+ if ((dh_modulus == null && dh_gen != null) ||
+ (dh_gen == null && dh_modulus != null))
+ {
+ if (dh_modulus == null)
+ missing = "modulus";
+ else
+ missing = "generator";
+
+ throw new ProtocolException(query, "If non-default modulus or generator is supplied, both must be supplied. Missing: " + missing);
+ }
+
+ if (dh_modulus != "" || dh_gen != "")
+ {
+ try
+ {
+ dh_modulus_bytes = Convert.FromBase64String(dh_modulus);
+ }
+ catch (FormatException)
+ {
+ throw new ProtocolException(query, "dh_modulus isn't properly base64ed");
+ }
+ }
+ else
+ {
+ dh_modulus_bytes = CryptUtil.DEFAULT_MOD;
+ dh_gen_bytes = CryptUtil.DEFAULT_GEN;
+ }
+
+ _dh = new DiffieHellmanManaged(dh_modulus_bytes, dh_gen_bytes, 1024);
+
+ string consumer_pubkey = query.Get("openid.dh_consumer_public");
+ if (consumer_pubkey == null)
+ throw new ProtocolException(query, "Public key for DH-SHA1 session not found in query");
+
+ try
+ {
+ _consumer_pubkey = Convert.FromBase64String(consumer_pubkey);
+ }
+ catch (FormatException)
+ {
+ throw new ProtocolException(query, "consumer_pubkey isn't properly base64ed");
+ }
+ }
+
+ #endregion
+
+ #region Methods
+
+ public override NameValueCollection Answer(byte[] secret)
+ {
+ byte[] mac_key = CryptUtil.SHA1XorSecret(_dh, _consumer_pubkey, secret);
+ NameValueCollection nvc = new NameValueCollection();
+
+ nvc.Add("dh_server_public", CryptUtil.UnsignedToBase64(_dh.CreateKeyExchange()));
+ nvc.Add("enc_mac_key", CryptUtil.ToBase64String(mac_key));
+
+ return nvc;
+ }
+
+ #endregion
+
+ }
+
+}
diff --git a/source/Janrain.OpenId/Server/WebResponse.cs b/source/Janrain.OpenId/Server/WebResponse.cs
new file mode 100644
index 0000000..b82a5ff
--- /dev/null
+++ b/source/Janrain.OpenId/Server/WebResponse.cs
@@ -0,0 +1,62 @@
+using System;
+using System.Collections.Specialized;
+using System.Text;
+
+namespace Janrain.OpenId.Server
+{
+
+ public enum HttpCode : int
+ {
+ HTTP_OK = 200,
+ HTTP_REDIRECT = 302,
+ HTTP_ERROR = 400
+ }
+
+ public class WebResponse
+ {
+
+ #region Private Members
+
+ private HttpCode _code;
+ private NameValueCollection _headers;
+ private byte[] _body;
+
+ #endregion
+
+ #region Constructor(s)
+
+ public WebResponse(HttpCode code, NameValueCollection headers, byte[] body)
+ {
+ _code = code;
+
+ if (headers == null)
+ _headers = new NameValueCollection();
+ else
+ _headers = headers;
+
+ _body = body;
+ }
+
+ #endregion
+
+ #region Properties
+
+ public HttpCode Code
+ {
+ get { return _code; }
+ }
+
+ public NameValueCollection Headers
+ {
+ get { return _headers; }
+ }
+
+ public byte[] Body
+ {
+ get { return _body; }
+ }
+
+ #endregion
+
+ }
+}
diff --git a/source/Janrain.OpenId/bin/Debug/Janrain.OpenId.dll b/source/Janrain.OpenId/bin/Debug/Janrain.OpenId.dll
index 54b4e35..4c92826 100644
--- a/source/Janrain.OpenId/bin/Debug/Janrain.OpenId.dll
+++ b/source/Janrain.OpenId/bin/Debug/Janrain.OpenId.dll
Binary files differ
diff --git a/source/Janrain.OpenId/bin/Debug/Janrain.OpenId.pdb b/source/Janrain.OpenId/bin/Debug/Janrain.OpenId.pdb
index 258e134..d602008 100644
--- a/source/Janrain.OpenId/bin/Debug/Janrain.OpenId.pdb
+++ b/source/Janrain.OpenId/bin/Debug/Janrain.OpenId.pdb
Binary files differ
diff --git a/source/Janrain.OpenId/obj/Debug/Janrain.OpenId.dll b/source/Janrain.OpenId/obj/Debug/Janrain.OpenId.dll
index 54b4e35..4c92826 100644
--- a/source/Janrain.OpenId/obj/Debug/Janrain.OpenId.dll
+++ b/source/Janrain.OpenId/obj/Debug/Janrain.OpenId.dll
Binary files differ
diff --git a/source/Janrain.OpenId/obj/Debug/Janrain.OpenId.pdb b/source/Janrain.OpenId/obj/Debug/Janrain.OpenId.pdb
index 258e134..d602008 100644
--- a/source/Janrain.OpenId/obj/Debug/Janrain.OpenId.pdb
+++ b/source/Janrain.OpenId/obj/Debug/Janrain.OpenId.pdb
Binary files differ