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/services/new_api/oauth.js | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/services/new_api/oauth.js (limited to 'src/services/new_api/oauth.js') diff --git a/src/services/new_api/oauth.js b/src/services/new_api/oauth.js new file mode 100644 index 00000000..becee3d0 --- /dev/null +++ b/src/services/new_api/oauth.js @@ -0,0 +1,64 @@ +import {reduce} from 'lodash' + +const getOrCreateApp = ({oauth, instance}) => { + const url = `${instance}/api/v1/apps` + const form = new window.FormData() + + form.append('client_name', `PleromaFE_${Math.random()}`) + form.append('redirect_uris', `${window.location.origin}/oauth-callback`) + form.append('scopes', 'read write follow') + + return window.fetch(url, { + method: 'POST', + body: form + }).then((data) => data.json()) +} +const login = (args) => { + getOrCreateApp(args).then((app) => { + args.commit('setClientData', app) + + const data = { + response_type: 'code', + client_id: app.client_id, + redirect_uri: app.redirect_uri, + scope: 'read write follow' + } + + const dataString = reduce(data, (acc, v, k) => { + const encoded = `${k}=${encodeURIComponent(v)}` + if (!acc) { + return encoded + } else { + return `${acc}&${encoded}` + } + }, false) + + // Do the redirect... + const url = `${args.instance}/oauth/authorize?${dataString}` + + window.location.href = url + }) +} + +const getToken = ({app, instance, code}) => { + 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', 'authorization_code') + form.append('code', code) + form.append('redirect_uri', `${window.location.origin}/oauth-callback`) + + return window.fetch(url, { + method: 'POST', + body: form + }).then((data) => data.json()) +} + +const oauth = { + login, + getToken +} + +export default oauth -- 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/services/new_api/oauth.js') 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