├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── auth0 │ │ └── android │ │ └── google │ │ └── app │ │ ├── ChooserActivity.java │ │ ├── FilesActivity.java │ │ └── SimpleActivity.java │ └── res │ ├── layout │ ├── activity_chooser.xml │ ├── activity_files.xml │ └── activity_simple.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ └── values │ ├── auth0.xml │ ├── colors.xml │ ├── dimens.xml │ ├── google.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── circle.yml ├── gradle.properties ├── gradle ├── lint.xml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── lock-google ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ └── java │ │ └── com │ │ └── auth0 │ │ └── android │ │ └── google │ │ ├── GoogleAPI.java │ │ ├── GoogleAuthHandler.java │ │ ├── GoogleAuthProvider.java │ │ └── GoogleCallback.java │ └── test │ └── java │ └── com │ └── auth0 │ └── android │ └── google │ ├── GoogleAuthHandlerTest.java │ ├── GoogleAuthProviderMock.java │ └── GoogleAuthProviderTest.java ├── proguard-rules.pro ├── scripts ├── bintray.gradle └── maven.gradle └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | /*/build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio 30 | .idea 31 | *.iml 32 | 33 | # OSX 34 | .DS_Store 35 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## [1.1.0](https://github.com/auth0/Lock-Google.Android/tree/1.1.0) (2017-01-23) 4 | [Full Changelog](https://github.com/auth0/Lock-Google.Android/compare/1.0.0...1.1.0) 5 | 6 | **Added** 7 | - Support authentication parameters [\#29](https://github.com/auth0/Lock-Google.Android/pull/29) ([lbalmaceda](https://github.com/lbalmaceda)) 8 | 9 | ## [1.0.0](https://github.com/auth0/Lock-Google.Android/tree/1.0.0) (2016-09-29) 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Auth0, Inc. (http://auth0.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Lock-Google 2 | 3 | [![CircleCI](https://img.shields.io/circleci/project/github/auth0/Lock-Google.Android.svg?style=flat-square)](https://circleci.com/gh/auth0/Lock-Google.Android/tree/master) 4 | [![License](http://img.shields.io/:license-mit-blue.svg?style=flat)](http://doge.mit-license.org) 5 | [![Maven Central](https://img.shields.io/maven-central/v/com.auth0.android/lock-google.svg)](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.auth0.android%22%20AND%20a%3A%22lock-google%22) 6 | [![Download](https://api.bintray.com/packages/auth0/android/lock-google/images/download.svg) ](https://bintray.com/auth0/android/lock-google/_latestVersion) 7 | 8 | [Auth0](https://auth0.com) is an authentication broker that supports social identity providers as well as enterprise identity providers such as Active Directory, LDAP, Google Apps and Salesforce. 9 | 10 | Lock-Google helps you integrate native Login with [Google Android SDK](https://developers.google.com/+/mobile/android/) and [Lock](https://auth0.com/lock) 11 | 12 | # Deprecation notice 13 | 14 | This package relies on a token endpoint that is now considered deprecated. **If your Auth0 client was created after Jun 8th 2017 you won't be able to use this package**. This repository is left for reference purposes. 15 | 16 | **We recommend using browser-based flows to authenticate users**. You can do that using the [auth0.android](https://github.com/auth0/auth0.android#authentication-with-hosted-login-page) package's `WebAuthProvider` class, as explained in [this document](https://auth0.com/docs/libraries/auth0-android). 17 | 18 | ## Requirements 19 | 20 | Android 4.0 or later & Google Play Services 10.2.+ 21 | 22 | ## Install 23 | 24 | The Lock-Google is available through [Maven Central](http://search.maven.org) and [JCenter](https://bintray.com/bintray/jcenter). To install it, simply add the following line to your `build.gradle`: 25 | 26 | ```gradle 27 | compile 'com.auth0.android:lock-google:1.1.0' 28 | ``` 29 | 30 | ### Google Developers Console 31 | 1. Go to the [Google Developers Console](https://console.developers.google.com/) and create a new Project. 32 | 2. Add a new **OAuth client ID** [credential](https://console.developers.google.com/apis/credentials/oauthclient) for a **Web Application**. Complete the **Authorized redirect URIs** by filling the field with your callback URL, which should look like `https://{YOUR_DOMAIN}.auth0.com/login/callback`. Take note of the `CLIENT ID` and `CLIENT SECRET` values as we're going to use them later, both in your android application as well as in the Auth0 dashboard configuration. 33 | 3. Add a new **OAuth client ID** [credential](https://console.developers.google.com/apis/credentials/oauthclient) for an **Android** application. Obtain the **SHA-1** of the certificate you're using to sign your application and complete the first field with it. Complete the last field with your android application **Package Name** and then click the Create button. Take note of the `CLIENT ID` value as we're going to use it later in the Auth0 dashboard configuration. 34 | 35 | ### Auth0 Dashboard 36 | 1. Go to the Auth0 Dashboard and click [Social Connections](https://manage.auth0.com/#/connections/social). Click **Google** and a dialog will prompt. 37 | 2. Complete the "Client ID" field with the `CLIENT ID` value obtained in the step 2 of the **Google Developers Console** section above. 38 | 3. Complete the "Client Secret" field with the `CLIENT SECRET` value obtained in the step 2 of the **Google Developers Console** section above. 39 | 4. Complete the "Allowed Mobile Client IDs" field with the `CLIENT ID` obtained in the step 3 of the **Google Developers Console** section above. Click the Save button. 40 | 5. Return to the Auth0 Dashboard and click [Clients](https://manage.auth0.com/#/clients). If you haven't created one yet, do that first and get into your client configuration page. At the bottom of the page, click the "Show Advanced Settings" link and go to the "Mobile Settings" tab. 41 | 6. In the Android section, complete the **Package Name** with your application's package name. Finally, complete the **Key Hashes** field with the SHA-256 of the certificate you're using to sign your application. Click the "Save Changes" button. 42 | 43 | ### Android Application 44 | 1. In your android application, create a new String resource in the `res/strings.xml` file. Name it `google_server_client_id` and set as value the `CLIENT_ID` obtained in the step 2 of the **Google Developers Console** setup section above. 45 | 2. Add the Google Play Services version MetaData to the `AndroidManifest.xml` file, inside the Application tag. 46 | 47 | ```xml 48 | 51 | ``` 52 | 53 | 3. Add the Internet Android permission to your `AndroidManifest.xml` file. 54 | 55 | ```xml 56 | 57 | ``` 58 | 59 | 4. When creating a new instance of the `GoogleAuthProvider` pass the `google_server_client_id` value as the first parameter: 60 | 61 | ```java 62 | public class MainActivity extends AppCompatActivity { 63 | private GoogleAuthProvider provider; 64 | // ... 65 | 66 | @Override 67 | protected void onCreate(Bundle savedInstanceState) { 68 | Auth0 auth0 = new Auth0(getString(R.string.com_auth0_client_id), getString(R.string.com_auth0_domain)); 69 | final AuthenticationAPIClient client = new AuthenticationAPIClient(auth0); 70 | provider = new GoogleAuthProvider(getString(R.string.google_server_client_id), client); 71 | } 72 | 73 | // ... 74 | } 75 | ``` 76 | 77 | Depending on your use case, you'll need to add a few more lines of code to capture the authorization result. Follow the guides below: 78 | 79 | > If you need further help with the setup, please check Google's [Sign-In for Android Guide](https://developers.google.com/identity/sign-in/android). 80 | 81 | 82 | ## Authenticate with Lock 83 | 84 | This library includes an implementation of the `AuthHandler` interface for you to use it directly with **Lock**. Create a new instance of the `GoogleAuthHandler` class passing a valid `GoogleAuthProvider`. Don't forget to customize the scopes if you need to. 85 | 86 | ```java 87 | Auth0 auth0 = new Auth0("auth0-client-id", "auth0-domain"); 88 | AuthenticationAPIClient client = new AuthenticationAPIClient(auth0); 89 | 90 | GoogleAuthProvider provider = new GoogleAuthProvider("google-server-client-id", client); 91 | provider.setScopes(new Scope(DriveScopes.DRIVE_METADATA_READONLY)); 92 | provider.setRequiredPermissions(new String[]{"android.permission.GET_ACCOUNTS"}); 93 | 94 | GoogleAuthHandler handler = new GoogleAuthHandler(provider); 95 | ``` 96 | 97 | Finally in the Lock Builder, call `withAuthHandlers` passing the recently created instance. 98 | 99 | ```java 100 | lock = Lock.newBuilder(auth0, authCallback) 101 | .withAuthHandlers(handler) 102 | //... 103 | .build(this); 104 | ``` 105 | 106 | That's it! When **Lock** needs to authenticate using that connection name, it will ask the `GoogleAuthHandler` for a valid `AuthProvider`. 107 | 108 | > We provide this demo in the `FilesActivity` class. We also use the Google Drive SDK to get the user's Drive Files and show them on a list. Because of the Drive Scope, the SDK requires the user to grant the `GET_ACCOUNTS` android permission first. Keep in mind that _this only affects this demo_ and that if you only need to authenticate the user and get his public profile, the `GoogleAuthProvider` won't ask for additional permissions. 109 | 110 | ## Authenticate without Lock 111 | 112 | Just create a new instance of `GoogleAuthProvider` with an `AuthenticationAPIClient` and the `Server Client ID` obtained in the Project's Credentials page. 113 | 114 | ```java 115 | Auth0 auth0 = new Auth0("auth0-client-id", "auth0-domain"); 116 | AuthenticationAPIClient client = new AuthenticationAPIClient(auth0); 117 | GoogleAuthProvider provider = new GoogleAuthProvider("google-server-client-id", client); 118 | ``` 119 | 120 | Override your activity's `onActivityResult` method and redirect the received parameters to the provider instance's `authorize` method. 121 | 122 | ```java 123 | @Override 124 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 125 | if (provider.authorize(requestCode, resultCode, data)) { 126 | return; 127 | } 128 | super.onActivityResult(requestCode, resultCode, data); 129 | } 130 | ``` 131 | 132 | Call `start` with a custom authentication request code to begin the authentication flow. 133 | 134 | ```java 135 | provider.start(this, callback, RC_PERMISSIONS, RC_AUTHENTICATION); 136 | ``` 137 | 138 | That's it! You'll receive the result in the `AuthCallback` you passed. 139 | 140 | > We provide this demo in the `SimpleActivity` class. 141 | 142 | 143 | ## Additional options 144 | 145 | ### Using a custom connection name 146 | To use a custom social connection name to authorize against Auth0, create the GoogleAuthProvider instance using the second constructor: 147 | 148 | ```java 149 | GoogleAuthProvider provider = new GoogleAuthProvider("my-connection", "google-server-client-id", client); 150 | ``` 151 | 152 | ### Requesting a custom Google Scope 153 | By default, the scope `Scopes.PLUS_LOGIN` is requested. You can customize the Scopes by calling `setScopes` with the list of Scopes. Each Google API (Auth, Drive, Plus..) specify it's own list of Scopes. 154 | 155 | ```java 156 | provider.setScopes(Arrays.asList(new Scope(Scopes.PLUS_ME), new Scope(Scopes.PLUS_LOGIN))); 157 | ``` 158 | 159 | ### Requesting custom Android Runtime Permissions 160 | This provider doesn't require any special Android Manifest Permission to authenticate the user. But if your use case requires them, you can let the AuthProvider handle them for you. Use the `setRequiredPermissions` method. 161 | 162 | ```java 163 | provider.setRequiredPermissions(new String[]{"android.permission.GET_ACCOUNTS"}); 164 | ``` 165 | 166 | If you're not using Lock, then you'll have to handle the permission request result yourself. To do so, make your activity implement `ActivityCompat.OnRequestPermissionsResultCallback` and override the `onRequestPermissionsResult` method, calling `provider.onRequestPermissionsResult` with the activity context and the received parameters. 167 | 168 | ### Log out / Clear account. 169 | To log out the user so that the next time he's prompted to input his credentials call `clearSession`. After you do this the provider state will be invalid and you will need to call `start` again before trying to `authorize` a result. Calling `stop` has the same effect. 170 | 171 | ```java 172 | provider.clearSession(); 173 | ``` 174 | 175 | ### Remember the Last Login 176 | By default, this provider will remember the last account used to log in. If you want to change this behavior, use the following method. 177 | 178 | ```java 179 | provider.rememberLastLogin(false); 180 | ``` 181 | 182 | ## Issue Reporting 183 | 184 | If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://auth0.com/whitehat) details the procedure for disclosing security issues. 185 | 186 | ## What is Auth0? 187 | 188 | Auth0 helps you to: 189 | 190 | * Add authentication with [multiple authentication sources](https://docs.auth0.com/identityproviders), either social like **Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, among others**, or enterprise identity systems like **Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider**. 191 | * Add authentication through more traditional **[username/password databases](https://docs.auth0.com/mysql-connection-tutorial)**. 192 | * Add support for **[linking different user accounts](https://docs.auth0.com/link-accounts)** with the same user. 193 | * Support for generating signed [Json Web Tokens](https://docs.auth0.com/jwt) to call your APIs and **flow the user identity** securely. 194 | * Analytics of how, when and where users are logging in. 195 | * Pull data from other sources and add it to the user profile, through [JavaScript rules](https://docs.auth0.com/rules). 196 | 197 | ## Create a free account in Auth0 198 | 199 | 1. Go to [Auth0](https://auth0.com) and click Sign Up. 200 | 2. Use Google, GitHub or Microsoft Account to login. 201 | 202 | ## Author 203 | 204 | [Auth0](auth0.com) 205 | 206 | ## License 207 | 208 | Lock-Google is available under the MIT license. See the [LICENSE](LICENSE) file for more info. 209 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion '25.0.2' 6 | 7 | defaultConfig { 8 | applicationId 'com.auth0.android.native.app' 9 | minSdkVersion 15 10 | targetSdkVersion 25 11 | versionCode 2 12 | versionName '2.0.0' 13 | } 14 | 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | 22 | lintOptions { 23 | lintConfig file('../gradle/lint.xml') 24 | } 25 | } 26 | 27 | dependencies { 28 | compile fileTree(dir: 'libs', include: ['*.jar']) 29 | compile project(':lock-google') 30 | compile 'com.android.support:appcompat-v7:25.1.0' 31 | compile 'com.auth0.android:lock:2.4.0' 32 | compile('com.google.apis:google-api-services-drive:v3-rev52-1.22.0') { 33 | exclude group: 'org.apache.httpcomponents' 34 | } 35 | compile('com.google.api-client:google-api-client-android:1.22.0') { 36 | exclude group: 'org.apache.httpcomponents' 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/nikolaseu/android-sdk-macosx/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 37 | 38 | 39 | 40 | 43 | 46 | 47 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /app/src/main/java/com/auth0/android/google/app/ChooserActivity.java: -------------------------------------------------------------------------------- 1 | package com.auth0.android.google.app; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.View; 8 | import android.widget.Button; 9 | 10 | public class ChooserActivity extends AppCompatActivity { 11 | 12 | @Override 13 | protected void onCreate(@Nullable Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_chooser); 16 | Button tokenLogin = (Button) findViewById(R.id.tokenLoginButton); 17 | tokenLogin.setOnClickListener(new View.OnClickListener() { 18 | @Override 19 | public void onClick(View v) { 20 | startActivity(new Intent(ChooserActivity.this, SimpleActivity.class)); 21 | } 22 | }); 23 | Button photosLogin = (Button) findViewById(R.id.photosLoginButton); 24 | photosLogin.setOnClickListener(new View.OnClickListener() { 25 | @Override 26 | public void onClick(View v) { 27 | startActivity(new Intent(ChooserActivity.this, FilesActivity.class)); 28 | } 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/auth0/android/google/app/FilesActivity.java: -------------------------------------------------------------------------------- 1 | package com.auth0.android.google.app; 2 | 3 | import android.os.AsyncTask; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.Log; 8 | import android.view.View; 9 | import android.widget.ArrayAdapter; 10 | import android.widget.Button; 11 | import android.widget.ListView; 12 | import android.widget.ProgressBar; 13 | import android.widget.Toast; 14 | 15 | import com.auth0.android.Auth0; 16 | import com.auth0.android.authentication.AuthenticationAPIClient; 17 | import com.auth0.android.authentication.AuthenticationException; 18 | import com.auth0.android.callback.BaseCallback; 19 | import com.auth0.android.google.GoogleAuthHandler; 20 | import com.auth0.android.google.GoogleAuthProvider; 21 | import com.auth0.android.lock.AuthenticationCallback; 22 | import com.auth0.android.lock.Lock; 23 | import com.auth0.android.lock.LockCallback; 24 | import com.auth0.android.lock.utils.LockException; 25 | import com.auth0.android.result.Credentials; 26 | import com.auth0.android.result.UserProfile; 27 | import com.google.android.gms.common.api.Scope; 28 | import com.google.api.client.extensions.android.http.AndroidHttp; 29 | import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; 30 | import com.google.api.client.json.jackson2.JacksonFactory; 31 | import com.google.api.services.drive.Drive; 32 | import com.google.api.services.drive.DriveScopes; 33 | import com.google.api.services.drive.model.File; 34 | import com.google.api.services.drive.model.FileList; 35 | 36 | import java.io.IOException; 37 | import java.util.ArrayList; 38 | import java.util.Collections; 39 | import java.util.List; 40 | 41 | public class FilesActivity extends AppCompatActivity { 42 | 43 | private static final String TAG = FilesActivity.class.getSimpleName(); 44 | private static final String GOOGLE_CONNECTION = "google-oauth2"; 45 | 46 | private Lock lock; 47 | private ProgressBar progressBar; 48 | private ArrayList files; 49 | private ArrayAdapter adapter; 50 | private Button loginButton; 51 | 52 | @Override 53 | protected void onCreate(@Nullable Bundle savedInstanceState) { 54 | super.onCreate(savedInstanceState); 55 | setContentView(R.layout.activity_files); 56 | 57 | final GoogleAuthProvider provider = createGoogleAuthProvider(); 58 | lock = Lock.newBuilder(getAccount(), authCallback) 59 | .withAuthHandlers(new GoogleAuthHandler(provider)) 60 | .allowedConnections(Collections.singletonList(GOOGLE_CONNECTION)) 61 | .closable(true) 62 | .build(this); 63 | 64 | loginButton = (Button) findViewById(R.id.loginButton); 65 | loginButton.setOnClickListener(new View.OnClickListener() { 66 | @Override 67 | public void onClick(View v) { 68 | startActivity(lock.newIntent(FilesActivity.this)); 69 | } 70 | }); 71 | progressBar = (ProgressBar) findViewById(R.id.progressBar); 72 | ListView listView = (ListView) findViewById(R.id.list); 73 | files = new ArrayList<>(); 74 | adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, files); 75 | listView.setAdapter(adapter); 76 | } 77 | 78 | @Override 79 | protected void onDestroy() { 80 | super.onDestroy(); 81 | lock.onDestroy(this); 82 | lock = null; 83 | } 84 | 85 | private Auth0 getAccount() { 86 | return new Auth0(getString(R.string.com_auth0_client_id), getString(R.string.com_auth0_domain)); 87 | } 88 | 89 | private GoogleAuthProvider createGoogleAuthProvider() { 90 | GoogleAuthProvider provider = new GoogleAuthProvider(getString(R.string.google_server_client_id), new AuthenticationAPIClient(getAccount())); 91 | provider.setScopes(new Scope(DriveScopes.DRIVE_METADATA_READONLY)); 92 | provider.setRequiredPermissions(new String[]{"android.permission.GET_ACCOUNTS"}); 93 | provider.rememberLastLogin(false); 94 | return provider; 95 | } 96 | 97 | private LockCallback authCallback = new AuthenticationCallback() { 98 | @Override 99 | public void onAuthentication(Credentials credentials) { 100 | Log.i(TAG, "Auth ok! User has given us all google requested permissions."); 101 | AuthenticationAPIClient client = new AuthenticationAPIClient(getAccount()); 102 | client.tokenInfo(credentials.getIdToken()) 103 | .start(new BaseCallback() { 104 | @Override 105 | public void onSuccess(UserProfile payload) { 106 | final GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(FilesActivity.this, Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY)); 107 | credential.setSelectedAccountName(payload.getEmail()); 108 | runOnUiThread(new Runnable() { 109 | @Override 110 | public void run() { 111 | new FetchFilesTask().execute(credential); 112 | } 113 | }); 114 | } 115 | 116 | @Override 117 | public void onFailure(AuthenticationException error) { 118 | } 119 | }); 120 | 121 | } 122 | 123 | @Override 124 | public void onCanceled() { 125 | Toast.makeText(FilesActivity.this, "Authentication cancelled", Toast.LENGTH_SHORT).show(); 126 | } 127 | 128 | @Override 129 | public void onError(LockException error) { 130 | Toast.makeText(FilesActivity.this, "Error occurred. Please retry.", Toast.LENGTH_SHORT).show(); 131 | Log.e(TAG, "Error occurred: " + error.getMessage()); 132 | } 133 | }; 134 | 135 | private class FetchFilesTask extends AsyncTask> { 136 | 137 | @Override 138 | protected void onPreExecute() { 139 | progressBar.setVisibility(View.VISIBLE); 140 | } 141 | 142 | @Override 143 | protected List doInBackground(GoogleAccountCredential... params) { 144 | final Drive service = new Drive.Builder( 145 | AndroidHttp.newCompatibleTransport(), 146 | JacksonFactory.getDefaultInstance(), 147 | params[0]) 148 | .setApplicationName("Auth0 Google Native Demo") 149 | .build(); 150 | 151 | List fileInfo = new ArrayList<>(); 152 | FileList result; 153 | try { 154 | result = service.files().list() 155 | .setPageSize(30) 156 | .setFields("files(name, size)") 157 | .execute(); 158 | List files = result.getFiles(); 159 | if (files != null) { 160 | for (File file : files) { 161 | if (file.getSize() != null) { 162 | fileInfo.add(String.format("[File] %s %s", file.getName(), file.getSize())); 163 | } else { 164 | fileInfo.add(String.format("[Folder] %s", file.getName())); 165 | } 166 | } 167 | } 168 | } catch (IOException e) { 169 | e.printStackTrace(); 170 | } 171 | return fileInfo; 172 | } 173 | 174 | @Override 175 | protected void onPostExecute(List names) { 176 | loginButton.setEnabled(false); 177 | progressBar.setVisibility(View.GONE); 178 | if (names.isEmpty()) { 179 | Toast.makeText(FilesActivity.this, "You have no files on Google Drive!", Toast.LENGTH_LONG).show(); 180 | return; 181 | } 182 | files.clear(); 183 | files.addAll(names); 184 | adapter.notifyDataSetChanged(); 185 | } 186 | } 187 | } -------------------------------------------------------------------------------- /app/src/main/java/com/auth0/android/google/app/SimpleActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MainActivity.java 3 | * 4 | * Copyright (c) 2015 Auth0 (http://auth0.com) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | package com.auth0.android.google.app; 26 | 27 | import android.app.AlertDialog; 28 | import android.app.Dialog; 29 | import android.content.Intent; 30 | import android.os.Bundle; 31 | import android.support.annotation.NonNull; 32 | import android.support.v7.app.AppCompatActivity; 33 | import android.util.Log; 34 | import android.view.View; 35 | import android.widget.Button; 36 | import android.widget.TextView; 37 | 38 | import com.auth0.android.Auth0; 39 | import com.auth0.android.authentication.AuthenticationAPIClient; 40 | import com.auth0.android.authentication.AuthenticationException; 41 | import com.auth0.android.google.GoogleAuthProvider; 42 | import com.auth0.android.provider.AuthCallback; 43 | import com.auth0.android.result.Credentials; 44 | 45 | 46 | public class SimpleActivity extends AppCompatActivity implements View.OnClickListener { 47 | 48 | private static final String TAG = SimpleActivity.class.getName(); 49 | private static final int RC_PERMISSIONS = 110; 50 | private static final int RC_AUTHENTICATION = 111; 51 | 52 | private GoogleAuthProvider provider; 53 | private AuthCallback callback; 54 | private TextView message; 55 | 56 | @Override 57 | protected void onCreate(Bundle savedInstanceState) { 58 | super.onCreate(savedInstanceState); 59 | setContentView(R.layout.activity_simple); 60 | 61 | message = (TextView) findViewById(R.id.textView); 62 | Button loginButton = (Button) findViewById(R.id.loginButton); 63 | Button logoutButton = (Button) findViewById(R.id.logoutButton); 64 | loginButton.setOnClickListener(this); 65 | logoutButton.setOnClickListener(this); 66 | 67 | Auth0 auth0 = new Auth0(getString(R.string.com_auth0_client_id), getString(R.string.com_auth0_domain)); 68 | final AuthenticationAPIClient client = new AuthenticationAPIClient(auth0); 69 | provider = new GoogleAuthProvider(getString(R.string.google_server_client_id), client); 70 | callback = new AuthCallback() { 71 | @Override 72 | public void onFailure(@NonNull final Dialog dialog) { 73 | Log.e(TAG, "Failed with dialog"); 74 | runOnUiThread(new Runnable() { 75 | @Override 76 | public void run() { 77 | dialog.show(); 78 | } 79 | }); 80 | } 81 | 82 | @Override 83 | public void onFailure(final AuthenticationException exception) { 84 | Log.e(TAG, "Failed", exception); 85 | runOnUiThread(new Runnable() { 86 | @Override 87 | public void run() { 88 | AlertDialog dialog = new AlertDialog.Builder(SimpleActivity.this) 89 | .setTitle("Failed!") 90 | .setMessage(exception.getLocalizedMessage()) 91 | .create(); 92 | dialog.show(); 93 | } 94 | }); 95 | } 96 | 97 | @Override 98 | public void onSuccess(@NonNull final Credentials credentials) { 99 | Log.d(TAG, "Authenticated with accessToken " + credentials.getAccessToken()); 100 | runOnUiThread(new Runnable() { 101 | @Override 102 | public void run() { 103 | message.setText(String.format("Welcome %s", credentials.getAccessToken())); 104 | } 105 | }); 106 | } 107 | 108 | }; 109 | } 110 | 111 | @Override 112 | public void onClick(View view) { 113 | final int id = view.getId(); 114 | switch (id) { 115 | case R.id.loginButton: 116 | provider.start(this, callback, RC_PERMISSIONS, RC_AUTHENTICATION); 117 | break; 118 | case R.id.logoutButton: 119 | provider.clearSession(); 120 | message.setText(""); 121 | break; 122 | } 123 | } 124 | 125 | @Override 126 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 127 | if (provider.authorize(requestCode, resultCode, data)) { 128 | return; 129 | } 130 | super.onActivityResult(requestCode, resultCode, data); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_chooser.xml: -------------------------------------------------------------------------------- 1 | 24 | 33 | 34 | 42 | 43 |