forked from invertase/react-native-apple-authentication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignInWithAppleService.kt
More file actions
84 lines (74 loc) · 3.07 KB
/
SignInWithAppleService.kt
File metadata and controls
84 lines (74 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.RNAppleAuthentication
import android.net.Uri
import android.os.Parcel
import android.os.Parcelable
import androidx.fragment.app.FragmentManager
import com.RNAppleAuthentication.webview.SignInWebViewDialogFragment
class SignInWithAppleService(
private val fragmentManager: FragmentManager,
private val fragmentTag: String,
private val configuration: SignInWithAppleConfiguration,
private val callback: (SignInWithAppleResult) -> Unit
) {
constructor(
fragmentManager: FragmentManager,
fragmentTag: String,
configuration: SignInWithAppleConfiguration,
callback: SignInWithAppleCallback
) : this(fragmentManager, fragmentTag, configuration, callback.toFunction())
init {
val fragmentIfShown = fragmentManager.findFragmentByTag(fragmentTag) as? SignInWebViewDialogFragment
fragmentIfShown?.configure(callback)
}
internal data class AuthenticationAttempt(
val authenticationUri: String,
val redirectUri: String,
val state: String
) : Parcelable {
constructor(parcel: Parcel) : this(
parcel.readString() ?: "invalid",
parcel.readString() ?: "invalid",
parcel.readString() ?: "invalid"
)
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(authenticationUri)
parcel.writeString(redirectUri)
parcel.writeString(state)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<AuthenticationAttempt> {
override fun createFromParcel(parcel: Parcel) = AuthenticationAttempt(parcel)
override fun newArray(size: Int): Array<AuthenticationAttempt?> = arrayOfNulls(size)
/**
* Build an Apple URL that supports `form_post` by intercepting the response
* with `FormInterceptorInterface`.
* [Sign In With Apple Javascript SDK](https://developer.apple.com/documentation/signinwithapplejs/configuring_your_webpage_for_sign_in_with_apple).
*/
fun create(configuration: SignInWithAppleConfiguration): AuthenticationAttempt {
val authenticationUri = Uri
.parse("https://appleid.apple.com/auth/authorize")
.buildUpon().apply {
appendQueryParameter("client_id", configuration.clientId)
appendQueryParameter("redirect_uri", configuration.redirectUri)
appendQueryParameter("response_type", configuration.responseType)
appendQueryParameter("scope", configuration.scope)
appendQueryParameter("response_mode", "form_post")
appendQueryParameter("state", configuration.state)
if (!configuration.nonce.isBlank()) {
appendQueryParameter("nonce", configuration.nonce)
}
}
.build()
.toString()
return AuthenticationAttempt(authenticationUri, configuration.redirectUri, configuration.state)
}
}
}
fun show() {
val fragment = SignInWebViewDialogFragment.newInstance(AuthenticationAttempt.create(configuration), configuration.fullScreen)
fragment.configure(callback)
fragment.show(fragmentManager, fragmentTag)
}
}