~ubuntu-branches/ubuntu/utopic/lasso/utopic-proposed

« back to all changes in this revision

Viewing changes to lasso/environs/lecp.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2004-09-13 09:26:34 UTC
  • Revision ID: james.westby@ubuntu.com-20040913092634-01vdfl8j9cp94exa
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: lecp.c,v 1.18 2004/09/01 09:59:53 fpeters Exp $
 
2
 *
 
3
 * Lasso - A free implementation of the Liberty Alliance specifications.
 
4
 *
 
5
 * Copyright (C) 2004 Entr'ouvert
 
6
 * http://lasso.entrouvert.org
 
7
 * 
 
8
 * Authors: Nicolas Clapies <nclapies@entrouvert.com>
 
9
 *          Valery Febvre <vfebvre@easter-eggs.com>
 
10
 *
 
11
 * This program is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2 of the License, or
 
14
 * (at your option) any later version.
 
15
 * 
 
16
 * This program is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 * 
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
24
 */
 
25
 
 
26
#include <lasso/environs/lecp.h>
 
27
 
 
28
static GObjectClass *parent_class = NULL;
 
29
 
 
30
/*****************************************************************************/
 
31
/* public methods                                                            */
 
32
/*****************************************************************************/
 
33
 
 
34
gint
 
35
lasso_lecp_build_authn_request_envelope_msg(LassoLecp *lecp)
 
36
{
 
37
  LassoProfile *profile;
 
38
  gchar *assertionConsumerServiceURL;
 
39
 
 
40
  g_return_val_if_fail(LASSO_IS_LECP(lecp), -1);
 
41
 
 
42
  profile = LASSO_PROFILE(lecp);
 
43
 
 
44
  assertionConsumerServiceURL = lasso_provider_get_assertionConsumerServiceURL(LASSO_PROVIDER(profile->server),
 
45
                                                                               lassoProviderTypeSp,
 
46
                                                                               NULL);
 
47
  if (assertionConsumerServiceURL == NULL) {
 
48
    message(G_LOG_LEVEL_CRITICAL, "AssertionConsumerServiceURL not found\n");
 
49
    return -1;
 
50
  }
 
51
 
 
52
  if (profile->request == NULL) {
 
53
    message(G_LOG_LEVEL_CRITICAL, "AuthnRequest not found\n");
 
54
    return -1;
 
55
  }
 
56
 
 
57
  lecp->authnRequestEnvelope = lasso_authn_request_envelope_new(LASSO_AUTHN_REQUEST(profile->request),
 
58
                                                                profile->server->providerID,
 
59
                                                                assertionConsumerServiceURL);
 
60
  if (lecp->authnRequestEnvelope == NULL) {
 
61
    message(G_LOG_LEVEL_CRITICAL, "Error while building AuthnRequestEnvelope\n");
 
62
    return -1;
 
63
  }
 
64
 
 
65
  profile->msg_body = lasso_node_export(lecp->authnRequestEnvelope);
 
66
  if (profile->msg_body == NULL) {
 
67
    message(G_LOG_LEVEL_CRITICAL, "Error while exporting the AuthnRequestEnvelope to POST msg\n");
 
68
    return -1;
 
69
  }
 
70
 
 
71
  return 0;
 
72
}
 
73
 
 
74
gint
 
75
lasso_lecp_build_authn_request_msg(LassoLecp   *lecp,
 
76
                                   const gchar *remote_providerID)
 
77
{
 
78
  LassoProfile *profile;
 
79
  LassoProvider *remote_provider;
 
80
 
 
81
  g_return_val_if_fail(LASSO_IS_LECP(lecp), -1);
 
82
 
 
83
  profile = LASSO_PROFILE(lecp);
 
84
 
 
85
  LASSO_PROFILE(lecp)->remote_providerID = g_strdup(remote_providerID);
 
86
  remote_provider = lasso_server_get_provider_ref(LASSO_PROFILE(lecp)->server,
 
87
                                                  LASSO_PROFILE(lecp)->remote_providerID,
 
88
                                                  NULL);
 
89
 
 
90
  profile->msg_url  = lasso_provider_get_singleSignOnServiceURL(remote_provider, NULL);
 
91
  profile->msg_body = lasso_node_export_to_soap(profile->request);
 
92
  if (profile->msg_body == NULL) {
 
93
    message(G_LOG_LEVEL_CRITICAL, "Error while building the AuthnRequest SOAP message\n");
 
94
    return -1;
 
95
  }
 
96
 
 
97
  return 0;
 
98
}
 
