forked from robfallows/tunguska-imgur
-
Notifications
You must be signed in to change notification settings - Fork 3
/
apple_client.js
72 lines (66 loc) · 2.27 KB
/
apple_client.js
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
/* global OAuth */
import Apple from './namespace.js';
/**
* Request Apple credentials for the user (boilerplate).
* Called from accounts-apple.
*
* @param {Object} options Optional
* @param {Function} credentialRequestCompleteCallback Callback function to call on completion. Takes one argument, credentialToken on success, or Error on error.
*/
Apple.requestCredential = function(options, credentialRequestCompleteCallback) {
// Support both (options, callback) and (callback).
if (!credentialRequestCompleteCallback && typeof options === 'function') {
credentialRequestCompleteCallback = options;
options = {};
} else if (!options) {
options = {};
}
const config = ServiceConfiguration.configurations.findOne({
service: 'apple',
});
if (!config) {
credentialRequestCompleteCallback
&& credentialRequestCompleteCallback(new ServiceConfiguration.ConfigError());
return;
}
const credentialToken = Random.secret();
const loginStyle = Apple._isNativeSignInWindow()
? 'redirect'
: OAuth._loginStyle('apple', config, options);
const scope = options && options.requestPermissions ? options.requestPermissions.join('%20') : 'name%20email';
const loginUrl = 'https://appleid.apple.com/auth/authorize'
+ '?response_type=code%20id_token'
+ '&response_mode=form_post'
+ `&redirect_uri=${config.redirectUri}`
+ `&client_id=${config.clientId}`
+ `&scope=${scope}`
+ `&state=${OAuth._stateParam(loginStyle, credentialToken)}`;
OAuth.launchLogin({
loginService: 'apple',
loginStyle,
loginUrl,
credentialRequestCompleteCallback,
credentialToken,
popupOptions: {
height: 600,
},
});
};
/**
* Checks if browser uses native sign in window
*
* webkit >=605 on iOS and macos shows sign in with apple as native ui screen
* and then we need to use a redirect login style
*
* (Would like to have a better way to check this but it works for now)
*/
Apple._isNativeSignInWindow = function() {
const minVersionNative = 605;
const userAgent = ((navigator && navigator.userAgent) || '').toLowerCase();
const match = userAgent.match(/applewebkit\/(\d+)/);
if (match === null) {
return false;
}
const version = match[1];
return version >= minVersionNative;
};