gsasl  1.8.0
saml20/client.c
Go to the documentation of this file.
00001 /* client.c --- SAML20 mechanism, client side.
00002  * Copyright (C) 2010-2012 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 *idp =
00076           gsasl_property_get (sctx, GSASL_SAML20_IDP_IDENTIFIER);
00077 
00078         if (!idp || !*idp)
00079           return GSASL_NO_SAML20_IDP_IDENTIFIER;
00080 
00081         res = _gsasl_gs2_generate_header (false, 'n', NULL, authzid,
00082                                           strlen (idp), idp,
00083                                           output, output_len);
00084         if (res != GSASL_OK)
00085           return res;
00086 
00087         res = GSASL_NEEDS_MORE;
00088         state->step++;
00089       }
00090       break;
00091 
00092     case 1:
00093       {
00094         gsasl_property_set_raw (sctx, GSASL_SAML20_REDIRECT_URL,
00095                                 input, input_len);
00096 
00097         res = gsasl_callback (NULL, sctx,
00098                               GSASL_SAML20_AUTHENTICATE_IN_BROWSER);
00099         if (res != GSASL_OK)
00100           return res;
00101 
00102         *output_len = 1;
00103         *output = strdup ("=");
00104         if (!*output)
00105           return GSASL_MALLOC_ERROR;
00106 
00107         res = GSASL_OK;
00108         state->step++;
00109       }
00110       break;
00111 
00112     default:
00113       break;
00114     }
00115 
00116   return res;
00117 }
00118 
00119 void
00120 _gsasl_saml20_client_finish (Gsasl_session * sctx, void *mech_data)
00121 {
00122   struct saml20_client_state *state = mech_data;
00123 
00124   if (!state)
00125     return;
00126 
00127   free (state);
00128 }