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