logo

pleroma-fe

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

oauth.js (4143B)


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