Skip to content

Commit

Permalink
Replace id.getmailspring.com auth with a dumb identity JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
notpushkin committed Jan 24, 2020
1 parent 1f70ec5 commit fc0b841
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 92 deletions.
125 changes: 106 additions & 19 deletions app/internal_packages/onboarding/lib/page-authenticate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,116 @@ export default class AuthenticatePage extends React.Component {
};

_src() {
const n1Version = AppEnv.getVersion();
return `${MailspringAPIRequest.rootURLForServer(
'identity'
)}/onboarding?utm_medium=N1&utm_source=OnboardingPage&N1_version=${n1Version}&client_edition=basic`;
/* */
return `data:text/html,
<div style="width: 100%; height: 100%; display: flex; align-items: center; justify-content: center">
<!-- By Sam Herbert, for everyone. More @ https://samherbert.net/svg-loaders/ -->
<svg width="120" height="30" viewBox="0 0 120 30" xmlns="http://www.w3.org/2000/svg" fill="#333">
<circle cx="15" cy="15" r="15">
<animate
attributeName="r" from="15" to="15"
begin="0s" dur="0.8s"
values="15;9;15" calcMode="linear"
repeatCount="indefinite" />
<animate
attributeName="fill-opacity" from="1" to="1"
begin="0s" dur="0.8s"
values="1;.5;1" calcMode="linear"
repeatCount="indefinite" />
</circle>
<circle cx="60" cy="15" r="9" fill-opacity="0.3">
<animate
attributeName="r" from="9" to="9"
begin="0s" dur="0.8s"
values="9;15;9" calcMode="linear"
repeatCount="indefinite" />
<animate
attributeName="fill-opacity" from="0.5" to="0.5"
begin="0s" dur="0.8s"
values=".5;1;.5" calcMode="linear"
repeatCount="indefinite" />
</circle>
<circle cx="105" cy="15" r="15">
<animate
attributeName="r" from="15" to="15"
begin="0s" dur="0.8s"
values="15;9;15" calcMode="linear"
repeatCount="indefinite" />
<animate
attributeName="fill-opacity" from="1" to="1"
begin="0s" dur="0.8s"
values="1;.5;1" calcMode="linear"
repeatCount="indefinite" />
</circle>
</svg>
</div>
`;
}

_onDidFinishLoad = webview => {
const receiveUserInfo = `
var a = document.querySelector('#identity-result');
result = a ? a.innerText : null;
`;
webview.executeJavaScript(receiveUserInfo, false, result => {
this.setState({ ready: true, webviewLoading: false });
if (result !== null) {
OnboardingActions.identityJSONReceived(JSON.parse(atob(result)));
}
OnboardingActions.identityJSONReceived({
id: 'ffffffff-ffff-ffff-ffff-ffffffffffff',
token: 'ffffffff-ffff-ffff-ffff-fffffffffff1',
firstName: '',
lastName: '',
emailAddress: '',
object: 'identity',
createdAt: '2020-01-24T18:41:25.000Z',
stripePlan: 'Basic',
stripePlanEffective: 'Basic',
stripeCustomerId: 'cus_Gbkb1jjCVyCu1W',
stripePeriodEnd: null,
featureUsage: {
snooze: {
quota: 15,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'basic-limit',
},
'send-later': {
quota: 5,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'basic-limit',
},
'thread-sharing': {
quota: 3,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'basic-limit',
},
'link-tracking': {
quota: 5,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'basic-limit',
},
'open-tracking': {
quota: 5,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'basic-limit',
},
'contact-profiles': {
quota: 3,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'basic-limit',
},
'send-reminders': {
quota: 5,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'basic-limit',
},
translation: {
quota: 50,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'basic-limit',
},
},
});

const openExternalLink = `
var el = document.querySelector('.open-external');
if (el) {el.addEventListener('click', function(event) {console.log(this.href); event.preventDefault(); return false;})}
`;
webview.executeJavaScript(openExternalLink);
};

render() {
Expand Down
70 changes: 2 additions & 68 deletions app/src/flux/stores/identity-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import url from 'url';
import * as Utils from '../models/utils';
import * as Actions from '../actions';
import KeyManager from '../../key-manager';
import { makeRequest, rootURLForServer } from '../mailspring-api-request';
import { Disposable } from 'event-kit';

// Note this key name is used when migrating to Mailspring Pro accounts from old N1.
Expand Down Expand Up @@ -132,80 +131,15 @@ class _IdentityStore extends MailspringStore {
* https://paper.dropbox.com/doc/Analytics-ID-Unification-oVDTkakFsiBBbk9aeuiA3
* for the full list of utm_ labels.
*/
async fetchSingleSignOnURL(
path,
{ source, campaign, content }: { source?: string; campaign?: string; content?: string } = {}
) {
if (!this._identity) {
return Promise.reject(new Error('fetchSingleSignOnURL: no identity set.'));
}

const qs: any = { utm_medium: 'N1' };
if (source) {
qs.utm_source = source;
}
if (campaign) {
qs.utm_campaign = campaign;
}
if (content) {
qs.utm_content = content;
}

let pathWithUtm = url.parse(path, true);
pathWithUtm.query = Object.assign({}, qs, pathWithUtm.query || {});

const pathWithUtmString = url.format({
pathname: pathWithUtm.pathname,
query: pathWithUtm.query,
});

if (!pathWithUtmString.startsWith('/')) {
throw new Error('fetchSingleSignOnURL: path must start with a leading slash.');
}

const body = new FormData();
for (const key of Object.keys(qs)) {
body.append(key, qs[key]);
}
body.append('next_path', pathWithUtmString);

try {
const json = await makeRequest({
server: 'identity',
path: '/api/login-link',
qs: qs,
body: body,
timeout: 1500,
method: 'POST',
});
return `${rootURLForServer('identity')}${json.path}`;
} catch (err) {
return `${rootURLForServer('identity')}${path}`;
}
async fetchSingleSignOnURL(path, params) {
return `data:text/html,Mailspring ID server is disabled in this build.`;
}

async fetchIdentity() {
if (!this._identity || !this._identity.token) {
return null;
}

const json = await makeRequest({
server: 'identity',
path: '/api/me',
method: 'GET',
});

if (!json || !json.id) {
AppEnv.reportError(new Error('/api/me returned invalid json'), json || {});
return this._identity;
}

if (json.id !== this._identity.id) {
console.log('Note: server returned a different identity object.');
}

const nextIdentity = Object.assign({}, this._identity, json);
await this.saveIdentity(nextIdentity);
return this._identity;
}
}
Expand Down
9 changes: 4 additions & 5 deletions app/src/mailsync-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ export class MailsyncProcess extends EventEmitter {
CONFIG_DIR_PATH: this.configDirPath,
IDENTITY_SERVER: 'unknown',
};
if (process.type === 'renderer') {
const rootURLForServer = require('./flux/mailspring-api-request').rootURLForServer;
env.IDENTITY_SERVER = rootURLForServer('identity');
}

const args = [`--mode`, mode];
if (this.verbose) {
Expand All @@ -162,7 +158,10 @@ export class MailsyncProcess extends EventEmitter {
var rs = new Readable();
rs.push(`${JSON.stringify(this.account)}\n${JSON.stringify(this.identity)}\n`);
rs.push(null);
rs.pipe(this._proc.stdin, { end: false });
rs.pipe(
this._proc.stdin,
{ end: false }
);
});
}
}
Expand Down

0 comments on commit fc0b841

Please sign in to comment.