-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d4d31b
commit bd306ea
Showing
47 changed files
with
1,183 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module external.linked.project.id="FitbitAuthDemo" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4"> | ||
<component name="FacetManager"> | ||
<facet type="java-gradle" name="Java-Gradle"> | ||
<configuration> | ||
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" /> | ||
<option name="BUILDABLE" value="false" /> | ||
</configuration> | ||
</facet> | ||
</component> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<excludeFolder url="file://$MODULE_DIR$/.gradle" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
apply plugin: 'kotlin-android' | ||
|
||
apply plugin: 'kotlin-android-extensions' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
buildToolsVersion "29.0.2" | ||
defaultConfig { | ||
applicationId "com.app.fitbitauthdemo" | ||
minSdkVersion 19 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
implementation 'androidx.appcompat:appcompat:1.1.0' | ||
implementation 'androidx.core:core-ktx:1.1.0' | ||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'androidx.test.ext:junit:1.1.1' | ||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
Binary file not shown.
24 changes: 24 additions & 0 deletions
24
app/src/androidTest/java/com/app/fitbitauthdemo/ExampleInstrumentedTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.app.fitbitauthdemo | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ExampleInstrumentedTest { | ||
@Test | ||
fun useAppContext() { | ||
// Context of the app under test. | ||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
assertEquals("com.app.fitbitauthdemo", appContext.packageName) | ||
} | ||
} |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.app.fitbitauthdemo"> | ||
|
||
|
||
<uses-permission android:name="android.permission.INTERNET" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name=".MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.app.fitbitauthdemo | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.util.Log | ||
import com.app.fitbitauthdemo.Utility.AuthenticatingWebView | ||
import com.app.fitbitauthdemo.Utility.AuthenticatingWebViewCallbackMethods | ||
import java.util.HashMap | ||
import com.app.fitbitauthdemo.Utility.* | ||
import java.net.URL | ||
|
||
|
||
class MainActivity : AppCompatActivity(), AuthenticatingWebViewCallbackMethods { | ||
|
||
lateinit var authenticatingWebView: AuthenticatingWebView | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
authenticatingWebView = | ||
AuthenticatingWebView( | ||
findViewById(R.id.webView), | ||
this | ||
) | ||
|
||
val urlFitbit = URL("$FITBIT_CONNECT_BASE_URL?response_type=code&client_id=$CLIENT_ID&redirect_uri=$FITBIT_REDIRECT_URL&scope=$DEFAULT_SCOPES") | ||
Log.e(MAIN_ACTIVITY_TAG, urlFitbit.toString()) | ||
|
||
authenticatingWebView.makeRequest(urlFitbit.toString()) | ||
} | ||
|
||
override fun displayResults(authorizationReturnParameters: HashMap<String, String>) { | ||
Log.e(MAIN_ACTIVITY_TAG, ""+authorizationReturnParameters) | ||
} | ||
|
||
override fun stopProgressDialog() { | ||
|
||
} | ||
|
||
override fun startProgressDialog() { | ||
|
||
} | ||
} |
Binary file not shown.
192 changes: 192 additions & 0 deletions
192
app/src/main/java/com/app/fitbitauthdemo/Utility/AuthenticatingWebView.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package com.app.fitbitauthdemo.Utility; | ||
|
||
/******************************************************************************* | ||
* Copyright (c) 2014 OCLC Inc. | ||
* | ||
* 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. | ||
* | ||
******************************************************************************/ | ||
|
||
import android.annotation.SuppressLint; | ||
import android.graphics.Bitmap; | ||
import android.view.View; | ||
import android.webkit.WebSettings; | ||
import android.webkit.WebView; | ||
import android.webkit.WebViewClient; | ||
|
||
import java.util.HashMap; | ||
|
||
|
||
/** | ||
* Extends a generic webView to execute an Oauth2 authentication. Returns a token and its associated parameters for use | ||
* in calling OCLC web services. | ||
* | ||
* @see WebView | ||
*/ | ||
public class AuthenticatingWebView { | ||
|
||
/** | ||
* Parameters returned by a successful authentication. | ||
*/ | ||
private final HashMap<String, String> authorizationReturnParameters = new HashMap<String, String>(); | ||
|
||
/** | ||
* Browser Window that hosts the authentication process. | ||
*/ | ||
private final WebView webView; | ||
|
||
/** | ||
* Listener for callbacks from webView. | ||
*/ | ||
private final AuthenticatingWebViewCallbackMethods listener; | ||
|
||
/** | ||
* Stores the webview and call back listener into class instance variables for later use. | ||
* | ||
* @param webView the WebView that is handling the authentication interaction with the user. | ||
* @param listener handle to the callback methods to MainActivity.java. | ||
*/ | ||
public AuthenticatingWebView(final WebView webView, final AuthenticatingWebViewCallbackMethods listener) { | ||
this.webView = webView; | ||
this.listener = listener; | ||
} | ||
|
||
/** | ||
* Sets up the WebView to make a a HTTP GET to the OCLC Authentication server to retrieve an access token. | ||
* <p> | ||
* The request URL is of this form: | ||
* <p> | ||
* {baseURL}/authorizeCode?client_id={wskey client ID}&authenticatingInstitutionId={Inst ID} | ||
* &contextInstitutionId={Inst ID}&redirect_uri={redirect Url}&response_type={token} &scope={scope_1 scope_2 ...} | ||
* | ||
* @param requestUrl the request URL that initiates the token request | ||
*/ | ||
@SuppressLint("SetJavaScriptEnabled") | ||
public final void makeRequest(final String requestUrl) { | ||
|
||
/* | ||
* Clear the webView, in case it is showing a previous authentication error. Make the webView visible, in case | ||
* the last attempt succeeded and it is hidden. | ||
*/ | ||
webView.loadUrl("about:blank"); | ||
webView.setVisibility(View.VISIBLE); | ||
|
||
/* | ||
* Enable javascript in the WebView (off by default). The annotation | ||
*/ | ||
WebSettings webSettings = webView.getSettings(); | ||
webSettings.setJavaScriptEnabled(true); | ||
|
||
/* | ||
* Set the callback function for the webview. Inspect the URL before loading to detect successful or failed | ||
* loads. | ||
*/ | ||
webView.setWebViewClient(new MyCustomWebViewClient()); | ||
|
||
/* | ||
* Execute the token request | ||
*/ | ||
webView.loadUrl(requestUrl); | ||
} | ||
|
||
/** | ||
* Handles callbacks from the webView. | ||
*/ | ||
private class MyCustomWebViewClient extends WebViewClient { | ||
|
||
/** | ||
* Callback executes BEFORE the WebView makes the http request We examine the url to see if it contains the | ||
* redirect URI, and if so intercept it, hide the webview and display the token. Note - if something goes wrong | ||
* in the long OAuth2 dance, the user will end up with an error displayed on the webview. They can restart sign | ||
* in by pushing the [Sign In Again] button. | ||
* | ||
* @param view the WebView that executed the callback | ||
* @param url the URL that the WebView is about to load | ||
* @return returns true to permit the url to be loaded into the webview | ||
*/ | ||
@Override | ||
public boolean shouldOverrideUrlLoading(final WebView view, final String url) { | ||
|
||
/* | ||
* Is this the "redirect URI" that we are about to load? If so, parse it and don't load it. Parsing is based | ||
* on the # and the & characters, so make sure they are present before accepting this as a valid redirect | ||
* URI. | ||
*/ | ||
if (url.startsWith(com.app.fitbitauthdemo.Utility.ConstantKt.FITBIT_REDIRECT_URL)) { | ||
|
||
parseRedirectURI(url); | ||
|
||
/* | ||
* Clear the webView and hide it | ||
*/ | ||
view.loadUrl("about:blank"); | ||
webView.setVisibility(View.INVISIBLE); | ||
|
||
/* | ||
* Display all the parameters returned with the token | ||
*/ | ||
listener.displayResults(authorizationReturnParameters); | ||
|
||
return true; | ||
|
||
} else { | ||
|
||
/* | ||
* The url we are about to load is not the "redirect URI", so load it. Note that if anything goes wrong | ||
* with the authentication, the last message in the webview, and | ||
* listener.displayResults(authorizationReturnParameters) will never be called. | ||
*/ | ||
view.loadUrl(url); | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Callback fires when page starts to load. Used to start the Progress Dialog. | ||
* | ||
* @param view the webView referred to by this callback | ||
* @param url the URL that the webView started to load | ||
* @param favicon the favicon of the page being loaded | ||
*/ | ||
@Override | ||
public void onPageStarted(final WebView view, final String url, final Bitmap favicon) { | ||
listener.startProgressDialog(); | ||
} | ||
|
||
/** | ||
* Callback fires when page finishes loading. We use it to turn off the Progress Dialog. | ||
* | ||
* @param view the webView referred to by this callback | ||
* @param url the URL that this page is loading | ||
*/ | ||
@Override | ||
public void onPageFinished(final WebView view, final String url) { | ||
listener.stopProgressDialog(); | ||
} | ||
} | ||
|
||
/** | ||
* Parse a redirect url into its parameters. The string has the form | ||
* [redirectURI]#[param1]=[val1]&[param2]=[val2]... | ||
* | ||
* @param redirectUrl the redirect url to be parsed | ||
*/ | ||
private void parseRedirectURI(final String redirectUrl) { | ||
|
||
String[] params = redirectUrl.split("\\?")[1].split("#"); | ||
|
||
for (String parameter : params) { | ||
if (parameter.contains("=")) { | ||
authorizationReturnParameters.put(parameter.split("=")[0], parameter.split("=")[1]); | ||
} else { | ||
authorizationReturnParameters.put(parameter, ""); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.