-rw-r--r-- | include/opkele/types.h | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/include/opkele/types.h b/include/opkele/types.h new file mode 100644 index 0000000..ba06776 --- a/dev/null +++ b/include/opkele/types.h | |||
@@ -0,0 +1,168 @@ | |||
1 | #ifndef __OPKELE_TYPES_H | ||
2 | #define __OPKELE_TYPES_H | ||
3 | |||
4 | /** | ||
5 | * @file | ||
6 | * @brief various types declarations | ||
7 | */ | ||
8 | |||
9 | #include <ostream> | ||
10 | #include <vector> | ||
11 | #include <string> | ||
12 | #include <map> | ||
13 | #include <memory> | ||
14 | |||
15 | /** | ||
16 | * @brief the main opkele namespace | ||
17 | */ | ||
18 | namespace opkele { | ||
19 | using std::vector; | ||
20 | using std::string; | ||
21 | using std::map; | ||
22 | using std::ostream; | ||
23 | using std::auto_ptr; | ||
24 | |||
25 | /** | ||
26 | * the OpenID operation mode | ||
27 | */ | ||
28 | typedef enum _mode_t { | ||
29 | mode_associate, | ||
30 | mode_checkid_immediate, | ||
31 | mode_checkid_setup, | ||
32 | mode_check_association | ||
33 | } mode_t; | ||
34 | |||
35 | /** | ||
36 | * the association secret container | ||
37 | */ | ||
38 | class secret_t : public vector<unsigned char> { | ||
39 | public: | ||
40 | |||
41 | /** | ||
42 | * xor the secret and hmac together and encode, using base64 | ||
43 | * @param key_sha1 pointer to the sha1 digest | ||
44 | * @param rv reference to the return value | ||
45 | */ | ||
46 | void enxor_to_base64(const unsigned char *key_sha1,string& rv) const; | ||
47 | /** | ||
48 | * decode base64-encoded secret and xor it with the sha1 digest | ||
49 | * @param key_sha1 pointer to the message digest | ||
50 | * @param b64 base64-encoded secret value | ||
51 | */ | ||
52 | void enxor_from_base64(const unsigned char *key_sha1,const string& b64); | ||
53 | /** | ||
54 | * plainly encode to base64 representation | ||
55 | * @param rv reference to the return value | ||
56 | */ | ||
57 | void to_base64(string& rv) const; | ||
58 | /** | ||
59 | * decode cleartext secret from base64 | ||
60 | * @param b64 base64-encoded representation of the secret value | ||
61 | */ | ||
62 | void from_base64(const string& b64); | ||
63 | }; | ||
64 | |||
65 | /** | ||
66 | * Interface to the association. | ||
67 | */ | ||
68 | class association_t { | ||
69 | public: | ||
70 | |||
71 | /** | ||
72 | * retrieve the server with which association was established. | ||
73 | * @return server name | ||
74 | */ | ||
75 | virtual string server() const = 0; | ||
76 | /** | ||
77 | * retrieve the association handle. | ||
78 | * @return handle | ||
79 | */ | ||
80 | virtual string handle() const = 0; | ||
81 | /** | ||
82 | * retrieve the association type. | ||
83 | * @return association type | ||
84 | */ | ||
85 | virtual string assoc_type() const = 0; | ||
86 | /** | ||
87 | * retrieve the association secret. | ||
88 | * @return association secret | ||
89 | */ | ||
90 | virtual secret_t secret() const = 0; | ||
91 | /** | ||
92 | * retrieve the number of seconds the association expires in. | ||
93 | * @return seconds till expiration | ||
94 | */ | ||
95 | virtual int expires_in() const = 0; | ||
96 | /** | ||
97 | * check whether the association is stateless. | ||
98 | * @return true if stateless | ||
99 | */ | ||
100 | virtual bool stateless() const = 0; | ||
101 | }; | ||
102 | |||
103 | /** | ||
104 | * the auto_ptr<> for association_t object type | ||
105 | */ | ||
106 | typedef auto_ptr<association_t> assoc_t; | ||
107 | |||
108 | /** | ||
109 | * request/response parameters map | ||
110 | */ | ||
111 | class params_t : public map<string,string> { | ||
112 | public: | ||
113 | |||
114 | /** | ||
115 | * check whether the parameter is present. | ||
116 | * @param n the parameter name | ||
117 | * @return true if yes | ||
118 | */ | ||
119 | bool has_param(const string& n) const; | ||
120 | /** | ||
121 | * retrieve the parameter (const version) | ||
122 | * @param n the parameter name | ||
123 | * @return the parameter value | ||
124 | * @throw failed_lookup if there is no such parameter | ||
125 | */ | ||
126 | const string& get_param(const string& n) const; | ||
127 | /** | ||
128 | * retrieve the parameter. | ||
129 | * @param n the parameter name | ||
130 | * @return the parameter value | ||
131 | * @throw failed_lookup if there is no such parameter | ||
132 | */ | ||
133 | string& get_param(const string& n); | ||
134 | |||
135 | /** | ||
136 | * parse the OpenID key/value data. | ||
137 | * @param kv the OpenID key/value data | ||
138 | */ | ||
139 | void parse_keyvalues(const string& kv); | ||
140 | /** | ||
141 | * sign the fields. | ||
142 | * @param secret the secret used for signing | ||
143 | * @param sig reference to the string, containing base64-encoded | ||
144 | * result | ||
145 | * @param slist the comma-separated list of fields to sign | ||
146 | * @param prefix the string to prepend to parameter names | ||
147 | */ | ||
148 | void sign(secret_t secret,string& sig,const string& slist,const char *prefix=0) const; | ||
149 | |||
150 | /** | ||
151 | * append parameters to the URL as a GET-request parameters. | ||
152 | * @param url the base URL | ||
153 | * @param prefix the string to prepend to parameter names | ||
154 | * @return the ready-to-use location | ||
155 | */ | ||
156 | string append_query(const string& url,const char *prefix = "openid.") const; | ||
157 | }; | ||
158 | |||
159 | /** | ||
160 | * dump the key/value pairs for the parameters to the stream. | ||
161 | * @param o output stream | ||
162 | * @param p the parameters | ||
163 | */ | ||
164 | ostream& operator << (ostream& o,const params_t& p); | ||
165 | |||
166 | } | ||
167 | |||
168 | #endif /* __OPKELE_TYPES_H */ | ||