99
 
 
100
gint
 
101
lasso_lecp_build_authn_response_msg(LassoLecp   *lecp)
 
102
{
 
103
  LassoProfile *profile;
 
104
 
 
105
  g_return_val_if_fail(LASSO_IS_LECP(lecp), -1);
 
106
 
 
107
  profile = LASSO_PROFILE(lecp);
 
108
  profile->msg_url = g_strdup(lecp->assertionConsumerServiceURL);
 
109
  if (profile->msg_url == NULL) {
 
110
    message(G_LOG_LEVEL_CRITICAL, "AssertionConsumerServiceURL not found\n");
 
111
    return -1;
 
112
  }
 
113
  profile->msg_body = lasso_node_export_to_base64(profile->response);
 
114
  if (profile->msg_body == NULL) {
 
115
    message(G_LOG_LEVEL_CRITICAL, "AuthnResponse Base64 msg not found\n");
 
116
    return -1;
 
117
  }
 
118
 
 
119
  return 0;
 
120
}
 
121
 
 
122
gint
 
123
lasso_lecp_build_authn_response_envelope_msg(LassoLecp   *lecp,
 
124
                                             gint         authentication_result,
 
125
                                             const gchar *authenticationMethod,
 
126
                                             const gchar *reauthenticateOnOrAfter)
 
127
{
 
128
  LassoProfile  *profile;
 
129
  LassoProvider *provider;
 
130
  gchar         *assertionConsumerServiceURL;
 
131
 
 
132
  g_return_val_if_fail(LASSO_IS_LECP(lecp), -1);
 
133
 
 
134
  profile = LASSO_PROFILE(lecp);
 
135
 
 
136
  if (LASSO_IS_AUTHN_RESPONSE(profile->response) == FALSE) {
 
137
    message(G_LOG_LEVEL_CRITICAL, "AuthnResponse not found\n");
 
138
    return -1;
 
139
  }
 
140
 
 
141
  provider = lasso_server_get_provider_ref(profile->server,
 
142
                                           profile->remote_providerID,
 
143
                                           NULL);
 
144
  if (provider == NULL) {
 
145
    message(G_LOG_LEVEL_CRITICAL, "Provider %s not found\n", profile->remote_providerID);
 
146
    return -1;
 
147
  }
 
148
 
 
149
  /* build lib:AuthnResponse */
 
150
  lasso_login_build_authn_response_msg(LASSO_LOGIN(lecp),
 
151
                                       authentication_result,
 
152
                                       authenticationMethod,
 
153
                                       reauthenticateOnOrAfter);
 
154
  
 
155
  assertionConsumerServiceURL = lasso_provider_get_assertionConsumerServiceURL(provider,
 
156
                                                                               lassoProviderTypeSp,
 
157
                                                                               NULL);
 
158
  if (assertionConsumerServiceURL == NULL) {
 
159
    message(G_LOG_LEVEL_CRITICAL, "AssertionConsumerServiceURL not found\n");
 
160
    return -1;
 
161
  }
 
162
 
 
163
  xmlFree(LASSO_PROFILE(lecp)->msg_body);
 
164
  LASSO_PROFILE(lecp)->msg_body = NULL;
 
165
  xmlFree(LASSO_PROFILE(lecp)->msg_url);
 
166
  LASSO_PROFILE(lecp)->msg_url = NULL;
 
167
  lecp->authnResponseEnvelope = lasso_authn_response_envelope_new(LASSO_AUTHN_RESPONSE(profile->response),
 
168
                                                                  assertionConsumerServiceURL);
 
169
  LASSO_PROFILE(lecp)->msg_body = lasso_node_export_to_soap(lecp->authnResponseEnvelope);
 
170
 
 
171
  if (LASSO_PROFILE(lecp)->msg_body == NULL) {
 
172
    message(G_LOG_LEVEL_CRITICAL, "Error while exporting the AuthnResponseEnvelope to SOAP msg\n");
 
173
    return -1;
 
174
  }
 
175
 
 
176
  return 0;
 
177
}
 
