mirror of
				https://github.com/owncloud/android-library.git
				synced 2025-10-31 02:17:41 +00:00 
			
		
		
		
	add oauth operations
This commit is contained in:
		
							parent
							
								
									b1f2c0cbef
								
							
						
					
					
						commit
						8540eed348
					
				| @ -31,19 +31,20 @@ import android.net.Uri; | ||||
| import com.owncloud.android.lib.common.authentication.OwnCloudBasicCredentials; | ||||
| import com.owncloud.android.lib.common.OwnCloudClient; | ||||
| import com.owncloud.android.lib.common.authentication.OwnCloudCredentials; | ||||
| import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperation; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.NameValuePair; | ||||
| import org.apache.commons.httpclient.methods.PostMethod; | ||||
| import org.json.JSONException; | ||||
| import org.json.JSONObject; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| import okhttp3.HttpUrl; | ||||
| import okhttp3.MultipartBody; | ||||
| import okhttp3.RequestBody; | ||||
| 
 | ||||
| 
 | ||||
| public class OAuth2GetAccessTokenOperation extends RemoteOperation { | ||||
|      | ||||
| @ -83,28 +84,34 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation { | ||||
|     @Override | ||||
|     protected RemoteOperationResult run(OwnCloudClient client) { | ||||
|         RemoteOperationResult result = null; | ||||
|         PostMethod postMethod = null; | ||||
|          | ||||
|         try { | ||||
|             NameValuePair[] nameValuePairs = new NameValuePair[4]; | ||||
|             nameValuePairs[0] = new NameValuePair(OAuth2Constants.KEY_GRANT_TYPE, mGrantType); | ||||
|             nameValuePairs[1] = new NameValuePair(OAuth2Constants.KEY_CODE, mCode); | ||||
|             nameValuePairs[2] = new NameValuePair(OAuth2Constants.KEY_REDIRECT_URI, mRedirectUri); | ||||
|             nameValuePairs[3] = new NameValuePair(OAuth2Constants.KEY_CLIENT_ID, mClientId); | ||||
| 
 | ||||
|             final RequestBody requestBody = new MultipartBody.Builder() | ||||
|                     .setType(MultipartBody.FORM) | ||||
|                     .addFormDataPart(OAuth2Constants.KEY_GRANT_TYPE, mGrantType) | ||||
|                     .addFormDataPart(OAuth2Constants.KEY_CODE, mCode) | ||||
|                     .addFormDataPart(OAuth2Constants.KEY_REDIRECT_URI, mRedirectUri) | ||||
|                     .addFormDataPart(OAuth2Constants.KEY_CLIENT_ID, mClientId) | ||||
|                     .build(); | ||||
| 
 | ||||
|             Uri.Builder uriBuilder = client.getBaseUri().buildUpon(); | ||||
|             uriBuilder.appendEncodedPath(mAccessTokenEndpointPath); | ||||
| 
 | ||||
|             postMethod = new PostMethod(uriBuilder.build().toString()); | ||||
|             postMethod.setRequestBody(nameValuePairs); | ||||
|             final PostMethod postMethod = new PostMethod(HttpUrl.parse( | ||||
|                     client.getBaseUri().buildUpon() | ||||
|                             .appendEncodedPath(mAccessTokenEndpointPath) | ||||
|                             .build() | ||||
|                             .toString())); | ||||
| 
 | ||||
|             OwnCloudCredentials oauthCredentials = new OwnCloudBasicCredentials( | ||||
|                 mClientId, | ||||
|                 mClientSecret | ||||
|             ); | ||||
|             postMethod.setRequestBody(requestBody); | ||||
| 
 | ||||
| 
 | ||||
|             // Do the B***S*** Switch and execute | ||||
|             OwnCloudCredentials oauthCredentials = | ||||
|                     new OwnCloudBasicCredentials(mClientId, mClientSecret); | ||||
|             OwnCloudCredentials oldCredentials = switchClientCredentials(oauthCredentials); | ||||
| 
 | ||||
|             client.executeMethod(postMethod); | ||||
|             client.executeHttpMethod(postMethod); | ||||
|             switchClientCredentials(oldCredentials); | ||||
| 
 | ||||
|             String response = postMethod.getResponseBodyAsString(); | ||||
| @ -117,25 +124,21 @@ public class OAuth2GetAccessTokenOperation extends RemoteOperation { | ||||
|                     result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR); | ||||
| 
 | ||||
|                 } else { | ||||
|                     result = new RemoteOperationResult(true, postMethod); | ||||
|                     result = new RemoteOperationResult(ResultCode.OK); | ||||
|                     ArrayList<Object> data = new ArrayList<>(); | ||||
|                     data.add(accessTokenResult); | ||||
|                     result.setData(data); | ||||
|                 } | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, postMethod); | ||||
|                 client.exhaustResponse(postMethod.getResponseBodyAsStream()); | ||||
|                 result = new RemoteOperationResult(ResultCode.OK); | ||||
|                 client.exhaustResponse(postMethod.getResponseAsStream()); | ||||
|             } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|             result = new RemoteOperationResult(e); | ||||
|              | ||||
|         } finally { | ||||
|             if (postMethod != null) | ||||
|                 postMethod.releaseConnection();    // let the connection available for other methods | ||||
|         } | ||||
|          | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -31,13 +31,17 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; | ||||
| import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode; | ||||
| import com.owncloud.android.lib.common.utils.Log_OC; | ||||
| 
 | ||||
