Skip to content

Commit 3f04af7

Browse files
Merge pull request #64 from Web3Auth/feat/add-openlogin-v5
Feat/add openlogin v5 and sapphire updates
2 parents d8305c6 + 8a5893f commit 3f04af7

File tree

17 files changed

+225
-71
lines changed

17 files changed

+225
-71
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ Open your app's `AndroidManifest.xml` file and add the following permission:
6161

6262
### Configure your Web3Auth project
6363

64-
Hop on to the [Web3Auth Dashboard](https://dashboard.web3auth.io/) and create a new project. Use the Client ID of the project to start your integration.
64+
Hop on to the [Web3Auth Dashboard](https://dashboard.web3auth.io/) and create a new project. Use the
65+
Client ID of the project to start your integration.
6566

66-
![Web3Auth Dashboard](https://web3auth.io/docs/assets/images/project_plug_n_play-89c39ec42ad993107bb2485b1ce64b89.png)
67+
![Web3Auth Dashboard](https://github-production-user-asset-6210df.s3.amazonaws.com/6962565/272779464-043f6383-e671-4aa5-80fb-ec87c569e5ab.png)
6768

6869
- Add `{YOUR_APP_PACKAGE_NAME}://auth` to **Whitelist URLs**.
6970

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ android {
1212
keyPassword 'torus123'
1313
}
1414
}
15-
compileSdkVersion 33
15+
compileSdkVersion 34
1616

1717
defaultConfig {
1818
applicationId "com.web3auth.app"
1919
minSdkVersion 24
20-
targetSdkVersion 33
20+
targetSdkVersion 34
2121
versionCode 1
2222
versionName "1.0"
2323
manifestPlaceholders = [

app/src/main/java/com/web3auth/app/MainActivity.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,21 @@ class MainActivity : AppCompatActivity(), AdapterView.OnItemClickListener {
119119
Web3AuthOptions(
120120
context = this,
121121
clientId = getString(R.string.web3auth_project_id),
122-
network = Network.MAINNET,
122+
network = Network.SAPPHIRE_MAINNET,
123+
buildEnv = BuildEnv.TESTING,
123124
redirectUrl = Uri.parse("torusapp://org.torusresearch.web3authexample/redirect"),
124125
whiteLabel = WhiteLabelData(
125-
"Web3Auth Sample App", null, null, "en", true,
126+
"Web3Auth Sample App", null, null, null,
127+
Language.DE, ThemeModes.LIGHT, true,
126128
hashMapOf(
127129
"primary" to "#123456"
128130
)
129131
),
130132
loginConfig = hashMapOf(
131-
"loginConfig" to LoginConfigItem(
132-
"torus",
133+
"google" to LoginConfigItem(
134+
verifier = "w3a-google-demo",
133135
typeOfLogin = TypeOfLogin.GOOGLE,
134-
name = ""
136+
clientId = "519228911939-cri01h55lsjbsia1k7ll6qpalrus75ps.apps.googleusercontent.com",
135137
)
136138
)
137139
)

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<resources>
22
<string name="app_name">Torus Web3Auth</string>
3-
<string name="web3auth_project_id">BEaGnq-mY0ZOXk2UT1ivWUe0PZ_iJX4Vyb6MtpOp7RMBu_6ErTrATlfuK3IaFcvHJr27h6L1T4owkBH6srLphIw</string>
3+
<string name="web3auth_project_id">BPi5PB_UiIZ-cPz1GtV5i1I2iOSOHuimiXBI0e-Oe_u6X3oVAbCiAZOTEBtTXw4tsluTITPqA8zMsfxIKMjiqNQ</string>
44
<string name="sign_in">Sign in</string>
55
<string name="not_logged_in">Not logged in</string>
66
<string name="sign_out">Sign out</string>

core/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ plugins {
55
}
66

77
android {
8-
compileSdkVersion 33
8+
compileSdkVersion 34
99

1010
defaultConfig {
1111
minSdkVersion 24
12-
targetSdkVersion 33
12+
targetSdkVersion 34
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
consumerProguardFiles "proguard-rules.pro"
@@ -47,7 +47,7 @@ dependencies {
4747
api 'net.sourceforge.streamsupport:streamsupport-cfuture:1.7.4'
4848

4949
// Custom Tabs
50-
implementation 'androidx.browser:browser:1.5.0'
50+
implementation 'androidx.browser:browser:1.6.0'
5151

5252
// Encoding
5353
implementation 'com.google.code.gson:gson:2.9.1'
@@ -68,7 +68,7 @@ dependencies {
6868
implementation 'org.web3j:core:4.8.8-android'
6969

7070
//session-manager-sdk
71-
implementation 'com.github.Web3Auth:session-manager-android:0.0.8'
71+
implementation 'com.github.Web3Auth:session-manager-android:1.0.0'
7272

7373
// Test
7474
testImplementation 'junit:junit:4.+'

core/src/main/java/com/web3auth/core/Web3Auth.kt

Lines changed: 99 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,63 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
3232
val sdkUrl = Uri.parse(web3AuthOption.sdkUrl)
3333
val context = web3AuthOption.context
3434

35-
val initParams = mutableMapOf(
35+
val initOptions = mutableMapOf(
3636
"clientId" to web3AuthOption.clientId,
3737
"network" to web3AuthOption.network.name.lowercase(Locale.ROOT)
3838
)
39-
if (web3AuthOption.redirectUrl != null) initParams["redirectUrl"] =
39+
if (web3AuthOption.redirectUrl != null) initOptions["redirectUrl"] =
4040
web3AuthOption.redirectUrl.toString()
41-
if (web3AuthOption.whiteLabel != null) initParams["whiteLabel"] =
41+
if (web3AuthOption.whiteLabel != null) initOptions["whiteLabel"] =
4242
gson.toJson(web3AuthOption.whiteLabel)
43-
if (web3AuthOption.loginConfig != null) initParams["loginConfig"] =
43+
if (web3AuthOption.loginConfig != null) initOptions["loginConfig"] =
4444
gson.toJson(web3AuthOption.loginConfig)
45+
if (web3AuthOption.buildEnv != null) initOptions["buildEnv"] =
46+
web3AuthOption.buildEnv.toString().lowercase()
4547

48+
val initParams = mutableMapOf(
49+
"loginProvider" to params?.loginProvider,
50+
"redirectUrl" to web3AuthOption.redirectUrl.toString()
51+
)
4652

4753
val paramMap = mapOf(
48-
"init" to initParams, "params" to params
54+
"options" to initOptions, "params" to initParams, "actionType" to path
4955
)
56+
5057
extraParams?.let { paramMap.plus("params" to extraParams) }
5158
val validParams = paramMap.filterValues { it != null }
5259

53-
val hash = gson.toJson(validParams).toByteArray(Charsets.UTF_8).toBase64URLString()
60+
val loginIdCf = getLoginId(validParams)
5461

55-
val url = Uri.Builder().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
56-
.encodedPath(sdkUrl.encodedPath).appendPath(path).fragment(hash).build()
62+
loginIdCf.whenComplete { loginId, error ->
63+
if (error == null) {
64+
val jsonObject = mapOf(
65+
"loginId" to loginId
66+
)
67+
val hash = "b64Params=" + gson.toJson(jsonObject).toByteArray(Charsets.UTF_8)
68+
.toBase64URLString()
5769

58-
val defaultBrowser = context.getDefaultBrowser()
59-
val customTabsBrowsers = context.getCustomTabsBrowsers()
70+
val url =
71+
Uri.Builder().scheme(sdkUrl.scheme).encodedAuthority(sdkUrl.encodedAuthority)
72+
.encodedPath(sdkUrl.encodedPath).appendPath("start").fragment(hash).build()
6073

61-
if (customTabsBrowsers.contains(defaultBrowser)) {
62-
val customTabs = CustomTabsIntent.Builder().build()
63-
customTabs.intent.setPackage(defaultBrowser)
64-
customTabs.launchUrl(context, url)
65-
} else if (customTabsBrowsers.isNotEmpty()) {
66-
val customTabs = CustomTabsIntent.Builder().build()
67-
customTabs.intent.setPackage(customTabsBrowsers[0])
68-
customTabs.launchUrl(context, url)
69-
} else {
70-
// Open in browser externally
71-
context.startActivity(Intent(Intent.ACTION_VIEW, url))
74+
val defaultBrowser = context.getDefaultBrowser()
75+
val customTabsBrowsers = context.getCustomTabsBrowsers()
76+
77+
if (customTabsBrowsers.contains(defaultBrowser)) {
78+
val customTabs = CustomTabsIntent.Builder().build()
79+
customTabs.intent.setPackage(defaultBrowser)
80+
customTabs.launchUrl(context, url)
81+
} else if (customTabsBrowsers.isNotEmpty()) {
82+
val customTabs = CustomTabsIntent.Builder().build()
83+
customTabs.intent.setPackage(customTabsBrowsers[0])
84+
customTabs.launchUrl(context, url)
85+
} else {
86+
// Open in browser externally
87+
context.startActivity(Intent(Intent.ACTION_VIEW, url))
88+
}
89+
}
7290
}
91+
7392
}
7493

7594
fun initialize(): CompletableFuture<Void> {
@@ -104,29 +123,50 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
104123
loginCompletableFuture.completeExceptionally(UnKnownException(error))
105124
}
106125

107-
web3AuthResponse = gson.fromJson(
108-
decodeBase64URLString(hash).toString(Charsets.UTF_8), Web3AuthResponse::class.java
109-
)
126+
val sessionId = hash.split("&")[0].split("=")[1]
110127

111-
if (web3AuthResponse?.error?.isNotBlank() == true) {
112-
loginCompletableFuture.completeExceptionally(
113-
UnKnownException(
114-
web3AuthResponse?.error ?: Web3AuthError.getError(ErrorCode.SOMETHING_WENT_WRONG)
115-
)
116-
)
117-
} else if (web3AuthResponse?.privKey.isNullOrBlank()) {
118-
loginCompletableFuture.completeExceptionally(Exception(Web3AuthError.getError(ErrorCode.SOMETHING_WENT_WRONG)))
119-
} else {
120-
web3AuthResponse?.sessionId?.let { sessionManager.saveSessionId(it) }
128+
if (sessionId != null) {
129+
sessionManager.saveSessionId(sessionId)
121130

122-
if (web3AuthResponse?.userInfo?.dappShare?.isNotEmpty() == true) {
123-
KeyStoreManagerUtils.encryptData(
124-
web3AuthResponse?.userInfo?.verifier.plus(" | ")
125-
.plus(web3AuthResponse?.userInfo?.verifierId),
126-
web3AuthResponse?.userInfo?.dappShare!!,
127-
)
131+
//Rehydrate Session
132+
if (ApiHelper.isNetworkAvailable(web3AuthOption.context)) {
133+
this.authorizeSession().whenComplete { resp, error ->
134+
if (error == null) {
135+
web3AuthResponse = resp
136+
if (web3AuthResponse?.error?.isNotBlank() == true) {
137+
loginCompletableFuture.completeExceptionally(
138+
UnKnownException(
139+
web3AuthResponse?.error
140+
?: Web3AuthError.getError(ErrorCode.SOMETHING_WENT_WRONG)
141+
)
142+
)
143+
} else if (web3AuthResponse?.privKey.isNullOrBlank()) {
144+
loginCompletableFuture.completeExceptionally(
145+
Exception(
146+
Web3AuthError.getError(
147+
ErrorCode.SOMETHING_WENT_WRONG
148+
)
149+
)
150+
)
151+
} else {
152+
web3AuthResponse?.sessionId?.let { sessionManager.saveSessionId(it) }
153+
154+
if (web3AuthResponse?.userInfo?.dappShare?.isNotEmpty() == true) {
155+
KeyStoreManagerUtils.encryptData(
156+
web3AuthResponse?.userInfo?.verifier.plus(" | ")
157+
.plus(web3AuthResponse?.userInfo?.verifierId),
158+
web3AuthResponse?.userInfo?.dappShare!!,
159+
)
160+
}
161+
loginCompletableFuture.complete(web3AuthResponse)
162+
}
163+
} else {
164+
print(error)
165+
}
166+
}
128167
}
129-
loginCompletableFuture.complete(web3AuthResponse)
168+
} else {
169+
loginCompletableFuture.completeExceptionally(Exception(Web3AuthError.getError(ErrorCode.SOMETHING_WENT_WRONG)))
130170
}
131171
}
132172

@@ -171,7 +211,7 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
171211
*/
172212
private fun authorizeSession(): CompletableFuture<Web3AuthResponse> {
173213
val sessionCompletableFuture: CompletableFuture<Web3AuthResponse> = CompletableFuture()
174-
val sessionResponse: CompletableFuture<String> = sessionManager.authorizeSession(true)
214+
val sessionResponse: CompletableFuture<String> = sessionManager.authorizeSession(false)
175215
sessionResponse.whenComplete { response, error ->
176216
if (error == null) {
177217
val tempJson = JSONObject(response)
@@ -207,6 +247,23 @@ class Web3Auth(web3AuthOptions: Web3AuthOptions) {
207247
return sessionCompletableFuture
208248
}
209249

250+
private fun getLoginId(jsonObject: Map<String, Any?>): CompletableFuture<String> {
251+
val createSessionCompletableFuture: CompletableFuture<String> = CompletableFuture()
252+
if (this.sessionManager == null) {
253+
createSessionCompletableFuture.completeExceptionally(Exception("Session Manager is not initialized"))
254+
}
255+
val sessionResponse: CompletableFuture<String> =
256+
sessionManager.createSession(gson.toJson(jsonObject), 600)
257+
sessionResponse.whenComplete { response, error ->
258+
if (error == null) {
259+
createSessionCompletableFuture.complete(response)
260+
} else {
261+
createSessionCompletableFuture.completeExceptionally(error)
262+
}
263+
}
264+
return createSessionCompletableFuture
265+
}
266+
210267
fun getPrivkey(): String {
211268
val privKey: String? = if (web3AuthResponse == null) {
212269
""
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.web3auth.core.types
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
enum class BuildEnv {
6+
@SerializedName("production")
7+
PRODUCTION,
8+
9+
@SerializedName("staging")
10+
STAGING,
11+
12+
@SerializedName("testing")
13+
TESTING
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.web3auth.core.types
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
enum class Language {
6+
@SerializedName("en") //English
7+
EN,
8+
9+
@SerializedName("de") //German
10+
DE,
11+
12+
@SerializedName("ja") //Japanese
13+
JA,
14+
15+
@SerializedName("ko") //Korean
16+
KO,
17+
18+
@SerializedName("zh") //Mandarin
19+
ZH,
20+
21+
@SerializedName("es") //Spanish
22+
ES,
23+
24+
@SerializedName("fr") //French
25+
FR,
26+
27+
@SerializedName("pt") //Portugese
28+
PT,
29+
30+
@SerializedName("nl") //Dutch
31+
NL,
32+
}

core/src/main/java/com/web3auth/core/types/LoginParams.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ data class LoginParams(
66
val loginProvider: Provider,
77
var dappShare: String? = null,
88
val extraLoginOptions: ExtraLoginOptions? = null,
9-
val redirectUrl: Uri? = null,
9+
var redirectUrl: Uri? = null,
1010
val appState: String? = null,
1111
val mfaLevel: MFALevel? = null,
1212
val sessionTime: Int? = null,

0 commit comments

Comments
 (0)