logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

oauth.js (4129B)


  1. import { reduce } from 'lodash'
  2. const REDIRECT_URI = `${window.location.origin}/oauth-callback`
  3. export const getOrCreateApp = ({ clientId, clientSecret, instance, commit }) => {
  4. if (clientId && clientSecret) {
  5. return Promise.resolve({ clientId, clientSecret })
  6. }
  7. const url = `${instance}/api/v1/apps`
  8. const form = new window.FormData()
  9. form.append('client_name', 'PleromaFE')
  10. form.append('website', 'https://pleroma.social')
  11. form.append('redirect_uris', REDIRECT_URI)
  12. form.append('scopes', 'read write follow push admin')
  13. return window.fetch(url, {
  14. method: 'POST',
  15. body: form
  16. })
  17. .then((data) => data.json())
  18. .then((app) => ({ clientId: app.client_id, clientSecret: app.client_secret }))
  19. .then((app) => commit('setClientData', app) || app)
  20. }
  21. const login = ({ instance, clientId }) => {
  22. const data = {
  23. response_type: 'code',
  24. client_id: clientId,
  25. redirect_uri: REDIRECT_URI,
  26. scope: 'read write follow push admin'
  27. }
  28. const dataString = reduce(data, (acc, v, k) => {
  29. const encoded = `${k}=${encodeURIComponent(v)}`
  30. if (!acc) {
  31. return encoded
  32. } else {
  33. return `${acc}&${encoded}`
  34. }
  35. }, false)
  36. // Do the redirect...
  37. const url = `${instance}/oauth/authorize?${dataString}`
  38. window.location.href = url
  39. }
  40. const getTokenWithCredentials = ({ clientId, clientSecret, instance, username, password }) => {
  41. const url = `${instance}/oauth/token`
  42. const form = new window.FormData()
  43. form.append('client_id', clientId)
  44. form.append('client_secret', clientSecret)
  45. form.append('grant_type', 'password')
  46. form.append('username', username)
  47. form.append('password', password)
  48. return window.fetch(url, {
  49. method: 'POST',
  50. body: form
  51. }).then((data) => data.json())
  52. }
  53. const getToken = ({ clientId, clientSecret, instance, code }) => {
  54. const url = `${instance}/oauth/token`
  55. const form = new window.FormData()
  56. form.append('client_id', clientId)
  57. form.append('client_secret', clientSecret)
  58. form.append('grant_type', 'authorization_code')
  59. form.append('code', code)
  60. form.append('redirect_uri', `${window.location.origin}/oauth-callback`)
  61. return window.fetch(url, {
  62. method: 'POST',
  63. body: form
  64. })
  65. .then((data) => data.json())
  66. }
  67. export const getClientToken = ({ clientId, clientSecret, instance }) => {
  68. const url = `${instance}/oauth/token`
  69. const form = new window.FormData()
  70. form.append('client_id', clientId)
  71. form.append('client_secret', clientSecret)
  72. form.append('grant_type', 'client_credentials')
  73. form.append('redirect_uri', `${window.location.origin}/oauth-callback`)
  74. return window.fetch(url, {
  75. method: 'POST',
  76. body: form
  77. }).then((data) => data.json())
  78. }
  79. const verifyOTPCode = ({ app, instance, mfaToken, code }) => {
  80. const url = `${instance}/oauth/mfa/challenge`
  81. const form = new window.FormData()
  82. form.append('client_id', app.client_id)
  83. form.append('client_secret', app.client_secret)
  84. form.append('mfa_token', mfaToken)
  85. form.append('code', code)
  86. form.append('challenge_type', 'totp')
  87. return window.fetch(url, {
  88. method: 'POST',
  89. body: form
  90. }).then((data) => data.json())
  91. }
  92. const verifyRecoveryCode = ({ app, instance, mfaToken, code }) => {
  93. const url = `${instance}/oauth/mfa/challenge`
  94. const form = new window.FormData()
  95. form.append('client_id', app.client_id)
  96. form.append('client_secret', app.client_secret)
  97. form.append('mfa_token', mfaToken)
  98. form.append('code', code)
  99. form.append('challenge_type', 'recovery')
  100. return window.fetch(url, {
  101. method: 'POST',
  102. body: form
  103. }).then((data) => data.json())
  104. }
  105. const revokeToken = ({ app, instance, token }) => {
  106. const url = `${instance}/oauth/revoke`
  107. const form = new window.FormData()
  108. form.append('client_id', app.clientId)
  109. form.append('client_secret', app.clientSecret)
  110. form.append('token', token)
  111. return window.fetch(url, {
  112. method: 'POST',
  113. body: form
  114. }).then((data) => data.json())
  115. }
  116. const oauth = {
  117. login,
  118. getToken,
  119. getTokenWithCredentials,
  120. getOrCreateApp,
  121. verifyOTPCode,
  122. verifyRecoveryCode,
  123. revokeToken
  124. }
  125. export default oauth