-rw-r--r-- | lib/sreg.cc | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/lib/sreg.cc b/lib/sreg.cc index 7e2d588..b40cd45 100644 --- a/lib/sreg.cc +++ b/lib/sreg.cc | |||
@@ -1,140 +1,160 @@ | |||
1 | #include <opkele/exception.h> | 1 | #include <opkele/exception.h> |
2 | #include <opkele/sreg.h> | 2 | #include <opkele/sreg.h> |
3 | #include <opkele/uris.h> | 3 | #include <opkele/uris.h> |
4 | #include <algorithm> | 4 | #include <algorithm> |
5 | 5 | ||
6 | namespace opkele { | 6 | namespace opkele { |
7 | using std::find; | 7 | using std::find; |
8 | 8 | ||
9 | static const struct _sreg_field { | 9 | static const struct _sreg_field { |
10 | const char *fieldname; | 10 | const char *fieldname; |
11 | sreg_t::fieldbit_t fieldbit; | 11 | sreg_t::fieldbit_t fieldbit; |
12 | }fields[] = { | 12 | }fields[] = { |
13 | { "nickname", sreg_t::field_nickname }, | 13 | { "nickname", sreg_t::field_nickname }, |
14 | { "email", sreg_t::field_email }, | 14 | { "email", sreg_t::field_email }, |
15 | { "fullname", sreg_t::field_fullname }, | 15 | { "fullname", sreg_t::field_fullname }, |
16 | { "dob", sreg_t::field_dob }, | 16 | { "dob", sreg_t::field_dob }, |
17 | { "gender", sreg_t::field_gender }, | 17 | { "gender", sreg_t::field_gender }, |
18 | { "postcode", sreg_t::field_postcode }, | 18 | { "postcode", sreg_t::field_postcode }, |
19 | { "country", sreg_t::field_country }, | 19 | { "country", sreg_t::field_country }, |
20 | { "language", sreg_t::field_language }, | 20 | { "language", sreg_t::field_language }, |
21 | { "timezone", sreg_t::field_timezone } | 21 | { "timezone", sreg_t::field_timezone } |
22 | }; | 22 | }; |
23 | # define fields_BEGINfields | 23 | # define fields_BEGINfields |
24 | # define fields_END &fields[sizeof(fields)/sizeof(*fields)] | 24 | # define fields_END &fields[sizeof(fields)/sizeof(*fields)] |
25 | typedef const struct _sreg_field *fields_iterator; | 25 | typedef const struct _sreg_field *fields_iterator; |
26 | 26 | ||
27 | bool operator==(const struct _sreg_field& fd,const string& fn) { | 27 | bool operator==(const struct _sreg_field& fd,const string& fn) { |
28 | return fd.fieldname==fn; | 28 | return fd.fieldname==fn; |
29 | } | 29 | } |
30 | 30 | ||
31 | void sreg_t::checkid_hook(basic_openid_message& om) { | 31 | void sreg_t::rp_checkid_hook(basic_openid_message& om) { |
32 | string fr, fo; | 32 | string fr, fo; |
33 | for(fields_iterator f=fields_BEGIN;f<fields_END;++f) { | 33 | for(fields_iterator f=fields_BEGIN;f<fields_END;++f) { |
34 | if(f->fieldbit&fields_required) { | 34 | if(f->fieldbit&fields_required) { |
35 | if(!fr.empty()) fr+=","; | 35 | if(!fr.empty()) fr+=","; |
36 | fr += f->fieldname; | 36 | fr += f->fieldname; |
37 | } | 37 | } |
38 | if(f->fieldbit&fields_optional) { | 38 | if(f->fieldbit&fields_optional) { |
39 | if(!fo.empty()) fo+=","; | 39 | if(!fo.empty()) fo+=","; |
40 | fo += f->fieldname; | 40 | fo += f->fieldname; |
41 | } | 41 | } |
42 | } | 42 | } |
43 | string pfx = om.allocate_ns(OIURI_SREG11,"sreg"); | 43 | string pfx = om.allocate_ns(OIURI_SREG11,"sreg"); |
44 | if(!fr.empty()) om.set_field(pfx+".required",fr); | 44 | if(!fr.empty()) om.set_field(pfx+".required",fr); |
45 | if(!fo.empty()) om.set_field(pfx+".optional",fo); | 45 | if(!fo.empty()) om.set_field(pfx+".optional",fo); |
46 | if(!policy_url.empty()) om.set_field(pfx+".policy_url",policy_url); | 46 | if(!policy_url.empty()) om.set_field(pfx+".policy_url",policy_url); |
47 | } | 47 | } |
48 | 48 | ||
49 | void sreg_t::id_res_hook(const basic_openid_message& om,const basic_openid_message& sp) { | 49 | void sreg_t::checkid_hook(basic_openid_message& om) { |
50 | rp_checkid_hook(om); } | ||
51 | |||
52 | void sreg_t::rp_id_res_hook(const basic_openid_message& om, | ||
53 | const basic_openid_message& sp) { | ||
50 | clear(); | 54 | clear(); |
51 | string pfx; | 55 | string pfx; |
52 | try { | 56 | try { |
53 | pfx = om.find_ns(OIURI_SREG11,"sreg"); | 57 | pfx = om.find_ns(OIURI_SREG11,"sreg"); |
54 | }catch(failed_lookup& fl) { | 58 | }catch(failed_lookup& fl) { |
55 | try { | 59 | try { |
56 | pfx = om.find_ns(OIURI_SREG10,"sreg"); | 60 | pfx = om.find_ns(OIURI_SREG10,"sreg"); |
57 | }catch(failed_lookup& fl) { | 61 | }catch(failed_lookup& fl) { |
58 | return; | 62 | return; |
59 | } | 63 | } |
60 | } | 64 | } |
61 | pfx += '.'; | 65 | pfx += '.'; |
62 | for(fields_iterator f=fields_BEGIN;f<fields_END;++f) { | 66 | for(fields_iterator f=fields_BEGIN;f<fields_END;++f) { |
63 | string fn = pfx; fn+=f->fieldname; | 67 | string fn = pfx; fn+=f->fieldname; |
64 | if(!sp.has_field(fn)) continue; | 68 | if(!sp.has_field(fn)) continue; |
65 | has_fields |= f->fieldbit; | 69 | has_fields |= f->fieldbit; |
66 | response[f->fieldbit]=sp.get_field(fn); | 70 | response[f->fieldbit]=sp.get_field(fn); |
67 | } | 71 | } |
68 | } | 72 | } |
69 | 73 | ||
74 | void sreg_t::id_res_hook(const basic_openid_message& om, | ||
75 | const basic_openid_message& sp) { | ||
76 | rp_id_res_hook(om,sp); } | ||
77 | |||
70 | const string& sreg_t::get_field(fieldbit_t fb) const { | 78 | const string& sreg_t::get_field(fieldbit_t fb) const { |
71 | response_t::const_iterator i = response.find(fb); | 79 | response_t::const_iterator i = response.find(fb); |
72 | if(i==response.end()) | 80 | if(i==response.end()) |
73 | throw failed_lookup(OPKELE_CP_ "no field data available"); | 81 | throw failed_lookup(OPKELE_CP_ "no field data available"); |
74 | return i->second; | 82 | return i->second; |
75 | } | 83 | } |
76 | 84 | ||
77 | void sreg_t::set_field(fieldbit_t fb,const string& fv) { | 85 | void sreg_t::set_field(fieldbit_t fb,const string& fv) { |
78 | response[fb] = fv; | 86 | response[fb] = fv; |
79 | has_fields |= fb; | 87 | has_fields |= fb; |
80 | } | 88 | } |
81 | 89 | ||
82 | void sreg_t::reset_field(fieldbit_t fb) { | 90 | void sreg_t::reset_field(fieldbit_t fb) { |
83 | has_fields &= ~fb; | 91 | has_fields &= ~fb; |
84 | response.erase(fb); | 92 | response.erase(fb); |
85 | } | 93 | } |
86 | 94 | ||
87 | void sreg_t::clear() { | 95 | void sreg_t::clear() { |
88 | has_fields = 0; response.clear(); | 96 | has_fields = 0; response.clear(); |
89 | } | 97 | } |
90 | 98 | ||
91 | static long fields_list_to_bitmask(string& fl) { | 99 | static long fields_list_to_bitmask(string& fl) { |
92 | long rv = 0; | 100 | long rv = 0; |
93 | while(!fl.empty()) { | 101 | while(!fl.empty()) { |
94 | string::size_type co = fl.find(','); | 102 | string::size_type co = fl.find(','); |
95 | string fn; | 103 | string fn; |
96 | if(co==string::npos) { | 104 | if(co==string::npos) { |
97 | fn = fl; fl.erase(); | 105 | fn = fl; fl.erase(); |
98 | }else{ | 106 | }else{ |
99 | fn = fl.substr(0,co); fl.erase(0,co+1); | 107 | fn = fl.substr(0,co); fl.erase(0,co+1); |
100 | } | 108 | } |
101 | fields_iterator f = find(fields_BEGIN,fields_END,fn); | 109 | fields_iterator f = find(fields_BEGIN,fields_END,fn); |
102 | if(f!=fields_END) | 110 | if(f!=fields_END) |
103 | rv |= f->fieldbit; | 111 | rv |= f->fieldbit; |
104 | } | 112 | } |
105 | return rv; | 113 | return rv; |
106 | } | 114 | } |
107 | 115 | ||
108 | void sreg_t::checkid_hook(const basic_openid_message& inm,basic_openid_message& oum) { | 116 | void sreg_t::op_checkid_hook(const basic_openid_message& inm) { |
109 | string ins = inm.find_ns(OIURI_SREG11,"sreg"); | 117 | string ins = inm.find_ns(OIURI_SREG11,"sreg"); |
110 | fields_optional = 0; fields_required = 0; policy_url.erase(); | 118 | fields_optional = 0; fields_required = 0; policy_url.erase(); |
111 | fields_response = 0; | 119 | fields_response = 0; |
112 | try { | 120 | try { |
113 | string fl = inm.get_field(ins+".required"); | 121 | string fl = inm.get_field(ins+".required"); |
114 | fields_required = fields_list_to_bitmask(fl); | 122 | fields_required = fields_list_to_bitmask(fl); |
115 | }catch(failed_lookup&) { } | 123 | }catch(failed_lookup&) { } |
116 | try { | 124 | try { |
117 | string fl = inm.get_field(ins+".optional"); | 125 | string fl = inm.get_field(ins+".optional"); |
118 | fields_optional = fields_list_to_bitmask(fl); | 126 | fields_optional = fields_list_to_bitmask(fl); |
119 | }catch(failed_lookup&) { } | 127 | }catch(failed_lookup&) { } |
120 | try { | 128 | try { |
121 | policy_url = inm.get_field(ins+".policy_url"); | 129 | policy_url = inm.get_field(ins+".policy_url"); |
122 | }catch(failed_lookup&) { } | 130 | }catch(failed_lookup&) { } |
123 | setup_response(inm,oum); | 131 | } |
132 | |||
133 | void sreg_t::op_id_res_hook(basic_openid_message& oum) { | ||
124 | string ons = oum.allocate_ns(OIURI_SREG11,"sreg"); | 134 | string ons = oum.allocate_ns(OIURI_SREG11,"sreg"); |
125 | fields_response &= has_fields; | 135 | fields_response &= has_fields; |
126 | string signeds = "ns."+ons; | 136 | string signeds = "ns."+ons; |
127 | for(fields_iterator f=fields_BEGIN;f<fields_END;++f) { | 137 | for(fields_iterator f=fields_BEGIN;f<fields_END;++f) { |
128 | if(!(f->fieldbit&fields_response)) continue; | 138 | if(!(f->fieldbit&fields_response)) continue; |
129 | signeds +=','; | 139 | signeds +=','; |
130 | string pn = ons; pn += '.'; pn += f->fieldname; | 140 | string pn = ons; pn += '.'; pn += f->fieldname; |
131 | signeds += pn; | 141 | signeds += pn; |
132 | oum.set_field(pn,get_field(f->fieldbit)); | 142 | oum.set_field(pn,get_field(f->fieldbit)); |
133 | } | 143 | } |
134 | oum.add_to_signed(signeds); | 144 | oum.add_to_signed(signeds); |
135 | } | 145 | } |
136 | 146 | ||
147 | void sreg_t::checkid_hook(const basic_openid_message& inm, | ||
148 | basic_openid_message& oum) { | ||
149 | op_checkid_hook(inm); | ||
150 | setup_response(inm,oum); | ||
151 | op_id_res_hook(oum); | ||
152 | } | ||
153 | |||
137 | void sreg_t::setup_response(const basic_openid_message& /* inm */,basic_openid_message& /* oum */) { | 154 | void sreg_t::setup_response(const basic_openid_message& /* inm */,basic_openid_message& /* oum */) { |
155 | setup_response(); | ||
156 | } | ||
157 | void sreg_t::setup_response() { | ||
138 | fields_response = (fields_required|fields_optional)&has_fields; | 158 | fields_response = (fields_required|fields_optional)&has_fields; |
139 | } | 159 | } |
140 | } | 160 | } |