Advertisement
shanemurphy

Untitled

Mar 3rd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1.  
  2. public class SharedPreferencesCredentialsProvider implements CredentialsProvider {
  3.  
  4. private static final String USERNAME_KEY = "USERNAME";
  5. private static final String PASSWORD_KEY = "PASSWORD";
  6.  
  7. private SecuredPreferences securePrefs;
  8.  
  9. public SharedPreferencesCredentialsProvider(Context context) {
  10. securePrefs = new SecuredPreferences(context);
  11. }
  12.  
  13. @Override
  14. public Credentials provideCredentials() throws IdentityAuthenticationException {
  15. String username = securePrefs.getString(USERNAME_KEY, null);
  16. String password = securePrefs.getString(PASSWORD_KEY, null);
  17.  
  18. if(username == null || password == null) {
  19. return null;
  20. }
  21. return new Credentials(username, password);
  22. }
  23.  
  24. @Override
  25. public void saveCredentials(Credentials credentials) throws IdentityAuthenticationException {
  26. String username = credentials.getUsername();
  27. String password = credentials.getPassword();
  28.  
  29. if (username != null && password != null) {
  30. SecuredPreferences.Editor editor = securePrefs.edit();
  31. editor.putString(USERNAME_KEY, credentials.getUsername());
  32. editor.putString(PASSWORD_KEY, credentials.getPassword());
  33. editor.apply();
  34. }
  35. }
  36.  
  37. @Override
  38. public void clearCredentials() throws IdentityAuthenticationException {
  39. SecuredPreferences.Editor editor = securePrefs.edit();
  40. editor.remove(USERNAME_KEY);
  41. editor.remove(PASSWORD_KEY);
  42. editor.apply();
  43. }
  44. }
  45.  
  46.  
  47.  
  48. public class ApiClientProvider {
  49. private DefaultIdentityAuthClient identityAuthClient;
  50.  
  51. .
  52. .
  53. .
  54.  
  55. private void initIdentityAuthClient() {
  56. this.identityAuthClient = new DefaultIdentityAuthClient(this.context.getString(R.string.ids_endpoint));
  57.  
  58. AuthenticationProvider authenticationProvider = new SharedPreferencesAuthenticationProvider(this.context);
  59. this.identityAuthClient.setAuthenticationProvider(authenticationProvider);
  60.  
  61. CredentialsProvider credentialsProvider = new SharedPreferencesCredentialsProvider(this.context);
  62. this.identityAuthClient.setCredentialsProvider(credentialsProvider);
  63. }
  64.  
  65. public DefaultIdentityAuthClient getIdentityAuthClient() {
  66. return identityAuthClient;
  67. }
  68.  
  69. public TripStoreTemplate getTripStoreTemplate() {
  70. return tripStoreTemplate;
  71. }
  72.  
  73. public void clearAuthentication() {
  74. identityAuthClient.getAuthenticationProvider().clearAuthentication();
  75. }
  76.  
  77. public void clearCredentials() {
  78. identityAuthClient.getCredentialsProvider().clearCredentials();
  79. }
  80.  
  81. public Credentials provideCredentials() {
  82. return identityAuthClient.getCredentialsProvider().provideCredentials();
  83. }
  84.  
  85. public void saveCredentials(Credentials credentials) {
  86. identityAuthClient.getCredentialsProvider().saveCredentials(credentials);
  87. }
  88.  
  89. public void saveAuthentication(Authentication authentication) {
  90. identityAuthClient.getAuthenticationProvider().saveAuthentication(authentication);
  91. }
  92.  
  93.  
  94.  
  95. //Login
  96. protected void login() {
  97. String username = this.username.getText().toString().trim();
  98. String password = this.password.getText().toString();
  99.  
  100. try {
  101. AuthenticationResult authenticationResult = apiClientProvider.getIdentityAuthClient().login(username, password);
  102. if (authenticationResult.succeed()) {
  103. onSuccess(authenticationResult.getAuthentication());
  104. } else {
  105. onFailure(authenticationResult.getError().getErrorCode().getCode());
  106. }
  107. } catch (Exception ex) {
  108. onFailure(ex);
  109. } finally {
  110. onFinish();
  111. }
  112. }
  113.  
  114. .
  115. .
  116. .
  117.  
  118. protected void onSuccess(Authentication authentication) {
  119. String usernameString = username.getText().toString().trim();
  120. String passwordString = password.getText().toString().trim();
  121. String userUuid = authentication.getUserUuid();
  122.  
  123. Credentials credentials = new Credentials(usernameString, passwordString);
  124.  
  125. if (rememberMe.isChecked()) {
  126. apiClientProvider.saveCredentials(credentials);
  127. } else {
  128. apiClientProvider.getIdentityAuthClient().setAuthenticationProvider(new MemoryAuthenticationProvider());
  129. apiClientProvider.getIdentityAuthClient().setCredentialsProvider(new MemoryCredentialsProvider(credentials));
  130. }
  131. apiClientProvider.saveAuthentication(authentication);
  132.  
  133. saveCurrentUser(usernameString, userUuid);
  134. }
  135.  
  136.  
  137. //forgot password
  138. protected void requestForgotPassword() {
  139. String usernameString = forgotPasswordEmail.getText().toString().trim();
  140. try {
  141. apiClientProvider.getIdentityAuthClient().forgotPassword(usernameString);
  142. onSuccess();
  143. } catch (Exception e) {
  144. onFailure(e);
  145. } finally {
  146. onFinish();
  147. }
  148. }
  149.  
  150.  
  151. //Logout
  152. public void doLogoutActions(Context context) {
  153. .
  154. .
  155. apiClientProvider.clearAuthentication();
  156. apiClientProvider.clearCredentials();
  157. .
  158. .
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement