-
Notifications
You must be signed in to change notification settings - Fork 5k
/
gpgkeys-list.tsx
199 lines (185 loc) · 9.19 KB
/
gpgkeys-list.tsx
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
import {DropDownMenu, FormField, NotificationType, SlidingPanel} from 'argo-ui';
import * as PropTypes from 'prop-types';
import * as React from 'react';
import {Form, FormApi, TextArea} from 'react-form';
import {RouteComponentProps} from 'react-router';
import {DataLoader, EmptyState, ErrorNotification, Page} from '../../../shared/components';
import {AppContext} from '../../../shared/context';
import * as models from '../../../shared/models';
import {services} from '../../../shared/services';
require('./gpgkeys-list.scss');
interface NewGnuPGPublicKeyParams {
keyData: string;
}
export class GpgKeysList extends React.Component<RouteComponentProps<any>> {
public static contextTypes = {
router: PropTypes.object,
apis: PropTypes.object,
history: PropTypes.object
};
private formApi: FormApi;
private loader: DataLoader;
public render() {
return (
<Page
title='GnuPG public keys'
toolbar={{
breadcrumbs: [{title: 'Settings', path: '/settings'}, {title: 'GnuPG public keys'}],
actionMenu: {
className: 'fa fa-plus',
items: [
{
title: 'Add GnuPG key',
iconClassName: 'fa fa-plus',
action: () => (this.showAddGnuPGKey = true)
}
]
}
}}>
<div className='gpgkeys-list'>
<div className='argo-container'>
<DataLoader load={() => services.gpgkeys.list()} ref={loader => (this.loader = loader)}>
{(gpgkeys: models.GnuPGPublicKey[]) =>
(gpgkeys.length > 0 && (
<div className='argo-table-list'>
<div className='argo-table-list__head'>
<div className='row'>
<div className='columns small-3'>KEY ID</div>
<div className='columns small-3'>KEY TYPE</div>
<div className='columns small-6'>IDENTITY</div>
</div>
</div>
{gpgkeys.map(gpgkey => (
<div className='argo-table-list__row' key={gpgkey.keyID}>
<div className='row'>
<div className='columns small-3'>
<i className='fa fa-key' /> {gpgkey.keyID}
</div>
<div className='columns small-3'>{gpgkey.subType.toUpperCase()}</div>
<div className='columns small-6'>
{gpgkey.owner}
<DropDownMenu
anchor={() => (
<button className='argo-button argo-button--light argo-button--lg argo-button--short'>
<i className='fa fa-ellipsis-v' />
</button>
)}
items={[
{
title: 'Remove',
action: () => this.removeKey(gpgkey.keyID)
}
]}
/>
</div>
</div>
</div>
))}
</div>
)) || (
<EmptyState icon='fa fa-key'>
<h4>No GnuPG public keys currently configured</h4>
<h5>You can add GnuPG public keys below..</h5>
<button className='argo-button argo-button--base' onClick={() => (this.showAddGnuPGKey = true)}>
Add GnuPG public key
</button>
</EmptyState>
)
}
</DataLoader>
</div>
</div>
<SlidingPanel
isShown={this.showAddGnuPGKey}
onClose={() => (this.showAddGnuPGKey = false)}
header={
<div>
<button className='argo-button argo-button--base' onClick={() => this.formApi.submitForm(null)}>
Create
</button>{' '}
<button onClick={() => (this.showAddGnuPGKey = false)} className='argo-button argo-button--base-o'>
Cancel
</button>
</div>
}>
<Form
onSubmit={params => this.addGnuPGPublicKey({keyData: params.keyData})}
getApi={api => (this.formApi = api)}
preSubmit={(params: NewGnuPGPublicKeyParams) => ({
keyData: params.keyData
})}
validateError={(params: NewGnuPGPublicKeyParams) => ({
keyData: !params.keyData && 'GnuPG public key data is required'
})}>
{formApi => (
<form onSubmit={formApi.submitForm} role='form' className='gpgkeys-list width-control' encType='multipart/form-data'>
<div className='white-box'>
<p>ADD GnuPG PUBLIC KEY</p>
<div className='argo-form-row'>
<FormField formApi={formApi} label='GnuPG public key data (ASCII-armored)' field='keyData' component={TextArea} />
</div>
</div>
</form>
)}
</Form>
</SlidingPanel>
</Page>
);
}
private clearForms() {
this.formApi.resetAll();
}
private validateKeyInputfield(data: string): boolean {
if (data == null || data === '') {
return false;
}
const str = data.trim();
const startNeedle = '-----BEGIN PGP PUBLIC KEY BLOCK-----\n';
const endNeedle = '\n-----END PGP PUBLIC KEY BLOCK-----';
if (str.length < startNeedle.length + endNeedle.length) {
return false;
}
if (!str.startsWith(startNeedle)) {
return false;
}
if (!str.endsWith(endNeedle)) {
return false;
}
return true;
}
private async addGnuPGPublicKey(params: NewGnuPGPublicKeyParams) {
try {
if (!this.validateKeyInputfield(params.keyData)) {
throw {
name: 'Invalid key exception',
message: 'Invalid GnuPG key data found - must be ASCII armored'
};
}
await services.gpgkeys.create({keyData: params.keyData});
this.showAddGnuPGKey = false;
this.loader.reload();
} catch (e) {
this.appContext.apis.notifications.show({
content: <ErrorNotification title='Unable to add GnuPG public key' e={e} />,
type: NotificationType.Error
});
}
}
private async removeKey(keyId: string) {
const confirmed = await this.appContext.apis.popup.confirm('Remove GPG public key', 'Are you sure you want to remove GPG key with ID ' + keyId + '?');
if (confirmed) {
await services.gpgkeys.delete(keyId);
this.loader.reload();
}
}
private get showAddGnuPGKey() {
return new URLSearchParams(this.props.location.search).get('addGnuPGPublicKey') === 'true';
}
private set showAddGnuPGKey(val: boolean) {
this.clearForms();
this.appContext.router.history.push(`${this.props.match.url}?addGnuPGPublicKey=${val}`);
}
private get appContext(): AppContext {
return this.context as AppContext;
}
}