diff options
| author | Henry Jameson <me@hjkos.com> | 2018-11-26 05:21:58 +0300 |
|---|---|---|
| committer | Henry Jameson <me@hjkos.com> | 2018-11-26 05:21:58 +0300 |
| commit | a806d43f05ddded69a00156bc31fe33806426ecb (patch) | |
| tree | afa9a3f43043bbce92739fb48d048605e0c45c2b /src/services/new_api/oauth.js | |
| parent | 08838774e41b9beba8f884da15ab1314eddf28f8 (diff) | |
| parent | 91272dc5558e1326dac872f927dc8da7f9109cd0 (diff) | |
Merge remote-tracking branch 'upstream/develop' into feature/theming2
* upstream/develop: (60 commits)
whoops
whoops
DM timeline: stream new statuses
update-japanese-translation
Add actual user search.
incorporate most translation changes from MR 368
update french translation
Always show dm panel.
Add direct message tab.
api service url
remove deploy stage
remove deploy stage
updated and completed German translation
On logout switch to public timeline.
minor modification of Chinese translation
update Chinese translation
Add Chinese language
Fix posting.
Put oauth text into description.
Display OAuth login on login form button.
...
Diffstat (limited to 'src/services/new_api/oauth.js')
| -rw-r--r-- | src/services/new_api/oauth.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/services/new_api/oauth.js b/src/services/new_api/oauth.js new file mode 100644 index 00000000..9e656507 --- /dev/null +++ b/src/services/new_api/oauth.js @@ -0,0 +1,82 @@ +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 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() + + 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, + getTokenWithCredentials, + getOrCreateApp +} + +export default oauth |
