From 58c54e5b94bf108d963a62f0cd72b0abf39dd6c4 Mon Sep 17 00:00:00 2001 From: vjrj Date: Fri, 26 Jan 2018 12:49:53 +0100 Subject: [PATCH] Telegram auth (wip) --- .../api/Subscriptions/server/publications.js | 2 +- imports/api/Users/server/methods.js | 9 ++++ .../components/SelectionMap/SelectionMap.js | 2 +- imports/ui/layouts/App/App.js | 4 +- imports/ui/pages/Auth/Auth.js | 48 +++++++++++++++++++ imports/ui/pages/Signup/Signup.js | 25 ++++++++-- test/encode.test.js | 11 +++-- 7 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 imports/ui/pages/Auth/Auth.js diff --git a/imports/api/Subscriptions/server/publications.js b/imports/api/Subscriptions/server/publications.js index f5f35cf..f67fc3a 100644 --- a/imports/api/Subscriptions/server/publications.js +++ b/imports/api/Subscriptions/server/publications.js @@ -41,7 +41,7 @@ Meteor.publishTransformed('userSubsToFires', function transform() { }); Meteor.publish('mysubscriptions', function subscriptions() { - return Subscriptions.find({ owner: this.userId, type: 'web' }); + return Subscriptions.find({ owner: this.userId }); // type: 'web' }); // Note: subscriptions.view is also used when editing an existing subscription. diff --git a/imports/api/Users/server/methods.js b/imports/api/Users/server/methods.js index 0a8e1df..dc3c232 100644 --- a/imports/api/Users/server/methods.js +++ b/imports/api/Users/server/methods.js @@ -1,6 +1,8 @@ +/* eslint-disable import/no-absolute-path */ import { Meteor } from 'meteor/meteor'; import { check, Match } from 'meteor/check'; import { Accounts } from 'meteor/accounts-base'; +import urlEnc from '/imports/modules/url-encode'; import editProfile from './edit-profile'; import rateLimit from '../../../modules/rate-limit'; @@ -34,6 +36,13 @@ Meteor.methods({ 'users.setLang': function userLang(lang) { check(lang, validLangCode); Meteor.users.update({ _id: this.userId }, { $set: { lang } }); + }, + 'auth.getHash': async function getHash() { + const token = Accounts._generateStampedLoginToken(); + const obj = Accounts._hashStampedToken(token); + // console.log(obj); + const sealed = await urlEnc.encrypt(obj); + return sealed; } }); diff --git a/imports/ui/components/SelectionMap/SelectionMap.js b/imports/ui/components/SelectionMap/SelectionMap.js index bbc008a..cd8cfbd 100644 --- a/imports/ui/components/SelectionMap/SelectionMap.js +++ b/imports/ui/components/SelectionMap/SelectionMap.js @@ -292,6 +292,6 @@ export default translate([], { wait: true })(withTracker((props) => { center: props.center[0] !== null ? props.center : geolocation.get(), distance: props.distance, loadingSubs: !subscription.ready(), - currentSubs: UserSubsToFiresCollection.find({ owner: Meteor.userId(), type: 'web' }).fetch() + currentSubs: UserSubsToFiresCollection.find({ owner: Meteor.userId() }).fetch() // type: 'web' }; })(SelectionMap)); diff --git a/imports/ui/layouts/App/App.js b/imports/ui/layouts/App/App.js index 9b49053..a06169a 100644 --- a/imports/ui/layouts/App/App.js +++ b/imports/ui/layouts/App/App.js @@ -27,6 +27,7 @@ import NewSubscription from '../../pages/NewSubscription/NewSubscription'; import ViewSubscription from '../../pages/ViewSubscription/ViewSubscription'; import EditSubscription from '../../pages/EditSubscription/EditSubscription'; import Signup from '../../pages/Signup/Signup'; +import Auth from '../../pages/Auth/Auth'; import Login from '../../pages/Login/Login'; import Logout from '../../pages/Logout/Logout'; import VerifyEmail from '../../pages/VerifyEmail/VerifyEmail'; @@ -70,6 +71,7 @@ const App = props => ( + @@ -121,7 +123,7 @@ export default withTracker(() => { const loading = !Roles.subscription.ready(); const name = user && user.profile && user.profile.name && getUserName(user.profile.name); const emailAddress = user && user.emails && user.emails[0].address; - console.log(`i18n ready?: ${i18nReady.get()}`); + // console.log(`i18n ready?: ${i18nReady.get()}`); return { loading, loggingIn, diff --git a/imports/ui/pages/Auth/Auth.js b/imports/ui/pages/Auth/Auth.js new file mode 100644 index 0000000..e8a4048 --- /dev/null +++ b/imports/ui/pages/Auth/Auth.js @@ -0,0 +1,48 @@ +/* eslint-disable react/jsx-indent-props */ +/* eslint-disable import/no-absolute-path */ +/* eslint-disable import/no-absolute-path */ + +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import { translate } from 'react-i18next'; +import { Bert } from 'meteor/themeteorchef:bert'; +import { Meteor } from 'meteor/meteor'; +import { T9n } from 'meteor-accounts-t9n'; +import { Accounts } from 'meteor/accounts-base'; + +class Auth extends Component { + constructor(props) { + super(props); + const { token } = props.match.params; + // console.log(token); + console.log(Meteor.connection); + console.log(Accounts.connection); + + Meteor.loginWithToken(token, (error) => { + // this is going to throw error if we logged out + if (error) { + Bert.alert(T9n.get(`error.accounts.${error.reason}`), 'danger'); + // props.history.push('/login'); + } else { + console.log('loginWithToken'); + } + }); + } + + render() { + return ( +
+ ); + } +} + +Auth.propTypes = { + // t: PropTypes.func.isRequired, + history: PropTypes.object.isRequired, + match: PropTypes.object.isRequired +}; + +Auth.defaultProps = { +}; + +export default translate([], { wait: true })(Auth); diff --git a/imports/ui/pages/Signup/Signup.js b/imports/ui/pages/Signup/Signup.js index 608885c..7857921 100644 --- a/imports/ui/pages/Signup/Signup.js +++ b/imports/ui/pages/Signup/Signup.js @@ -8,7 +8,6 @@ import { Link } from 'react-router-dom'; import { Meteor } from 'meteor/meteor'; import { Accounts } from 'meteor/accounts-base'; import { Bert } from 'meteor/themeteorchef:bert'; -import randomHex from 'crypto-random-hex'; import Icon from '../../components/Icon/Icon'; import OAuthLoginButtons from '../../components/OAuthLoginButtons/OAuthLoginButtons'; import InputHint from '../../components/InputHint/InputHint'; @@ -96,6 +95,26 @@ class Signup extends React.Component { this.setState({ termsAccept }); } + onTelegramAuth() { + /* dynamic import sample + // https://github.com/meteor/meteor/issues/9038 + import('crypto-random-hex').then(({ default: randomHex }) => { + // console.log(randomHex); + const hex = randomHex(20); + window.open(`https://t.me/TodosContraElFuego_bot?start=${hex}`); + }); */ + Meteor.call('auth.getHash', (error, response) => { + if (error) { + console.warn(error); + } else { + const bot = Meteor.isDevelopment ? 'rednodetest_bot' : 'TodosContraElFuego_bot'; + const url = `https://t.me/${bot}?start=${response}`; + // console.log(url); + window.open(url); + } + }); + } + render() { const { t, history } = this.props; return (
@@ -103,12 +122,12 @@ class Signup extends React.Component {

{t('Registrarse')}

- { false && // disabled + { Meteor.settings.public.telegramAuth && diff --git a/test/encode.test.js b/test/encode.test.js index 236e5b1..8d30306 100644 --- a/test/encode.test.js +++ b/test/encode.test.js @@ -5,6 +5,7 @@ import { chai } from 'meteor/practicalmeteor:chai'; import ActiveFiresCollection from '/imports/api/ActiveFires/ActiveFires'; import urlEnc from '/imports/modules/url-encode'; +import { Accounts } from 'meteor/accounts-base'; describe('url encoding', () => { it('should encrypt and dcrypt basic objects', async () => { @@ -73,13 +74,17 @@ describe('url encoding', () => { it('should decrypt node-red enc objects', async () => { const sealed = 'Fe26.2**750f04110aef0f20d1093aa3f2a54dac3d7d5cc86e864e2c7d71b0ead88bc5b9*rX653dosl4BiM2L4fmZiiA*xuKj8XojCaS38R6X1EXkXBWjOidpnB0EVgGbdod_4vACHSl1w61hgXkU6SOq8HdURakJKCyIqWrsrdGt6DI6bl1xgfZ4ejlRMV_Keu0-WV0BVqrxbw0NlMN9KPexRDsy15yZou4ExU1yE36PBsfZIYysUrIsXwiBz3D5KvDaW0BnAXZ4sUR5Kyvk8g75QQmBth_LVHaEWE_OMarQUXDvFZ33R2_vM_i89QSj-wzwG5v-lbYrimE_5SMhEngRJFjihtQ_LfQlkH0wrpe5SUIdNL-DzsRxswvY7RIuMKHVLWSy8So66PxCuVJKa-DGvclX7tj7NO9RgNidfqP8U0izTpoV7dJB44Bwi4NOwLKGwNO9NG7jt-KGb2P6FeLTJYq8Mt0qqNsYXWUMpuhgXlKc2SuKT0avh-kYdovFO3YztGg5dz_Asu5hGZtkzRG6oGrvZxU8j7VDDNSQLLHo67Skmqg5_0e6Bp0gqpz13bmCSVvM_IpOkJLIgkRkGAvFoPy-woBqWiBU3NICo0z9X35WJ8j2xFY5niidGPXkP_uo5JbpGkmLH1tL06UUqjXZ5GS7gVhi8ii0vZt5zgaM4z0g4Q**96aa408156b0952f0d90e7c6d3960c06595543ffcfd642274138631b51b03383*BRnu9clkqVeKeXfWVe8i_Dh6TgqstVgV9HafoiLKxks'; const unsealed = await urlEnc.decrypt(sealed); - console.log(unsealed); + // console.log(unsealed); }); - it('should encrypt and dcrypt hashes', async () => { - const obj = '7e01bd8d25e8d1a7459a82d8dd8348594484641f'; + it('should encrypt and dcrypt Accounts hashes', async () => { + const token = Accounts._generateStampedLoginToken(); + const obj = Accounts._hashStampedToken(token); + console.log(obj); const sealed = await urlEnc.encrypt(obj); const unsealed = await urlEnc.decrypt(sealed); + const w = unsealed.when; + unsealed.when = new Date(w); chai.expect(unsealed).to.deep.equal(obj); }); });