Skip to content

Commit 75264e8

Browse files
dnys1HuiSF
andauthored
chore: 0.2.5 release (#975)
* fix(datastore): Sync Issues (#963) * Bump iOS * Bump version * Bump version * Update Changelogs * Update changelogs * Update changelogs * fix(datastore): Remove temporary fix to issue #395 (#967) * fix(datastore): OIDC Rework (#966) * Bump version * Update Changelogs * Update changelogs * Update changelogs * Change internal OIDC implementation * Remove iOS tests for now * Remove AuthProviderTests * Update conventions * Fix whitespace * Add tests * Clean up * Clean up concurrency * Fix tests * Bump iOS versions * Clean up Android * Update changelog date * Update changelogs Co-authored-by: Hui Zhao <[email protected]>
1 parent a5a7dfa commit 75264e8

File tree

50 files changed

+432
-439
lines changed

Some content is hidden

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

50 files changed

+432
-439
lines changed

packages/amplify_analytics_pinpoint/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## 0.2.5 (2021-10-14)
2+
13
## 0.2.4 (2021-09-10)
24

35
### Fixes

packages/amplify_analytics_pinpoint/ios/amplify_analytics_pinpoint.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ This code is the iOS part of the Amplify Flutter Pinpoint Analytics Plugin. The
1515
s.source = { :path => '.' }
1616
s.source_files = 'Classes/**/*'
1717
s.dependency 'Flutter'
18-
s.dependency 'Amplify', '~> 1.13.0'
19-
s.dependency 'AmplifyPlugins/AWSPinpointAnalyticsPlugin', '~> 1.13.0'
18+
s.dependency 'Amplify', '~> 1.15.2'
19+
s.dependency 'AmplifyPlugins/AWSPinpointAnalyticsPlugin', '~> 1.15.2'
2020
s.dependency 'amplify_core'
2121
s.platform = :ios, '11.0'
2222

packages/amplify_analytics_pinpoint/pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
name: amplify_analytics_pinpoint
22
description: The Amplify Flutter Analytics category plugin using the AWS Pinpoint provider.
3-
version: 0.2.4
3+
version: 0.2.5
44
homepage: https://github.com/aws-amplify/amplify-flutter/tree/main/packages/amplify_analytics_pinpoint
55

66
environment:
77
sdk: ">=2.12.0 <3.0.0"
88
flutter: ">=1.20.0"
99

1010
dependencies:
11-
amplify_analytics_plugin_interface: 0.2.4
12-
amplify_core: 0.2.4
11+
amplify_analytics_plugin_interface: 0.2.5
12+
amplify_core: 0.2.5
1313
flutter:
1414
sdk: flutter
1515
plugin_platform_interface: ^2.0.0

packages/amplify_analytics_plugin_interface/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## 0.2.5 (2021-10-14)
2+
13
## 0.2.4 (2021-09-10)
24

35
### Fixes

packages/amplify_analytics_plugin_interface/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name: amplify_analytics_plugin_interface
22
description: The platform interface for the analytics module of Amplify Flutter.
3-
version: 0.2.4
3+
version: 0.2.5
44
homepage: https://github.com/aws-amplify/amplify-flutter/tree/main/packages/amplify_analytics_plugin_interface
55

66
environment:
77
sdk: ">=2.12.0 <3.0.0"
88

99
dependencies:
10-
amplify_core: 0.2.4
10+
amplify_core: 0.2.5
1111
flutter:
1212
sdk: flutter
1313
meta: ^1.1.8

packages/amplify_api/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.5 (2021-10-14)
2+
3+
### Fixes
4+
- fix(api): OIDC/Lambda changes for DataStore
5+
16
## 0.2.4 (2021-09-10)
27

38
### Fixes

packages/amplify_api/android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dependencies {
7272
testImplementation 'junit:junit:4.13.2'
7373
testImplementation 'org.mockito:mockito-core:3.10.0'
7474
testImplementation 'org.mockito:mockito-inline:3.10.0'
75+
testImplementation "org.mockito.kotlin:mockito-kotlin:3.2.0"
7576
testImplementation 'androidx.test:core:1.4.0'
7677
testImplementation 'org.robolectric:robolectric:4.3.1'
7778
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.9'

packages/amplify_api/android/src/main/kotlin/com/amazonaws/amplify/amplify_api/AmplifyApiPlugin.kt

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ import androidx.annotation.NonNull
2222
import androidx.annotation.VisibleForTesting
2323
import com.amazonaws.amplify.amplify_api.auth.FlutterAuthProviders
2424
import com.amazonaws.amplify.amplify_api.rest_api.FlutterRestApi
25-
import com.amazonaws.amplify.amplify_core.cast
2625
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.createSerializedUnrecognizedError
2726
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.handleAddPluginException
2827
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.postExceptionToFlutterChannel
29-
import com.amplifyframework.api.ApiException
3028
import com.amplifyframework.api.aws.AWSApiPlugin
31-
import com.amplifyframework.api.aws.AuthorizationType
3229
import com.amplifyframework.core.Amplify
3330
import io.flutter.embedding.engine.plugins.FlutterPlugin
3431
import io.flutter.plugin.common.EventChannel
@@ -39,15 +36,6 @@ import io.flutter.plugin.common.MethodChannel.Result
3936

4037
/** AmplifyApiPlugin */
4138
class AmplifyApiPlugin : FlutterPlugin, MethodCallHandler {
42-
companion object {
43-
/**
44-
* Thrown when [tokenType] is used but is not a valid [AuthorizationType].
45-
*/
46-
private fun invalidTokenType(tokenType: String? = null) = ApiException.ApiAuthException(
47-
"Invalid arguments",
48-
"Invalid token type: $tokenType"
49-
)
50-
}
5139

5240
private lateinit var channel: MethodChannel
5341
private lateinit var eventchannel: EventChannel
@@ -89,7 +77,7 @@ class AmplifyApiPlugin : FlutterPlugin, MethodCallHandler {
8977
Amplify.addPlugin(
9078
AWSApiPlugin
9179
.builder()
92-
.apiAuthProviders(FlutterAuthProviders.factory)
80+
.apiAuthProviders(FlutterAuthProviders(channel).factory)
9381
.build()
9482
)
9583
logger.info("Added API plugin")
@@ -103,12 +91,6 @@ class AmplifyApiPlugin : FlutterPlugin, MethodCallHandler {
10391
try {
10492
val arguments: Map<String, Any> = call.arguments as Map<String, Any>
10593

106-
// Update tokens if included with request
107-
val tokens = arguments["tokens"] as? List<*>
108-
if (tokens != null && tokens.isNotEmpty()) {
109-
updateTokens(tokens)
110-
}
111-
11294
when (call.method) {
11395
"get" -> FlutterRestApi.get(result, arguments)
11496
"post" -> FlutterRestApi.post(result, arguments)
@@ -123,17 +105,6 @@ class AmplifyApiPlugin : FlutterPlugin, MethodCallHandler {
123105
arguments,
124106
graphqlSubscriptionStreamHandler
125107
)
126-
"updateTokens" -> {
127-
if (tokens == null || tokens.isEmpty()) {
128-
throw ApiException(
129-
"Invalid token map provided",
130-
"Provide tokens in the \"tokens\" field"
131-
)
132-
}
133-
134-
// Tokens already updated
135-
result.success(null)
136-
}
137108
else -> result.notImplemented()
138109
}
139110
} catch (e: Exception) {
@@ -162,19 +133,6 @@ class AmplifyApiPlugin : FlutterPlugin, MethodCallHandler {
162133
}
163134
}
164135

165-
private fun updateTokens(tokens: List<*>) {
166-
for (authToken in tokens.cast<Map<String, Any?>>()) {
167-
val token = authToken["token"] as? String?
168-
val tokenType = authToken["type"] as? String ?: throw invalidTokenType()
169-
val authType: AuthorizationType = try {
170-
AuthorizationType.from(tokenType)
171-
} catch (e: Exception) {
172-
throw invalidTokenType(tokenType)
173-
}
174-
FlutterAuthProviders.setToken(authType, token)
175-
}
176-
}
177-
178136
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
179137
channel.setMethodCallHandler(null)
180138
}

packages/amplify_api/android/src/main/kotlin/com/amazonaws/amplify/amplify_api/auth/FlutterAuthProvider.kt

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,137 @@
1414
*/
1515
package com.amazonaws.amplify.amplify_api.auth
1616

17+
import android.os.Looper
1718
import com.amplifyframework.api.ApiException
1819
import com.amplifyframework.api.aws.ApiAuthProviders
1920
import com.amplifyframework.api.aws.AuthorizationType
2021
import com.amplifyframework.api.aws.sigv4.FunctionAuthProvider
2122
import com.amplifyframework.api.aws.sigv4.OidcAuthProvider
23+
import io.flutter.Log
24+
import io.flutter.plugin.common.MethodChannel
25+
import kotlinx.coroutines.*
2226

2327
/**
2428
* Manages the shared state of all [FlutterAuthProvider] instances.
2529
*/
26-
object FlutterAuthProviders {
30+
class FlutterAuthProviders(private val methodChannel: MethodChannel) {
31+
32+
private companion object {
33+
/**
34+
* Timeout on a single [getToken] call.
35+
*/
36+
const val getTokenTimeoutMillis = 2000L
37+
38+
/**
39+
* Logger tag.
40+
*/
41+
const val tag = "FlutterAuthProviders"
42+
43+
/**
44+
* Name for suspending block in [getToken]. Used for debugging
45+
*/
46+
val coroutineName = CoroutineName(tag)
47+
}
48+
2749
/**
2850
* A factory of [FlutterAuthProvider] instances.
2951
*/
3052
val factory: ApiAuthProviders by lazy {
3153
ApiAuthProviders
3254
.Builder()
33-
.functionAuthProvider(FlutterAuthProvider(AuthorizationType.AWS_LAMBDA))
34-
.oidcAuthProvider(FlutterAuthProvider(AuthorizationType.OPENID_CONNECT))
55+
.functionAuthProvider(FlutterAuthProvider(this, AuthorizationType.AWS_LAMBDA))
56+
.oidcAuthProvider(FlutterAuthProvider(this, AuthorizationType.OPENID_CONNECT))
3557
.build()
3658
}
3759

38-
/**
39-
* Token cache for all [FlutterAuthProvider] instances.
40-
*/
41-
private var tokens: MutableMap<AuthorizationType, String?> = mutableMapOf()
42-
4360
/**
4461
* Retrieves the token for [authType] or `null`, if unavailable.
62+
*
63+
* This function is typically called from within the Amplify library and from a thread besides
64+
* the main one, where it is safe to block. In API REST, the calling thread is main and we must
65+
* return `null`.
66+
*
67+
* Not blocking the main thread is required for making platform channel calls without deadlock.
4568
*/
46-
fun getToken(authType: AuthorizationType): String? = tokens[authType]
69+
fun getToken(authType: AuthorizationType): String? {
70+
if (Thread.currentThread() == Looper.getMainLooper().thread) {
71+
// API REST will call this function from the main thread on configuration. This is bad.
72+
// Since we have to block the calling thread to retrieve the token, just return null.
73+
Log.e(tag, "REST OIDC/Lambda is not supported yet.")
74+
return null
75+
}
76+
try {
77+
return runBlocking(coroutineName) {
78+
val completer = Job()
4779

48-
/**
49-
* Sets the token for [authType] to [value].
50-
*/
51-
fun setToken(authType: AuthorizationType, value: String?) {
52-
tokens[authType] = value
80+
val result = object : MethodChannel.Result {
81+
var token: String? = null
82+
83+
override fun success(result: Any?) {
84+
token = result as? String
85+
launch(Dispatchers.Main) {
86+
completer.complete()
87+
}
88+
}
89+
90+
override fun error(
91+
errorCode: String?,
92+
errorMessage: String?,
93+
errorDetails: Any?
94+
) {
95+
launch(Dispatchers.Main) {
96+
completer.complete()
97+
}
98+
}
99+
100+
override fun notImplemented() {
101+
launch(Dispatchers.Main) {
102+
completer.complete()
103+
}
104+
}
105+
}
106+
launch(Dispatchers.Main) {
107+
methodChannel.invokeMethod(
108+
"getLatestAuthToken",
109+
authType.name,
110+
result
111+
)
112+
}
113+
114+
withTimeout(getTokenTimeoutMillis) {
115+
completer.join()
116+
}
117+
118+
return@runBlocking result.token
119+
}
120+
} catch (e: Exception) {
121+
Log.e(tag, "Exception in getToken", e)
122+
return null
123+
}
53124
}
54125
}
55126

56127
/**
57128
* A provider which manages token retrieval for its [AuthorizationType].
58129
*/
59-
class FlutterAuthProvider(private val type: AuthorizationType) : FunctionAuthProvider,
130+
class FlutterAuthProvider(
131+
private val provider: FlutterAuthProviders,
132+
private val type: AuthorizationType
133+
) : FunctionAuthProvider,
60134
OidcAuthProvider {
61135
private companion object {
62136
/**
63137
* Thrown when there is no token available for [type].
64138
*/
65139
fun noTokenAvailable(type: AuthorizationType) = ApiException.ApiAuthException(
66-
"No $type token available",
67-
"Ensure that `getLatestAuthToken` returns a value"
140+
"Unable to retrieve token for $type",
141+
"""
142+
Make sure you register your auth providers in the addPlugin call and
143+
that getLatestAuthToken returns a value.
144+
""".trimIndent()
68145
)
69146
}
70147

71148
override fun getLatestAuthToken(): String =
72-
FlutterAuthProviders.getToken(type) ?: throw noTokenAvailable(type)
149+
provider.getToken(type) ?: throw noTokenAvailable(type)
73150
}

packages/amplify_api/android/src/test/kotlin/com/amazonaws/amplify/amplify_api/AuthProviderTests.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ import org.junit.After
3434
import org.junit.Before
3535
import org.junit.Test
3636
import org.junit.runner.RunWith
37-
import org.mockito.Mockito.*
37+
import org.mockito.kotlin.clearInvocations
38+
import org.mockito.kotlin.mock
39+
import org.mockito.kotlin.times
40+
import org.mockito.kotlin.verify
3841

3942
/**
4043
* Mock model object for building dummy GraphQL requests.
@@ -49,6 +52,7 @@ class MockModel : Model {
4952
@ExperimentalCoroutinesApi
5053
@RunWith(AndroidJUnit4::class)
5154
class AuthProviderTests {
55+
5256
/**
5357
* Using categories allows us to create a new one per-test and configure as appropriate.
5458
*/
@@ -67,13 +71,12 @@ class AuthProviderTests {
6771
/**
6872
* Mock OIDC provider.
6973
*/
70-
private val mockOidcAuthProvider: OidcAuthProvider = mock(OidcAuthProvider::class.java)
74+
private val mockOidcAuthProvider: OidcAuthProvider = mock()
7175

7276
/**
7377
* Mock Lambda provider.
7478
*/
75-
private val mockFunctionAuthProvider: FunctionAuthProvider =
76-
mock(FunctionAuthProvider::class.java)
79+
private val mockFunctionAuthProvider: FunctionAuthProvider = mock()
7780

7881
/**
7982
* Multi-auth configuration JSON for API category.

0 commit comments

Comments
 (0)