Skip to content

Commit aea561f

Browse files
authored
Merge pull request #116 from android/add-webkit-webview
Add webkit webview sample
2 parents 50788d1 + 0b11662 commit aea561f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1241
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# https://editorconfig.org/
2+
# This configuration is used by ktlint when spotless invokes it
3+
4+
[*.{kt,kts}]
5+
ij_kotlin_allow_trailing_comma=true
6+
ij_kotlin_allow_trailing_comma_on_call_site=true
7+
ktlint_function_naming_ignore_when_annotated_with=Composable, Test
8+
ktlint_standard_backing-property-naming = disabled
9+
ktlint_standard_binary-expression-wrapping = disabled
10+
ktlint_standard_chain-method-continuation = disabled
11+
ktlint_standard_class-signature = disabled
12+
ktlint_standard_condition-wrapping = disabled
13+
ktlint_standard_function-expression-body = disabled
14+
ktlint_standard_function-literal = disabled
15+
ktlint_standard_function-type-modifier-spacing = disabled
16+
ktlint_standard_multiline-loop = disabled
17+
ktlint_standard_function-signature = disabled

WebView/WebkitWebView/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Webkit Web View Sample
2+
This is a sample app which uses the [Webkit library](https://developer.android.com/jetpack/androidx/releases/webkit#1.12.0)
3+
to implement passkey creation in WebView. It's a simple app that supports passkey creation and
4+
deletion via a webpage and server hosted on glitch.me.
5+
6+
# Set Up Instructions
7+
Assuming the GlitchMe site and server is up and running (the glitch me site can
8+
be found [here](https://glitch.com/edit/#!/alder-sunny-bicycle)), this app should
9+
be fairly easy to setup. There is one process that requires ensuring your app
10+
signature is known to the server, which will be in the steps below.
11+
12+
1. Clone this repo anywhere on your machine. Ensure you have AndroidStudio and
13+
all the setup with that is correct (sdk/etc..).
14+
2. Ensure your phone can connect to Android Studio. This often involves ensuring
15+
debug mode is on and adb is installed.
16+
3. Double check there is a 'debug.keystore' file at the root. This is important
17+
as the server expects your app to have the SHA256 from this keystore.
18+
4. 'Run' the app via the 'run' button (the green play triangle).
19+
5. Wait for the webpage to load, and then try creating and deleting passkeys.
20+
21+
## Using the app
22+
1. Enter a username to register with the app. This can be any username you want to use.
23+
2. Tap through the password screen as the password will not be used in this sample.
24+
3. On the next screen, tap "Create a passkey" to go initiate the passkey creation process and go
25+
through the authentication process.
26+
4. If successful, a passkey will show up under "Your registered passkeys." Note that you can only
27+
create one passkey per platform for each user.
28+
5. You can delete passkeys from the list once they are created by selecting the delete icon.
29+
30+
## Running your own server
31+
If you'd like to manage a server directly, you can choose to Remix this project on glitch.me which
32+
will clone the current project to management under your account. Note that you will likely need to
33+
change the env variables to your app's package name and sha-256 hash, as well as update the
34+
expectedOrigin variables in server.mjs with your updated apk-key-hash (covert your app's sha-256 to
35+
base 64).
36+
37+
# Notice
38+
Please note that accounts created with this sample app are stored on glitch.me. While this does not
39+
impact the security of your Google accounts, please be wary of putting sensitive information in your
40+
username or password fields.
41+
42+
## Known Issues
43+
- We have noticed the GlitchMe service sometimes has reliability issues.
44+
- See status [here](https://status.glitch.com/).
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# built application files
2+
*.apk
3+
*.ap_
4+
5+
# Mac files
6+
.DS_Store
7+
8+
# files for the dex VM
9+
*.dex
10+
11+
# Java class files
12+
*.class
13+
14+
# generated files
15+
bin/
16+
gen/
17+
18+
# Ignore gradle files
19+
.gradle/
20+
build/
21+
22+
# Local configuration file (sdk path, etc)
23+
local.properties
24+
25+
# Proguard folder generated by Eclipse
26+
proguard/
27+
proguard-project.txt
28+
29+
# Eclipse files
30+
.project
31+
.classpath
32+
.settings/
33+
34+
# Android Studio/IDEA
35+
*.iml
36+
.idea
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import java.io.FileInputStream
17+
import java.util.Properties
18+
19+
plugins {
20+
alias(libs.plugins.android.application)
21+
alias(libs.plugins.kotlin.android)
22+
}
23+
24+
// Load keystore properties for our signing key
25+
// (see developer.android.com/studio/publish/app-signing)
26+
val keystorePropertiesFile: File = rootProject.file("keystore.properties")
27+
val keystoreProperties = Properties()
28+
keystoreProperties.load(FileInputStream(keystorePropertiesFile))
29+
30+
android {
31+
namespace = "com.google.webkit.webviewsample"
32+
compileSdk = 35
33+
34+
defaultConfig {
35+
applicationId = "com.google.webkit.webviewsample"
36+
minSdk = 29
37+
targetSdk = 35
38+
versionCode = 1
39+
versionName = "1.0"
40+
41+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
42+
vectorDrawables {
43+
useSupportLibrary = true
44+
}
45+
buildConfigField("String", "API_BASE_URL", "\"https://alder-sunny-bicycle.glitch.me/auth\"")
46+
resValue("string", "host", "https://alder-sunny-bicycle.glitch.me")
47+
}
48+
49+
signingConfigs {
50+
create("config") {
51+
keyAlias = keystoreProperties["keyAlias"].toString()
52+
keyPassword = keystoreProperties["keyPassword"].toString()
53+
storeFile = file(keystoreProperties["storeFile"].toString())
54+
storePassword = keystoreProperties["storePassword"].toString()
55+
}
56+
}
57+
58+
buildTypes {
59+
release {
60+
isMinifyEnabled = false
61+
proguardFiles(
62+
getDefaultProguardFile("proguard-android-optimize.txt"),
63+
"proguard-rules.pro"
64+
)
65+
signingConfig = signingConfigs.getByName("config")
66+
}
67+
68+
debug {
69+
signingConfig = signingConfigs.getByName("config")
70+
}
71+
}
72+
compileOptions {
73+
sourceCompatibility = JavaVersion.VERSION_1_8
74+
targetCompatibility = JavaVersion.VERSION_1_8
75+
}
76+
kotlinOptions {
77+
jvmTarget = "1.8"
78+
}
79+
buildFeatures {
80+
compose = true
81+
buildConfig = true
82+
}
83+
composeOptions {
84+
kotlinCompilerExtensionVersion = "1.5.1"
85+
}
86+
packaging {
87+
resources {
88+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
89+
}
90+
}
91+
}
92+
93+
dependencies {
94+
implementation(libs.core.ktx)
95+
implementation(libs.lifecycle.runtime.ktx)
96+
implementation(libs.activity.compose)
97+
implementation(platform(libs.compose.bom))
98+
implementation(libs.compose.ui)
99+
implementation(libs.compose.graphics)
100+
implementation(libs.compose.tooling.preview)
101+
implementation(libs.compose.material)
102+
implementation(libs.webkit)
103+
androidTestImplementation(platform(libs.compose.bom))
104+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.kts.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2025 Google LLC
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:tools="http://schemas.android.com/tools">
20+
<uses-permission android:name="android.permission.INTERNET" />
21+
<application
22+
android:allowBackup="true"
23+
android:dataExtractionRules="@xml/data_extraction_rules"
24+
android:fullBackupContent="@xml/backup_rules"
25+
android:icon="@mipmap/ic_launcher"
26+
android:label="@string/app_name"
27+
android:supportsRtl="true"
28+
android:theme="@style/Theme.WebViewSample"
29+
tools:targetApi="34">
30+
<activity
31+
android:name="com.google.webkit.webviewsample.MainActivity"
32+
android:exported="true"
33+
android:label="@string/app_name"
34+
android:theme="@style/Theme.WebViewSample">
35+
<intent-filter>
36+
<action android:name="android.intent.action.MAIN" />
37+
38+
<category android:name="android.intent.category.LAUNCHER" />
39+
</intent-filter>
40+
41+
<meta-data
42+
android:name="android.app.lib_name"
43+
android:value="" />
44+
</activity>
45+
</application>
46+
47+
</manifest>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.webkit.webviewsample
17+
18+
import android.os.Bundle
19+
import android.webkit.WebView
20+
import androidx.activity.ComponentActivity
21+
import androidx.activity.compose.setContent
22+
import androidx.compose.foundation.layout.fillMaxSize
23+
import androidx.compose.material.MaterialTheme
24+
import androidx.compose.material.Surface
25+
import androidx.compose.ui.Modifier
26+
import androidx.compose.ui.res.stringResource
27+
import androidx.compose.ui.viewinterop.AndroidView
28+
import androidx.webkit.WebSettingsCompat
29+
import androidx.webkit.WebViewFeature
30+
import com.google.webkit.webviewsample.theme.WebViewSampleTheme
31+
32+
/**
33+
* Generates a WebView that uses the Webkit API to handle WebView passkey authentication requests.
34+
*/
35+
class MainActivity : ComponentActivity() {
36+
override fun onCreate(savedInstanceState: Bundle?) {
37+
super.onCreate(savedInstanceState)
38+
setContent {
39+
val url = stringResource(R.string.server_url)
40+
WebViewSampleTheme {
41+
Surface(
42+
modifier = Modifier.fillMaxSize(),
43+
color = MaterialTheme.colors.background,
44+
) {
45+
AndroidView(
46+
factory = {
47+
WebView(it).apply {
48+
settings.javaScriptEnabled = true
49+
webViewClient = WebViewClientImpl()
50+
}
51+
},
52+
update = { webView ->
53+
run {
54+
webView.loadUrl(url)
55+
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_AUTHENTICATION)) {
56+
WebSettingsCompat.setWebAuthenticationSupport(
57+
webView.settings,
58+
WebSettingsCompat.WEB_AUTHENTICATION_SUPPORT_FOR_APP,
59+
)
60+
} else {
61+
// App does not support passkeys in WebView.
62+
}
63+
}
64+
},
65+
)
66+
}
67+
}
68+
}
69+
}
70+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.webkit.webviewsample
17+
18+
import android.webkit.WebView
19+
import android.webkit.WebViewClient
20+
21+
/**
22+
* A simple WebViewClient class that causes the WebView to continue loading the URL during load.
23+
* See the corresponding [documentation](https://developer.android.com/reference/android/webkit/WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView,%20android.webkit.WebResourceRequest)
24+
*/
25+
class WebViewClientImpl : WebViewClient() {
26+
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
27+
return false
28+
}
29+
}

0 commit comments

Comments
 (0)