178
 
 
179
gint
 
180
lasso_lecp_init_authn_request(LassoLecp *lecp)
 
181
{
 
182
  gint res;
 
183
 
 
184
  g_return_val_if_fail(LASSO_IS_LECP(lecp), -1);
 
185
 
 
186
  /* FIXME : BAD usage of http_method
 
187
     using POST method so that the lib:AuthnRequest is initialize with
 
188
     a signature template */
 
189
  res = lasso_login_init_authn_request(LASSO_LOGIN(lecp), lassoHttpMethodPost);
 
190
 
 
191
  return res;
 
192
}
 
193
 
 
194
gint
 
195
lasso_lecp_init_from_authn_request_msg(LassoLecp       *lecp,
 
196
                                       gchar           *authn_request_msg,
 
197
                                       lassoHttpMethod  authn_request_method)
 
198
{
 
199
  gint res;
 
200
 
 
201
  g_return_val_if_fail(LASSO_IS_LECP(lecp), -1);
 
202
  g_return_val_if_fail(authn_request_msg!=NULL, -1);
 
203
 
 
204
  if (authn_request_method != lassoHttpMethodSoap) {
 
205
    message(G_LOG_LEVEL_CRITICAL, "Invalid authentication request method\n");
 
206
    return -1;
 
207
  }
 
208
  res = lasso_login_init_from_authn_request_msg(LASSO_LOGIN(lecp), authn_request_msg, authn_request_method);
 
209
  return res;
 
210
}
 
211
 
 
212
gint
 
213
lasso_lecp_process_authn_request_envelope_msg(LassoLecp *lecp,
 
214
                                              gchar     *request_msg)
 
215
{
 
216
  g_return_val_if_fail(LASSO_IS_LECP(lecp), -1);
 
217
  g_return_val_if_fail(request_msg!=NULL, -1);
 
218
 
 
219
  lecp->authnRequestEnvelope = lasso_authn_request_envelope_new_from_export(request_msg, lassoNodeExportTypeXml);
 
220
  if (lecp->authnRequestEnvelope == NULL) {
 
221
    message(G_LOG_LEVEL_CRITICAL, "Error while building the authentication request envelope\n");
 
222
    return -1;
 
223
  }
 
224
 
 
225
  LASSO_PROFILE(lecp)->request = lasso_authn_request_envelope_get_authnRequest(LASSO_AUTHN_REQUEST_ENVELOPE(lecp->authnRequestEnvelope));
 
226
  if (LASSO_PROFILE(lecp)->request == NULL) {
 
227
    message(G_LOG_LEVEL_CRITICAL, "AuthnRequest not found\n");
 
228
    return -1;
 
229
  }
 
230
 
 
231
  return 0;
 
232
}
 
233
 
 
234
gint
 
235
lasso_lecp_process_authn_response_envelope_msg(LassoLecp *lecp,
 
236
                                               gchar      *response_msg)
 
