aboutsummaryrefslogtreecommitdiff
path: root/src/services
diff options
context:
space:
mode:
Diffstat (limited to 'src/services')
-rw-r--r--src/services/api/api.service.js16
-rw-r--r--src/services/new_api/oauth.js64
2 files changed, 67 insertions, 13 deletions
diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js
index ab746918..499e9b16 100644
--- a/src/services/api/api.service.js
+++ b/src/services/api/api.service.js
@@ -52,16 +52,6 @@ let fetch = (url, options) => {
return oldfetch(fullUrl, options)
}
-// from https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
-let utoa = (str) => {
- // first we use encodeURIComponent to get percent-encoded UTF-8,
- // then we convert the percent encodings into raw bytes which
- // can be fed into btoa.
- return btoa(encodeURIComponent(str)
- .replace(/%([0-9A-F]{2})/g,
- (match, p1) => { return String.fromCharCode('0x' + p1) }))
-}
-
// Params
// cropH
// cropW
@@ -175,9 +165,9 @@ const register = (params) => {
})
}
-const authHeaders = (user) => {
- if (user && user.username && user.password) {
- return { 'Authorization': `Basic ${utoa(`${user.username}:${user.password}`)}` }
+const authHeaders = (accessToken) => {
+ if (accessToken) {
+ return { 'Authorization': `Bearer ${accessToken}` }
} else {
return { }
}
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