summaryrefslogtreecommitdiffstats
path: root/source/Janrain.OpenId/Server/ProtocolException.cs
blob: 12a463a62f6d2c980233e824cffd3463ea6375b8 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;

namespace Janrain.OpenId.Server
{
    public class ProtocolException : ApplicationException, IEncodable
    {

        #region Private Members

        private NameValueCollection _query = new NameValueCollection();

        #endregion

        #region Constructor(s)

        public ProtocolException(NameValueCollection query, string text)
            : base(text)
        {
            _query = query;
        }

        #endregion

        #region Properties

        public bool HasReturnTo
        {
            get
            {
                return (_query[QueryStringArgs.openid.return_to] != null);
            }
        }

        #endregion

        #region IEncodable Members

        public EncodingType WhichEncoding
        {
            get 
            {
                if (this.HasReturnTo)
                    return EncodingType.ENCODE_URL;

                string mode = _query.Get(QueryStringArgs.openid.mode);
                if (mode != null)
                    if (mode != QueryStringArgs.Modes.checkid_setup &&
                        mode != QueryStringArgs.Modes.checkid_immediate)
                        return EncodingType.ENCODE_KVFORM;

                // Notes from the original port
                //# According to the OpenID spec as of this writing, we are
                //# probably supposed to switch on request type here (GET
                //# versus POST) to figure out if we're supposed to print
                //# machine-readable or human-readable content at this
                //# point.  GET/POST seems like a pretty lousy way of making
                //# the distinction though, as it's just as possible that
                //# the user agent could have mistakenly been directed to
                //# post to the server URL.

                //# Basically, if your request was so broken that you didn't
                //# manage to include an openid.mode, I'm not going to worry
                //# too much about returning you something you can't parse.
                return EncodingType.ENCODE_NONE;
            }
        }

        public Uri EncodeToUrl()
        {
            string return_to = _query.Get(QueryStringArgs.openid.return_to);
            if (return_to == null)
                throw new ApplicationException("return_to URL has not been set.");

            NameValueCollection q = new NameValueCollection();
            q.Add(QueryStringArgs.openid.mode, QueryStringArgs.Modes.error);
            q.Add(QueryStringArgs.openid.error, this.Message);

            UriBuilder builder = new UriBuilder(return_to);
            UriUtil.AppendQueryArgs(builder, q);

            return new Uri(builder.ToString());
        }

        public byte[] EncodeToKVForm()
        {
            Hashtable d = new Hashtable();

            d.Add(QueryStringArgs.openidnp.mode, QueryStringArgs.Modes.error);
            d.Add(QueryStringArgs.openidnp.error, this.Message);

            return KVUtil.DictToKV((IDictionary)d);
        }

        #endregion

    }
}