login.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import * as request from 'supertest'
  2. import { ServerInfo } from '../server/servers'
  3. import { getClient } from '../server/clients'
  4. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  5. type Client = { id: string, secret: string }
  6. type User = { username: string, password: string }
  7. type Server = { url: string, client: Client, user: User }
  8. function login (url: string, client: Client, user: User, expectedStatus = HttpStatusCode.OK_200) {
  9. const path = '/api/v1/users/token'
  10. const body = {
  11. client_id: client.id,
  12. client_secret: client.secret,
  13. username: user.username,
  14. password: user.password,
  15. response_type: 'code',
  16. grant_type: 'password',
  17. scope: 'upload'
  18. }
  19. return request(url)
  20. .post(path)
  21. .type('form')
  22. .send(body)
  23. .expect(expectedStatus)
  24. }
  25. function logout (url: string, token: string, expectedStatus = HttpStatusCode.OK_200) {
  26. const path = '/api/v1/users/revoke-token'
  27. return request(url)
  28. .post(path)
  29. .set('Authorization', 'Bearer ' + token)
  30. .type('form')
  31. .expect(expectedStatus)
  32. }
  33. async function serverLogin (server: Server) {
  34. const res = await login(server.url, server.client, server.user, HttpStatusCode.OK_200)
  35. return res.body.access_token as string
  36. }
  37. function refreshToken (server: ServerInfo, refreshToken: string, expectedStatus = HttpStatusCode.OK_200) {
  38. const path = '/api/v1/users/token'
  39. const body = {
  40. client_id: server.client.id,
  41. client_secret: server.client.secret,
  42. refresh_token: refreshToken,
  43. response_type: 'code',
  44. grant_type: 'refresh_token'
  45. }
  46. return request(server.url)
  47. .post(path)
  48. .type('form')
  49. .send(body)
  50. .expect(expectedStatus)
  51. }
  52. async function userLogin (server: Server, user: User, expectedStatus = HttpStatusCode.OK_200) {
  53. const res = await login(server.url, server.client, user, expectedStatus)
  54. return res.body.access_token as string
  55. }
  56. async function getAccessToken (url: string, username: string, password: string) {
  57. const resClient = await getClient(url)
  58. const client = {
  59. id: resClient.body.client_id,
  60. secret: resClient.body.client_secret
  61. }
  62. const user = { username, password }
  63. try {
  64. const res = await login(url, client, user)
  65. return res.body.access_token
  66. } catch (err) {
  67. throw new Error('Cannot authenticate. Please check your username/password.')
  68. }
  69. }
  70. function setAccessTokensToServers (servers: ServerInfo[]) {
  71. const tasks: Promise<any>[] = []
  72. for (const server of servers) {
  73. const p = serverLogin(server).then(t => { server.accessToken = t })
  74. tasks.push(p)
  75. }
  76. return Promise.all(tasks)
  77. }
  78. function loginUsingExternalToken (server: Server, username: string, externalAuthToken: string, expectedStatus = HttpStatusCode.OK_200) {
  79. const path = '/api/v1/users/token'
  80. const body = {
  81. client_id: server.client.id,
  82. client_secret: server.client.secret,
  83. username: username,
  84. response_type: 'code',
  85. grant_type: 'password',
  86. scope: 'upload',
  87. externalAuthToken
  88. }
  89. return request(server.url)
  90. .post(path)
  91. .type('form')
  92. .send(body)
  93. .expect(expectedStatus)
  94. }
  95. // ---------------------------------------------------------------------------
  96. export {
  97. login,
  98. logout,
  99. serverLogin,
  100. refreshToken,
  101. userLogin,
  102. getAccessToken,
  103. setAccessTokensToServers,
  104. Server,
  105. Client,
  106. User,
  107. loginUsingExternalToken
  108. }