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