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