diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /shared/server-commands/users/login-command.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'shared/server-commands/users/login-command.ts')
-rw-r--r-- | shared/server-commands/users/login-command.ts | 159 |
1 files changed, 0 insertions, 159 deletions
diff --git a/shared/server-commands/users/login-command.ts b/shared/server-commands/users/login-command.ts deleted file mode 100644 index f2fc6d1c5..000000000 --- a/shared/server-commands/users/login-command.ts +++ /dev/null | |||
@@ -1,159 +0,0 @@ | |||
1 | import { HttpStatusCode, PeerTubeProblemDocument } from '@shared/models' | ||
2 | import { unwrapBody } from '../requests' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | type LoginOptions = OverrideCommandOptions & { | ||
6 | client?: { id?: string, secret?: string } | ||
7 | user?: { username: string, password?: string } | ||
8 | otpToken?: string | ||
9 | } | ||
10 | |||
11 | export class LoginCommand extends AbstractCommand { | ||
12 | |||
13 | async login (options: LoginOptions = {}) { | ||
14 | const res = await this._login(options) | ||
15 | |||
16 | return this.unwrapLoginBody(res.body) | ||
17 | } | ||
18 | |||
19 | async loginAndGetResponse (options: LoginOptions = {}) { | ||
20 | const res = await this._login(options) | ||
21 | |||
22 | return { | ||
23 | res, | ||
24 | body: this.unwrapLoginBody(res.body) | ||
25 | } | ||
26 | } | ||
27 | |||
28 | getAccessToken (arg1?: { username: string, password?: string }): Promise<string> | ||
29 | getAccessToken (arg1: string, password?: string): Promise<string> | ||
30 | async getAccessToken (arg1?: { username: string, password?: string } | string, password?: string) { | ||
31 | let user: { username: string, password?: string } | ||
32 | |||
33 | if (!arg1) user = this.server.store.user | ||
34 | else if (typeof arg1 === 'object') user = arg1 | ||
35 | else user = { username: arg1, password } | ||
36 | |||
37 | try { | ||
38 | const body = await this.login({ user }) | ||
39 | |||
40 | return body.access_token | ||
41 | } catch (err) { | ||
42 | throw new Error(`Cannot authenticate. Please check your username/password. (${err})`) | ||
43 | } | ||
44 | } | ||
45 | |||
46 | loginUsingExternalToken (options: OverrideCommandOptions & { | ||
47 | username: string | ||
48 | externalAuthToken: string | ||
49 | }) { | ||
50 | const { username, externalAuthToken } = options | ||
51 | const path = '/api/v1/users/token' | ||
52 | |||
53 | const body = { | ||
54 | client_id: this.server.store.client.id, | ||
55 | client_secret: this.server.store.client.secret, | ||
56 | username, | ||
57 | response_type: 'code', | ||
58 | grant_type: 'password', | ||
59 | scope: 'upload', | ||
60 | externalAuthToken | ||
61 | } | ||
62 | |||
63 | return this.postBodyRequest({ | ||
64 | ...options, | ||
65 | |||
66 | path, | ||
67 | requestType: 'form', | ||
68 | fields: body, | ||
69 | implicitToken: false, | ||
70 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
71 | }) | ||
72 | } | ||
73 | |||
74 | logout (options: OverrideCommandOptions & { | ||
75 | token: string | ||
76 | }) { | ||
77 | const path = '/api/v1/users/revoke-token' | ||
78 | |||
79 | return unwrapBody<{ redirectUrl: string }>(this.postBodyRequest({ | ||
80 | ...options, | ||
81 | |||
82 | path, | ||
83 | requestType: 'form', | ||
84 | implicitToken: false, | ||
85 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
86 | })) | ||
87 | } | ||
88 | |||
89 | refreshToken (options: OverrideCommandOptions & { | ||
90 | refreshToken: string | ||
91 | }) { | ||
92 | const path = '/api/v1/users/token' | ||
93 | |||
94 | const body = { | ||
95 | client_id: this.server.store.client.id, | ||
96 | client_secret: this.server.store.client.secret, | ||
97 | refresh_token: options.refreshToken, | ||
98 | response_type: 'code', | ||
99 | grant_type: 'refresh_token' | ||
100 | } | ||
101 | |||
102 | return this.postBodyRequest({ | ||
103 | ...options, | ||
104 | |||
105 | path, | ||
106 | requestType: 'form', | ||
107 | fields: body, | ||
108 | implicitToken: false, | ||
109 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
110 | }) | ||
111 | } | ||
112 | |||
113 | getClient (options: OverrideCommandOptions = {}) { | ||
114 | const path = '/api/v1/oauth-clients/local' | ||
115 | |||
116 | return this.getRequestBody<{ client_id: string, client_secret: string }>({ | ||
117 | ...options, | ||
118 | |||
119 | path, | ||
120 | host: this.server.host, | ||
121 | implicitToken: false, | ||
122 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
123 | }) | ||
124 | } | ||
125 | |||
126 | private _login (options: LoginOptions) { | ||
127 | const { client = this.server.store.client, user = this.server.store.user, otpToken } = options | ||
128 | const path = '/api/v1/users/token' | ||
129 | |||
130 | const body = { | ||
131 | client_id: client.id, | ||
132 | client_secret: client.secret, | ||
133 | username: user.username, | ||
134 | password: user.password ?? 'password', | ||
135 | response_type: 'code', | ||
136 | grant_type: 'password', | ||
137 | scope: 'upload' | ||
138 | } | ||
139 | |||
140 | const headers = otpToken | ||
141 | ? { 'x-peertube-otp': otpToken } | ||
142 | : {} | ||
143 | |||
144 | return this.postBodyRequest({ | ||
145 | ...options, | ||
146 | |||
147 | path, | ||
148 | headers, | ||
149 | requestType: 'form', | ||
150 | fields: body, | ||
151 | implicitToken: false, | ||
152 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
153 | }) | ||
154 | } | ||
155 | |||
156 | private unwrapLoginBody (body: any) { | ||
157 | return body as { access_token: string, refresh_token: string } & PeerTubeProblemDocument | ||
158 | } | ||
159 | } | ||