br.ufsc.das.gtscted.shibbauth.ShibAuthenticationActivity.java Source code

Java tutorial

Introduction

Here is the source code for br.ufsc.das.gtscted.shibbauth.ShibAuthenticationActivity.java

Source

/**
 *  Copyright (C) 2011 GT-STCFed - RNP - http://gtstcfed.das.ufsc.br
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright (C) 2011 GT-STCFed - RNP - http://gtstcfed.das.ufsc.br
 *
 *  Este programa  software livre; voc pode redistribu-lo e/ou
 *  modific-lo sob os termos da Licena Pblica Geral GNU, conforme
 *  publicada pela Free Software Foundation; tanto a verso 2 da
 *  Licena como (a seu critrio) qualquer verso mais nova.
 *
 *  Este programa  distribudo na expectativa de ser til, mas SEM
 *  QUALQUER GARANTIA; sem mesmo a garantia implcita de
 *  COMERCIALIZAO ou de ADEQUAO A QUALQUER PROPSITO EM
 *  PARTICULAR. Consulte a Licena Pblica Geral GNU para obter mais
 *  detalhes.
 *
 *  Voc deve ter recebido uma cpia da Licena Pblica Geral GNU
 *  junto com este programa; se no, escreva para a Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307, USA.
*/
package br.ufsc.das.gtscted.shibbauth;

import java.io.IOException;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import br.ufsc.das.gtscted.shibbauth.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class ShibAuthenticationActivity extends Activity {

    private EditText usernameTxt;
    private EditText passwordTxt;
    private Button loginButton;
    private Button backButton;
    private Spinner idpSpinner;
    private Elements idpElements;
    private Elements formElements;
    private String wayfActionPath;
    private String selectedIdpUrl;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.idp_selection);
        loginButton = (Button) findViewById(R.id.loginButton);
        backButton = (Button) findViewById(R.id.backButton);
        usernameTxt = (EditText) findViewById(R.id.usernameTxt);
        passwordTxt = (EditText) findViewById(R.id.passwordTxt);
        idpSpinner = (Spinner) findViewById(R.id.idpSpinner);

        //Configura o ArrayAdapter do spinner.
        ArrayAdapter<CharSequence> spinnerArrayAdapter;
        spinnerArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        idpSpinner.setAdapter(spinnerArrayAdapter);

        // Obtm os parmetros passados pela Activity anterior 
        // (no caso, a pgina do WAYF como uma String e o
        // nico cookie da Connection usada anteriormente)
        Bundle bundle = this.getIntent().getExtras();
        String wayfHtml = bundle.getString("html_source");
        final String wayfLocation = bundle.getString("wayf_location");
        final SerializableCookie receivedCookie = (SerializableCookie) bundle.getSerializable("cookie");

        //Obtm todos os tags de nome "option", que correspondem
        // aos IdPs, da pgina do WAYF.
        Document wayfDocument = Jsoup.parse(wayfHtml);
        idpElements = wayfDocument.select("option");

        //Popula o spinner com os nomes dos IdPs encontrados.      
        for (Element idpElement : idpElements) {
            String idpName = idpElement.text();
            spinnerArrayAdapter.add(idpName);
        }

        // Obtm o caminho para o qual deve ser passado o IdP do usurio.
        formElements = wayfDocument.select("form");
        for (Element formElement : formElements) {
            if (formElement.attr("id").equals("IdPList")) {
                wayfActionPath = formElement.attr("action");
            }
        }

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Obtm a URL correspondente ao idP selecionado no spinner.
                int selectedIdpPosition = idpSpinner.getSelectedItemPosition();
                Element selectedIdp = idpElements.get(selectedIdpPosition);
                selectedIdpUrl = selectedIdp.attr("value");

                try {
                    // Obtm os campos "username" e "password" fornecidos
                    // pelo usurio e necessrios para a autenticao.
                    String username = usernameTxt.getText().toString();
                    String password = passwordTxt.getText().toString();

                    // Cria um novo objeto Connection, e adiciona o 
                    // cookie passado pela Activity anterior.
                    Connection connection = new Connection();
                    BasicClientCookie newCookie = new BasicClientCookie(receivedCookie.getName(),
                            receivedCookie.getValue());
                    newCookie.setDomain(receivedCookie.getDomain());
                    connection.addCookie(newCookie);

                    // Tenta realizar a autenticao no IdP selecionado. O resultado corresponde
                    //  pgina para a qual o cliente  redirecionado em caso de autenticao 
                    // bem-sucedida.
                    String authResult = connection.authenticate(wayfLocation, wayfActionPath, selectedIdpUrl,
                            username, password);

                    // Apenas mostra o recurso que o usurio queria acessar (neste caso, mostra a pg. de
                    // "Homologao de atributos").
                    Intent newIntent = new Intent(ShibAuthenticationActivity.this.getApplicationContext(),
                            TestActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("arg", authResult);
                    newIntent.putExtras(bundle);
                    startActivity(newIntent);

                } catch (IOException e) {
                    String message = "IOException - problema na conexo";
                    Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
                    toast.show();
                } catch (Exception e) {
                    String message = "Exception - problema na autenticao";
                    Toast toast = Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG);
                    toast.show();
                }
            }
        });

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}