saml20/client.c

Go to the documentation of this file.
00001 /* client.c --- SAML20 mechanism, client side.
00002  * Copyright (C) 2010  Simon Josefsson
00003  *
00004  * This file is part of GNU SASL Library.
00005  *
00006  * GNU SASL Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * GNU SASL Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with GNU SASL Library; if not, write to the Free
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 # include "config.h"
00025 #endif
00026 
00027 /* Get specification. */
00028 #include "saml20.h"
00029 
00030 /* Get strdup, strlen. */
00031 #include <string.h>
00032 
00033 /* Get free. */
00034 #include <stdlib.h>
00035 
00036 /* Get bool. */
00037 #include <stdbool.h>
00038 
00039 /* Get _gsasl_gs2_generate_header. */
00040 #include "mechtools.h"
00041 
00042 struct saml20_client_state
00043 {
00044   int step;
00045 };
00046 
00047 int
00048 _gsasl_saml20_client_start (Gsasl_session * sctx, void **mech_data)
00049 {
00050   struct saml20_client_state *state;
00051 
00052   state = (struct saml20_client_state *) calloc (sizeof (*state), 1);
00053   if (state == NULL)
00054     return GSASL_MALLOC_ERROR;
00055 
00056   *mech_data = state;
00057 
00058   return GSASL_OK;
00059 }
00060 
00061 int
00062 _gsasl_saml20_client_step (Gsasl_session * sctx,
00063                            void *mech_data,
00064                            const char *input, size_t input_len,
00065                            char **output, size_t * output_len)
00066 {
00067   struct saml20_client_state *state = mech_data;
00068   int res = GSASL_MECHANISM_CALLED_TOO_MANY_TIMES;
00069 
00070   switch (state->step)
00071     {
00072     case 0:
00073       {
00074         const char *authzid = gsasl_property_get (sctx, GSASL_AUTHZID);
00075         const char *p;
00076         int len;
00077 
00078         p = gsasl_property_get (sctx, GSASL_SAML20_IDP_IDENTIFIER);
00079         if (!p || !*p)
00080           return GSASL_NO_SAML20_IDP_IDENTIFIER;
00081 
00082         res = _gsasl_gs2_generate_header (false, 'n', NULL, authzid,
00083                                           strlen (p), p,
00084                                           output, output_len);
00085         if (res == GSASL_OK)
00086           return res;
00087 
00088         res = GSASL_NEEDS_MORE;
00089         state->step++;
00090         break;
00091       }
00092 
00093     case 1:
00094       {
00095         gsasl_property_set_raw (sctx, GSASL_SAML20_REDIRECT_URL,
00096                                 input, input_len);
00097 
00098         res = gsasl_callback (NULL, sctx,
00099                               GSASL_SAML20_AUTHENTICATE_IN_BROWSER);
00100         if (res == GSASL_OK)
00101           {
00102             *output_len = 0;
00103             *output = NULL;
00104           }
00105 
00106         state->step++;
00107       }
00108       break;
00109 
00110     default:
00111       break;
00112     }
00113 
00114   return res;
00115 }
00116 
00117 void
00118 _gsasl_saml20_client_finish (Gsasl_session * sctx, void *mech_data)
00119 {
00120   struct saml20_client_state *state = mech_data;
00121 
00122   if (!state)
00123     return;
00124 
00125   free (state);
00126 }