-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLoginActivity.kt
More file actions
164 lines (142 loc) · 5.96 KB
/
LoginActivity.kt
File metadata and controls
164 lines (142 loc) · 5.96 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright 2015 The AppAuth for Android Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fusionauth.sdk
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.annotation.MainThread
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.Snackbar
import io.fusionauth.mobilesdk.AuthorizationConfiguration
import io.fusionauth.mobilesdk.AuthorizationManager
import io.fusionauth.mobilesdk.exceptions.AuthorizationException
import io.fusionauth.mobilesdk.oauth.OAuthAuthorizeOptions
import io.fusionauth.mobilesdk.storage.SharedPreferencesStorage
import kotlinx.coroutines.launch
/**
* Demonstrates the usage of the AppAuth to authorize a user with an OAuth2 / OpenID Connect
* provider. Based on the configuration provided in `res/raw/auth_config.json`, the code
* contained here will:
*
* - Retrieve an OpenID Connect discovery document for the provider, or use a local static
* configuration.
* - Utilize dynamic client registration, if no static client id is specified.
* - Initiate the authorization request using the built-in heuristics or a user-selected browser.
*
* _NOTE_: From a clean checkout of this project, the authorization service is not configured.
* Edit `res/raw/auth_config.json` to provide the required configuration properties. See the
* README.md in the app/ directory for configuration instructions, and the adjacent IDP-specific
* instructions.
*/
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
AuthorizationManager.initialize(
AuthorizationConfiguration.fromResources(this, R.raw.fusionauth_config),
SharedPreferencesStorage(this)
)
if (AuthorizationManager.isAuthenticated()) {
Log.i(TAG, "User is already authenticated, proceeding to token activity")
startActivity(Intent(this, TokenActivity::class.java))
finish()
return
}
setContentView(R.layout.activity_login)
findViewById<View>(R.id.retry).setOnClickListener {
startAuth()
}
findViewById<View>(R.id.start_auth).setOnClickListener {
startAuth()
}
findViewById<View>(R.id.device_login).setOnClickListener {
startActivity(Intent(this, DeviceLoginActivity::class.java))
}
if (AuthorizationManager.oAuth(this@LoginActivity).isCancelled(intent)) {
displayAuthCancelled()
}
if (AuthorizationManager.oAuth(this@LoginActivity).isLoggedOut(intent)) {
displayLoggedOut()
}
displayAuthOptions()
}
override fun onDestroy() {
super.onDestroy()
AuthorizationManager.dispose()
}
@MainThread
fun startAuth() {
displayLoading("Making authorization request")
lifecycleScope.launch {
try {
displayLoading("Making authorization request")
AuthorizationManager
.oAuth(this@LoginActivity)
.authorize(
Intent(this@LoginActivity, TokenActivity::class.java),
OAuthAuthorizeOptions(
cancelIntent = Intent(this@LoginActivity, LoginActivity::class.java)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
state = "state-${System.currentTimeMillis()}"
)
)
} catch (e: AuthorizationException) {
Log.e(TAG, "Error while authorizing", e)
displayError(e.message ?: "Error while authorizing", true)
}
}
}
@MainThread
private fun displayLoading(loadingMessage: String) {
findViewById<View>(R.id.loading_container).visibility = View.VISIBLE
findViewById<View>(R.id.auth_container).visibility = View.GONE
findViewById<View>(R.id.error_container).visibility = View.GONE
(findViewById<View>(R.id.loading_description) as TextView).text = loadingMessage
}
@MainThread
private fun displayError(error: String, recoverable: Boolean) {
findViewById<View>(R.id.error_container).visibility = View.VISIBLE
findViewById<View>(R.id.loading_container).visibility = View.GONE
findViewById<View>(R.id.auth_container).visibility = View.GONE
(findViewById<View>(R.id.error_description) as TextView).text = error
findViewById<View>(R.id.retry).visibility = if (recoverable) View.VISIBLE else View.GONE
}
@MainThread
private fun displayAuthOptions() {
findViewById<View>(R.id.auth_container).visibility = View.VISIBLE
findViewById<View>(R.id.loading_container).visibility = View.GONE
findViewById<View>(R.id.error_container).visibility = View.GONE
}
private fun displayAuthCancelled() {
Snackbar.make(
findViewById<View>(R.id.coordinator),
"Authorization canceled",
Snackbar.LENGTH_SHORT
)
.show()
}
private fun displayLoggedOut() {
Snackbar.make(
findViewById<View>(R.id.coordinator),
"Logged out",
Snackbar.LENGTH_SHORT
)
.show()
}
companion object {
private const val TAG = "LoginActivity"
}
}