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