| import org.apache.commons.httpclient.NameValuePair; | ||||
| import org.apache.commons.httpclient.methods.PostMethod; | ||||
| import org.json.JSONObject; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| import com.owncloud.android.lib.common.http.methods.nonwebdav.PostMethod; | ||||
| 
 | ||||
| import okhttp3.HttpUrl; | ||||
| import okhttp3.MultipartBody; | ||||
| import okhttp3.RequestBody; | ||||
| 
 | ||||
| public class OAuth2RefreshAccessTokenOperation extends RemoteOperation { | ||||
| 
 | ||||
|     private static final String TAG = OAuth2RefreshAccessTokenOperation.class.getSimpleName(); | ||||
| @ -73,68 +77,59 @@ public class OAuth2RefreshAccessTokenOperation extends RemoteOperation { | ||||
|     @Override | ||||
|     protected RemoteOperationResult run(OwnCloudClient client) { | ||||
| 
 | ||||
|         RemoteOperationResult result = null; | ||||
|         PostMethod postMethod = null; | ||||
| 
 | ||||
|         try { | ||||
|             NameValuePair[] nameValuePairs = new NameValuePair[3]; | ||||
|             nameValuePairs[0] = new NameValuePair( | ||||
|                 OAuth2Constants.KEY_GRANT_TYPE, | ||||
|                 OAuth2GrantType.REFRESH_TOKEN.getValue()    // always for this operation | ||||
|             ); | ||||
|             nameValuePairs[1] = new NameValuePair(OAuth2Constants.KEY_CLIENT_ID, mClientId); | ||||
|             nameValuePairs[2] = new NameValuePair(OAuth2Constants.KEY_REFRESH_TOKEN, mRefreshToken); | ||||
| 
 | ||||
|             final RequestBody requestBody = new MultipartBody.Builder() | ||||
|                     .setType(MultipartBody.FORM) | ||||
|                     .addFormDataPart(OAuth2Constants.KEY_GRANT_TYPE, | ||||
|                             OAuth2GrantType.REFRESH_TOKEN.getValue()) | ||||
|                     .addFormDataPart(OAuth2Constants.KEY_CLIENT_ID, mClientId) | ||||
|                     .addFormDataPart(OAuth2Constants.KEY_REFRESH_TOKEN, mRefreshToken) | ||||
|                     .build(); | ||||
| 
 | ||||
|             Uri.Builder uriBuilder = client.getBaseUri().buildUpon(); | ||||
|             uriBuilder.appendEncodedPath(mAccessTokenEndpointPath); | ||||
| 
 | ||||
|             postMethod = new PostMethod(uriBuilder.build().toString()); | ||||
|             postMethod.setRequestBody(nameValuePairs); | ||||
|             final PostMethod postMethod = new PostMethod(HttpUrl.parse( | ||||
|                     client.getBaseUri().buildUpon() | ||||
|                             .appendEncodedPath(mAccessTokenEndpointPath) | ||||
|                             .build() | ||||
|                             .toString())); | ||||
|             postMethod.setRequestBody(requestBody); | ||||
| 
 | ||||
|             OwnCloudCredentials oauthCredentials = new OwnCloudBasicCredentials( | ||||
|                     mClientId, | ||||
|                     mClientSecret | ||||
|             ); | ||||
| 
 | ||||
|             OwnCloudCredentials oldCredentials = switchClientCredentials(oauthCredentials); | ||||
| 
 | ||||
|             client.executeMethod(postMethod); | ||||
|             final OwnCloudCredentials oauthCredentials = | ||||
|                     new OwnCloudBasicCredentials(mClientId, mClientSecret); | ||||
| 
 | ||||
|             // Do the B***S*** switch | ||||
|             final OwnCloudCredentials oldCredentials = switchClientCredentials(oauthCredentials); | ||||
|             client.executeHttpMethod(postMethod); | ||||
|             switchClientCredentials(oldCredentials); | ||||
| 
 | ||||
|             String response = postMethod.getResponseBodyAsString(); | ||||
|             Log_OC.d(TAG, "OAUTH2: raw response from POST TOKEN: " + response); | ||||
|             final String responseData = postMethod.getResponseBodyAsString(); | ||||
|             Log_OC.d(TAG, "OAUTH2: raw response from POST TOKEN: " + responseData); | ||||
| 
 | ||||
|             if (response != null && response.length() > 0) { | ||||
|                 JSONObject tokenJson = new JSONObject(response); | ||||
|                 Map<String, String> accessTokenResult = | ||||
|                     mResponseParser.parseAccessTokenResult(tokenJson); | ||||
|                 if (accessTokenResult.get(OAuth2Constants.KEY_ERROR) != null || | ||||
|                         accessTokenResult.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) { | ||||
|                     result = new RemoteOperationResult(ResultCode.OAUTH2_ERROR); | ||||
|             if (responseData != null && responseData.length() > 0) { | ||||
|                 final JSONObject tokenJson = new JSONObject(responseData); | ||||
| 
 | ||||
|                 } else { | ||||
|                     result = new RemoteOperationResult(true, postMethod); | ||||
|                     ArrayList<Object> data = new ArrayList<>(); | ||||
|                     data.add(accessTokenResult); | ||||
|                     result.setData(data); | ||||
|                 } | ||||
|                 final Map<String, String> accessTokenResult = | ||||
|                         mResponseParser.parseAccessTokenResult(tokenJson); | ||||
|                 final ArrayList<Object> resultData = new ArrayList<>(1); | ||||
|                 resultData.add(accessTokenResult); | ||||
|                 final RemoteOperationResult result = new RemoteOperationResult(ResultCode.OK); | ||||
|                 result.setData(resultData); | ||||
|                 return (accessTokenResult.get(OAuth2Constants.KEY_ERROR) != null || | ||||
|                         accessTokenResult.get(OAuth2Constants.KEY_ACCESS_TOKEN) == null) | ||||
|                         ? new RemoteOperationResult(ResultCode.OAUTH2_ERROR) | ||||
|                         : result; | ||||
| 
 | ||||
|             } else { | ||||
|                 result = new RemoteOperationResult(false, postMethod); | ||||
|                 client.exhaustResponse(postMethod.getResponseBodyAsStream()); | ||||
|                 return new RemoteOperationResult(postMethod); | ||||
|             } | ||||
| 
 | ||||
|         } catch (Exception e) { | ||||
|             result = new RemoteOperationResult(e); | ||||
| 
 | ||||
|         } finally { | ||||
|             if (postMethod != null) { | ||||
|                 postMethod.releaseConnection();    // let the connection available for other methods | ||||
|             } | ||||
|             return new RemoteOperationResult(e); | ||||
|         } | ||||
| 
 | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
|     private OwnCloudCredentials switchClientCredentials(OwnCloudCredentials newCredentials) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user