Last active
June 20, 2021 17:06
-
-
Save Alqueraf/7580cdd36590921e5b4ca0bf331224ac to your computer and use it in GitHub Desktop.
OAuth Redirect Listener
This file contains 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
// Set Redirect Listener | |
webView.webViewClient = object : WebViewClient() { | |
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { | |
request?.let { | |
// Check if this url is our OAuth redirectUrl, otherwise ignore it | |
if (request.url.toString().startsWith(redirectUri)) { | |
// To prevent CSRF attacks, check that we got the same state value we sent, otherwise ignore it | |
val responseState = request.url.getQueryParameter("state") | |
if (responseState == uniqueState) { | |
// This is our request. Parse the redirect URL query parameters to get the code | |
request.url.getQueryParameter("code")?.let { code -> | |
// Got it! | |
Log.d("OAuth", "Here is the authorization code! $code") | |
// TODO: Use authorization code to continue with API flow | |
} ?: run { | |
// User cancelled the login flow | |
Log.d("OAuth", "Authorization code not received :(") | |
// TODO: Handle error | |
} | |
} | |
} | |
} | |
return super.shouldOverrideUrlLoading(view, request) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment