diff options
author | Chocobozzz <me@florianbigard.com> | 2021-12-17 09:29:23 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-12-17 09:29:23 +0100 |
commit | bf54587a3e2ad9c2c186828f2a5682b91ee2cc00 (patch) | |
tree | 54b40aaf01bae210632473285c3c7571d51e4f89 /shared/extra-utils/users/login-command.ts | |
parent | 6b5f72beda96d8b7e4d6329c4001827334de27dd (diff) | |
download | PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.gz PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.tar.zst PeerTube-bf54587a3e2ad9c2c186828f2a5682b91ee2cc00.zip |
shared/ typescript types dir server-commands
Diffstat (limited to 'shared/extra-utils/users/login-command.ts')
-rw-r--r-- | shared/extra-utils/users/login-command.ts | 132 |
1 files changed, 0 insertions, 132 deletions
diff --git a/shared/extra-utils/users/login-command.ts b/shared/extra-utils/users/login-command.ts deleted file mode 100644 index 143f72a59..000000000 --- a/shared/extra-utils/users/login-command.ts +++ /dev/null | |||
@@ -1,132 +0,0 @@ | |||
1 | import { HttpStatusCode, PeerTubeProblemDocument } from '@shared/models' | ||
2 | import { unwrapBody } from '../requests' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | export class LoginCommand extends AbstractCommand { | ||
6 | |||
7 | login (options: OverrideCommandOptions & { | ||
8 | client?: { id?: string, secret?: string } | ||
9 | user?: { username: string, password?: string } | ||
10 | } = {}) { | ||
11 | const { client = this.server.store.client, user = this.server.store.user } = options | ||
12 | const path = '/api/v1/users/token' | ||
13 | |||
14 | const body = { | ||
15 | client_id: client.id, | ||
16 | client_secret: client.secret, | ||
17 | username: user.username, | ||
18 | password: user.password ?? 'password', | ||
19 | response_type: 'code', | ||
20 | grant_type: 'password', | ||
21 | scope: 'upload' | ||
22 | } | ||
23 | |||
24 | return unwrapBody<{ access_token: string, refresh_token: string } & PeerTubeProblemDocument>(this.postBodyRequest({ | ||
25 | ...options, | ||
26 | |||
27 | path, | ||
28 | requestType: 'form', | ||
29 | fields: body, | ||
30 | implicitToken: false, | ||
31 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
32 | })) | ||
33 | } | ||
34 | |||
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 } | ||
39 | |||
40 | if (!arg1) user = this.server.store.user | ||
41 | else if (typeof arg1 === 'object') user = arg1 | ||
42 | else user = { username: arg1, password } | ||
43 | |||
44 | try { | ||
45 | const body = await this.login({ user }) | ||
46 | |||
47 | return body.access_token | ||
48 | } catch (err) { | ||
49 | throw new Error(`Cannot authenticate. Please check your username/password. (${err})`) | ||
50 | } | ||
51 | } | ||
52 | |||
53 | loginUsingExternalToken (options: OverrideCommandOptions & { | ||
54 | username: string | ||
55 | externalAuthToken: string | ||
56 | }) { | ||
57 | const { username, externalAuthToken } = options | ||
58 | const path = '/api/v1/users/token' | ||
59 | |||
60 | const body = { | ||
61 | client_id: this.server.store.client.id, | ||
62 | client_secret: this.server.store.client.secret, | ||
63 | username: username, | ||
64 | response_type: 'code', | ||
65 | grant_type: 'password', | ||
66 | scope: 'upload', | ||
67 | externalAuthToken | ||
68 | } | ||
69 | |||
70 | return this.postBodyRequest({ | ||
71 | ...options, | ||
72 | |||
73 | path, | ||
74 | requestType: 'form', | ||
75 | fields: body, | ||
76 | implicitToken: false, | ||
77 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
78 | }) | ||
79 | } | ||
80 | |||
81 | logout (options: OverrideCommandOptions & { | ||
82 | token: string | ||
83 | }) { | ||
84 | const path = '/api/v1/users/revoke-token' | ||
85 | |||
86 | return unwrapBody<{ redirectUrl: string }>(this.postBodyRequest({ | ||
87 | ...options, | ||
88 | |||
89 | path, | ||
90 | requestType: 'form', | ||
91 | implicitToken: false, | ||
92 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
93 | })) | ||
94 | } | ||
95 | |||
96 | refreshToken (options: OverrideCommandOptions & { | ||
97 | refreshToken: string | ||
98 | }) { | ||
99 | const path = '/api/v1/users/token' | ||
100 | |||
101 | const body = { | ||
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' | ||
107 | } | ||
108 | |||
109 | return this.postBodyRequest({ | ||
110 | ...options, | ||
111 | |||
112 | path, | ||
113 | requestType: 'form', | ||
114 | fields: body, | ||
115 | implicitToken: false, | ||
116 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
117 | }) | ||
118 | } | ||
119 | |||
120 | getClient (options: OverrideCommandOptions = {}) { | ||
121 | const path = '/api/v1/oauth-clients/local' | ||
122 | |||
123 | return this.getRequestBody<{ client_id: string, client_secret: string }>({ | ||
124 | ...options, | ||
125 | |||
126 | path, | ||
127 | host: this.server.host, | ||
128 | implicitToken: false, | ||
129 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
130 | }) | ||
131 | } | ||
132 | } | ||