--- /dev/null
+package com.owncloud.android.operations;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.NameValuePair;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.owncloud.android.authenticator.oauth2.OAuth2Context;
+import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
+
+import android.util.Log;
+
+import eu.alefzero.webdav.WebdavClient;
+
+public class OAuth2GetAccessToken extends RemoteOperation {
+
+ private static final String TAG = OAuth2GetAccessToken.class.getSimpleName();
+
+ private Map<String, String> mOAuth2AuthorizationResponse;
+ private Map<String, String> mResultTokenMap;
+
+
+ public OAuth2GetAccessToken(Map<String, String> oAuth2AuthorizationResponse) {
+ mOAuth2AuthorizationResponse = oAuth2AuthorizationResponse;
+ mResultTokenMap = null;
+ }
+
+
+ public Map<String, String> getOauth2AutorizationResponse() {
+ return mOAuth2AuthorizationResponse;
+ }
+
+ public Map<String, String> getResultTokenMap() {
+ return mResultTokenMap;
+ }
+
+ @Override
+ protected RemoteOperationResult run(WebdavClient client) {
+ RemoteOperationResult result = null;
+ PostMethod postMethod = null;
+
+ try {
+ NameValuePair[] nameValuePairs = new NameValuePair[5];
+ nameValuePairs[0] = new NameValuePair(OAuth2Context.KEY_CLIENT_ID, OAuth2Context.OAUTH2_F_CLIENT_ID);
+ nameValuePairs[1] = new NameValuePair(OAuth2Context.KEY_CODE, mOAuth2AuthorizationResponse.get(OAuth2Context.KEY_CODE));
+ nameValuePairs[2] = new NameValuePair(OAuth2Context.KEY_SCOPE, mOAuth2AuthorizationResponse.get(OAuth2Context.KEY_SCOPE));
+ nameValuePairs[3] = new NameValuePair(OAuth2Context.KEY_REDIRECT_URI, OAuth2Context.MY_REDIRECT_URI);
+ nameValuePairs[4] = new NameValuePair(OAuth2Context.KEY_GRANT_TYPE, OAuth2Context.OAUTH2_AUTH_CODE_GRANT_TYPE);
+
+ postMethod = new PostMethod(client.getBaseUri().toString());
+ postMethod.setRequestBody(nameValuePairs);
+ int status = client.executeMethod(postMethod);
+ if (status >= 300) {
+ client.exhaustResponse(postMethod.getResponseBodyAsStream());
+ result = new RemoteOperationResult(false, status);
+
+ } else {
+ JSONObject tokenJson = new JSONObject(postMethod.getResponseBodyAsString());
+ parseResult(tokenJson);
+ if (mResultTokenMap.get(OAuth2Context.OAUTH2_TOKEN_RECEIVED_ERROR) != null) {
+ result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR);
+
+ } else {
+ result = new RemoteOperationResult(true, status);
+ }
+ }
+
+ } catch (Exception e) {
+ result = new RemoteOperationResult(e);
+
+ } finally {
+ if (postMethod != null)
+ postMethod.releaseConnection(); // let the connection available for other methods
+
+ if (result.isSuccess()) {
+ Log.i(TAG, "OAuth2 TOKEN REQUEST with code " + mOAuth2AuthorizationResponse.get("code") + " to " + client.getBaseUri() + ": " + result.getLogMessage());
+
+ } else if (result.getException() != null) {
+ Log.e(TAG, "OAuth2 TOKEN REQUEST with code " + mOAuth2AuthorizationResponse.get("code") + " to " + client.getBaseUri() + ": " + result.getLogMessage(), result.getException());
+
+ } else if (result.getCode() == ResultCode.OAUTH2_ERROR) {
+ Log.e(TAG, "OAuth2 TOKEN REQUEST with code " + mOAuth2AuthorizationResponse.get("code") + " to " + client.getBaseUri() + ": " + mResultTokenMap.get(OAuth2Context.OAUTH2_TOKEN_RECEIVED_ERROR));
+
+ } else {
+ Log.e(TAG, "OAuth2 TOKEN REQUEST with code " + mOAuth2AuthorizationResponse.get("code") + " to " + client.getBaseUri() + ": " + result.getLogMessage());
+ }
+ }
+
+ return result;
+ }
+
+
+ private void parseResult (JSONObject tokenJson) throws JSONException {
+ mResultTokenMap = new HashMap<String, String>();
+
+ if (tokenJson.has(OAuth2Context.KEY_ACCESS_TOKEN)) {
+ mResultTokenMap.put(OAuth2Context.KEY_ACCESS_TOKEN, tokenJson.getString(OAuth2Context.KEY_ACCESS_TOKEN));
+ }
+ if (tokenJson.has(OAuth2Context.KEY_TOKEN_TYPE)) {
+ mResultTokenMap.put(OAuth2Context.KEY_TOKEN_TYPE, tokenJson.getString(OAuth2Context.KEY_TOKEN_TYPE));
+ }
+ if (tokenJson.has(OAuth2Context.KEY_EXPIRES_IN)) {
+ mResultTokenMap.put(OAuth2Context.KEY_EXPIRES_IN, tokenJson.getString(OAuth2Context.KEY_EXPIRES_IN));
+ }
+ if (tokenJson.has(OAuth2Context.KEY_REFRESH_TOKEN)) {
+ mResultTokenMap.put(OAuth2Context.KEY_REFRESH_TOKEN, tokenJson.getString(OAuth2Context.KEY_REFRESH_TOKEN));
+ }
+ if (tokenJson.has(OAuth2Context.KEY_SCOPE)) {
+ mResultTokenMap.put(OAuth2Context.KEY_SCOPE, tokenJson.getString(OAuth2Context.KEY_SCOPE));
+ }
+ if (tokenJson.has(OAuth2Context.KEY_ERROR)) {
+ mResultTokenMap.put(OAuth2Context.KEY_ERROR, tokenJson.getString(OAuth2Context.KEY_ERROR));
+ }
+ if (tokenJson.has(OAuth2Context.KEY_ERROR_DESCRIPTION)) {
+ mResultTokenMap.put(OAuth2Context.KEY_ERROR_DESCRIPTION, tokenJson.getString(OAuth2Context.KEY_ERROR_DESCRIPTION));
+ }
+ if (tokenJson.has(OAuth2Context.KEY_ERROR_URI)) {
+ mResultTokenMap.put(OAuth2Context.KEY_ERROR_URI, tokenJson.getString(OAuth2Context.KEY_ERROR_URI));
+ }
+ }
+
+}
--- /dev/null
+package com.owncloud.android.operations;
+
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.http.NameValuePair;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.message.BasicNameValuePair;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.ConnectivityManager;
+import android.net.Uri;
+import android.os.Handler;
+import android.util.Log;
+
+import com.owncloud.android.authenticator.oauth2.OAuth2Context;
+import com.owncloud.android.authenticator.oauth2.OnOAuth2GetCodeResultListener;
+import com.owncloud.android.authenticator.oauth2.OnOAuth2GetCodeResultListener.ResultOAuthType;
+import com.owncloud.android.authenticator.oauth2.connection.ConnectorOAuth2;
+
+/**
+ * Implements the communication with oAuth2 server to get User Code and other useful values.
+ *
+ * @author SolidGear S.L.
+ *
+ */
+public class OAuth2GetAuthorizationToken implements Runnable {
+
+ public static final String CODE_USER_CODE = "user_code";
+ public static final String CODE_CLIENT_ID = "client_id";
+ public static final String CODE_SCOPE = "scope";
+ public static final String CODE_VERIFICATION_URL = "verification_url";
+ public static final String CODE_EXPIRES_IN = "expires_in";
+ public static final String CODE_DEVICE_CODE = "device_code";
+ public static final String CODE_INTERVAL = "interval";
+
+ private static final String CODE_RESPONSE_TYPE = "response_type";
+ private static final String CODE_REDIRECT_URI = "redirect_uri";
+
+ private String mGrantType = OAuth2Context.OAUTH2_AUTH_CODE_GRANT_TYPE;
+
+ private static final String TAG = "OAuth2GetCodeRunnable";
+ private OnOAuth2GetCodeResultListener mListener;
+ private String mUrl;
+ private Handler mHandler;
+ private Context mContext;
+ //private JSONObject codeResponseJson = null;
+ ResultOAuthType mLatestResult;
+
+
+ public void setListener(OnOAuth2GetCodeResultListener listener, Handler handler) {
+ mListener = listener;
+ mHandler = handler;
+ }
+
+ public OAuth2GetAuthorizationToken(String url, Context context) {
+ mListener = null;
+ mHandler = null;
+ mUrl = url;
+ mContext = context;
+ }
+
+ @Override
+ public void run() {
+
+ if (!isOnline()) {
+ postResult(ResultOAuthType.NO_NETWORK_CONNECTION,null);
+ return;
+ }
+
+ if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
+ mLatestResult = (mUrl.startsWith("https://"))? ResultOAuthType.OK_SSL : ResultOAuthType.OK_NO_SSL;
+ } else {
+ mUrl = "https://" + mUrl;
+ mLatestResult = ResultOAuthType.OK_SSL;
+ }
+
+ if (mGrantType.equals(OAuth2Context.OAUTH2_AUTH_CODE_GRANT_TYPE)) {
+ requestBrowserToGetAuthorizationCode();
+
+ } /*else if (mGrantType.equals(OAuth2Context.OAUTH_G_DEVICE_GETTOKEN_GRANT_TYPE)) {
+ getAuthorizationCode();
+ }*/
+ }
+
+ /// open the authorization endpoint in a web browser!
+ private void requestBrowserToGetAuthorizationCode() {
+ Uri uri = Uri.parse(mUrl);
+ Uri.Builder uriBuilder = uri.buildUpon();
+ uriBuilder.appendQueryParameter(CODE_RESPONSE_TYPE, OAuth2Context.OAUTH2_CODE_RESPONSE_TYPE);
+ uriBuilder.appendQueryParameter(CODE_REDIRECT_URI, OAuth2Context.MY_REDIRECT_URI);
+ uriBuilder.appendQueryParameter(CODE_CLIENT_ID, OAuth2Context.OAUTH2_F_CLIENT_ID);
+ uriBuilder.appendQueryParameter(CODE_SCOPE, OAuth2Context.OAUTH2_F_SCOPE);
+ //uriBuilder.appendQueryParameter(CODE_STATE, whateverwewant);
+
+ uri = uriBuilder.build();
+ Log.d(TAG, "Starting browser to view " + uri.toString());
+
+ Intent i = new Intent(Intent.ACTION_VIEW, uri);
+ mContext.startActivity(i);
+
+ postResult(mLatestResult, null);
+ }
+
+
+ /*
+ private void getAuthorizationCode() {
+ ConnectorOAuth2 connectorOAuth2 = new ConnectorOAuth2(mUrl);
+ try {
+ List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
+ nameValuePairs.add(new BasicNameValuePair(CODE_CLIENT_ID, OAuth2Context.OAUTH2_G_DEVICE_CLIENT_ID));
+ nameValuePairs.add(new BasicNameValuePair(CODE_SCOPE,OAuth2Context.OAUTH2_G_DEVICE_GETCODE_SCOPES));
+ UrlEncodedFormEntity params = new UrlEncodedFormEntity(nameValuePairs);
+ codeResponseJson = new JSONObject(connectorOAuth2.connPost(params));
+ } catch (JSONException e) {
+ Log.e(TAG, "JSONException converting to Json: " + e.toString());
+ } catch (UnsupportedEncodingException e) {
+ Log.e(TAG, "UnsupportedEncodingException encoding URL values: " + e.toString());
+ } catch (Exception e) {
+ Log.e(TAG, "Exception : " + e.toString());
+ }
+
+ if (codeResponseJson == null) {
+ mLatestResult = ResultOAuthType.HOST_NOT_AVAILABLE;
+ }
+ postResult(mLatestResult, codeResponseJson);
+ }
+ */
+
+
+ private boolean isOnline() {
+ ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
+ return cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();
+ }
+
+ private void postResult(final ResultOAuthType result,final JSONObject codeResponseJson) {
+ if (mHandler != null && mListener != null) {
+ mHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ mListener.onOAuth2GetCodeResult(result, codeResponseJson);
+ }
+ });
+ }
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/* ownCloud Android client application
+ * Copyright (C) 2012 Bartek Przybylski
+ *
+ * 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/>.
+ *
+ */
+
+package com.owncloud.android.operations;
+
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.owncloud.android.AccountUtils;
+import com.owncloud.android.utils.OwnCloudVersion;
+
+import eu.alefzero.webdav.WebdavClient;
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.net.Uri;
+import android.util.Log;
+
+public class OwnCloudServerCheckOperation extends RemoteOperation {
+
+ /** Maximum time to wait for a response from the server when the connection is being tested, in MILLISECONDs. */
+ public static final int TRY_CONNECTION_TIMEOUT = 5000;
+
+ private static final String TAG = OwnCloudServerCheckOperation.class.getSimpleName();
+
+ private String mUrl;
+ private RemoteOperationResult mLatestResult;
+ private Context mContext;
+ private OwnCloudVersion mOCVersion;
+
+ public OwnCloudServerCheckOperation(String url, Context context) {
+ mUrl = url;
+ mContext = context;
+ mOCVersion = null;
+ }
+
+ public OwnCloudVersion getDiscoveredVersion() {
+ return mOCVersion;
+ }
+
+ private boolean tryConnection(WebdavClient wc, String urlSt) {
+ boolean retval = false;
+ GetMethod get = null;
+ try {
+ get = new GetMethod(urlSt);
+ int status = wc.executeMethod(get, TRY_CONNECTION_TIMEOUT, TRY_CONNECTION_TIMEOUT);
+ String response = get.getResponseBodyAsString();
+ if (status == HttpStatus.SC_OK) {
+ JSONObject json = new JSONObject(response);
+ if (!json.getBoolean("installed")) {
+ mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
+ } else {
+ mOCVersion = new OwnCloudVersion(json.getString("version"));
+ if (!mOCVersion.isVersionValid()) {
+ mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.BAD_OC_VERSION);
+
+ } else {
+ mLatestResult = new RemoteOperationResult(urlSt.startsWith("https://") ?
+ RemoteOperationResult.ResultCode.OK_SSL :
+ RemoteOperationResult.ResultCode.OK_NO_SSL
+ );
+
+ retval = true;
+ }
+ }
+
+ } else {
+ mLatestResult = new RemoteOperationResult(false, status);
+ }
+
+ } catch (JSONException e) {
+ mLatestResult = new RemoteOperationResult(RemoteOperationResult.ResultCode.INSTANCE_NOT_CONFIGURED);
+
+ } catch (Exception e) {
+ mLatestResult = new RemoteOperationResult(e);
+
+ } finally {
+ if (get != null)
+ get.releaseConnection();
+ }
+
+ if (mLatestResult.isSuccess()) {
+ Log.i(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
+
+ } else if (mLatestResult.getException() != null) {
+ Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage(), mLatestResult.getException());
+
+ } else {
+ Log.e(TAG, "Connection check at " + urlSt + ": " + mLatestResult.getLogMessage());
+ }
+
+ return retval;
+ }
+
+ private boolean isOnline() {
+ ConnectivityManager cm = (ConnectivityManager) mContext
+ .getSystemService(Context.CONNECTIVITY_SERVICE);
+ return cm != null && cm.getActiveNetworkInfo() != null
+ && cm.getActiveNetworkInfo().isConnectedOrConnecting();
+ }
+
+ @Override
+ protected RemoteOperationResult run(WebdavClient client) {
+ if (!isOnline()) {
+ return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
+ }
+ if (mUrl.startsWith("http://") || mUrl.startsWith("https://")) {
+ tryConnection(client, mUrl + AccountUtils.STATUS_PATH);
+
+ } else {
+ client.setBaseUri(Uri.parse("https://" + mUrl + AccountUtils.STATUS_PATH));
+ boolean httpsSuccess = tryConnection(client, "https://" + mUrl + AccountUtils.STATUS_PATH);
+ if (!httpsSuccess && !mLatestResult.isSslRecoverableException()) {
+ Log.d(TAG, "establishing secure connection failed, trying non secure connection");
+ client.setBaseUri(Uri.parse("http://" + mUrl + AccountUtils.STATUS_PATH));
+ tryConnection(client, "http://" + mUrl + AccountUtils.STATUS_PATH);
+ }
+ }
+ return mLatestResult;
+ }
+
+}