Android Open Source - sms-backup-plus X O Auth Consumer






From Project

Back to project page sms-backup-plus.

License

The source code is released under:

Apache License

If you think the Android project sms-backup-plus listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2010 Jan Berkel <jan.berkel@gmail.com>
 */*w  ww.  j  av a  2  s  .  c om*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zegoggles.smssync.auth;

import android.content.Context;
import android.util.Log;
import com.zegoggles.smssync.R;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthException;
import oauth.signpost.http.HttpParameters;
import oauth.signpost.http.HttpRequest;
import oauth.signpost.signature.SignatureBaseString;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedSet;

import static com.zegoggles.smssync.App.TAG;
import static oauth.signpost.OAuth.ENCODING;
import static oauth.signpost.OAuth.OAUTH_SIGNATURE;
import static oauth.signpost.OAuth.percentEncode;

public class XOAuthConsumer extends CommonsHttpOAuthConsumer {
    private static final String MAC_NAME = "HmacSHA1";
    private static final String ANONYMOUS = "anonymous";

    // Scopes as defined in http://code.google.com/apis/accounts/docs/OAuth.html#prepScope
    private static final String GMAIL_SCOPE = "https://mail.google.com/";
    private static final String CONTACTS_SCOPE = "https://www.google.com/m8/feeds/";
    private static final String DEFAULT_SCOPE  = GMAIL_SCOPE + " " + CONTACTS_SCOPE;

    // endpoints
    private static final String CONTACTS_URL = "https://www.google.com/m8/feeds/contacts/default/thin?max-results=1";
    private static final String REQUEST_TOKEN_URL = "https://www.google.com/accounts/OAuthGetRequestToken" +
            "?scope=%s&xoauth_displayname=%s";
    private static final String ACCESS_TOKEN_ENDPOINT_URL = "https://www.google.com/accounts/OAuthGetAccessToken";
    private static final String AUTHORIZE_TOKEN_URL       = "https://www.google.com/accounts/OAuthAuthorizeToken?btmpl=mobile";

    private String mUsername;

    public XOAuthConsumer(String username) {
        super(ANONYMOUS, ANONYMOUS);
        this.mUsername = username;
    }

    public XOAuthConsumer(String username, String token, String secret) {
        this(username);
        setTokenWithSecret(token, secret);
    }

    public String generateXOAuthString() {
        return generateXOAuthString(mUsername);
    }

    public String generateXOAuthString(final String username) {
        if (username == null) throw new IllegalArgumentException("username is null");

        try {
            final URI uri = new URI(String.format("https://mail.google.com/mail/b/%s/imap/", urlEncode(username)));
            final HttpRequest request = wrap(new HttpGet(uri));
            final HttpParameters requestParameters = new HttpParameters();

            completeOAuthParameters(requestParameters);

            StringBuilder sasl = new StringBuilder()
                    .append("GET ")
                    .append(uri.toString())
                    .append(" ");

            requestParameters.put(OAUTH_SIGNATURE, generateSig(request, requestParameters), true);

            Iterator<Map.Entry<String, SortedSet<String>>> it = requestParameters.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, SortedSet<String>> e = it.next();

                sasl.append(e.getKey()).append("=\"").append(
                        e.getValue().iterator().next()
                ).append("\"");

                if (it.hasNext()) {
                    sasl.append(",");
                }
            }

            return base64(sasl.toString().getBytes(ENCODING));
        } catch (URISyntaxException e) {
            throw new IllegalArgumentException(e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public CommonsHttpOAuthProvider getProvider(Context context) {
        //System.setProperty("debug", "true");
        return new CommonsHttpOAuthProvider(
                requestTokenEndpointUrl(context),
                ACCESS_TOKEN_ENDPOINT_URL,
                AUTHORIZE_TOKEN_URL) {
            {
                setOAuth10a(true);
            }
        };
    }

    private String requestTokenEndpointUrl(Context context) {
        return String.format(REQUEST_TOKEN_URL,
            urlEncode(DEFAULT_SCOPE),
            urlEncode(context.getString(R.string.app_name)));
    }

    public String getUsername() {
        return mUsername;
    }

    public String loadUsernameFromContacts() {
        this.mUsername = getUsernameFromContacts();
        return this.mUsername;
    }

    // Retrieves the google email account address using the contacts API
    protected String getUsernameFromContacts() {
        final HttpClient httpClient = new DefaultHttpClient();

        try {
            HttpGet get = new HttpGet(sign(CONTACTS_URL));
            return extractEmail(httpClient.execute(get));
        } catch (OAuthException e) {
            Log.e(TAG, "error", e);
            return null;
        } catch (SAXException e) {
            Log.e(TAG, "error", e);
            return null;
        } catch (IOException e) {
            Log.e(TAG, "error", e);
            return null;
        } catch (ParserConfigurationException e) {
            Log.e(TAG, "error", e);
            return null;
        }
    }

    private String extractEmail(HttpResponse response) throws ParserConfigurationException, SAXException, IOException {
        final XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
        final FeedHandler feedHandler = new FeedHandler();
        xmlReader.setContentHandler(feedHandler);
        xmlReader.parse(new InputSource(response.getEntity().getContent()));
        return feedHandler.getEmail();
    }

    private String urlEncode(String s) {
        try {
            return URLEncoder.encode(s, "utf-8");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private String generateSig(HttpRequest request, HttpParameters requestParameters) throws Exception {
        String keyString = percentEncode(getConsumerSecret()) + '&' + percentEncode(getTokenSecret());

        SecretKey key = new SecretKeySpec(keyString.getBytes(ENCODING), MAC_NAME);
        Mac mac = Mac.getInstance(MAC_NAME);
        mac.init(key);

        String sbs = new SignatureBaseString(request, requestParameters).generate();
        return base64(mac.doFinal(sbs.getBytes(ENCODING)));
    }

    private String base64(byte[] data) {
        try {
            return new String(Base64.encodeBase64(data), "UTF-8");
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    private static class FeedHandler extends DefaultHandler {
        private static final String EMAIL = "email";
        private static final String AUTHOR = "author";
        private final StringBuilder email = new StringBuilder();
        private boolean inEmail;
        private boolean inAuthor;

        @Override
        public void startElement(String uri, String localName, String qName, Attributes atts) {
            inEmail = EMAIL.equals(qName);
            if (AUTHOR.equals(qName)) {
                inAuthor = true;
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            if (inAuthor && AUTHOR.equals(qName)) {
                inAuthor = false;
            }
        }

        @Override
        public void characters(char[] c, int start, int length) {
            if (inAuthor && inEmail) {
                email.append(c, start, length);
            }
        }

        @Override
        public void error(SAXParseException e) throws SAXException {
            Log.e(TAG, "error during parsing", e);
        }

        @Override public void warning(SAXParseException e) throws SAXException {
            Log.w(TAG, "error during parsing", e);
        }

        public String getEmail() {
            return email.toString().trim();
        }
    }
}




Java Source Code List

com.zegoggles.smssync.App.java
com.zegoggles.smssync.Consts.java
com.zegoggles.smssync.MmsConsts.java
com.zegoggles.smssync.SmsConsts.java
com.zegoggles.smssync.activity.Dialogs.java
com.zegoggles.smssync.activity.MainActivity.java
com.zegoggles.smssync.activity.StatusPreference.java
com.zegoggles.smssync.activity.auth.AccountManagerAuthActivity.java
com.zegoggles.smssync.activity.auth.WebAuthActivity.java
com.zegoggles.smssync.activity.donation.DonationActivity.java
com.zegoggles.smssync.auth.TokenRefreshException.java
com.zegoggles.smssync.auth.TokenRefresher.java
com.zegoggles.smssync.auth.XOAuthConsumer.java
com.zegoggles.smssync.calendar.CalendarAccessorPost40.java
com.zegoggles.smssync.calendar.CalendarAccessorPre40.java
com.zegoggles.smssync.calendar.CalendarAccessor.java
com.zegoggles.smssync.compat.ComposeSmsActivity.java
com.zegoggles.smssync.compat.HeadlessSmsSendService.java
com.zegoggles.smssync.compat.MmsReceiver.java
com.zegoggles.smssync.compat.SmsReceiver.java
com.zegoggles.smssync.compat.package-info.java
com.zegoggles.smssync.contacts.ContactAccessorPost20.java
com.zegoggles.smssync.contacts.ContactAccessorPre20.java
com.zegoggles.smssync.contacts.ContactAccessor.java
com.zegoggles.smssync.contacts.ContactGroupIds.java
com.zegoggles.smssync.contacts.ContactGroup.java
com.zegoggles.smssync.contacts.Group.java
com.zegoggles.smssync.mail.AllTrustedSocketFactory.java
com.zegoggles.smssync.mail.Attachment.java
com.zegoggles.smssync.mail.BackupImapStore.java
com.zegoggles.smssync.mail.BackupStoreConfig.java
com.zegoggles.smssync.mail.CallFormatter.java
com.zegoggles.smssync.mail.ConversionResult.java
com.zegoggles.smssync.mail.DataType.java
com.zegoggles.smssync.mail.HeaderGenerator.java
com.zegoggles.smssync.mail.Headers.java
com.zegoggles.smssync.mail.MessageConverter.java
com.zegoggles.smssync.mail.MessageGenerator.java
com.zegoggles.smssync.mail.MmsSupport.java
com.zegoggles.smssync.mail.PersonLookup.java
com.zegoggles.smssync.mail.PersonRecord.java
com.zegoggles.smssync.preferences.AddressStyle.java
com.zegoggles.smssync.preferences.AuthMode.java
com.zegoggles.smssync.preferences.AuthPreferences.java
com.zegoggles.smssync.preferences.BackupManagerWrapper.java
com.zegoggles.smssync.preferences.CalendarPreferences.java
com.zegoggles.smssync.preferences.CallLogTypes.java
com.zegoggles.smssync.preferences.Defaults.java
com.zegoggles.smssync.preferences.MarkAsReadTypes.java
com.zegoggles.smssync.preferences.PreferenceBackupAgent.java
com.zegoggles.smssync.preferences.Preferences.java
com.zegoggles.smssync.preferences.ServerPreferences.java
com.zegoggles.smssync.receiver.BackupBroadcastReceiver.java
com.zegoggles.smssync.receiver.SmsBroadcastReceiver.java
com.zegoggles.smssync.service.Alarms.java
com.zegoggles.smssync.service.BackupConfig.java
com.zegoggles.smssync.service.BackupCursors.java
com.zegoggles.smssync.service.BackupItemsFetcher.java
com.zegoggles.smssync.service.BackupQueryBuilder.java
com.zegoggles.smssync.service.BackupTask.java
com.zegoggles.smssync.service.BackupType.java
com.zegoggles.smssync.service.BulkFetcher.java
com.zegoggles.smssync.service.CalendarSyncer.java
com.zegoggles.smssync.service.RestoreConfig.java
com.zegoggles.smssync.service.RestoreTask.java
com.zegoggles.smssync.service.ServiceBase.java
com.zegoggles.smssync.service.SmsBackupService.java
com.zegoggles.smssync.service.SmsRestoreService.java
com.zegoggles.smssync.service.UserCanceled.java
com.zegoggles.smssync.service.WhatsAppItemsFetcher.java
com.zegoggles.smssync.service.exception.BackupDisabledException.java
com.zegoggles.smssync.service.exception.ConnectivityException.java
com.zegoggles.smssync.service.exception.LocalizableException.java
com.zegoggles.smssync.service.exception.NoConnectionException.java
com.zegoggles.smssync.service.exception.RequiresBackgroundDataException.java
com.zegoggles.smssync.service.exception.RequiresLoginException.java
com.zegoggles.smssync.service.exception.RequiresWifiException.java
com.zegoggles.smssync.service.exception.SmsProviderNotWritableException.java
com.zegoggles.smssync.service.state.BackupState.java
com.zegoggles.smssync.service.state.RestoreState.java
com.zegoggles.smssync.service.state.SmsSyncState.java
com.zegoggles.smssync.service.state.State.java
com.zegoggles.smssync.tasks.OAuthCallbackTask.java
com.zegoggles.smssync.tasks.RequestTokenTask.java
com.zegoggles.smssync.utils.AppLog.java
com.zegoggles.smssync.utils.ListPreferenceHelper.java
com.zegoggles.smssync.utils.Sanitizer.java
com.zegoggles.smssync.utils.ThreadHelper.java