From f996c0de2c234d2e297502745dc289398996c775 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 15 Dec 2021 19:38:30 -0300 Subject: [PATCH 1/5] WIP --- app/apple/server/appleOauthManifest.ts | 13 +++ app/apple/server/appleOauthRegisterService.ts | 81 +++++++++++++++++++ app/apple/server/startup.ts | 29 +++---- app/ui-login/client/login/services.html | 23 ++++-- client/lib/utils/createAnchor.ts | 4 +- client/templates.ts | 8 ++ .../login/AppleOauth/AppleOauthButton.tsx | 68 ++++++++++++++++ definition/rest/v1/settings.ts | 2 +- 8 files changed, 198 insertions(+), 30 deletions(-) create mode 100644 app/apple/server/appleOauthManifest.ts create mode 100644 app/apple/server/appleOauthRegisterService.ts create mode 100644 client/views/login/AppleOauth/AppleOauthButton.tsx diff --git a/app/apple/server/appleOauthManifest.ts b/app/apple/server/appleOauthManifest.ts new file mode 100644 index 000000000000..fa4fbeeff7b6 --- /dev/null +++ b/app/apple/server/appleOauthManifest.ts @@ -0,0 +1,13 @@ +import { Meteor } from 'meteor/meteor'; +import { WebApp } from 'meteor/webapp'; + +import { settings } from '../../settings/server'; + +WebApp.connectHandlers.use('/.well-known/apple-developer-domain-association.txt', Meteor.bindEnvironment(function(req, res) { + res.writeHead(200, { + 'Content-Type': 'text/plain', + 'Access-Control-Allow-Origin': '*', + }); + + res.end(settings.get('Accounts_OAuth_Apple_manifest')); +})); diff --git a/app/apple/server/appleOauthRegisterService.ts b/app/apple/server/appleOauthRegisterService.ts new file mode 100644 index 000000000000..666e24508151 --- /dev/null +++ b/app/apple/server/appleOauthRegisterService.ts @@ -0,0 +1,81 @@ +// import { Meteor } from 'meteor/meteor'; +// import { Tracker } from 'meteor/tracker'; +// import _ from 'underscore'; + +import { jws } from 'jsrsasign'; +import { ServiceConfiguration } from 'meteor/service-configuration'; + + +/* eslint-disable @typescript-eslint/camelcase */ + +import { CustomOAuth } from '../../custom-oauth/server/custom_oauth_server'; +import { settings, settingsRegistry } from '../../settings/server'; + + +const config = { + serverURL: 'https://appleid.apple.com', + identityPath: '/auth/token', + scope: 'name email', + mergeUsers: true, + accessTokenParam: 'access_token', + loginStyle: 'redirect', +}; + +// chat.rocket.gazzo + +new CustomOAuth('apple', config); + +settingsRegistry.addGroup('OAuth', function() { + this.section('Apple', function() { + this.add('Accounts_OAuth_Apple', false, { type: 'boolean', public: true }); + + + this.add('Accounts_OAuth_Apple_clientId', '', { type: 'string', enableQuery: { Accounts_OAuth_Apple: true } }); + this.add('Accounts_OAuth_Apple_secret', '', { type: 'string', enableQuery: { Accounts_OAuth_Apple: true } }); + this.add('Accounts_OAuth_Apple_manifest', '', { type: 'string', enableQuery: { Accounts_OAuth_Apple: true } }); + this.add('Accounts_OAuth_Apple_redirectUri', '', { type: 'string', enableQuery: { Accounts_OAuth_Apple: true } }); + + this.add('Accounts_OAuth_Apple_iss', '', { type: 'string', enableQuery: { Accounts_OAuth_Apple: true } }); + this.add('Accounts_OAuth_Apple_kid', '', { type: 'string', enableQuery: { Accounts_OAuth_Apple: true } }); + }); +}); + + +settings.watchMultiple(['Accounts_OAuth_Apple', 'Accounts_OAuth_Apple_clientId', 'Accounts_OAuth_Apple_secret', 'Accounts_OAuth_Apple_iss', 'Accounts_OAuth_Apple_kid'], ([enabled, clientId, serverSecret, iss, kid]) => { + if (!enabled) { + return ServiceConfiguration.configurations.remove({ + service: 'apple', + }); + } + + const HEADER = { + typ: 'JWT', + kid, + alg: 'ES256', + }; + + + const tokenPayload = { + iss, + iat: jws.IntDate.get('now'), + exp: 15780000, + aud: 'https://appleid.apple.com', + sub: clientId, + }; + + const header = JSON.stringify(HEADER); + + + ServiceConfiguration.configurations.upsert({ + service: 'apple', + }, { + $set: { + // We'll hide this button on Web Client + showButton: false, + secret: jws.JWS.sign(HEADER.alg, header, JSON.stringify(tokenPayload), { rstr: serverSecret }), + enabled: settings.get('Accounts_OAuth_Apple'), + loginStyle: 'redirect', + clientId, + }, + }); +}); diff --git a/app/apple/server/startup.ts b/app/apple/server/startup.ts index 97ba8b3a239f..6e7dab81ebb6 100644 --- a/app/apple/server/startup.ts +++ b/app/apple/server/startup.ts @@ -1,28 +1,17 @@ -import { ServiceConfiguration } from 'meteor/service-configuration'; - -import { settings, settingsRegistry } from '../../settings/server'; +/* eslint-disable @typescript-eslint/camelcase */ +import { settingsRegistry } from '../../settings/server'; +import './appleOauthManifest'; +import './appleOauthRegisterService'; settingsRegistry.addGroup('OAuth', function() { this.section('Apple', function() { this.add('Accounts_OAuth_Apple', false, { type: 'boolean', public: true }); - }); -}); - + this.add('Accounts_OAuth_Apple_clientId', '', { type: 'string', public: true }); + this.add('Accounts_OAuth_Apple_secret', '', { type: 'string', public: true }); + this.add('Accounts_OAuth_Apple_manifest', '', { type: 'string' }); -settings.watch('Accounts_OAuth_Apple', (enabled) => { - if (!enabled) { - return ServiceConfiguration.configurations.remove({ - service: 'apple', - }); - } - ServiceConfiguration.configurations.upsert({ - service: 'apple', - }, { - $set: { - // We'll hide this button on Web Client - showButton: false, - enabled: settings.get('Accounts_OAuth_Apple'), - }, + this.add('Accounts_OAuth_Apple_iss', '', { type: 'string' }); + this.add('Accounts_OAuth_Apple_kid', '', { type: 'string' }); }); }); diff --git a/app/ui-login/client/login/services.html b/app/ui-login/client/login/services.html index bb19c56e00d3..cc013fc5162b 100644 --- a/app/ui-login/client/login/services.html +++ b/app/ui-login/client/login/services.html @@ -1,9 +1,18 @@ -