summaryrefslogtreecommitdiff
path: root/web/source/settings/views/user/applications/detail.tsx
blob: 5beeb0cceecf1edec91261f4ce20bd2f73e0829f (plain)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
	GoToSocial
	Copyright (C) GoToSocial Authors admin@gotosocial.org
	SPDX-License-Identifier: AGPL-3.0-or-later

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import React, { useState } from "react";
import { useLocation, useParams } from "wouter";
import FormWithData from "../../../lib/form/form-with-data";
import BackButton from "../../../components/back-button";
import { useBaseUrl } from "../../../lib/navigation/util";
import { useDeleteAppMutation, useGetAppQuery, useGetOOBAuthCodeMutation } from "../../../lib/query/user/applications";
import { App } from "../../../lib/types/application";
import { useAppWebsite, useCallbackURL, useCreated, useRedirectURIs } from "./common";
import MutationButton from "../../../components/form/mutation-button";
import { useTextInput } from "../../../lib/form";
import { TextInput } from "../../../components/form/inputs";
import { useScopesPermittedBy, useScopesValidator } from "../../../lib/util/formvalidators";

export default function AppDetail({ }) {
	const params: { appId: string } = useParams();
	const baseUrl = useBaseUrl();
	const backLocation: String = history.state?.backLocation ?? `~${baseUrl}`;

	return (
		<div className="application-details">
			<h1><BackButton to={backLocation}/> Application Details</h1>
			<FormWithData
				dataQuery={useGetAppQuery}
				queryArg={params.appId}
				DataForm={AppDetailForm}
				{...{ backLocation: backLocation }}
			/>
		</div>
	);
}

function AppDetailForm({ data: app, backLocation }: { data: App, backLocation: string }) {	
	return (
		<>
			<AppBasicInfo app={app} />
			<AccessTokenForm app={app} />
			<DeleteAppForm app={app} backLocation={backLocation} />
		</>
	);
}

function AppBasicInfo({ app }: { app: App }) {
	const appWebsite = useAppWebsite(app);
	const created = useCreated(app);
	const redirectURIs = useRedirectURIs(app);
	const [ showClient, setShowClient ] = useState(false);
	const [ showSecret, setShowSecret ] = useState(false);

	return (
		<dl className="info-list">
			<div className="info-list-entry">
				<dt>Name:</dt>
				<dd className="text-cutoff">{app.name}</dd>
			</div>

			{ appWebsite && 
			<div className="info-list-entry">
				<dt>Website:</dt>
				<dd>{appWebsite}</dd>
			</div>
			}

			<div className="info-list-entry">
				<dt>Created:</dt>
				<dd>{created}</dd>
			</div>

			<div className="info-list-entry">
				<dt>Scopes:</dt>
				<dd className="monospace">{app.scopes.join(" ")}</dd>
			</div>

			<div className="info-list-entry">
				<dt>Redirect URI(s):</dt>
				<dd className="monospace">{redirectURIs}</dd>
			</div>

			<div className="info-list-entry">
				<dt>Vapid key:</dt>
				<dd className="monospace">{app.vapid_key}</dd>
			</div>

			<div className="info-list-entry">
				<dt>Client ID:</dt>
				{ showClient
					? <dd className="monospace">{app.client_id}</dd>
					: <dd><button onClick={() => setShowClient(true)}>Show client ID</button></dd>
			 	}
			</div>

			<div className="info-list-entry">
				<dt>Client secret:</dt>
				{ showSecret
					? <dd className="monospace">{app.client_secret}</dd>
					: <dd><button onClick={() => setShowSecret(true)}>Show secret</button></dd>
			 	}
			</div>
		</dl>
	);
}

function AccessTokenForm({ app }: { app: App }) {
	const [ getOOBAuthCode, result ] = useGetOOBAuthCodeMutation();
	const permittedScopes = useScopesPermittedBy();
	const validateScopes = useScopesValidator();
	const scope = useTextInput("scope", {
		defaultValue: app.scopes.join(" "),
		validator: (wantsScopesStr: string) => {
			if (wantsScopesStr === "") {
				return "";
			}

			// Check requested scopes are valid scopes.
			const wantsScopes = wantsScopesStr.split(" ");
			const invalidScopesMsg = validateScopes(wantsScopes);
			if (invalidScopesMsg !== "") {
				return invalidScopesMsg;
			}

			// Check requested scopes are permitted by the app.
			return permittedScopes(app.scopes, wantsScopes);
		}
	});
	
	const callbackURL = useCallbackURL();
	const disabled = !app.redirect_uris.includes(callbackURL);
	return (
		<form
			autoComplete="off"
			onSubmit={(e) => {
				e.preventDefault();
				getOOBAuthCode({
					app,
					scope: scope.value ?? "",
					redirectURI: callbackURL,
				});
			}}
		>
			<div className="form-section-docs">
				<h2>Request An API Access Token</h2>
				<p>
					If your application redirect URIs includes the settings panel callback URL,
					you can use this section to request an access token that you can use to make API calls.
					<br/>The token scopes specified below must be equal to, or a subset of, the scopes
					you provided when you created the application.
					<br/>After clicking "Request access token", you will be redirected to the sign in
					page for your instance, where you must provide your credentials in order to authorize
					your application to act on your behalf. You will then be redirected again to a page
					where you can view your new access token.
				</p>
				<a
					href="https://docs.gotosocial.org/en/latest/api/authentication/"
					target="_blank"
					className="docslink"
					rel="noreferrer"
				>
					Learn more about the OAuth authentication flow (opens in a new tab)
				</a>
			</div>

			<TextInput
				field={scope}
				label="Token scopes (space-separated list)"
				autoCapitalize="off"
				autoCorrect="off"
				disabled={disabled}
			/>

			<MutationButton
				disabled={disabled}
				label="Request access token"
				result={result}
			/>
		</form>
	);
}

function DeleteAppForm({ app, backLocation }: { app: App, backLocation: string }) {
	const [ _location, setLocation ] = useLocation();
	const [ deleteApp, result ] = useDeleteAppMutation();

	return (
		<form>
			<div className="form-section-docs">
				<h2>Delete Application</h2>
				<p>
					You can use this button to delete the application.
					<br/>Any tokens created by the application will also be deleted.
				</p>
			</div>
			<MutationButton
				label={`Delete`}
				title={`Delete`}
				type="button"
				className="button danger"
				onClick={(e) => {
					e.preventDefault();
					deleteApp(app.id);
					setLocation(backLocation);
				}}
				disabled={false}
				showError={false}
				result={result}
			/>
		</form>
	);
}