@@ -2,22 +2,14 @@ import React, { FC, useCallback } from 'react';
2
2
import { useSelector } from 'react-redux' ;
3
3
4
4
import {
5
- AUTH_ASAP ,
6
- AUTH_AWS_IAM ,
7
- AUTH_BASIC ,
8
- AUTH_BEARER ,
9
- AUTH_DIGEST ,
10
- AUTH_HAWK ,
11
- AUTH_NETRC ,
12
- AUTH_NONE ,
13
- AUTH_NTLM ,
14
- AUTH_OAUTH_1 ,
15
- AUTH_OAUTH_2 ,
5
+ AuthType ,
16
6
getAuthTypeName ,
7
+ HAWK_ALGORITHM_SHA256 ,
17
8
} from '../../../common/constants' ;
18
- import * as models from '../../../models' ;
19
9
import { update } from '../../../models/helpers/request-operations' ;
20
- import { isRequest } from '../../../models/request' ;
10
+ import { RequestAuthentication } from '../../../models/request' ;
11
+ import { SIGNATURE_METHOD_HMAC_SHA1 } from '../../../network/o-auth-1/constants' ;
12
+ import { GRANT_TYPE_AUTHORIZATION_CODE } from '../../../network/o-auth-2/constants' ;
21
13
import { selectActiveRequest } from '../../redux/selectors' ;
22
14
import { Dropdown } from '../base/dropdown/dropdown' ;
23
15
import { DropdownButton } from '../base/dropdown/dropdown-button' ;
@@ -26,11 +18,110 @@ import { DropdownItem } from '../base/dropdown/dropdown-item';
26
18
import { showModal } from '../modals' ;
27
19
import { AlertModal } from '../modals/alert-modal' ;
28
20
21
+ const defaultTypes : AuthType [ ] = [
22
+ 'basic' ,
23
+ 'digest' ,
24
+ 'oauth1' ,
25
+ 'oauth2' ,
26
+ 'ntlm' ,
27
+ 'iam' ,
28
+ 'bearer' ,
29
+ 'hawk' ,
30
+ 'asap' ,
31
+ 'netrc' ,
32
+ ] ;
33
+
34
+ function makeNewAuth ( type : string , oldAuth : RequestAuthentication = { } ) : RequestAuthentication {
35
+ switch ( type ) {
36
+ // No Auth
37
+ case 'none' :
38
+ return { } ;
39
+
40
+ // HTTP Basic Authentication
41
+ case 'basic' :
42
+ return {
43
+ type,
44
+ useISO88591 : oldAuth . useISO88591 || false ,
45
+ disabled : oldAuth . disabled || false ,
46
+ username : oldAuth . username || '' ,
47
+ password : oldAuth . password || '' ,
48
+ } ;
49
+
50
+ case 'digest' :
51
+ case 'ntlm' :
52
+ return {
53
+ type,
54
+ disabled : oldAuth . disabled || false ,
55
+ username : oldAuth . username || '' ,
56
+ password : oldAuth . password || '' ,
57
+ } ;
58
+
59
+ case 'oauth1' :
60
+ return {
61
+ type,
62
+ disabled : false ,
63
+ signatureMethod : SIGNATURE_METHOD_HMAC_SHA1 ,
64
+ consumerKey : '' ,
65
+ consumerSecret : '' ,
66
+ tokenKey : '' ,
67
+ tokenSecret : '' ,
68
+ privateKey : '' ,
69
+ version : '1.0' ,
70
+ nonce : '' ,
71
+ timestamp : '' ,
72
+ callback : '' ,
73
+ } ;
74
+
75
+ // OAuth 2.0
76
+ case 'oauth2' :
77
+ return {
78
+ type,
79
+ grantType : GRANT_TYPE_AUTHORIZATION_CODE ,
80
+ } ;
81
+
82
+ // Aws IAM
83
+ case 'iam' :
84
+ return {
85
+ type,
86
+ disabled : oldAuth . disabled || false ,
87
+ accessKeyId : oldAuth . accessKeyId || '' ,
88
+ secretAccessKey : oldAuth . secretAccessKey || '' ,
89
+ sessionToken : oldAuth . sessionToken || '' ,
90
+ } ;
91
+
92
+ // Hawk
93
+ case 'hawk' :
94
+ return {
95
+ type,
96
+ algorithm : HAWK_ALGORITHM_SHA256 ,
97
+ } ;
98
+
99
+ // Atlassian ASAP
100
+ case 'asap' :
101
+ return {
102
+ type,
103
+ issuer : '' ,
104
+ subject : '' ,
105
+ audience : '' ,
106
+ additionalClaims : '' ,
107
+ keyId : '' ,
108
+ privateKey : '' ,
109
+ } ;
110
+
111
+ // Types needing no defaults
112
+ case 'netrc' :
113
+ default :
114
+ return {
115
+ type,
116
+ } ;
117
+ }
118
+ }
119
+
29
120
const AuthItem : FC < {
30
- type : string ;
121
+ type : AuthType ;
31
122
nameOverride ?: string ;
32
- isCurrent : ( type : string ) => boolean ;
33
- onClick : ( type : string ) => void ;
123
+ isCurrent : ( type : AuthType ) => boolean ;
124
+ onClick : ( type : AuthType ) => void ;
34
125
} > = ( { type, nameOverride, isCurrent, onClick } ) => (
35
126
< DropdownItem onClick = { onClick } value = { type } >
36
127
{ < i className = { `fa fa-${ isCurrent ( type ) ? 'check' : 'empty' } ` } /> } { ' ' }
@@ -39,15 +130,15 @@ const AuthItem: FC<{
39
130
) ;
40
131
AuthItem . displayName = DropdownItem . name ;
41
132
42
- export const AuthDropdown : FC = ( ) => {
133
+ interface Props {
134
+ authTypes ?: AuthType [ ] ;
135
+ disabled ?: boolean ;
136
+ }
137
+ export const AuthDropdown : FC < Props > = ( { authTypes = defaultTypes , disabled = false } ) => {
43
138
const activeRequest = useSelector ( selectActiveRequest ) ;
44
139
45
- const onClick = useCallback ( async ( type : string ) => {
46
- if ( ! activeRequest ) {
47
- return ;
48
- }
49
-
50
- if ( ! isRequest ( activeRequest ) ) {
140
+ const onClick = useCallback ( async ( type : AuthType ) => {
141
+ if ( ! activeRequest || ! ( 'authentication' in activeRequest ) ) {
51
142
return ;
52
143
}
53
144
@@ -58,8 +149,8 @@ export const AuthDropdown: FC = () => {
58
149
return ;
59
150
}
60
151
61
- const newAuthentication = models . request . newAuth ( type , authentication ) ;
62
- const defaultAuthentication = models . request . newAuth ( authentication . type ) ;
152
+ const newAuthentication = makeNewAuth ( type , authentication ) ;
153
+ const defaultAuthentication = makeNewAuth ( authentication . type ) ;
63
154
64
155
// Prompt the user if fields will change between new and old
65
156
for ( const key of Object . keys ( authentication ) ) {
@@ -80,16 +171,13 @@ export const AuthDropdown: FC = () => {
80
171
break ;
81
172
}
82
173
}
83
- update ( activeRequest , { authentication :newAuthentication } ) ;
174
+ update ( activeRequest , { authentication : newAuthentication } ) ;
84
175
} , [ activeRequest ] ) ;
85
- const isCurrent = useCallback ( ( type : string ) => {
86
- if ( ! activeRequest ) {
87
- return false ;
88
- }
89
- if ( ! isRequest ( activeRequest ) ) {
176
+ const isCurrent = useCallback ( ( type : AuthType ) => {
177
+ if ( ! activeRequest || ! ( 'authentication' in activeRequest ) ) {
90
178
return false ;
91
179
}
92
- return type === ( activeRequest . authentication . type || AUTH_NONE ) ;
180
+ return type === ( activeRequest . authentication . type || 'none' ) ;
93
181
} , [ activeRequest ] ) ;
94
182
95
183
if ( ! activeRequest ) {
@@ -101,22 +189,25 @@ export const AuthDropdown: FC = () => {
101
189
return (
102
190
< Dropdown beside >
103
191
< DropdownDivider > Auth Types</ DropdownDivider >
104
- < DropdownButton className = "tall" >
192
+ < DropdownButton className = "tall" disabled = { disabled } >
105
193
{ 'authentication' in activeRequest ? getAuthTypeName ( activeRequest . authentication . type ) || 'Auth' : 'Auth' }
106
194
< i className = "fa fa-caret-down space-left" />
107
195
</ DropdownButton >
108
- < AuthItem type = { AUTH_BASIC } { ...itemProps } />
109
- < AuthItem type = { AUTH_DIGEST } { ...itemProps } />
110
- < AuthItem type = { AUTH_OAUTH_1 } { ...itemProps } />
111
- < AuthItem type = { AUTH_OAUTH_2 } { ...itemProps } />
112
- < AuthItem type = { AUTH_NTLM } { ...itemProps } />
113
- < AuthItem type = { AUTH_AWS_IAM } { ...itemProps } />
114
- < AuthItem type = { AUTH_BEARER } { ...itemProps } />
115
- < AuthItem type = { AUTH_HAWK } { ...itemProps } />
116
- < AuthItem type = { AUTH_ASAP } { ...itemProps } />
117
- < AuthItem type = { AUTH_NETRC } { ...itemProps } />
118
- < DropdownDivider > Other</ DropdownDivider >
119
- < AuthItem type = { AUTH_NONE } nameOverride = "No Authentication" { ...itemProps } />
196
+ { authTypes . map ( authType =>
197
+ < AuthItem
198
+ key = { authType }
199
+ type = { authType }
200
+ { ...itemProps }
201
+ /> ) }
202
+ < DropdownDivider key = "divider-other" >
203
+ Other
204
+ </ DropdownDivider >
205
+ < AuthItem
206
+ key = "none"
207
+ type = "none"
208
+ nameOverride = "No Authentication"
209
+ { ...itemProps }
210
+ />
120
211
</ Dropdown >
121
212
) ;
122
213
} ;
0 commit comments