2
1

login-command.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { HttpStatusCode, PeerTubeProblemDocument } from '@shared/models'
  2. import { unwrapBody } from '../requests'
  3. import { AbstractCommand, OverrideCommandOptions } from '../shared'
  4. export class LoginCommand extends AbstractCommand {
  5. login (options: OverrideCommandOptions & {
  6. client?: { id?: string, secret?: string }
  7. user?: { username: string, password?: string }
  8. } = {}) {
  9. const { client = this.server.store.client, user = this.server.store.user } = options
  10. const path = '/api/v1/users/token'
  11. const body = {
  12. client_id: client.id,
  13. client_secret: client.secret,
  14. username: user.username,
  15. password: user.password ?? 'password',
  16. response_type: 'code',
  17. grant_type: 'password',
  18. scope: 'upload'
  19. }
  20. return unwrapBody<{ access_token: string, refresh_token: string } & PeerTubeProblemDocument>(this.postBodyRequest({
  21. ...options,
  22. path,
  23. requestType: 'form',
  24. fields: body,
  25. implicitToken: false,
  26. defaultExpectedStatus: HttpStatusCode.OK_200
  27. }))
  28. }
  29. getAccessToken (arg1?: { username: string, password?: string }): Promise<string>
  30. getAccessToken (arg1: string, password?: string): Promise<string>
  31. async getAccessToken (arg1?: { username: string, password?: string } | string, password?: string) {
  32. let user: { username: string, password?: string }
  33. if (!arg1) user = this.server.store.user
  34. else if (typeof arg1 === 'object') user = arg1
  35. else user = { username: arg1, password }
  36. try {
  37. const body = await this.login({ user })
  38. return body.access_token
  39. } catch (err) {
  40. throw new Error(`Cannot authenticate. Please check your username/password. (${err})`)
  41. }
  42. }
  43. loginUsingExternalToken (options: OverrideCommandOptions & {
  44. username: string
  45. externalAuthToken: string
  46. }) {
  47. const { username, externalAuthToken } = options
  48. const path = '/api/v1/users/token'
  49. const body = {
  50. client_id: this.server.store.client.id,
  51. client_secret: this.server.store.client.secret,
  52. username,
  53. response_type: 'code',
  54. grant_type: 'password',
  55. scope: 'upload',
  56. externalAuthToken
  57. }
  58. return this.postBodyRequest({
  59. ...options,
  60. path,
  61. requestType: 'form',
  62. fields: body,
  63. implicitToken: false,
  64. defaultExpectedStatus: HttpStatusCode.OK_200
  65. })
  66. }
  67. logout (options: OverrideCommandOptions & {
  68. token: string
  69. }) {
  70. const path = '/api/v1/users/revoke-token'
  71. return unwrapBody<{ redirectUrl: string }>(this.postBodyRequest({
  72. ...options,
  73. path,
  74. requestType: 'form',
  75. implicitToken: false,
  76. defaultExpectedStatus: HttpStatusCode.OK_200
  77. }))
  78. }
  79. refreshToken (options: OverrideCommandOptions & {
  80. refreshToken: string
  81. }) {
  82. const path = '/api/v1/users/token'
  83. const body = {
  84. client_id: this.server.store.client.id,
  85. client_secret: this.server.store.client.secret,
  86. refresh_token: options.refreshToken,
  87. response_type: 'code',
  88. grant_type: 'refresh_token'
  89. }
  90. return this.postBodyRequest({
  91. ...options,
  92. path,
  93. requestType: 'form',
  94. fields: body,
  95. implicitToken: false,
  96. defaultExpectedStatus: HttpStatusCode.OK_200
  97. })
  98. }
  99. getClient (options: OverrideCommandOptions = {}) {
  100. const path = '/api/v1/oauth-clients/local'
  101. return this.getRequestBody<{ client_id: string, client_secret: string }>({
  102. ...options,
  103. path,
  104. host: this.server.host,
  105. implicitToken: false,
  106. defaultExpectedStatus: HttpStatusCode.OK_200
  107. })
  108. }
  109. }