-
Notifications
You must be signed in to change notification settings - Fork 2
Add Device Authorization Grant #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ColinFrick
wants to merge
1
commit into
main
Choose a base branch
from
feat/device_authorization_grant
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,35 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
<uses-permission android:name="android.permission.INTERNET"/> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:dataExtractionRules="@xml/data_extraction_rules" | ||
android:fullBackupContent="@xml/backup_rules" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name_short" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" | ||
android:usesCleartextTraffic="true" | ||
tools:targetApi="31"> | ||
android:allowBackup="true" | ||
android:dataExtractionRules="@xml/data_extraction_rules" | ||
android:fullBackupContent="@xml/backup_rules" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name_short" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" | ||
android:usesCleartextTraffic="true" | ||
tools:targetApi="31"> | ||
<activity | ||
android:name=".LoginActivity" | ||
android:exported="true"> | ||
android:name=".DeviceLoginActivity" | ||
android:exported="false"/> | ||
<activity | ||
android:name=".LoginActivity" | ||
android:exported="true"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<action android:name="android.intent.action.MAIN"/> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
<category android:name="android.intent.category.LAUNCHER"/> | ||
</intent-filter> | ||
</activity> | ||
|
||
<activity android:name=".TokenActivity" | ||
android:windowSoftInputMode="stateHidden" /> | ||
<activity | ||
android:name=".TokenActivity" | ||
android:windowSoftInputMode="stateHidden"/> | ||
</application> | ||
|
||
</manifest> | ||
</manifest> |
120 changes: 120 additions & 0 deletions
120
app/src/main/java/io/fusionauth/sdk/DeviceLoginActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package io.fusionauth.sdk | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.View | ||
import android.widget.ImageView | ||
import android.widget.TextView | ||
import androidx.annotation.MainThread | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.lifecycleScope | ||
import com.google.zxing.BarcodeFormat | ||
import com.journeyapps.barcodescanner.BarcodeEncoder | ||
import io.fusionauth.mobilesdk.AuthorizationManager | ||
import io.fusionauth.mobilesdk.ExperimentalApi | ||
import io.fusionauth.mobilesdk.exceptions.AuthorizationException | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* Demonstrates the usage of the FusionAuth SDK to authorize a user utilizing the Device Authorization | ||
* Grant. This is useful for devices that do not have a browser or other input mechanism. | ||
*/ | ||
class DeviceLoginActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
setContentView(R.layout.activity_device_login) | ||
|
||
findViewById<View>(R.id.device_login).setOnClickListener { | ||
startAuth() | ||
} | ||
|
||
displayAuthOptions() | ||
} | ||
|
||
@OptIn(ExperimentalApi::class) | ||
@MainThread | ||
fun startAuth() { | ||
displayLoading("Starting device authorization request") | ||
|
||
lifecycleScope.launch { | ||
try { | ||
val deviceCodeResponse = AuthorizationManager | ||
.oAuth(this@DeviceLoginActivity) | ||
.deviceAuthorize() | ||
|
||
findViewById<TextView>(R.id.device_code_code).let { | ||
(it as TextView).text = deviceCodeResponse.user_code | ||
} | ||
findViewById<TextView>(R.id.device_code_link).let { | ||
(it as TextView).text = deviceCodeResponse.verification_uri | ||
} | ||
|
||
BarcodeEncoder().encodeBitmap( | ||
deviceCodeResponse.verification_uri_complete, | ||
BarcodeFormat.QR_CODE, | ||
QR_CODE_DIMENSION, QR_CODE_DIMENSION | ||
) | ||
.let { bitmap -> | ||
findViewById<ImageView>(R.id.device_code_qr).setImageBitmap(bitmap) | ||
} | ||
|
||
displayDeviceCode() | ||
|
||
displayLoading("Polling for authorization") | ||
|
||
val authState = AuthorizationManager | ||
Check noticeCode scanning / CodeQL Unread local variable
Variable 'FusionAuthState authState' is never read.
|
||
.oAuth(this@DeviceLoginActivity) | ||
.getDeviceFusionAuthState(deviceCodeResponse) | ||
|
||
// Is logged in! | ||
startActivity(Intent(this@DeviceLoginActivity, TokenActivity::class.java)) | ||
} catch (e: AuthorizationException) { | ||
Log.e(DeviceLoginActivity.TAG, "Error while authorizing", e) | ||
Check noticeCode scanning / mobsfscan The App logs information. Sensitive information should never be logged.
The App logs information. Sensitive information should never be logged.
|
||
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 | ||
Check failureCode scanning / mobsfscan Hidden elements in view can be used to hide data from user. But this data can be leaked.
Hidden elements in view can be used to hide data from user. But this data can be leaked.
|
||
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.device_code_container).visibility = View.GONE | ||
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 | ||
} | ||
|
||
@MainThread | ||
private fun displayDeviceCode() { | ||
findViewById<View>(R.id.device_code_container).visibility = View.VISIBLE | ||
findViewById<View>(R.id.auth_container).visibility = View.GONE | ||
findViewById<View>(R.id.loading_container).visibility = View.GONE | ||
findViewById<View>(R.id.error_container).visibility = View.GONE | ||
} | ||
|
||
companion object { | ||
private const val TAG = "DeviceLoginActivity" | ||
private const val QR_CODE_DIMENSION = 512 | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / detekt
Property is unused and should be removed.