Guest User

Modified

a guest
Feb 19th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1. /*
  2. * Copyright 2015-2016 Red Hat, Inc. and/or its affiliates
  3. * and other contributors as indicated by the @author tags.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.hawkular.client.android.auth;
  18.  
  19. import java.net.MalformedURLException;
  20. import java.net.URI;
  21. import java.net.URL;
  22. import java.util.UUID;
  23.  
  24. import org.jboss.aerogear.android.authorization.AuthzModule;
  25. import org.jboss.aerogear.android.authorization.oauth2.OAuth2AuthzSession;
  26. import org.jboss.aerogear.android.core.Callback;
  27. import org.jboss.aerogear.android.pipe.http.HeaderAndBody;
  28. import org.jboss.aerogear.android.pipe.http.HttpException;
  29. import org.jboss.aerogear.android.pipe.http.HttpProvider;
  30. import org.jboss.aerogear.android.pipe.http.HttpRestProvider;
  31. import org.jboss.aerogear.android.pipe.module.AuthorizationFields;
  32. import org.jboss.aerogear.android.pipe.module.ModuleFields;
  33. import org.jboss.aerogear.android.store.DataManager;
  34. import org.jboss.aerogear.android.store.generator.IdGenerator;
  35. import org.jboss.aerogear.android.store.sql.SQLStore;
  36. import org.jboss.aerogear.android.store.sql.SQLStoreConfiguration;
  37. import org.json.JSONException;
  38. import org.json.JSONObject;
  39.  
  40. import android.app.Activity;
  41. import android.content.Context;
  42. import android.content.Intent;
  43. import android.util.Base64;
  44. import timber.log.Timber;
  45.  
  46. /**
  47. * ModifiedAuthzModule.
  48. * <p/>
  49. * Performs all related work of generating new key and secret pair, Storing them as account,
  50. * Retrieving stored account, Deleting stored account.
  51. */
  52.  
  53. public class SecretStoreAuthzModule implements AuthzModule {
  54. private final Context context;
  55. private final SQLStore<OAuth2AuthzSession> sessionStore;
  56.  
  57. public SecretStoreAuthzModule(Context context) {
  58. this.context = context.getApplicationContext();
  59. this.sessionStore = openSessionStore();
  60. sessionStore.openSync();
  61. }
  62.  
  63. private SQLStore<OAuth2AuthzSession> openSessionStore() {
  64. DataManager.config(AuthData.STORE, SQLStoreConfiguration.class)
  65. .forClass(OAuth2AuthzSession.class)
  66. .withContext(context)
  67. .withIdGenerator(new IdGenerator() {
  68. @Override
  69. public String generate() {
  70. return UUID.randomUUID().toString();
  71. }
  72. }).store();
  73. return (SQLStore<OAuth2AuthzSession>) DataManager.getStore(AuthData.STORE);
  74. }
  75.  
  76. @Override
  77. public boolean isAuthorized() {
  78. return true;
  79. }
  80.  
  81. @Override
  82. public boolean hasCredentials() {
  83. return sessionStore.read(AuthData.NAME) != null;
  84. }
  85.  
  86. @Override
  87. public void requestAccess(final Activity activity, final Callback<String> callback) {
  88. if (activity != null && activity.getIntent() != null) {
  89. Intent intent = activity.getIntent();
  90. if (intent.getStringExtra(AuthData.Credentials.CONTAIN) == null) {
  91. callback.onSuccess(sessionStore.read(AuthData.NAME).getAccessToken());
  92. } else {
  93. doRequestAccess(activity, callback);
  94. }
  95. }
  96. }
  97.  
  98. private void doRequestAccess(final Activity activity, final Callback<String> callback) {
  99. Intent intent = activity.getIntent();
  100. final String username = intent.getStringExtra(AuthData.Credentials.USERNAME);
  101. final String password = intent.getStringExtra(AuthData.Credentials.PASSWORD);
  102. final String url = intent.getStringExtra(AuthData.Credentials.URL);
  103. new Thread(new Runnable() {
  104. @Override public void run() {
  105. HeaderAndBody result;
  106. try {
  107. HttpProvider provider =
  108. new HttpRestProvider(new URL(url.toString() + AuthData.Endpoints.ACCESS));
  109. provider.setDefaultHeader("Accept", "application/json");
  110. provider.setDefaultHeader("Authorization", "Basic "
  111. + buildLoginData(username, password));
  112. result = provider.post("");
  113. addAccount(new String(result.getBody()));
  114. callback.onSuccess(new String(result.getBody()));
  115. } catch (MalformedURLException e) {
  116. Timber.d(SecretStoreAuthzModule.class.getSimpleName(), "Error with URL", e);
  117. callback.onFailure(e);
  118. } catch (HttpException e) {
  119. Timber.d(SecretStoreAuthzModule.class.getSimpleName(), "HTTP error", e);
  120. callback.onFailure(e);
  121. } catch (RuntimeException e) {
  122. Timber.d(SecretStoreAuthzModule.class.getSimpleName(), "Connection Exception", e);
  123. callback.onFailure(e);
  124. } catch (JSONException e) {
  125. Timber.d(SecretStoreAuthzModule.class.getSimpleName(), "Json parse Exception", e);
  126. callback.onFailure(e);
  127. }
  128. }
  129. }).start();
  130. }
  131.  
  132. private String buildLoginData(String username, String password) {
  133. String cred = username + ":" + password;
  134. return new String(Base64.encode(cred.getBytes(), Base64.NO_WRAP));
  135. }
  136.  
  137. @Override
  138. public boolean refreshAccess() {
  139. return true;
  140. }
  141.  
  142. @Override
  143. public void deleteAccount() {
  144. sessionStore.remove(AuthData.NAME);
  145. }
  146.  
  147. @Override
  148. public AuthorizationFields getAuthorizationFields(URI requestUri, String method, byte[] requestBody) {
  149. AuthorizationFields fields = new AuthorizationFields();
  150. OAuth2AuthzSession storedAccount = sessionStore.read(AuthData.NAME);
  151. String cred = new String(Base64.encode((storedAccount.getAccessToken()).getBytes(), Base64.NO_WRAP));
  152. fields.addHeader("Authorization", "Basic " + cred);
  153. return fields;
  154. }
  155.  
  156. @Override
  157. public ModuleFields loadModule(URI relativeURI, String httpMethod, byte[] requestBody) {
  158. AuthorizationFields authzFields = getAuthorizationFields(relativeURI, httpMethod, requestBody);
  159. ModuleFields moduleFields = new ModuleFields();
  160. moduleFields.setHeaders(authzFields.getHeaders());
  161. moduleFields.setQueryParameters(authzFields.getQueryParameters());
  162. return moduleFields;
  163. }
  164.  
  165. @Override
  166. public boolean handleError(HttpException exception) {
  167. return false;
  168. }
  169.  
  170. private void addAccount(String data) throws JSONException {
  171. JSONObject auth = new JSONObject(data);
  172. OAuth2AuthzSession session = new OAuth2AuthzSession();
  173. session.setAccountId(AuthData.NAME);
  174. session.setAccessToken(auth.getString("key") + ":" + auth.getString("secret"));
  175. sessionStore.save(session);
  176. }
  177. }
Add Comment
Please, Sign In to add comment