X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=shared%2Fserver-commands%2Fusers%2Flogin-command.ts;h=f2fc6d1c514e6cc7407fc1a2d02ecf17e630cb64;hb=56f47830758ff8e92abcfcc5f35d474ab12fe215;hp=143f72a59c0e994bebfe4d4115257b26819afce7;hpb=c3edc5b074aa4bb1861ed0a94d3713808e87170f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/shared/server-commands/users/login-command.ts b/shared/server-commands/users/login-command.ts index 143f72a59..f2fc6d1c5 100644 --- a/shared/server-commands/users/login-command.ts +++ b/shared/server-commands/users/login-command.ts @@ -2,34 +2,27 @@ import { HttpStatusCode, PeerTubeProblemDocument } from '@shared/models' import { unwrapBody } from '../requests' import { AbstractCommand, OverrideCommandOptions } from '../shared' +type LoginOptions = OverrideCommandOptions & { + client?: { id?: string, secret?: string } + user?: { username: string, password?: string } + otpToken?: string +} + export class LoginCommand extends AbstractCommand { - login (options: OverrideCommandOptions & { - client?: { id?: string, secret?: string } - user?: { username: string, password?: string } - } = {}) { - const { client = this.server.store.client, user = this.server.store.user } = options - const path = '/api/v1/users/token' + async login (options: LoginOptions = {}) { + const res = await this._login(options) - const body = { - client_id: client.id, - client_secret: client.secret, - username: user.username, - password: user.password ?? 'password', - response_type: 'code', - grant_type: 'password', - scope: 'upload' - } + return this.unwrapLoginBody(res.body) + } - return unwrapBody<{ access_token: string, refresh_token: string } & PeerTubeProblemDocument>(this.postBodyRequest({ - ...options, + async loginAndGetResponse (options: LoginOptions = {}) { + const res = await this._login(options) - path, - requestType: 'form', - fields: body, - implicitToken: false, - defaultExpectedStatus: HttpStatusCode.OK_200 - })) + return { + res, + body: this.unwrapLoginBody(res.body) + } } getAccessToken (arg1?: { username: string, password?: string }): Promise @@ -60,7 +53,7 @@ export class LoginCommand extends AbstractCommand { const body = { client_id: this.server.store.client.id, client_secret: this.server.store.client.secret, - username: username, + username, response_type: 'code', grant_type: 'password', scope: 'upload', @@ -129,4 +122,38 @@ export class LoginCommand extends AbstractCommand { defaultExpectedStatus: HttpStatusCode.OK_200 }) } + + private _login (options: LoginOptions) { + const { client = this.server.store.client, user = this.server.store.user, otpToken } = options + const path = '/api/v1/users/token' + + const body = { + client_id: client.id, + client_secret: client.secret, + username: user.username, + password: user.password ?? 'password', + response_type: 'code', + grant_type: 'password', + scope: 'upload' + } + + const headers = otpToken + ? { 'x-peertube-otp': otpToken } + : {} + + return this.postBodyRequest({ + ...options, + + path, + headers, + requestType: 'form', + fields: body, + implicitToken: false, + defaultExpectedStatus: HttpStatusCode.OK_200 + }) + } + + private unwrapLoginBody (body: any) { + return body as { access_token: string, refresh_token: string } & PeerTubeProblemDocument + } }