-
Notifications
You must be signed in to change notification settings - Fork 445
/
DropboxOAuthUtil.kt
91 lines (81 loc) · 3.4 KB
/
DropboxOAuthUtil.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.dropbox.core.examples.android.internal.api
import android.content.Context
import androidx.appcompat.app.AlertDialog
import com.dropbox.core.DbxRequestConfig
import com.dropbox.core.android.Auth
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class DropboxOAuthUtil(
private val dropboxCredentialUtil: DropboxCredentialUtil,
private val dropboxAppConfig: DropboxAppConfig
) {
fun showWarningDialogIfAppKeyNotSet(context: Context) {
if (dropboxAppConfig.apiKey == "PUT_YOUR_APP_KEY_HERE") {
AlertDialog.Builder(context)
.setTitle("API KEY Required")
.setMessage(
"""
You must specify a value for DROPBOX_APP_KEY in examples/android/local.properties.
You can register an application and obtain an API key at https://developers.dropbox.com/
""".trimIndent()
)
.create()
.show()
}
}
var isAwaitingResult: Boolean = false
/**
* Starts the Dropbox OAuth process by launching the Dropbox official app or web
* browser if dropbox official app is not available. In browser flow, normally user needs to
* sign in.
*
* Because mobile apps need to keep Dropbox secrets in their binaries we need to use PKCE.
* Read more about this here: https://dropbox.tech/developers/pkce--what-and-why-
**/
fun startDropboxAuthorization2PKCE(context: Context) {
val requestConfig = DbxRequestConfig(dropboxAppConfig.clientIdentifier)
// The scope's your app will need from Dropbox
// Read more about Scopes here: https://developers.dropbox.com/oauth-guide#dropbox-api-permissions
val scopes = listOf(
"account_info.read",
"files.content.write",
"files.content.read",
"sharing.read"
)
Auth.startOAuth2PKCE(context, dropboxAppConfig.apiKey, requestConfig, scopes)
isAwaitingResult = true
}
/**
* Starts the Dropbox OAuth process by launching the Dropbox official app or web
* browser if dropbox official app is not available. In browser flow, normally user needs to
* sign in.
*
* Because mobile apps need to keep Dropbox secrets in their binaries we need to use PKCE.
* Read more about this here: https://dropbox.tech/developers/pkce--what-and-why-
**/
fun startDropboxAuthorizationOAuth2(context: Context) {
Auth.startOAuth2Authentication(context, dropboxAppConfig.apiKey)
isAwaitingResult = true
}
fun revokeDropboxAuthorization(dropboxApiWrapper: DropboxApiWrapper) {
if (dropboxCredentialUtil.isAuthenticated()) {
CoroutineScope(Dispatchers.IO).launch {
dropboxApiWrapper.revokeDropboxAuthorization()
}
dropboxCredentialUtil.removeCredentialLocally()
}
}
/**
* Call this from onResume() in the activity you are awaiting an Auth Result
*/
fun onResume() {
if (isAwaitingResult) {
val authDbxCredential = Auth.getDbxCredential() //fetch the result from the AuthActivity
isAwaitingResult = false
if (authDbxCredential != null) {
dropboxCredentialUtil.storeCredentialLocally(authDbxCredential)
}
}
}
}