├── .gitignore
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── google-services.json
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── github
│ │ └── pavlospt
│ │ └── signindemo
│ │ ├── MainActivity.kt
│ │ └── SignInActivity.kt
│ └── res
│ ├── layout
│ ├── activity_main.xml
│ └── activity_sign_in.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-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── authmanager
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ └── java
│ └── com
│ └── github
│ └── charbgr
│ └── authmanager
│ ├── AuthManager.kt
│ ├── AuthManagerCodes.kt
│ ├── GoogleAuthManager.kt
│ ├── HintsManager.kt
│ ├── SmartLockManager.kt
│ ├── models
│ └── SuccessPayload.java
│ └── views
│ ├── BaseView.kt
│ ├── GoogleView.kt
│ ├── HintView.kt
│ └── SmartLockView.kt
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
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 Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 |
38 | # Keystore files
39 | *.jks
40 | *.iml
41 | /.idea/
42 | .idea/
43 | .gradle
44 | /local.properties
45 | /.idea/workspace.xml
46 | /.idea/libraries
47 | .DS_Store
48 | /build
49 | /captures
50 | .externalNativeBuild
51 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2016 Pavlos-Petros Tournaris
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 | []()
2 |
3 | # Auth Manager
4 |
5 | AuthManager is a library which eliminates the boilerplate of Google SignIn and SmartLock integration.
6 |
7 | # Usage
8 |
9 | ```groovy
10 | compile 'com.github.charbgr:authmanager:1.0'
11 | ```
12 |
13 |
14 | AuthManager includes all of the managers below.
15 |
16 | ```kotlin
17 | AuthManager
18 | .Builder(this)
19 | .withGoogleApiClient(googleApiClient)
20 | .withGoogle(this)
21 | .withHints(this, hintRequest)
22 | .withSmartLock(this, smartlockRequest)
23 | .build()
24 |
25 | override fun onDestroy() {
26 | super.onDestroy()
27 | authManager.destroy()
28 | }
29 |
30 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
31 | super.onActivityResult(requestCode, resultCode, data)
32 | authManager?.handle(requestCode, resultCode, data)
33 | }
34 |
35 | ```
36 |
37 | # Standalone Managers
38 |
39 | ## Google Manager
40 | _Functionality included: Google Sign-In, Google Sign-Out, Google Revoke Access_
41 |
42 | ```kotlin
43 | GoogleAuthManager
44 | .Builder(this)
45 | .withGoogleApiClient(googleApiClient)
46 | .withGoogleView(this)
47 | .build()
48 |
49 | override fun onDestroy() {
50 | super.onDestroy()
51 | googleAuthManager?.clear()
52 | }
53 |
54 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
55 | super.onActivityResult(requestCode, resultCode, data)
56 | when (requestCode) {
57 | RC_SIGN_IN -> googleAuthManager?.handle(resultCode, data)
58 | }
59 | }
60 | ```
61 |
62 | ### API Overview
63 |
64 | ```kotlin
65 | fun signInWithGoogle()
66 | fun signOutOfGoogle()
67 | fun revokeGoogleAccess()
68 | fun handle(resultCode: Int, data: Intent?)
69 | fun clear()
70 | ```
71 |
72 | ## Smartlock Manager
73 | _Functionality included: SmartLock Credentials Request, SmartLock Credentials Save, SmartLock Credentials Delete_
74 |
75 | ```kotlin
76 | SmartLockManager
77 | .Builder(this)
78 | .withGoogleApiClient(googleApiClient)
79 | .withSmartLockView(this)
80 | .build()
81 |
82 |
83 | override fun onDestroy() {
84 | super.onDestroy()
85 | smartLockManager?.clear()
86 | }
87 |
88 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
89 | super.onActivityResult(requestCode, resultCode, data)
90 | when (requestCode) {
91 | RC_CREDENTIAL_SAVE -> smartLockManager?.handleCredentialSave(resultCode)
92 | RC_CREDENTIALS_REQUEST -> smartLockManager?.handleCredentialRequest(resultCode, data)
93 | }
94 | }
95 | ```
96 | ### API Overview
97 |
98 | ```kotlin
99 | fun requestCredentials()
100 | fun saveCredential(credential: Credential)
101 | fun deleteCredential(credential: Credential?)
102 | fun handleCredentialSave(resultCode: Int)
103 | fun handleCredentialRequest(resultCode: Int, data: Intent?)
104 | fun clear()
105 | ```
106 |
107 | ## Hints Manager
108 | _Functionality included: E-mail Addresses Hints_
109 |
110 | ```kotlin
111 | HintsManager
112 | .Builder(this)
113 | .withGoogleApiClient(googleApiClient)
114 | .withHintsView(this)
115 | .build()
116 |
117 |
118 | override fun onDestroy() {
119 | super.onDestroy()
120 | hintsManager?.clear()
121 | }
122 |
123 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
124 | super.onActivityResult(requestCode, resultCode, data)
125 | when (requestCode) {
126 | RC_HINT_REQUEST -> hintsManager?.handle(resultCode, data)
127 | }
128 | }
129 | ```
130 | ### API Overview
131 |
132 | ```kotlin
133 | fun requestEmailHints()
134 | fun handle(resultCode: Int, data: Intent?)
135 | fun clear()
136 | ```
137 |
138 | You need to call in ```Activity.onDestroy()``` to clear view references otherwise you risk having a memory leak.
139 |
140 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 |
4 | android {
5 | compileSdkVersion 25
6 | buildToolsVersion "25.0.1"
7 | defaultConfig {
8 | applicationId "com.github.pavlospt.signindemo"
9 | minSdkVersion 21
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | compile fileTree(include: ['*.jar'], dir: 'libs')
25 | compile 'com.android.support:appcompat-v7:25.1.0'
26 | compile 'com.android.support:design:25.1.0'
27 | compile 'com.google.android.gms:play-services-auth:10.0.1'
28 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
29 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
30 | exclude group: 'com.android.support', module: 'support-annotations'
31 | })
32 | testCompile 'junit:junit:4.12'
33 | compile project(path: ':authmanager')
34 | }
--------------------------------------------------------------------------------
/app/google-services.json:
--------------------------------------------------------------------------------
1 | {
2 | "project_info": {
3 | "project_number": "1071636679426",
4 | "project_id": "ac-schedule"
5 | },
6 | "client": [
7 | {
8 | "client_info": {
9 | "mobilesdk_app_id": "1:1071636679426:android:988a70013241dfe3",
10 | "android_client_info": {
11 | "package_name": "com.github.pavlospt.signindemo"
12 | }
13 | },
14 | "oauth_client": [
15 | {
16 | "client_id": "1071636679426-2uv97ohle2q838i0dpv6j4bqblgncp06.apps.googleusercontent.com",
17 | "client_type": 1,
18 | "android_info": {
19 | "package_name": "com.github.pavlospt.signindemo",
20 | "certificate_hash": "2968F8DCC4C9A51B7A2D2AD8F90970BAE780CBD0"
21 | }
22 | },
23 | {
24 | "client_id": "1071636679426-bulvu8g7mq2goir8t6vmgvr5icjv26r9.apps.googleusercontent.com",
25 | "client_type": 3
26 | }
27 | ],
28 | "api_key": [
29 | {
30 | "current_key": "AIzaSyCJ9W7RZIlO7a3rbH2zGwI7i_VHc9_Hvag"
31 | }
32 | ],
33 | "services": {
34 | "analytics_service": {
35 | "status": 1
36 | },
37 | "appinvite_service": {
38 | "status": 1,
39 | "other_platform_oauth_client": []
40 | },
41 | "ads_service": {
42 | "status": 1
43 | }
44 | }
45 | }
46 | ],
47 | "configuration_version": "1"
48 | }
--------------------------------------------------------------------------------
/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/pavlostournaris/Library/Android/sdk/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 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/pavlospt/signindemo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.github.pavlospt.signindemo
2 |
3 | import android.content.Intent
4 | import android.support.v7.app.AppCompatActivity
5 | import android.os.Bundle
6 | import android.widget.TextView
7 |
8 | class MainActivity : AppCompatActivity() {
9 |
10 | companion object {
11 | @JvmField
12 | val USER_EMAIL_KEY: String = "user-email-key"
13 |
14 | @JvmStatic
15 | fun startActivity(activity: AppCompatActivity, userEmail: String?) {
16 | val intent: Intent = Intent(activity, MainActivity::class.java)
17 | intent.putExtra(USER_EMAIL_KEY, userEmail)
18 | activity.startActivity(intent)
19 | }
20 | }
21 |
22 | private lateinit var userEmailTextView: TextView
23 |
24 | private val userEmail: String by lazy {
25 | intent.getStringExtra(USER_EMAIL_KEY)
26 | }
27 |
28 | override fun onCreate(savedInstanceState: Bundle?) {
29 | super.onCreate(savedInstanceState)
30 | setContentView(R.layout.activity_main)
31 |
32 | initViews()
33 | initData()
34 | }
35 |
36 | private fun initData() {
37 | userEmailTextView.text = userEmail
38 | }
39 |
40 | private fun initViews() {
41 | userEmailTextView = findViewById(R.id.user_email_text_view) as TextView
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/github/pavlospt/signindemo/SignInActivity.kt:
--------------------------------------------------------------------------------
1 | package com.github.pavlospt.signindemo
2 |
3 | import android.content.Intent
4 | import android.os.Bundle
5 | import android.support.design.widget.TextInputLayout
6 | import android.support.v7.app.AlertDialog
7 | import android.support.v7.app.AppCompatActivity
8 | import android.util.Log
9 | import android.widget.Button
10 | import android.widget.Toast
11 | import com.github.charbgr.authmanager.AuthManager
12 | import com.github.charbgr.authmanager.models.SuccessPayload
13 | import com.github.charbgr.authmanager.views.GoogleView
14 | import com.github.charbgr.authmanager.views.HintView
15 | import com.github.charbgr.authmanager.views.SmartLockView
16 | import com.google.android.gms.auth.api.Auth
17 | import com.google.android.gms.auth.api.credentials.Credential
18 | import com.google.android.gms.auth.api.credentials.CredentialPickerConfig
19 | import com.google.android.gms.auth.api.credentials.CredentialRequest
20 | import com.google.android.gms.auth.api.credentials.HintRequest
21 | import com.google.android.gms.auth.api.signin.GoogleSignInOptions
22 | import com.google.android.gms.common.ConnectionResult
23 | import com.google.android.gms.common.SignInButton
24 | import com.google.android.gms.common.api.GoogleApiClient
25 | import com.google.android.gms.common.api.Status
26 |
27 |
28 | class SignInActivity : AppCompatActivity(),
29 | GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks,
30 | GoogleView, SmartLockView, HintView {
31 |
32 | private var authManager: AuthManager? = null
33 |
34 | /*
35 | * Views
36 | * */
37 | private lateinit var signInButton: SignInButton
38 | private lateinit var signOutButton: Button
39 | private lateinit var revokeAccessButton: Button
40 | private lateinit var requestHintsButton: Button
41 | private lateinit var requestUserCredentials: Button
42 | private lateinit var saveCredentialsButton: Button
43 | private lateinit var emailAddressTextInput: TextInputLayout
44 | private lateinit var passwordTextInput: TextInputLayout
45 |
46 | override fun onCreate(savedInstanceState: Bundle?) {
47 | super.onCreate(savedInstanceState)
48 |
49 | setContentView(R.layout.activity_sign_in)
50 |
51 | initViews()
52 | initAuthManager()
53 | }
54 |
55 | private fun initViews() {
56 | saveCredentialsButton = findViewById(R.id.save_credentials_button) as Button
57 | requestUserCredentials = findViewById(R.id.request_user_credentials_button) as Button
58 | requestHintsButton = findViewById(R.id.request_hints_button) as Button
59 |
60 | signInButton = findViewById(R.id.sign_in_button) as SignInButton
61 | signOutButton = findViewById(R.id.sign_out_google) as Button
62 | revokeAccessButton = findViewById(R.id.revoke_access_button) as Button
63 |
64 | emailAddressTextInput = findViewById(R.id.email_address_text_input) as TextInputLayout
65 | passwordTextInput = findViewById(R.id.password_text_input) as TextInputLayout
66 |
67 | emailAddressTextInput.clearFocus()
68 | passwordTextInput.clearFocus()
69 |
70 | signInButton.setSize(SignInButton.SIZE_WIDE)
71 |
72 | requestUserCredentials.setOnClickListener {
73 | authManager?.requestCredential()
74 | }
75 |
76 | requestHintsButton.setOnClickListener {
77 | authManager?.requestEmailHints()
78 | }
79 |
80 | signInButton.setOnClickListener {
81 | authManager?.signInWithGoogle()
82 | }
83 |
84 | signOutButton.setOnClickListener {
85 | authManager?.signOutOfGoogle()
86 | }
87 |
88 | revokeAccessButton.setOnClickListener {
89 | authManager?.revokeGoogleAccess()
90 | }
91 |
92 | saveCredentialsButton.setOnClickListener {
93 | saveCredentials()
94 | }
95 | }
96 |
97 | //region Initializations
98 |
99 | private fun initAuthManager() {
100 | val googleApiClient = createGoogleApiClient()
101 | val hintRequest = createHintRequest()
102 | val smartlockRequest = createSmartlockCredentialsRequest()
103 |
104 | authManager = AuthManager
105 | .Builder(this)
106 | .withGoogleApiClient(googleApiClient)
107 | .withGoogle(this)
108 | .withHints(this, hintRequest)
109 | .withSmartLock(this, smartlockRequest)
110 | .build()
111 | }
112 |
113 | /*
114 | * Initialize Hint request
115 | * */
116 | private fun createHintRequest() = HintRequest.Builder()
117 | .setHintPickerConfig(
118 | CredentialPickerConfig.Builder()
119 | .setShowCancelButton(true)
120 | .setPrompt(CredentialPickerConfig.Prompt.SIGN_IN)
121 | .build()
122 | )
123 | .setEmailAddressIdentifierSupported(true)
124 | .build()
125 |
126 | /*
127 | * Initialize Google Sign-in options
128 | * */
129 | private fun createGoogleSignInOptions() = GoogleSignInOptions
130 | .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
131 | .requestEmail()
132 | .build()
133 |
134 | /*
135 | * Initialize Smartlock credentials request
136 | * */
137 | private fun createSmartlockCredentialsRequest() = CredentialRequest.Builder()
138 | .setPasswordLoginSupported(true)
139 | .build()
140 |
141 | /*
142 | * Initialize Google API Client
143 | * */
144 | private fun createGoogleApiClient() = GoogleApiClient.Builder(this)
145 | .addConnectionCallbacks(this)
146 | .enableAutoManage(this, this)
147 | .addApi(Auth.GOOGLE_SIGN_IN_API, createGoogleSignInOptions())
148 | .addApi(Auth.CREDENTIALS_API)
149 | .build()
150 |
151 | //endregion
152 |
153 | //region GoogleSignIn
154 |
155 | /*
156 | * Google Sign-In Failure
157 | * */
158 | override fun googleSignInResultFailure() {
159 | Toast.makeText(this, "Authentication failure", Toast.LENGTH_SHORT).show()
160 | }
161 |
162 | /*
163 | * User Cancelled Google Sign-In
164 | * */
165 | override fun userCancelledGoogleSignIn() {
166 | Toast.makeText(this, "User cancelled Google Sign-In flow", Toast.LENGTH_SHORT).show()
167 | }
168 | //endregion
169 |
170 | //region CredentialsRequest
171 |
172 |
173 | /*
174 | * Credential Request Cancelled
175 | * */
176 | override fun credentialRequestCancelled() {
177 | Toast.makeText(this, "Credential Request cancelled", Toast.LENGTH_SHORT).show()
178 | }
179 |
180 | /*
181 | * Credential Request Resolution Failed
182 | * */
183 | override fun credentialRequestResolutionFailure() {
184 | Toast.makeText(this, "Credential Request Resolution failure", Toast.LENGTH_SHORT).show()
185 | }
186 |
187 | /*
188 | * Credential Request Failed
189 | * */
190 | override fun credentialRequestFailure() {
191 | Toast.makeText(this, "Credential Request failure", Toast.LENGTH_SHORT).show()
192 | }
193 |
194 | //endregion
195 |
196 | /*
197 | * Hint Request Cancelled
198 | * */
199 | override fun emailHintRequestCancelled() {
200 | Toast.makeText(this, "Hint Request cancelled by user", Toast.LENGTH_SHORT).show()
201 | }
202 |
203 | /*
204 | * Hint Request Failed
205 | * */
206 | override fun emailHintRequestFailure() {
207 | Toast.makeText(this, "Hint Request failure", Toast.LENGTH_SHORT).show()
208 | }
209 |
210 | //endregion
211 |
212 | //region CredentialsSave
213 |
214 | /*
215 | * Initiate Credential save flow
216 | * */
217 | private fun saveCredentials() {
218 |
219 | val emailInvalid: Boolean = emailAddressTextInput.editText?.text.toString().trim().isNullOrEmpty() ?: false
220 | val passwordInvalid: Boolean = passwordTextInput.editText?.text.toString().trim().isNullOrEmpty() ?: false
221 |
222 | if (emailInvalid) {
223 | emailRequirementError()
224 | return
225 | }
226 |
227 | if (passwordInvalid) {
228 | passwordRequirementError()
229 | return
230 | }
231 |
232 | val credentialToSave: Credential =
233 | Credential
234 | .Builder(emailAddressTextInput.editText?.text.toString())
235 | .setPassword(passwordTextInput.editText?.text.toString().trim())
236 | .build()
237 |
238 | authManager?.saveCredential(credentialToSave)
239 | }
240 |
241 | /*
242 | * User Cancelled Credential Save Resolution
243 | * */
244 | override fun credentialSaveResolutionCancelled() {
245 | Toast.makeText(this, "User cancelled credential save resolution", Toast.LENGTH_SHORT).show()
246 | }
247 |
248 | /*
249 | * Credential Save Resolution Failed
250 | * */
251 | override fun credentialSaveResolutionFailure() {
252 | Toast.makeText(this, "Credential save resolution failed", Toast.LENGTH_SHORT).show()
253 | }
254 |
255 | /*
256 | * Credential Save Failed
257 | * */
258 | override fun credentialSaveFailure() {
259 | Toast.makeText(this, "Credential save failed", Toast.LENGTH_SHORT).show()
260 | }
261 |
262 | /*
263 | * Credential Save Success
264 | * */
265 | override fun credentialSaveSuccess() {
266 | Toast.makeText(this, "Credentials successfully saved", Toast.LENGTH_SHORT).show()
267 | }
268 |
269 | /*
270 | * Require Email Address Not Empty
271 | * */
272 | private fun emailRequirementError() {
273 | Toast.makeText(this, "Email address must not be empty", Toast.LENGTH_SHORT).show()
274 | }
275 |
276 | /*
277 | * Require Password Not Empty
278 | * */
279 | private fun passwordRequirementError() {
280 | Toast.makeText(this, "Password must not be empty", Toast.LENGTH_SHORT).show()
281 | }
282 | //endregion
283 |
284 | override fun emailHintSelected(credential: Credential?) {
285 | MainActivity.startActivity(this, credential?.id)
286 | }
287 |
288 | override fun signInSuccess(signInSuccess: SuccessPayload) {
289 | if (signInSuccess.hasCredential()) {
290 | showCredentialDialog(signInSuccess.credential)
291 | } else if (signInSuccess.hasGoogleSignInAccount()) {
292 | MainActivity.startActivity(this, signInSuccess.googleSignInAccount.email)
293 | }
294 | }
295 |
296 | private fun showCredentialDialog(credential: Credential?) {
297 | val alertDialogBuilder: AlertDialog.Builder = AlertDialog
298 | .Builder(this)
299 | .setTitle(R.string.credential_received)
300 | .setMessage(getString(R.string.what_do_you_want_to_do_with_credential, credential?.id))
301 | .setPositiveButton(getString(R.string.use_credential), { dialogInterface, i ->
302 | MainActivity.startActivity(this@SignInActivity, credential?.id)
303 | })
304 | .setNegativeButton(getString(R.string.delete_credential), { dialogInterface, i ->
305 | authManager?.deleteCredential(credential)
306 | dialogInterface.dismiss()
307 | dialogInterface.cancel()
308 | })
309 |
310 | val alertDialog: AlertDialog = alertDialogBuilder.create()
311 | alertDialog.show()
312 | }
313 |
314 | override fun googleSignOut(status: Status) {
315 | Toast.makeText(this, "Google Sign-out with status: $status", Toast.LENGTH_SHORT).show()
316 | }
317 |
318 | override fun googleAccessRevoked(status: Status) {
319 | Toast.makeText(this, "Google Access Revoked with status: $status", Toast.LENGTH_SHORT).show()
320 | }
321 |
322 |
323 | override fun credentialDelete(status: Status) {
324 | Toast.makeText(this, "Credential deleted with status: $status", Toast.LENGTH_SHORT).show()
325 | }
326 |
327 | /*
328 | * Google API Connection Failed
329 | * */
330 | override fun onConnectionFailed(p0: ConnectionResult) {
331 | Log.e(localClassName, "GoogleApiClient connection failed")
332 | }
333 |
334 | /*
335 | * Google API Connection Connected
336 | * */
337 | override fun onConnected(p0: Bundle?) {
338 | Log.e(localClassName, "GoogleApiClient connected")
339 | }
340 |
341 | /*
342 | * Google API Connection Suspended
343 | * */
344 | override fun onConnectionSuspended(p0: Int) {
345 | Log.e(localClassName, "GoogleApiClient connection suspended")
346 | }
347 |
348 | /*
349 | * Handle activity result after Google Sign-In, Credential Request or Hints requested resolution
350 | * */
351 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
352 | super.onActivityResult(requestCode, resultCode, data)
353 | authManager?.handle(requestCode, resultCode, data)
354 | }
355 |
356 | override fun onDestroy() {
357 | super.onDestroy()
358 | authManager?.destroy()
359 | }
360 | }
361 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_sign_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
16 |
17 |
21 |
22 |
27 |
28 |
29 |
30 |
34 |
35 |
40 |
41 |
42 |
43 |
48 |
49 |
54 |
55 |
60 |
61 |
66 |
67 |
72 |
73 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/infamous-riddles/AuthManager/f0b3f053cf51dbb05e1e53206a0d83ac8022946d/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/infamous-riddles/AuthManager/f0b3f053cf51dbb05e1e53206a0d83ac8022946d/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/infamous-riddles/AuthManager/f0b3f053cf51dbb05e1e53206a0d83ac8022946d/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/infamous-riddles/AuthManager/f0b3f053cf51dbb05e1e53206a0d83ac8022946d/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/infamous-riddles/AuthManager/f0b3f053cf51dbb05e1e53206a0d83ac8022946d/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SignInDemoApp
3 | 1071636679426-2uv97ohle2q838i0dpv6j4bqblgncp06.apps.googleusercontent.com
4 | Request Hints
5 | Request User Credentials
6 | Email Address
7 | Password
8 | Save Credentials
9 | Google Sign-Out
10 | Revoke Access
11 | Credential
12 | Use it
13 | Delete it
14 | What do you want to do with the "%1$s" Credential?
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/authmanager/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/authmanager/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'kotlin-android'
3 |
4 | android {
5 | compileSdkVersion 25
6 | buildToolsVersion "25.0.1"
7 |
8 | defaultConfig {
9 | minSdkVersion 19
10 | targetSdkVersion 25
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 |
15 | sourceSets {
16 | main.java.srcDirs += 'src/main/kotlin'
17 | }
18 | }
19 |
20 | tasks.withType(Javadoc).all { enabled = false }
21 |
22 | dependencies {
23 | compile 'com.android.support:appcompat-v7:25.1.0'
24 | compile 'com.google.android.gms:play-services-auth:10.0.1'
25 | compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
26 | }
27 |
28 | repositories {
29 | mavenCentral()
30 | }
31 |
32 | ext {
33 | PUBLISH_GROUP_ID = 'com.github.charbgr'
34 | PUBLISH_ARTIFACT_ID = 'authmanager'
35 | PUBLISH_VERSION = '1.0'
36 | }
37 |
38 | apply from: 'https://raw.githubusercontent.com/ArthurHub/release-android-library/master/android-release-aar.gradle'
39 |
40 | //apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
--------------------------------------------------------------------------------
/authmanager/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/pavlostournaris/Library/Android/sdk/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 |
--------------------------------------------------------------------------------
/authmanager/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/AuthManager.kt:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager
2 |
3 | import android.content.Intent
4 | import android.support.v7.app.AppCompatActivity
5 | import com.github.charbgr.authmanager.views.GoogleView
6 | import com.github.charbgr.authmanager.views.HintView
7 | import com.github.charbgr.authmanager.views.SmartLockView
8 | import com.google.android.gms.auth.api.credentials.Credential
9 | import com.google.android.gms.auth.api.credentials.CredentialRequest
10 | import com.google.android.gms.auth.api.credentials.HintRequest
11 | import com.google.android.gms.common.api.GoogleApiClient
12 |
13 | class AuthManager private constructor(builder: AuthManagerBuilder) {
14 |
15 | companion object {
16 |
17 | @JvmStatic
18 | fun Builder(activity: AppCompatActivity): AuthManagerBuilder = AuthManagerBuilder(activity)
19 |
20 | @JvmStatic
21 | fun withBuilder(authManagerBuilder: AuthManagerBuilder): AuthManager {
22 | return AuthManager(authManagerBuilder)
23 | }
24 | }
25 |
26 | private var activity: AppCompatActivity
27 |
28 | private var googleApiClient: GoogleApiClient? = null
29 |
30 | private var googleAuthManager: GoogleAuthManager? = null
31 | private var smartLockManager: SmartLockManager? = null
32 | private var hintsManager: HintsManager? = null
33 |
34 | init {
35 |
36 | activity = builder.activity
37 |
38 | googleApiClient = builder.googleApiClient
39 |
40 | googleAuthManager = builder.googleAuthManager
41 | smartLockManager = builder.smartLockManager
42 | hintsManager = builder.hintsManager
43 | }
44 |
45 | fun signInWithGoogle() {
46 | googleAuthManager?.signInWithGoogle()
47 | }
48 |
49 | fun signOutOfGoogle() {
50 | googleAuthManager?.signOutOfGoogle()
51 | }
52 |
53 | fun revokeGoogleAccess() {
54 | googleAuthManager?.revokeGoogleAccess()
55 | }
56 |
57 | fun requestCredential() {
58 | smartLockManager?.requestCredentials()
59 | }
60 |
61 | fun saveCredential(credential: Credential) {
62 | smartLockManager?.saveCredential(credential)
63 | }
64 |
65 | fun deleteCredential(credential: Credential?) {
66 | smartLockManager?.deleteCredential(credential)
67 | }
68 |
69 | /**
70 | * Request Email Hints
71 | * */
72 | fun requestEmailHints() {
73 | hintsManager?.requestEmailHints()
74 | }
75 |
76 | fun handle(requestCode: Int, resultCode: Int, data: Intent?) {
77 | when (requestCode) {
78 | AuthManagerCodes.RC_SIGN_IN -> googleAuthManager?.handle(resultCode, data)
79 | AuthManagerCodes.RC_HINT_REQUEST -> hintsManager?.handle(resultCode, data)
80 | AuthManagerCodes.RC_CREDENTIAL_SAVE -> smartLockManager?.handleCredentialSave(resultCode)
81 | AuthManagerCodes.RC_CREDENTIALS_REQUEST -> smartLockManager?.handleCredentialRequest(resultCode, data)
82 | }
83 | }
84 |
85 | fun destroy() {
86 | googleAuthManager?.clear()
87 | smartLockManager?.clear()
88 | hintsManager?.clear()
89 | }
90 |
91 | class AuthManagerBuilder internal constructor(val activity: AppCompatActivity) {
92 |
93 | lateinit var googleAuthManager: GoogleAuthManager
94 | private set
95 |
96 | lateinit var smartLockManager: SmartLockManager
97 | private set
98 |
99 | lateinit var hintsManager: HintsManager
100 | private set
101 |
102 | lateinit var googleApiClient: GoogleApiClient
103 | private set
104 |
105 | fun withGoogleApiClient(googleApiClient: GoogleApiClient) = apply {
106 | this.googleApiClient = googleApiClient
107 | }
108 |
109 | fun withGoogle(googleView: GoogleView) = apply {
110 | this.googleAuthManager = GoogleAuthManager
111 | .Builder(activity)
112 | .withGoogleApiClient(googleApiClient)
113 | .withGoogleView(googleView)
114 | .build()
115 | }
116 |
117 | fun withSmartLock(smartLockView: SmartLockView, smartLockCredentialRequest: CredentialRequest) = apply {
118 | this.smartLockManager = SmartLockManager
119 | .Builder(activity)
120 | .withGoogleApiClient(googleApiClient)
121 | .withSmartLockView(smartLockView)
122 | .withSmartLockCredentialRequest(smartLockCredentialRequest)
123 | .build()
124 | }
125 |
126 | fun withHints(hintView: HintView, hintRequest: HintRequest) = apply {
127 | this.hintsManager = HintsManager
128 | .Builder(activity)
129 | .withGoogleApiClient(googleApiClient)
130 | .withHintsView(hintView)
131 | .withHintRequest(hintRequest)
132 | .build()
133 | }
134 |
135 | fun build(): AuthManager = withBuilder(this)
136 | }
137 |
138 | }
139 |
140 |
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/AuthManagerCodes.kt:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager
2 |
3 |
4 | class AuthManagerCodes private constructor() {
5 |
6 | companion object {
7 | @JvmField
8 | val RC_SIGN_IN = 1
9 |
10 | @JvmField
11 | val RC_CREDENTIALS_REQUEST = 2
12 |
13 | @JvmField
14 | val RC_HINT_REQUEST = 3
15 |
16 | @JvmField
17 | val RC_CREDENTIAL_SAVE = 4
18 | }
19 |
20 | }
21 |
22 |
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/GoogleAuthManager.kt:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager
2 |
3 | import android.content.Intent
4 | import android.support.v7.app.AppCompatActivity
5 | import android.util.Log
6 | import com.github.charbgr.authmanager.models.SuccessPayload
7 | import com.github.charbgr.authmanager.views.GoogleView
8 | import com.google.android.gms.auth.api.Auth
9 | import com.google.android.gms.auth.api.signin.GoogleSignInAccount
10 | import com.google.android.gms.auth.api.signin.GoogleSignInResult
11 | import com.google.android.gms.auth.api.signin.GoogleSignInStatusCodes
12 | import com.google.android.gms.common.api.GoogleApiClient
13 | import java.lang.ref.WeakReference
14 |
15 | class GoogleAuthManager private constructor(builder: GoogleAuthManagerBuilder) {
16 |
17 | companion object {
18 |
19 | @JvmStatic
20 | fun Builder(activity: AppCompatActivity): GoogleAuthManagerBuilder
21 | = GoogleAuthManagerBuilder(activity)
22 |
23 | @JvmStatic
24 | fun withBuilder(googleAuthManagerBuilder: GoogleAuthManagerBuilder): GoogleAuthManager {
25 | return GoogleAuthManager(googleAuthManagerBuilder)
26 | }
27 | }
28 |
29 | private var activity: AppCompatActivity
30 |
31 | private var googleView: WeakReference
32 | private var googleApiClient: GoogleApiClient? = null
33 |
34 | init {
35 | activity = builder.activity
36 |
37 | googleView = WeakReference(builder.googleView)
38 | googleApiClient = builder.googleApiClient
39 | }
40 |
41 | fun signInWithGoogle() {
42 | val signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient)
43 | activity.startActivityForResult(signInIntent, AuthManagerCodes.RC_SIGN_IN)
44 | }
45 |
46 | fun signOutOfGoogle() {
47 | Auth
48 | .GoogleSignInApi
49 | .signOut(googleApiClient)
50 | .setResultCallback {
51 | googleView.get()?.googleSignOut(it)
52 | }
53 | }
54 |
55 | fun revokeGoogleAccess() {
56 | Auth
57 | .GoogleSignInApi
58 | .revokeAccess(googleApiClient)
59 | .setResultCallback {
60 | googleView.get()?.googleAccessRevoked(it)
61 | }
62 | }
63 |
64 | fun handle(resultCode: Int, data: Intent?) {
65 | handleGoogleSignInResolution(resultCode, data)
66 | }
67 |
68 | fun clear() {
69 | googleView.clear()
70 | }
71 |
72 | /**
73 | * Handle Sign In Resolution
74 | * */
75 | private fun handleGoogleSignInResolution(resultCode: Int, data: Intent?) {
76 |
77 | val googleSignInResult: GoogleSignInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
78 |
79 | if (resultCode == AppCompatActivity.RESULT_CANCELED) {
80 | googleView.get()?.userCancelledGoogleSignIn()
81 | } else {
82 |
83 |
84 | if (googleSignInResult.isSuccess) {
85 | val googleSignInAccount: GoogleSignInAccount? = googleSignInResult.signInAccount
86 | googleView.get()?.signInSuccess(SuccessPayload(googleSignInAccount, null))
87 | } else {
88 | googleView.get()?.googleSignInResultFailure()
89 | }
90 | }
91 | }
92 |
93 | class GoogleAuthManagerBuilder internal constructor(val activity: AppCompatActivity) {
94 |
95 | lateinit var googleView: GoogleView
96 | private set
97 |
98 | lateinit var googleApiClient: GoogleApiClient
99 | private set
100 |
101 | fun withGoogleApiClient(googleApiClient: GoogleApiClient) = apply {
102 | this.googleApiClient = googleApiClient
103 | }
104 |
105 | fun withGoogleView(googleView: GoogleView) = apply {
106 | this.googleView = googleView
107 | }
108 |
109 | fun build(): GoogleAuthManager = Companion.withBuilder(this)
110 |
111 | }
112 |
113 | }
114 |
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/HintsManager.kt:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager
2 |
3 | import android.content.Intent
4 | import android.content.IntentSender
5 | import android.support.v7.app.AppCompatActivity
6 | import com.github.charbgr.authmanager.views.HintView
7 | import com.google.android.gms.auth.api.Auth
8 | import com.google.android.gms.auth.api.credentials.Credential
9 | import com.google.android.gms.auth.api.credentials.HintRequest
10 | import com.google.android.gms.common.api.GoogleApiClient
11 | import java.lang.ref.WeakReference
12 |
13 |
14 | class HintsManager private constructor(builder: HintsManagerBuilder) {
15 |
16 | companion object {
17 |
18 | @JvmStatic
19 | fun Builder(activity: AppCompatActivity?): HintsManagerBuilder
20 | = HintsManagerBuilder(activity)
21 |
22 | @JvmStatic
23 | fun withBuilder(hintsManagerBuilder: HintsManagerBuilder): HintsManager {
24 | return HintsManager(hintsManagerBuilder)
25 | }
26 | }
27 |
28 | private var activity: AppCompatActivity?
29 |
30 | private var googleApiClient: GoogleApiClient? = null
31 |
32 | private var hintView: WeakReference
33 | private var hintRequest: HintRequest? = null
34 |
35 | init {
36 | activity = builder.activity
37 |
38 | googleApiClient = builder.googleApiClient
39 |
40 | hintView = WeakReference(builder.hintView)
41 | hintRequest = builder.hintRequest
42 | }
43 |
44 | /**
45 | * Request Email Hints
46 | * */
47 | fun requestEmailHints() {
48 | val intent = Auth.CredentialsApi.getHintPickerIntent(googleApiClient, hintRequest)
49 | try {
50 | activity?.startIntentSenderForResult(intent.intentSender, AuthManagerCodes.RC_HINT_REQUEST, null, 0, 0, 0)
51 | } catch (e: IntentSender.SendIntentException) {
52 | hintView.get()?.emailHintRequestFailure()
53 | }
54 | }
55 |
56 | fun handle(resultCode: Int, data: Intent?) {
57 | handleEmailHintRequestResolution(resultCode, data)
58 | }
59 |
60 | fun clear() {
61 | hintView.clear()
62 | }
63 |
64 | /**
65 | * Handle Hint Request Resolution
66 | * */
67 | private fun handleEmailHintRequestResolution(resultCode: Int, data: Intent?) {
68 | if (resultCode == AppCompatActivity.RESULT_CANCELED) {
69 | hintView.get()?.emailHintRequestCancelled()
70 | } else {
71 | val credential: Credential? = data?.getParcelableExtra(Credential.EXTRA_KEY)
72 | credential?.let {
73 | hintView.get()?.emailHintSelected(it)
74 | }
75 | }
76 | }
77 |
78 | class HintsManagerBuilder internal constructor(val activity: AppCompatActivity?) {
79 |
80 | lateinit var hintView: HintView
81 | private set
82 |
83 | lateinit var hintRequest: HintRequest
84 | private set
85 |
86 | lateinit var googleApiClient: GoogleApiClient
87 | private set
88 |
89 | fun withGoogleApiClient(googleApiClient: GoogleApiClient) = apply {
90 | this.googleApiClient = googleApiClient
91 | }
92 |
93 | fun withHintsView(hintView: HintView) = apply {
94 | this.hintView = hintView
95 | }
96 |
97 | fun withHintRequest(hintRequest: HintRequest) = apply {
98 | this.hintRequest = hintRequest
99 | }
100 |
101 | fun build(): HintsManager = Companion.withBuilder(this)
102 | }
103 |
104 | }
105 |
106 |
107 |
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/SmartLockManager.kt:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager
2 |
3 | import android.content.Intent
4 | import android.content.IntentSender
5 | import android.support.v7.app.AppCompatActivity
6 | import com.github.charbgr.authmanager.models.SuccessPayload
7 | import com.github.charbgr.authmanager.views.SmartLockView
8 | import com.google.android.gms.auth.api.Auth
9 | import com.google.android.gms.auth.api.credentials.Credential
10 | import com.google.android.gms.auth.api.credentials.CredentialRequest
11 | import com.google.android.gms.auth.api.credentials.CredentialRequestResult
12 | import com.google.android.gms.common.api.CommonStatusCodes
13 | import com.google.android.gms.common.api.GoogleApiClient
14 | import com.google.android.gms.common.api.Result
15 | import com.google.android.gms.common.api.Status
16 | import java.lang.ref.WeakReference
17 |
18 | class SmartLockManager private constructor(builder: SmartLockManagerBuilder) {
19 |
20 | companion object {
21 |
22 | @JvmStatic
23 | fun Builder(activity: AppCompatActivity): SmartLockManagerBuilder
24 | = SmartLockManagerBuilder(activity)
25 |
26 | @JvmStatic
27 | fun withBuilder(smartLockManagerBuilder: SmartLockManagerBuilder): SmartLockManager {
28 | return SmartLockManager(smartLockManagerBuilder)
29 | }
30 | }
31 |
32 | private var activity: AppCompatActivity
33 |
34 | private var googleApiClient: GoogleApiClient? = null
35 |
36 | private var smartLockView: WeakReference
37 | private var smartLockCredentialsRequest: CredentialRequest? = null
38 |
39 | init {
40 | activity = builder.activity
41 |
42 | googleApiClient = builder.googleApiClient
43 |
44 | smartLockView = WeakReference(builder.smartLockView)
45 | smartLockCredentialsRequest = builder.smartLockCredentialRequest
46 | }
47 |
48 | /**
49 | * Request Credentials
50 | * */
51 | fun requestCredentials() {
52 | Auth.CredentialsApi
53 | .request(googleApiClient, smartLockCredentialsRequest)
54 | .setResultCallback {
55 | handleCredentialRequestResult(it)
56 | }
57 | }
58 |
59 | /**
60 | * Save Credentials
61 | * */
62 | fun saveCredential(credential: Credential) {
63 | Auth
64 | .CredentialsApi
65 | .save(googleApiClient, credential)
66 | .setResultCallback({
67 | handleCredentialSaveResult(it)
68 | })
69 | }
70 |
71 | /**
72 | * Delete Credentials
73 | * */
74 | fun deleteCredential(credential: Credential?) {
75 | credential?.let {
76 | Auth
77 | .CredentialsApi
78 | .delete(googleApiClient, credential)
79 | .setResultCallback {
80 | smartLockView.get()?.credentialDelete(it)
81 | }
82 | }
83 | }
84 |
85 | /**
86 | * Handle Credential Save Resolution
87 | * */
88 | fun handleCredentialSave(resultCode: Int) {
89 | if (resultCode == AppCompatActivity.RESULT_CANCELED) {
90 | smartLockView.get()?.credentialSaveResolutionCancelled()
91 | } else {
92 | smartLockView.get()?.credentialSaveSuccess()
93 | }
94 | }
95 |
96 | /**
97 | * Handle Credential Request Resolution
98 | * */
99 | fun handleCredentialRequest(resultCode: Int, data: Intent?) {
100 | if (resultCode == AppCompatActivity.RESULT_CANCELED) {
101 | smartLockView.get()?.credentialRequestCancelled()
102 | } else {
103 | val credential: Credential? = data?.getParcelableExtra(Credential.EXTRA_KEY)
104 | credential?.let {
105 | smartLockView.get()?.signInSuccess(SuccessPayload(null, it))
106 | }
107 | }
108 | }
109 |
110 | fun clear() {
111 | smartLockView.clear()
112 | }
113 |
114 | /**
115 | * Handle Credential Save Result
116 | * */
117 | private fun handleCredentialSaveResult(result: Result) {
118 | val status: Status = result.status
119 |
120 | if (status.isSuccess) {
121 | smartLockView.get()?.credentialSaveSuccess()
122 | } else {
123 | handlePossibleCredentialSaveResolution(status)
124 | }
125 | }
126 |
127 | /**
128 | * Handle Possible Credential Save Resolution
129 | * */
130 | private fun handlePossibleCredentialSaveResolution(status: Status) {
131 | if (status.hasResolution()) {
132 | try {
133 | status.startResolutionForResult(activity, AuthManagerCodes.RC_CREDENTIAL_SAVE)
134 | } catch (e: IntentSender.SendIntentException) {
135 | smartLockView.get()?.credentialSaveResolutionFailure()
136 | }
137 | } else {
138 | smartLockView.get()?.credentialSaveFailure()
139 | }
140 | }
141 |
142 | /**
143 | * Handle Credential Request Result
144 | * */
145 | private fun handleCredentialRequestResult(credentialRequestResult: CredentialRequestResult) {
146 | if (credentialRequestResult.status.isSuccess) {
147 | smartLockView.get()?.signInSuccess(SuccessPayload(null, credentialRequestResult.credential))
148 | } else {
149 | resolveCredentialRequest(credentialRequestResult.status)
150 | }
151 | }
152 |
153 |
154 | private fun resolveCredentialRequest(status: Status?) {
155 | if (eligibleForResolution(status)) {
156 | initiateCredentialRequestResolution(status)
157 | } else {
158 | smartLockView.get()?.credentialRequestFailure()
159 | }
160 | }
161 |
162 | private fun eligibleForResolution(status: Status?) =
163 | status?.statusCode == CommonStatusCodes.RESOLUTION_REQUIRED
164 | || status?.statusCode == CommonStatusCodes.SIGN_IN_REQUIRED
165 |
166 | /**
167 | * Initiate Credential Request Resolution
168 | * */
169 | private fun initiateCredentialRequestResolution(status: Status?) {
170 | try {
171 | status?.startResolutionForResult(activity, AuthManagerCodes.RC_CREDENTIALS_REQUEST)
172 | } catch (sendIntentException: IntentSender.SendIntentException) {
173 | smartLockView.get()?.credentialRequestResolutionFailure()
174 | }
175 | }
176 |
177 | class SmartLockManagerBuilder internal constructor(val activity: AppCompatActivity) {
178 |
179 | lateinit var smartLockView: SmartLockView
180 | private set
181 |
182 | lateinit var smartLockCredentialRequest: CredentialRequest
183 | private set
184 |
185 | lateinit var googleApiClient: GoogleApiClient
186 | private set
187 |
188 | fun withGoogleApiClient(googleApiClient: GoogleApiClient) = apply {
189 | this.googleApiClient = googleApiClient
190 | }
191 |
192 | fun withSmartLockView(smartLockView: SmartLockView) = apply {
193 | this.smartLockView = smartLockView
194 | }
195 |
196 | fun withSmartLockCredentialRequest(smartLockCredentialRequest: CredentialRequest) = apply {
197 | this.smartLockCredentialRequest = smartLockCredentialRequest
198 | }
199 |
200 | fun build(): SmartLockManager = withBuilder(this)
201 | }
202 |
203 | }
204 |
205 |
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/models/SuccessPayload.java:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager.models;
2 |
3 | import com.google.android.gms.auth.api.credentials.Credential;
4 | import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
5 |
6 | public class SuccessPayload {
7 |
8 | private GoogleSignInAccount googleSignInAccount;
9 | private Credential credential;
10 |
11 | public SuccessPayload(GoogleSignInAccount googleSignInAccount, Credential credential) {
12 | this.googleSignInAccount = googleSignInAccount;
13 | this.credential = credential;
14 | }
15 |
16 | public boolean hasGoogleSignInAccount() {
17 | return googleSignInAccount != null;
18 | }
19 |
20 | public GoogleSignInAccount getGoogleSignInAccount() {
21 | return googleSignInAccount;
22 | }
23 |
24 | public boolean hasCredential() {
25 | return credential != null;
26 | }
27 |
28 | public Credential getCredential() {
29 | return credential;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/views/BaseView.kt:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager.views
2 |
3 | import com.github.charbgr.authmanager.models.SuccessPayload
4 |
5 | interface BaseView {
6 | fun signInSuccess(signInSuccess: SuccessPayload)
7 | }
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/views/GoogleView.kt:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager.views
2 |
3 | import com.google.android.gms.common.api.Status
4 |
5 | interface GoogleView : BaseView {
6 | fun googleSignInResultFailure()
7 | fun userCancelledGoogleSignIn()
8 | fun googleSignOut(status: Status)
9 | fun googleAccessRevoked(status: Status)
10 | }
11 |
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/views/HintView.kt:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager.views
2 |
3 | import com.google.android.gms.auth.api.credentials.Credential
4 |
5 | interface HintView : BaseView {
6 | fun emailHintRequestCancelled()
7 | fun emailHintRequestFailure()
8 | fun emailHintSelected(credential: Credential?)
9 | }
10 |
--------------------------------------------------------------------------------
/authmanager/src/main/java/com/github/charbgr/authmanager/views/SmartLockView.kt:
--------------------------------------------------------------------------------
1 | package com.github.charbgr.authmanager.views
2 |
3 | import com.google.android.gms.common.api.Status
4 |
5 | interface SmartLockView : BaseView {
6 |
7 | fun credentialSaveResolutionCancelled()
8 | fun credentialSaveResolutionFailure()
9 |
10 | fun credentialRequestFailure()
11 | fun credentialRequestResolutionFailure()
12 | fun credentialRequestCancelled()
13 |
14 | fun credentialSaveFailure()
15 | fun credentialSaveSuccess()
16 | fun credentialDelete(status: Status)
17 | }
18 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | ext.kotlin_version = '1.0.5-2'
5 | repositories {
6 | jcenter()
7 | }
8 | dependencies {
9 | classpath 'com.android.tools.build:gradle:2.2.3'
10 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11 |
12 | // NOTE: Do not place your application dependencies here; they belong
13 | // in the individual module build.gradle files
14 | }
15 | }
16 |
17 | allprojects {
18 | repositories {
19 | jcenter()
20 | }
21 | }
22 |
23 | task clean(type: Delete) {
24 | delete rootProject.buildDir
25 | }
26 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 | VERSION_NAME=1.0
19 | VERSION_CODE=1
20 | GROUP=com.github.charbgr
21 |
22 | POM_DESCRIPTION=Google Sign-In and SmartLock Manager
23 | POM_URL=https://github.com/charbgr/AuthManager
24 | POM_SCM_URL=https://github.com/charbgr/AuthManager
25 | POM_SCM_CONNECTION=scm:git@github.com:charbgr/AuthManager.git
26 | POM_SCM_DEV_CONNECTION=scm:git@github.com:charbgr/AuthManager.git
27 | POM_LICENCE_NAME=MIT License
28 | POM_LICENCE_URL=https://opensource.org/licenses/MIT
29 | POM_LICENCE_DIST=repo
30 | POM_ARTIFACT_ID=authmanager
31 | POM_NAME=AuthManager
32 | POM_PACKAGING=aar
33 | POM_DEVELOPER_ID=charbgr
34 | POM_DEVELOPER_NAME=Charalampakis Vasilis
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/infamous-riddles/AuthManager/f0b3f053cf51dbb05e1e53206a0d83ac8022946d/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':authmanager'
2 |
--------------------------------------------------------------------------------