From 9af204b293a9c1b15e472423a4badff505fd662a Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 26 Oct 2018 15:16:23 +0200 Subject: Move login to oauth. --- src/components/login_form/login_form.js | 8 ++++++++ src/components/login_form/login_form.vue | 18 ++---------------- src/components/oauth_callback/oauth_callback.js | 20 ++++++++++++++++++++ src/components/oauth_callback/oauth_callback.vue | 5 +++++ 4 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 src/components/oauth_callback/oauth_callback.js create mode 100644 src/components/oauth_callback/oauth_callback.vue (limited to 'src/components') diff --git a/src/components/login_form/login_form.js b/src/components/login_form/login_form.js index 4405fb92..b1899666 100644 --- a/src/components/login_form/login_form.js +++ b/src/components/login_form/login_form.js @@ -1,3 +1,4 @@ +import oauthApi from "../../services/new_api/oauth.js"; const LoginForm = { data: () => ({ user: {}, @@ -8,6 +9,13 @@ const LoginForm = { registrationOpen () { return this.$store.state.instance.registrationOpen } }, methods: { + oAuthLogin () { + oauthApi.login({ + oauth: this.$store.state.oauth, + instance: this.$store.state.instance.server, + commit: this.$store.commit + }); + }, submit () { this.$store.dispatch('loginUser', this.user).then( () => {}, diff --git a/src/components/login_form/login_form.vue b/src/components/login_form/login_form.vue index b7fed48a..aaaca777 100644 --- a/src/components/login_form/login_form.vue +++ b/src/components/login_form/login_form.vue @@ -5,23 +5,9 @@ {{$t('login.login')}}
-
diff --git a/src/components/oauth_callback/oauth_callback.js b/src/components/oauth_callback/oauth_callback.js new file mode 100644 index 00000000..60a15412 --- /dev/null +++ b/src/components/oauth_callback/oauth_callback.js @@ -0,0 +1,20 @@ +import oauth from '../../services/new_api/oauth.js' + +const oac = { + props: ['code'], + mounted () { + if (this.code) { + oauth.getToken({ + app: this.$store.state.oauth, + instance: this.$store.state.instance.server, + code: this.code + }).then((result) => { + this.$store.commit("setToken", result.access_token) + this.$store.dispatch("loginUser", result.access_token) + this.$router.push('/main/friends') + }) + } + } +} + +export default oac diff --git a/src/components/oauth_callback/oauth_callback.vue b/src/components/oauth_callback/oauth_callback.vue new file mode 100644 index 00000000..9c806916 --- /dev/null +++ b/src/components/oauth_callback/oauth_callback.vue @@ -0,0 +1,5 @@ + + + -- cgit v1.2.3-70-g09d2 From 60b3e4f40f122f2dfea3f4fc0360673169cd6f99 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 26 Oct 2018 15:20:39 +0200 Subject: Fix linting. --- src/components/login_form/login_form.js | 4 ++-- src/components/oauth_callback/oauth_callback.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/components') diff --git a/src/components/login_form/login_form.js b/src/components/login_form/login_form.js index b1899666..ffdd5504 100644 --- a/src/components/login_form/login_form.js +++ b/src/components/login_form/login_form.js @@ -1,4 +1,4 @@ -import oauthApi from "../../services/new_api/oauth.js"; +import oauthApi from '../../services/new_api/oauth.js' const LoginForm = { data: () => ({ user: {}, @@ -14,7 +14,7 @@ const LoginForm = { oauth: this.$store.state.oauth, instance: this.$store.state.instance.server, commit: this.$store.commit - }); + }) }, submit () { this.$store.dispatch('loginUser', this.user).then( diff --git a/src/components/oauth_callback/oauth_callback.js b/src/components/oauth_callback/oauth_callback.js index 60a15412..7a5132ad 100644 --- a/src/components/oauth_callback/oauth_callback.js +++ b/src/components/oauth_callback/oauth_callback.js @@ -9,8 +9,8 @@ const oac = { instance: this.$store.state.instance.server, code: this.code }).then((result) => { - this.$store.commit("setToken", result.access_token) - this.$store.dispatch("loginUser", result.access_token) + this.$store.commit('setToken', result.access_token) + this.$store.dispatch('loginUser', result.access_token) this.$router.push('/main/friends') }) } -- cgit v1.2.3-70-g09d2 From 4d9680e79734c38cbc0343053523fbdccb002f6e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 6 Nov 2018 21:48:05 +0100 Subject: Re-activate registration, use oauth password flow to fetch token. --- src/components/login_form/login_form.vue | 5 ++++- src/components/registration/registration.js | 21 ++++++++++++++++++--- src/services/new_api/oauth.js | 20 +++++++++++++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) (limited to 'src/components') diff --git a/src/components/login_form/login_form.vue b/src/components/login_form/login_form.vue index aaaca777..db389716 100644 --- a/src/components/login_form/login_form.vue +++ b/src/components/login_form/login_form.vue @@ -7,7 +7,10 @@
diff --git a/src/components/registration/registration.js b/src/components/registration/registration.js index 8f59878d..cdcc33c9 100644 --- a/src/components/registration/registration.js +++ b/src/components/registration/registration.js @@ -1,3 +1,5 @@ +import oauthApi from '../../services/new_api/oauth.js' + const registration = { data: () => ({ user: {}, @@ -25,9 +27,22 @@ const registration = { this.$store.state.api.backendInteractor.register(this.user).then( (response) => { if (response.ok) { - this.$store.dispatch('loginUser', this.user) - this.$router.push('/main/all') - this.registering = false + const data = { + oauth: this.$store.state.oauth, + instance: this.$store.state.instance.server + } + oauthApi.getOrCreateApp(data).then((app) => { + oauthApi.getTokenWithCredentials( + {app, + instance: data.instance, + username: this.user.username, + password: this.user.password}) + .then((result) => { + this.$store.commit('setToken', result.access_token) + this.$store.dispatch('loginUser', result.access_token) + this.$router.push('/main/friends') + }) + }) } else { this.registering = false response.json().then((data) => { diff --git a/src/services/new_api/oauth.js b/src/services/new_api/oauth.js index becee3d0..9e656507 100644 --- a/src/services/new_api/oauth.js +++ b/src/services/new_api/oauth.js @@ -40,6 +40,22 @@ const login = (args) => { }) } +const getTokenWithCredentials = ({app, instance, username, password}) => { + const url = `${instance}/oauth/token` + const form = new window.FormData() + + form.append('client_id', app.client_id) + form.append('client_secret', app.client_secret) + form.append('grant_type', 'password') + form.append('username', username) + form.append('password', password) + + return window.fetch(url, { + method: 'POST', + body: form + }).then((data) => data.json()) +} + const getToken = ({app, instance, code}) => { const url = `${instance}/oauth/token` const form = new window.FormData() @@ -58,7 +74,9 @@ const getToken = ({app, instance, code}) => { const oauth = { login, - getToken + getToken, + getTokenWithCredentials, + getOrCreateApp } export default oauth -- cgit v1.2.3-70-g09d2 From bcbaf5d7ee970a1bd35e05ad12f4dfbb00f4282e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 6 Nov 2018 21:51:22 +0100 Subject: Linting. --- src/components/registration/registration.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/components') diff --git a/src/components/registration/registration.js b/src/components/registration/registration.js index cdcc33c9..f7f8a720 100644 --- a/src/components/registration/registration.js +++ b/src/components/registration/registration.js @@ -33,10 +33,11 @@ const registration = { } oauthApi.getOrCreateApp(data).then((app) => { oauthApi.getTokenWithCredentials( - {app, - instance: data.instance, - username: this.user.username, - password: this.user.password}) + { + app, + instance: data.instance, + username: this.user.username, + password: this.user.password}) .then((result) => { this.$store.commit('setToken', result.access_token) this.$store.dispatch('loginUser', result.access_token) -- cgit v1.2.3-70-g09d2 From 50264410f541c8f9f3416bd0027fc199cb96e424 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 7 Nov 2018 16:56:12 +0100 Subject: Add login form back in. --- README.md | 6 ++++++ src/boot/after_store.js | 2 ++ src/components/login_form/login_form.js | 26 ++++++++++++++++++-------- src/components/login_form/login_form.vue | 19 ++++++++++++++++++- src/modules/instance.js | 1 + static/config.json | 3 ++- 6 files changed, 47 insertions(+), 10 deletions(-) (limited to 'src/components') diff --git a/README.md b/README.md index 5a3e2a4b..b6e5a586 100644 --- a/README.md +++ b/README.md @@ -32,3 +32,9 @@ npm run unit # Configuration Edit config.json for configuration. scopeOptionsEnabled gives you input fields for CWs and the scope settings. + +## Options + +### Login methods + +```loginMethod``` can be set to either ```password``` (the default) or ```token```, which will use the full oauth redirection flow, which is useful for SSO situations. diff --git a/src/boot/after_store.js b/src/boot/after_store.js index 65d1ea02..6b8aef7f 100644 --- a/src/boot/after_store.js +++ b/src/boot/after_store.js @@ -53,6 +53,7 @@ const afterStoreSetup = ({store, i18n}) => { var scopeOptionsEnabled = (config.scopeOptionsEnabled) var formattingOptionsEnabled = (config.formattingOptionsEnabled) var collapseMessageWithSubject = (config.collapseMessageWithSubject) + var loginMethod = (config.loginMethod) store.dispatch('setInstanceOption', { name: 'theme', value: theme }) store.dispatch('setInstanceOption', { name: 'background', value: background }) @@ -67,6 +68,7 @@ const afterStoreSetup = ({store, i18n}) => { store.dispatch('setInstanceOption', { name: 'scopeOptionsEnabled', value: scopeOptionsEnabled }) store.dispatch('setInstanceOption', { name: 'formattingOptionsEnabled', value: formattingOptionsEnabled }) store.dispatch('setInstanceOption', { name: 'collapseMessageWithSubject', value: collapseMessageWithSubject }) + store.dispatch('setInstanceOption', { name: 'loginMethod', value: loginMethod }) if (chatDisabled) { store.dispatch('disableChat') } diff --git a/src/components/login_form/login_form.js b/src/components/login_form/login_form.js index ffdd5504..49868aed 100644 --- a/src/components/login_form/login_form.js +++ b/src/components/login_form/login_form.js @@ -5,6 +5,7 @@ const LoginForm = { authError: false }), computed: { + loginMethod () { return this.$store.state.instance.loginMethod }, loggingIn () { return this.$store.state.users.loggingIn }, registrationOpen () { return this.$store.state.instance.registrationOpen } }, @@ -17,14 +18,23 @@ const LoginForm = { }) }, submit () { - this.$store.dispatch('loginUser', this.user).then( - () => {}, - (error) => { - this.authError = error - this.user.username = '' - this.user.password = '' - } - ) + const data = { + oauth: this.$store.state.oauth, + instance: this.$store.state.instance.server + } + oauthApi.getOrCreateApp(data).then((app) => { + oauthApi.getTokenWithCredentials( + { + app, + instance: data.instance, + username: this.user.username, + password: this.user.password}) + .then((result) => { + this.$store.commit('setToken', result.access_token) + this.$store.dispatch('loginUser', result.access_token) + this.$router.push('/main/friends') + }) + }) } } } diff --git a/src/components/login_form/login_form.vue b/src/components/login_form/login_form.vue index db389716..8a5174d7 100644 --- a/src/components/login_form/login_form.vue +++ b/src/components/login_form/login_form.vue @@ -5,7 +5,24 @@ {{$t('login.login')}}
- + +