1 import { HttpStatusCode, PeerTubeProblemDocument } from '@shared/models'
2 import { unwrapBody } from '../requests'
3 import { AbstractCommand, OverrideCommandOptions } from '../shared'
5 export class LoginCommand extends AbstractCommand {
7 login (options: OverrideCommandOptions & {
8 client?: { id?: string, secret?: string }
9 user?: { username: string, password?: string }
11 const { client = this.server.store.client, user = this.server.store.user } = options
12 const path = '/api/v1/users/token'
16 client_secret: client.secret,
17 username: user.username,
18 password: user.password ?? 'password',
19 response_type: 'code',
20 grant_type: 'password',
24 return unwrapBody<{ access_token: string, refresh_token: string } & PeerTubeProblemDocument>(this.postBodyRequest({
31 defaultExpectedStatus: HttpStatusCode.OK_200
35 getAccessToken (arg1?: { username: string, password?: string }): Promise<string>
36 getAccessToken (arg1: string, password?: string): Promise<string>
37 async getAccessToken (arg1?: { username: string, password?: string } | string, password?: string) {
38 let user: { username: string, password?: string }
40 if (!arg1) user = this.server.store.user
41 else if (typeof arg1 === 'object') user = arg1
42 else user = { username: arg1, password }
45 const body = await this.login({ user })
47 return body.access_token
49 throw new Error(`Cannot authenticate. Please check your username/password. (${err})`)
53 loginUsingExternalToken (options: OverrideCommandOptions & {
55 externalAuthToken: string
57 const { username, externalAuthToken } = options
58 const path = '/api/v1/users/token'
61 client_id: this.server.store.client.id,
62 client_secret: this.server.store.client.secret,
64 response_type: 'code',
65 grant_type: 'password',
70 return this.postBodyRequest({
77 defaultExpectedStatus: HttpStatusCode.OK_200
81 logout (options: OverrideCommandOptions & {
84 const path = '/api/v1/users/revoke-token'
86 return unwrapBody<{ redirectUrl: string }>(this.postBodyRequest({
92 defaultExpectedStatus: HttpStatusCode.OK_200
96 refreshToken (options: OverrideCommandOptions & {
99 const path = '/api/v1/users/token'
102 client_id: this.server.store.client.id,
103 client_secret: this.server.store.client.secret,
104 refresh_token: options.refreshToken,
105 response_type: 'code',
106 grant_type: 'refresh_token'
109 return this.postBodyRequest({
115 implicitToken: false,
116 defaultExpectedStatus: HttpStatusCode.OK_200
120 getClient (options: OverrideCommandOptions = {}) {
121 const path = '/api/v1/oauth-clients/local'
123 return this.getRequestBody<{ client_id: string, client_secret: string }>({
127 host: this.server.host,
128 implicitToken: false,
129 defaultExpectedStatus: HttpStatusCode.OK_200