@capawesome/capacitor-apple-sign-in 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapawesomeCapacitorAppleSignIn.podspec +17 -0
- package/Package.swift +28 -0
- package/README.md +220 -0
- package/android/build.gradle +58 -0
- package/android/src/main/AndroidManifest.xml +9 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/AppleSignIn.java +98 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/AppleSignInActivity.java +366 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/AppleSignInPlugin.java +106 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/classes/CustomException.java +20 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/classes/CustomExceptions.java +12 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/classes/options/SignInOptions.java +84 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/classes/results/SignInResult.java +65 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/applesignin/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/android/src/main/res/drawable/ic_close.xml +9 -0
- package/dist/docs.json +345 -0
- package/dist/esm/definitions.d.ts +181 -0
- package/dist/esm/definitions.js +55 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +10 -0
- package/dist/esm/web.js +79 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +148 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +151 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/AppleSignIn.swift +48 -0
- package/ios/Plugin/AppleSignInPlugin.swift +67 -0
- package/ios/Plugin/Classes/Options/SignInOptions.swift +31 -0
- package/ios/Plugin/Classes/Results/SignInResult.swift +46 -0
- package/ios/Plugin/Enums/CustomError.swift +26 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/Protocols/Result.swift +6 -0
- package/package.json +93 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = 'CapawesomeCapacitorAppleSignIn'
|
|
7
|
+
s.version = package['version']
|
|
8
|
+
s.summary = package['description']
|
|
9
|
+
s.license = package['license']
|
|
10
|
+
s.homepage = package['repository']['url']
|
|
11
|
+
s.author = package['author']
|
|
12
|
+
s.source = { :git => package['repository']['url'], :tag => s.version.to_s }
|
|
13
|
+
s.source_files = 'ios/Plugin/**/*.{swift,h,m,c,cc,mm,cpp}'
|
|
14
|
+
s.ios.deployment_target = '15.0'
|
|
15
|
+
s.dependency 'Capacitor'
|
|
16
|
+
s.swift_version = '5.1'
|
|
17
|
+
end
|
package/Package.swift
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// swift-tools-version: 5.9
|
|
2
|
+
import PackageDescription
|
|
3
|
+
|
|
4
|
+
let package = Package(
|
|
5
|
+
name: "CapawesomeCapacitorAppleSignIn",
|
|
6
|
+
platforms: [.iOS(.v15)],
|
|
7
|
+
products: [
|
|
8
|
+
.library(
|
|
9
|
+
name: "CapawesomeCapacitorAppleSignIn",
|
|
10
|
+
targets: ["AppleSignInPlugin"])
|
|
11
|
+
],
|
|
12
|
+
dependencies: [
|
|
13
|
+
.package(url: "https://github.com/ionic-team/capacitor-swift-pm.git", from: "8.0.0")
|
|
14
|
+
],
|
|
15
|
+
targets: [
|
|
16
|
+
.target(
|
|
17
|
+
name: "AppleSignInPlugin",
|
|
18
|
+
dependencies: [
|
|
19
|
+
.product(name: "Capacitor", package: "capacitor-swift-pm"),
|
|
20
|
+
.product(name: "Cordova", package: "capacitor-swift-pm")
|
|
21
|
+
],
|
|
22
|
+
path: "ios/Plugin"),
|
|
23
|
+
.testTarget(
|
|
24
|
+
name: "AppleSignInPluginTests",
|
|
25
|
+
dependencies: ["AppleSignInPlugin"],
|
|
26
|
+
path: "ios/PluginTests")
|
|
27
|
+
]
|
|
28
|
+
)
|
package/README.md
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# @capawesome/capacitor-apple-sign-in
|
|
2
|
+
|
|
3
|
+
Unofficial Capacitor plugin to sign-in with Apple.[^1]
|
|
4
|
+
|
|
5
|
+
<div class="capawesome-z29o10a">
|
|
6
|
+
<a href="https://cloud.capawesome.io/" target="_blank">
|
|
7
|
+
<img alt="Deliver Live Updates to your Capacitor app with Capawesome Cloud" src="https://cloud.capawesome.io/assets/banners/cloud-build-and-deploy-capacitor-apps.png?t=1" />
|
|
8
|
+
</a>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
We are proud to offer one of the most complete and feature-rich Capacitor plugins for Apple Sign-In. Here are some of the key features:
|
|
14
|
+
|
|
15
|
+
- ðĨïļ **Cross-platform**: Supports Android, iOS, and Web.
|
|
16
|
+
- ð **Native Sign-In**: Uses native AuthenticationServices on iOS.
|
|
17
|
+
- ð **WebView OAuth**: Implements Apple OAuth flow on Android without external dependencies.
|
|
18
|
+
- ð§ **Scope support**: Request email and full name on all platforms.
|
|
19
|
+
- ð **Nonce & state**: Supports nonce for replay protection and state for CSRF protection.
|
|
20
|
+
- ðŠķ **Lightweight**: Just a single dependency and zero unnecessary bloat.
|
|
21
|
+
- ðĪ **Compatibility**: Compatible with the [Google Sign-In](https://capawesome.io/plugins/google-sign-in) and [OAuth](https://capawesome.io/plugins/oauth) plugins.
|
|
22
|
+
- ðĶ **CocoaPods & SPM**: Supports CocoaPods and Swift Package Manager for iOS.
|
|
23
|
+
- ð **Up-to-date**: Always supports the latest Capacitor version.
|
|
24
|
+
|
|
25
|
+
Missing a feature? Just [open an issue](https://github.com/capawesome-team/capacitor-plugins/issues) and we'll take a look!
|
|
26
|
+
|
|
27
|
+
## Compatibility
|
|
28
|
+
|
|
29
|
+
| Plugin Version | Capacitor Version | Status |
|
|
30
|
+
| -------------- | ----------------- | -------------- |
|
|
31
|
+
| 0.1.x | >=8.x.x | Active support |
|
|
32
|
+
|
|
33
|
+
## Guides
|
|
34
|
+
|
|
35
|
+
- [How to Sign In with Apple using Capacitor](https://capawesome.io/blog/how-to-sign-in-with-apple-using-capacitor/)
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install @capawesome/capacitor-apple-sign-in
|
|
41
|
+
npx cap sync
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### iOS
|
|
45
|
+
|
|
46
|
+
Add the **Sign in with Apple** capability to your app in Xcode:
|
|
47
|
+
|
|
48
|
+
1. Open your app target in Xcode.
|
|
49
|
+
1. Go to the **Signing & Capabilities** tab.
|
|
50
|
+
1. Click **+ Capability** and add **Sign in with Apple**.
|
|
51
|
+
|
|
52
|
+
### Web
|
|
53
|
+
|
|
54
|
+
The plugin loads the [Apple JS SDK](https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js) automatically.
|
|
55
|
+
Make sure you have configured your Apple Service ID with the correct redirect URL and web domain in the [Apple Developer Portal](https://developer.apple.com/account/resources/identifiers/list/serviceId).
|
|
56
|
+
|
|
57
|
+
## Configuration
|
|
58
|
+
|
|
59
|
+
No configuration required for this plugin.
|
|
60
|
+
|
|
61
|
+
## Usage
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { AppleSignIn, SignInScope } from '@capawesome/capacitor-apple-sign-in';
|
|
65
|
+
|
|
66
|
+
const initialize = async () => {
|
|
67
|
+
await AppleSignIn.initialize({
|
|
68
|
+
clientId: 'com.example.app.signin',
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const signIn = async () => {
|
|
73
|
+
const result = await AppleSignIn.signIn({
|
|
74
|
+
scopes: [SignInScope.Email, SignInScope.FullName],
|
|
75
|
+
redirectUrl: 'https://example.com/callback',
|
|
76
|
+
nonce: 'random-nonce',
|
|
77
|
+
state: 'random-state',
|
|
78
|
+
});
|
|
79
|
+
return result;
|
|
80
|
+
};
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## API
|
|
84
|
+
|
|
85
|
+
<docgen-index>
|
|
86
|
+
|
|
87
|
+
* [`initialize(...)`](#initialize)
|
|
88
|
+
* [`signIn(...)`](#signin)
|
|
89
|
+
* [Interfaces](#interfaces)
|
|
90
|
+
* [Enums](#enums)
|
|
91
|
+
|
|
92
|
+
</docgen-index>
|
|
93
|
+
|
|
94
|
+
<docgen-api>
|
|
95
|
+
<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
|
|
96
|
+
|
|
97
|
+
### initialize(...)
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
initialize(options: InitializeOptions) => Promise<void>
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Initialize the plugin.
|
|
104
|
+
|
|
105
|
+
This method must be called before `signIn()` on **Android** and **Web**.
|
|
106
|
+
|
|
107
|
+
Only available on Android and Web.
|
|
108
|
+
|
|
109
|
+
| Param | Type |
|
|
110
|
+
| ------------- | --------------------------------------------------------------- |
|
|
111
|
+
| **`options`** | <code><a href="#initializeoptions">InitializeOptions</a></code> |
|
|
112
|
+
|
|
113
|
+
**Since:** 0.1.0
|
|
114
|
+
|
|
115
|
+
--------------------
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
### signIn(...)
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
signIn(options?: SignInOptions | undefined) => Promise<SignInResult>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Sign in with Apple.
|
|
125
|
+
|
|
126
|
+
| Param | Type |
|
|
127
|
+
| ------------- | ------------------------------------------------------- |
|
|
128
|
+
| **`options`** | <code><a href="#signinoptions">SignInOptions</a></code> |
|
|
129
|
+
|
|
130
|
+
**Returns:** <code>Promise<<a href="#signinresult">SignInResult</a>></code>
|
|
131
|
+
|
|
132
|
+
**Since:** 0.1.0
|
|
133
|
+
|
|
134
|
+
--------------------
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
### Interfaces
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
#### InitializeOptions
|
|
141
|
+
|
|
142
|
+
| Prop | Type | Description | Since |
|
|
143
|
+
| -------------- | ------------------- | ---------------------------------------- | ----- |
|
|
144
|
+
| **`clientId`** | <code>string</code> | The Apple Service ID to use for sign-in. | 0.1.0 |
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
#### SignInResult
|
|
148
|
+
|
|
149
|
+
| Prop | Type | Description | Since |
|
|
150
|
+
| ----------------------- | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ----- |
|
|
151
|
+
| **`authorizationCode`** | <code>string</code> | The authorization code. | 0.1.0 |
|
|
152
|
+
| **`idToken`** | <code>string</code> | The ID token (JWT). | 0.1.0 |
|
|
153
|
+
| **`user`** | <code>string</code> | The stable Apple user identifier. On **Android** and **Web**, this is decoded from the JWT `sub` claim. | 0.1.0 |
|
|
154
|
+
| **`email`** | <code>string \| null</code> | The user's email address. On **iOS**, this is only provided on the first sign-in. | 0.1.0 |
|
|
155
|
+
| **`givenName`** | <code>string \| null</code> | The user's given name. On **iOS**, this is only provided on the first sign-in. | 0.1.0 |
|
|
156
|
+
| **`familyName`** | <code>string \| null</code> | The user's family name. On **iOS**, this is only provided on the first sign-in. | 0.1.0 |
|
|
157
|
+
| **`state`** | <code>string</code> | The state value from the sign-in request. Only available on Android and Web. | 0.1.0 |
|
|
158
|
+
| **`realUserStatus`** | <code><a href="#realuserstatus">RealUserStatus</a></code> | The real user status. Only available on iOS. | 0.1.0 |
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
#### SignInOptions
|
|
162
|
+
|
|
163
|
+
| Prop | Type | Description | Since |
|
|
164
|
+
| ----------------- | -------------------------- | ----------------------------------------------------------------------------- | ----- |
|
|
165
|
+
| **`redirectUrl`** | <code>string</code> | The OAuth redirect URL to use for sign-in. Only available on Android and Web. | 0.1.0 |
|
|
166
|
+
| **`scopes`** | <code>SignInScope[]</code> | The scopes to request during sign-in. | 0.1.0 |
|
|
167
|
+
| **`nonce`** | <code>string</code> | A nonce for replay protection. | 0.1.0 |
|
|
168
|
+
| **`state`** | <code>string</code> | A state value for CSRF protection. Only available on Android and Web. | 0.1.0 |
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
### Enums
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
#### RealUserStatus
|
|
175
|
+
|
|
176
|
+
| Members | Value | Description | Since |
|
|
177
|
+
| ----------------- | -------------------------- | ------------------------------------------------------------- | ----- |
|
|
178
|
+
| **`LikelyReal`** | <code>'LIKELY_REAL'</code> | The user appears to be a real person. | 0.1.0 |
|
|
179
|
+
| **`Unknown`** | <code>'UNKNOWN'</code> | The system can't determine whether the user is a real person. | 0.1.0 |
|
|
180
|
+
| **`Unsupported`** | <code>'UNSUPPORTED'</code> | The real user status is not supported on this platform. | 0.1.0 |
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
#### SignInScope
|
|
184
|
+
|
|
185
|
+
| Members | Value | Description | Since |
|
|
186
|
+
| -------------- | ------------------------ | --------------------------------- | ----- |
|
|
187
|
+
| **`Email`** | <code>'EMAIL'</code> | Request the user's email address. | 0.1.0 |
|
|
188
|
+
| **`FullName`** | <code>'FULL_NAME'</code> | Request the user's full name. | 0.1.0 |
|
|
189
|
+
|
|
190
|
+
</docgen-api>
|
|
191
|
+
|
|
192
|
+
## Security
|
|
193
|
+
|
|
194
|
+
This plugin handles the OAuth flow and returns tokens to your app. To keep your integration secure, be aware of the following:
|
|
195
|
+
|
|
196
|
+
- **Server-side token verification is required.** The `idToken` (JWT) is **not** verified client-side. Your backend **must** verify the JWT signature using [Apple's public keys](https://appleid.apple.com/auth/keys) before trusting any claims (e.g. `user`, `email`). Never use client-side token data for authorization decisions without server-side verification.
|
|
197
|
+
- **Validate the `state` parameter.** The plugin passes the `state` value through to the result but does **not** validate it. Your app must compare the returned `state` against the value it originally sent to protect against CSRF attacks.
|
|
198
|
+
- **Android uses a WebView-based OAuth flow.** Unlike iOS (which uses native `AuthenticationServices`), the Android implementation renders Apple's sign-in page in a WebView. Unlike a system browser flow, the WebView is controlled by the app and has access to page content. Ensure your `redirectUrl` uses HTTPS and points to a domain you control.
|
|
199
|
+
|
|
200
|
+
## FAQ
|
|
201
|
+
|
|
202
|
+
### What's the difference between this plugin and other Apple Sign-In plugins?
|
|
203
|
+
|
|
204
|
+
This plugin is purpose-built for Apple Sign-In and focuses on providing a clean and modern API with the latest platform features. Here are some of the key differences:
|
|
205
|
+
|
|
206
|
+
- **Cross-platform**: Supports Android, iOS, and Web.
|
|
207
|
+
- **Lightweight**: No unnecessary dependencies. Just Apple Sign-In, nothing else.
|
|
208
|
+
- **No deprecated APIs**: Uses the latest platform APIs (AuthenticationServices on iOS).
|
|
209
|
+
- **Scope support**: Request email and full name on all platforms.
|
|
210
|
+
- **Error codes**: Provides typed error codes for proper error handling.
|
|
211
|
+
|
|
212
|
+
## Changelog
|
|
213
|
+
|
|
214
|
+
See [CHANGELOG.md](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/apple-sign-in/CHANGELOG.md).
|
|
215
|
+
|
|
216
|
+
## License
|
|
217
|
+
|
|
218
|
+
See [LICENSE](https://github.com/capawesome-team/capacitor-plugins/blob/main/packages/apple-sign-in/LICENSE).
|
|
219
|
+
|
|
220
|
+
[^1]: This project is not affiliated with, endorsed by, sponsored by, or approved by Apple Inc. or any of their affiliates or subsidiaries.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
ext {
|
|
2
|
+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.13.2'
|
|
3
|
+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.7.1'
|
|
4
|
+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.3.0'
|
|
5
|
+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.7.0'
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
buildscript {
|
|
9
|
+
repositories {
|
|
10
|
+
google()
|
|
11
|
+
mavenCentral()
|
|
12
|
+
}
|
|
13
|
+
dependencies {
|
|
14
|
+
classpath 'com.android.tools.build:gradle:8.13.0'
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
apply plugin: 'com.android.library'
|
|
19
|
+
|
|
20
|
+
android {
|
|
21
|
+
namespace = "io.capawesome.capacitorjs.plugins.applesignin"
|
|
22
|
+
compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 36
|
|
23
|
+
defaultConfig {
|
|
24
|
+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 24
|
|
25
|
+
targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 36
|
|
26
|
+
versionCode 1
|
|
27
|
+
versionName "1.0"
|
|
28
|
+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
29
|
+
}
|
|
30
|
+
buildTypes {
|
|
31
|
+
release {
|
|
32
|
+
minifyEnabled false
|
|
33
|
+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
lintOptions {
|
|
37
|
+
abortOnError = false
|
|
38
|
+
}
|
|
39
|
+
compileOptions {
|
|
40
|
+
sourceCompatibility JavaVersion.VERSION_21
|
|
41
|
+
targetCompatibility JavaVersion.VERSION_21
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
repositories {
|
|
46
|
+
google()
|
|
47
|
+
mavenCentral()
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
dependencies {
|
|
52
|
+
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
|
53
|
+
implementation project(':capacitor-android')
|
|
54
|
+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
|
|
55
|
+
testImplementation "junit:junit:$junitVersion"
|
|
56
|
+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
|
|
57
|
+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
|
|
58
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|
2
|
+
<uses-permission android:name="android.permission.INTERNET" />
|
|
3
|
+
<application>
|
|
4
|
+
<activity
|
|
5
|
+
android:name=".AppleSignInActivity"
|
|
6
|
+
android:exported="false"
|
|
7
|
+
android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
|
|
8
|
+
</application>
|
|
9
|
+
</manifest>
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.applesignin;
|
|
2
|
+
|
|
3
|
+
import android.content.Intent;
|
|
4
|
+
import android.net.Uri;
|
|
5
|
+
import android.util.Base64;
|
|
6
|
+
import androidx.annotation.NonNull;
|
|
7
|
+
import androidx.annotation.Nullable;
|
|
8
|
+
import io.capawesome.capacitorjs.plugins.applesignin.classes.CustomExceptions;
|
|
9
|
+
import io.capawesome.capacitorjs.plugins.applesignin.classes.options.SignInOptions;
|
|
10
|
+
import io.capawesome.capacitorjs.plugins.applesignin.classes.results.SignInResult;
|
|
11
|
+
import io.capawesome.capacitorjs.plugins.applesignin.interfaces.NonEmptyResultCallback;
|
|
12
|
+
import java.nio.charset.StandardCharsets;
|
|
13
|
+
import java.util.List;
|
|
14
|
+
import org.json.JSONObject;
|
|
15
|
+
|
|
16
|
+
public class AppleSignIn {
|
|
17
|
+
|
|
18
|
+
private static final String AUTH_URL = "https://appleid.apple.com/auth/authorize";
|
|
19
|
+
|
|
20
|
+
@Nullable
|
|
21
|
+
private String clientId;
|
|
22
|
+
|
|
23
|
+
public void initialize(@NonNull String clientId) {
|
|
24
|
+
this.clientId = clientId;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@NonNull
|
|
28
|
+
public String buildAuthUrl(@NonNull SignInOptions options) throws Exception {
|
|
29
|
+
if (clientId == null) {
|
|
30
|
+
throw CustomExceptions.CLIENT_ID_MISSING;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
Uri.Builder builder = Uri.parse(AUTH_URL)
|
|
34
|
+
.buildUpon()
|
|
35
|
+
.appendQueryParameter("client_id", clientId)
|
|
36
|
+
.appendQueryParameter("redirect_uri", options.getRedirectUrl())
|
|
37
|
+
.appendQueryParameter("response_type", "code id_token")
|
|
38
|
+
.appendQueryParameter("response_mode", "form_post");
|
|
39
|
+
|
|
40
|
+
List<String> scopes = options.getScopes();
|
|
41
|
+
if (!scopes.isEmpty()) {
|
|
42
|
+
builder.appendQueryParameter("scope", String.join(" ", scopes));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
String nonce = options.getNonce();
|
|
46
|
+
if (nonce != null) {
|
|
47
|
+
builder.appendQueryParameter("nonce", nonce);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
String state = options.getState();
|
|
51
|
+
if (state != null) {
|
|
52
|
+
builder.appendQueryParameter("state", state);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return builder.build().toString();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public void handleActivityResult(int resultCode, @Nullable Intent data, @NonNull NonEmptyResultCallback<SignInResult> callback) {
|
|
59
|
+
if (resultCode != android.app.Activity.RESULT_OK || data == null) {
|
|
60
|
+
callback.error(CustomExceptions.SIGN_IN_CANCELED);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
String authorizationCode = data.getStringExtra(AppleSignInActivity.EXTRA_AUTHORIZATION_CODE);
|
|
65
|
+
String idToken = data.getStringExtra(AppleSignInActivity.EXTRA_ID_TOKEN);
|
|
66
|
+
String state = data.getStringExtra(AppleSignInActivity.EXTRA_STATE);
|
|
67
|
+
String email = data.getStringExtra(AppleSignInActivity.EXTRA_EMAIL);
|
|
68
|
+
String givenName = data.getStringExtra(AppleSignInActivity.EXTRA_GIVEN_NAME);
|
|
69
|
+
String familyName = data.getStringExtra(AppleSignInActivity.EXTRA_FAMILY_NAME);
|
|
70
|
+
|
|
71
|
+
if (authorizationCode == null || idToken == null) {
|
|
72
|
+
callback.error(CustomExceptions.SIGN_IN_FAILED);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
String user = null;
|
|
77
|
+
try {
|
|
78
|
+
JSONObject payload = decodeJwtPayload(idToken);
|
|
79
|
+
user = payload.getString("sub");
|
|
80
|
+
if (email == null) {
|
|
81
|
+
email = payload.optString("email", null);
|
|
82
|
+
}
|
|
83
|
+
} catch (Exception e) {
|
|
84
|
+
callback.error(CustomExceptions.SIGN_IN_FAILED);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
SignInResult result = new SignInResult(authorizationCode, idToken, user, email, givenName, familyName, state);
|
|
89
|
+
callback.success(result);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@NonNull
|
|
93
|
+
private static JSONObject decodeJwtPayload(@NonNull String jwt) throws Exception {
|
|
94
|
+
String[] parts = jwt.split("\\.");
|
|
95
|
+
byte[] decoded = Base64.decode(parts[1], Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
|
|
96
|
+
return new JSONObject(new String(decoded, StandardCharsets.UTF_8));
|
|
97
|
+
}
|
|
98
|
+
}
|