237
{
 
238
  LassoProfile *profile;
 
239
 
 
240
  g_return_val_if_fail(LASSO_IS_LECP(lecp), -1);
 
241
  g_return_val_if_fail(response_msg!=NULL, -2);
 
242
 
 
243
  profile = LASSO_PROFILE(lecp);
 
244
 
 
245
  lecp->authnResponseEnvelope = lasso_authn_response_envelope_new_from_export(response_msg, lassoNodeExportTypeSoap);
 
246
  if (lecp->authnResponseEnvelope == NULL) {
 
247
    message(G_LOG_LEVEL_CRITICAL, "Error while building AuthnResponseEnvelope\n");
 
248
    return -1;
 
249
  }
 
250
 
 
251
  profile->response = lasso_authn_response_envelope_get_authnResponse(LASSO_AUTHN_RESPONSE_ENVELOPE(lecp->authnResponseEnvelope));
 
252
  if (profile->response == NULL) {
 
253
    message(G_LOG_LEVEL_CRITICAL, "AuthnResponse not found\n");
 
254
    return -1;
 
255
  }
 
256
 
 
257
  lecp->assertionConsumerServiceURL = lasso_authn_response_envelope_get_assertionConsumerServiceURL(
 
258
    LASSO_AUTHN_RESPONSE_ENVELOPE(lecp->authnResponseEnvelope));
 
259
  if (lecp->assertionConsumerServiceURL == NULL){
 
260
    message(G_LOG_LEVEL_CRITICAL, "AssertionConsumerServiceURL not found\n");
 
261
    return -1;
 
262
  }
 
263
 
 
264
  return 0;
 
265
}
 
266
 
 
267
void
 
268
lasso_lecp_destroy(LassoLecp *lecp)
 
269
{
 
270
  g_object_unref(G_OBJECT(lecp));
 
271
}
 
272
 
 
273
 
 
274
/*****************************************************************************/
 
275
/* overrided parent class methods                                            */
 
276
/*****************************************************************************/
 
277
 
 
278
static void
 
279
lasso_lecp_finalize(LassoLecp *lecp)
 
280
{  
 
281
  debug("Lecp object 0x%x finalized ...\n", lecp);
 
282
 
 
283
  parent_class->finalize(G_OBJECT(lecp));
 
284
}
 
285
 
 
286
/*****************************************************************************/
 
287
/* instance and class init functions                                         */
 
288
/*****************************************************************************/
 
289
 
 
290
static void
 
291
lasso_lecp_instance_init(LassoLecp *lecp)
 
292
{
 
293
  lecp->authnRequestEnvelope        = NULL;
 
294
  lecp->authnResponseEnvelope       = NULL;
 
295
  lecp->assertionConsumerServiceURL = NULL;
 
296
}
 
297
 
 
298
static void
 
299
lasso_lecp_class_init(LassoLecpClass *class)
 
300
{
 
301
  GObjectClass *gobject_class = G_OBJECT_CLASS(class);
 
302
  
 
303
  parent_class = g_type_class_peek_parent(class);
 
304
  /* override parent class methods */
 
305
  gobject_class->finalize = (void *)lasso_lecp_finalize;
 
306
}
 
307
 
 
308
GType lasso_lecp_get_type() {
 
309
  static GType this_type = 0;
 
310
 
 
311
  if (!this_type) {
 
312
    static const GTypeInfo this_info = {
 
313
      sizeof (LassoLecpClass),
 
314
      NULL,
 
315
      NULL,
 
316
      (GClassInitFunc) lasso_lecp_class_init,
 
317
      NULL,
 
318
      NULL,
 
319
      sizeof(LassoLecp),
 
320
      0,
 
321
      (GInstanceInitFunc) lasso_lecp_instance_init,
 
322
    };
 
323
    
 
324
    this_type = g_type_register_static(LASSO_TYPE_LOGIN,
 
325
                                       "LassoLecp",
 
326
                                       &this_info, 0);
 
327
  }
 
328
  return this_type;
 
329
}
 
330
 
 
331
LassoLecp *
 
332
lasso_lecp_new(LassoServer *server)
 
333
{
 
334
  LassoLecp *lecp;
 
335
 
 
336
  lecp = g_object_new(LASSO_TYPE_LECP, NULL);
 
337
 
 
338
  if (LASSO_IS_SERVER(server)) {
 
339
    debug("Add server to lecp object\n");
 
340
    LASSO_PROFILE(lecp)->server = lasso_server_copy(server);
 
341
  }
 
342
      
 
343
 
 
344
  return lecp;
 
345
}